diff --git a/masque/file/gdsii.py b/masque/file/gdsii.py index 3d97fe3..68b69a9 100644 --- a/masque/file/gdsii.py +++ b/masque/file/gdsii.py @@ -35,6 +35,7 @@ import gdsii.structure import gdsii.elements 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 ..shapes import Polygon, Path 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. - Will automatically decompress files with a .gz suffix. + Will automatically decompress gzipped files. Args: filename: Filename to save to. @@ -198,7 +199,7 @@ def readfile(filename: Union[str, pathlib.Path], **kwargs: passed to `masque.file.gdsii.read` """ path = pathlib.Path(filename) - if path.suffix == '.gz': + if is_gzipped(path): open_func: Callable = gzip.open else: open_func = open diff --git a/masque/file/klamath.py b/masque/file/klamath.py index 192ce9f..ba89638 100644 --- a/masque/file/klamath.py +++ b/masque/file/klamath.py @@ -34,7 +34,7 @@ import numpy # type: ignore import klamath 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 ..shapes import Polygon, Path 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. - Will automatically decompress files with a .gz suffix. + Will automatically decompress gzipped files. Args: filename: Filename to save to. @@ -178,7 +178,7 @@ def readfile(filename: Union[str, pathlib.Path], **kwargs: passed to `masque.file.gdsii.read` """ path = pathlib.Path(filename) - if path.suffix == '.gz': + if is_gzipped(path): open_func: Callable = gzip.open else: open_func = open diff --git a/masque/file/oasis.py b/masque/file/oasis.py index 8d9e074..5e20121 100644 --- a/masque/file/oasis.py +++ b/masque/file/oasis.py @@ -27,7 +27,7 @@ import fatamorgana import fatamorgana.records as fatrec 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 ..shapes import Polygon, Path, Circle 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. - Will automatically decompress files with a .gz suffix. + Will automatically decompress gzipped files. Args: filename: Filename to save to. @@ -215,7 +215,7 @@ def readfile(filename: Union[str, pathlib.Path], **kwargs: passed to `oasis.read` """ path = pathlib.Path(filename) - if path.suffix == '.gz': + if is_gzipped(path): open_func: Callable = gzip.open else: open_func = open diff --git a/masque/file/utils.py b/masque/file/utils.py index 30b2808..6239a1a 100644 --- a/masque/file/utils.py +++ b/masque/file/utils.py @@ -4,6 +4,8 @@ Helper functions for file reading and writing from typing import Set, Tuple, List import re import copy +import gzip +import pathlib from .. import Pattern, PatternError from ..shapes import Polygon, Path @@ -176,3 +178,9 @@ def dose2dtype(patterns: List[Pattern], subpat.pattern = new_pats[(id(subpat.pattern), dose_mult)] 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'