Determine if an existing file is gzipped based on magic bytes, not suffix
This commit is contained in:
parent
03a359e446
commit
ce5d386a24
@ -35,6 +35,7 @@ import gdsii.structure
|
|||||||
import gdsii.elements
|
import gdsii.elements
|
||||||
|
|
||||||
from .utils import mangle_name, make_dose_table, dose2dtype, dtype2dose, clean_pattern_vertices
|
from .utils import mangle_name, make_dose_table, dose2dtype, dtype2dose, clean_pattern_vertices
|
||||||
|
from .utils import is_gzipped
|
||||||
from .. import Pattern, SubPattern, PatternError, Label, Shape
|
from .. import Pattern, SubPattern, PatternError, Label, Shape
|
||||||
from ..shapes import Polygon, Path
|
from ..shapes import Polygon, Path
|
||||||
from ..repetition import Grid
|
from ..repetition import Grid
|
||||||
@ -190,7 +191,7 @@ def readfile(filename: Union[str, pathlib.Path],
|
|||||||
"""
|
"""
|
||||||
Wrapper for `masque.file.gdsii.read()` that takes a filename or path instead of a stream.
|
Wrapper for `masque.file.gdsii.read()` that takes a filename or path instead of a stream.
|
||||||
|
|
||||||
Will automatically decompress files with a .gz suffix.
|
Will automatically decompress gzipped files.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filename: Filename to save to.
|
filename: Filename to save to.
|
||||||
@ -198,7 +199,7 @@ def readfile(filename: Union[str, pathlib.Path],
|
|||||||
**kwargs: passed to `masque.file.gdsii.read`
|
**kwargs: passed to `masque.file.gdsii.read`
|
||||||
"""
|
"""
|
||||||
path = pathlib.Path(filename)
|
path = pathlib.Path(filename)
|
||||||
if path.suffix == '.gz':
|
if is_gzipped(path):
|
||||||
open_func: Callable = gzip.open
|
open_func: Callable = gzip.open
|
||||||
else:
|
else:
|
||||||
open_func = open
|
open_func = open
|
||||||
|
@ -34,7 +34,7 @@ import numpy # type: ignore
|
|||||||
import klamath
|
import klamath
|
||||||
from klamath import records
|
from klamath import records
|
||||||
|
|
||||||
from .utils import mangle_name, make_dose_table, dose2dtype, dtype2dose
|
from .utils import mangle_name, make_dose_table, dose2dtype, dtype2dose, is_gzipped
|
||||||
from .. import Pattern, SubPattern, PatternError, Label, Shape
|
from .. import Pattern, SubPattern, PatternError, Label, Shape
|
||||||
from ..shapes import Polygon, Path
|
from ..shapes import Polygon, Path
|
||||||
from ..repetition import Grid
|
from ..repetition import Grid
|
||||||
@ -170,7 +170,7 @@ def readfile(filename: Union[str, pathlib.Path],
|
|||||||
"""
|
"""
|
||||||
Wrapper for `masque.file.gdsii.read()` that takes a filename or path instead of a stream.
|
Wrapper for `masque.file.gdsii.read()` that takes a filename or path instead of a stream.
|
||||||
|
|
||||||
Will automatically decompress files with a .gz suffix.
|
Will automatically decompress gzipped files.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filename: Filename to save to.
|
filename: Filename to save to.
|
||||||
@ -178,7 +178,7 @@ def readfile(filename: Union[str, pathlib.Path],
|
|||||||
**kwargs: passed to `masque.file.gdsii.read`
|
**kwargs: passed to `masque.file.gdsii.read`
|
||||||
"""
|
"""
|
||||||
path = pathlib.Path(filename)
|
path = pathlib.Path(filename)
|
||||||
if path.suffix == '.gz':
|
if is_gzipped(path):
|
||||||
open_func: Callable = gzip.open
|
open_func: Callable = gzip.open
|
||||||
else:
|
else:
|
||||||
open_func = open
|
open_func = open
|
||||||
|
@ -27,7 +27,7 @@ import fatamorgana
|
|||||||
import fatamorgana.records as fatrec
|
import fatamorgana.records as fatrec
|
||||||
from fatamorgana.basic import PathExtensionScheme, AString, NString, PropStringReference
|
from fatamorgana.basic import PathExtensionScheme, AString, NString, PropStringReference
|
||||||
|
|
||||||
from .utils import mangle_name, make_dose_table, clean_pattern_vertices
|
from .utils import mangle_name, make_dose_table, clean_pattern_vertices, is_gzipped
|
||||||
from .. import Pattern, SubPattern, PatternError, Label, Shape
|
from .. import Pattern, SubPattern, PatternError, Label, Shape
|
||||||
from ..shapes import Polygon, Path, Circle
|
from ..shapes import Polygon, Path, Circle
|
||||||
from ..repetition import Grid, Arbitrary, Repetition
|
from ..repetition import Grid, Arbitrary, Repetition
|
||||||
@ -207,7 +207,7 @@ def readfile(filename: Union[str, pathlib.Path],
|
|||||||
"""
|
"""
|
||||||
Wrapper for `oasis.read()` that takes a filename or path instead of a stream.
|
Wrapper for `oasis.read()` that takes a filename or path instead of a stream.
|
||||||
|
|
||||||
Will automatically decompress files with a .gz suffix.
|
Will automatically decompress gzipped files.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filename: Filename to save to.
|
filename: Filename to save to.
|
||||||
@ -215,7 +215,7 @@ def readfile(filename: Union[str, pathlib.Path],
|
|||||||
**kwargs: passed to `oasis.read`
|
**kwargs: passed to `oasis.read`
|
||||||
"""
|
"""
|
||||||
path = pathlib.Path(filename)
|
path = pathlib.Path(filename)
|
||||||
if path.suffix == '.gz':
|
if is_gzipped(path):
|
||||||
open_func: Callable = gzip.open
|
open_func: Callable = gzip.open
|
||||||
else:
|
else:
|
||||||
open_func = open
|
open_func = open
|
||||||
|
@ -4,6 +4,8 @@ Helper functions for file reading and writing
|
|||||||
from typing import Set, Tuple, List
|
from typing import Set, Tuple, List
|
||||||
import re
|
import re
|
||||||
import copy
|
import copy
|
||||||
|
import gzip
|
||||||
|
import pathlib
|
||||||
|
|
||||||
from .. import Pattern, PatternError
|
from .. import Pattern, PatternError
|
||||||
from ..shapes import Polygon, Path
|
from ..shapes import Polygon, Path
|
||||||
@ -176,3 +178,9 @@ def dose2dtype(patterns: List[Pattern],
|
|||||||
subpat.pattern = new_pats[(id(subpat.pattern), dose_mult)]
|
subpat.pattern = new_pats[(id(subpat.pattern), dose_mult)]
|
||||||
|
|
||||||
return patterns, dose_vals_list
|
return patterns, dose_vals_list
|
||||||
|
|
||||||
|
|
||||||
|
def is_gzipped(path: pathlib.Path) -> bool:
|
||||||
|
with open(path, 'rb') as stream:
|
||||||
|
magic_bytes = stream.read(2)
|
||||||
|
return magic_bytes == b'\x1f\x8b'
|
||||||
|
Loading…
Reference in New Issue
Block a user