From b8894b350eedb5c6f32eb956444ce4a4e13bc9ae Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 11:46:27 -0700 Subject: [PATCH 01/16] Allow pathlib.Path types in monty.shutil --- monty/shutil.py | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/monty/shutil.py b/monty/shutil.py index 58141b5a1..94d006753 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -6,21 +6,22 @@ import shutil import warnings from gzip import GzipFile - +from pathlib import Path from .io import zopen -def copy_r(src, dst): +def copy_r(src: str | Path, dst: str | Path): """ Implements a recursive copy function similar to Unix's "cp -r" command. Surprisingly, python does not have a real equivalent. shutil.copytree only works if the destination directory is not present. Args: - src (str): Source folder to copy. - dst (str): Destination folder. + src (str | Path): Source folder to copy. + dst (str | Path): Destination folder. """ - + src = str(src) + dst = str(dst) abssrc = os.path.abspath(src) absdst = os.path.abspath(dst) try: @@ -38,7 +39,7 @@ def copy_r(src, dst): warnings.warn(f"Cannot copy {fpath} to itself") -def gzip_dir(path, compresslevel=6): +def gzip_dir(path: str | Path, compresslevel=6): """ Gzips all files in a directory. Note that this is different from shutil.make_archive, which creates a tar archive. The aim of this method @@ -46,10 +47,11 @@ def gzip_dir(path, compresslevel=6): commands like zless or zcat. Args: - path (str): Path to directory. + path (str | Path): Path to directory. compresslevel (int): Level of compression, 1-9. 9 is default for GzipFile, 6 is default for gzip. """ + path = str(path) for root, _, files in os.walk(path): for f in files: full_f = os.path.abspath(os.path.join(root, f)) @@ -60,17 +62,18 @@ def gzip_dir(path, compresslevel=6): os.remove(full_f) -def compress_file(filepath, compression="gz"): +def compress_file(filepath: str | Path, compression="gz"): """ Compresses a file with the correct extension. Functions like standard Unix command line gzip and bzip2 in the sense that the original uncompressed files are not retained. Args: - filepath (str): Path to file. + filepath (str | Path): Path to file. compression (str): A compression mode. Valid options are "gz" or "bz2". Defaults to "gz". """ + filepath = str(filepath) if compression not in ["gz", "bz2"]: raise ValueError("Supported compression formats are 'gz' and 'bz2'.") if not filepath.lower().endswith(f".{compression}"): @@ -79,23 +82,24 @@ def compress_file(filepath, compression="gz"): os.remove(filepath) -def compress_dir(path, compression="gz"): +def compress_dir(path: str | Path, compression="gz"): """ Recursively compresses all files in a directory. Note that this compresses all files singly, i.e., it does not create a tar archive. For that, just use Python tarfile class. Args: - path (str): Path to parent directory. + path (str | Path): Path to parent directory. compression (str): A compression mode. Valid options are "gz" or "bz2". Defaults to gz. """ - for parent, subdirs, files in os.walk(path): + path = str(path) + for parent, _, files in os.walk(path): for f in files: compress_file(os.path.join(parent, f), compression=compression) -def decompress_file(filepath): +def decompress_file(filepath: str | Path): """ Decompresses a file with the correct extension. Automatically detects gz, bz2 or z extension. @@ -105,6 +109,7 @@ def decompress_file(filepath): compression (str): A compression mode. Valid options are "gz" or "bz2". Defaults to "gz". """ + filepath = str(filepath) toks = filepath.split(".") file_ext = toks[-1].upper() if file_ext in ["BZ2", "GZ", "Z"]: @@ -113,19 +118,20 @@ def decompress_file(filepath): os.remove(filepath) -def decompress_dir(path): +def decompress_dir(path: str | Path): """ Recursively decompresses all files in a directory. Args: - path (str): Path to parent directory. + path (str | Path): Path to parent directory. """ - for parent, subdirs, files in os.walk(path): + path = str(path) + for parent, _, files in os.walk(path): for f in files: decompress_file(os.path.join(parent, f)) -def remove(path, follow_symlink=False): +def remove(path: str | Path, follow_symlink=False): """ Implements a remove function that will delete files, folder trees and symlink trees @@ -135,9 +141,10 @@ def remove(path, follow_symlink=False): 3.) Remove directory with rmtree Args: - path (str): path to remove + path (str | Path): path to remove follow_symlink(bool): follow symlinks and removes whatever is in them """ + path = str(path) if os.path.isfile(path): os.remove(path) elif os.path.islink(path): From 98e88c0cb954df121b58d3c234b3b3e77dc57359 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Sep 2023 18:49:25 +0000 Subject: [PATCH 02/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monty/shutil.py | 1 + 1 file changed, 1 insertion(+) diff --git a/monty/shutil.py b/monty/shutil.py index 1d08d0169..b4ada0381 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -6,6 +6,7 @@ import warnings from gzip import GzipFile from pathlib import Path + from .io import zopen From ff9c3b7f4084b4e653ad00abbbbeb60404731426 Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 11:56:03 -0700 Subject: [PATCH 03/16] Use `Path` operations in `shutil.py` --- monty/shutil.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/monty/shutil.py b/monty/shutil.py index b4ada0381..613bb9c15 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -10,7 +10,7 @@ from .io import zopen -def copy_r(src: str | Path, dst: str | Path): +def copy_r(src: str | Path, dst: str | Path) -> None: """ Implements a recursive copy function similar to Unix's "cp -r" command. Surprisingly, python does not have a real equivalent. shutil.copytree @@ -20,21 +20,21 @@ def copy_r(src: str | Path, dst: str | Path): src (str | Path): Source folder to copy. dst (str | Path): Destination folder. """ - src = str(src) - dst = str(dst) - abssrc = os.path.abspath(src) - absdst = os.path.abspath(dst) + src = Path(src) + dst = Path(dst) + abssrc = src.resolve() + absdst = dst.resolve() try: os.makedirs(absdst) except OSError: # If absdst exists, an OSError is raised. We ignore this error. pass for f in os.listdir(abssrc): - fpath = os.path.join(abssrc, f) - if os.path.isfile(fpath): + fpath = Path(abssrc, f) + if Path(fpath).is_file(): shutil.copy(fpath, absdst) elif not absdst.startswith(fpath): - copy_r(fpath, os.path.join(absdst, f)) + copy_r(fpath, Path(absdst, f)) else: warnings.warn(f"Cannot copy {fpath} to itself") @@ -51,7 +51,7 @@ def gzip_dir(path: str | Path, compresslevel=6): compresslevel (int): Level of compression, 1-9. 9 is default for GzipFile, 6 is default for gzip. """ - path = str(path) + path = Path(path) for root, _, files in os.walk(path): for f in files: full_f = os.path.abspath(os.path.join(root, f)) @@ -73,7 +73,7 @@ def compress_file(filepath: str | Path, compression="gz"): compression (str): A compression mode. Valid options are "gz" or "bz2". Defaults to "gz". """ - filepath = str(filepath) + filepath = Path(filepath) if compression not in ["gz", "bz2"]: raise ValueError("Supported compression formats are 'gz' and 'bz2'.") if not filepath.lower().endswith(f".{compression}"): @@ -93,7 +93,7 @@ def compress_dir(path: str | Path, compression="gz"): compression (str): A compression mode. Valid options are "gz" or "bz2". Defaults to gz. """ - path = str(path) + path = Path(path) for parent, _, files in os.walk(path): for f in files: compress_file(os.path.join(parent, f), compression=compression) @@ -110,10 +110,10 @@ def decompress_file(filepath: str | Path) -> str | None: Returns: str: The decompressed file path. """ - filepath = str(filepath) - toks = filepath.split(".") + filepath = Path(filepath) + toks = str(filepath).split(".") file_ext = toks[-1].upper() - if file_ext in ["BZ2", "GZ", "Z"] and os.path.isfile(filepath): + if file_ext in ["BZ2", "GZ", "Z"] and filepath.is_file(): decompressed_file = ".".join(toks[0:-1]) with zopen(filepath, "rb") as f_in, open(decompressed_file, "wb") as f_out: f_out.writelines(f_in) @@ -130,10 +130,10 @@ def decompress_dir(path: str | Path): Args: path (str | Path): Path to parent directory. """ - path = str(path) + path = Path(path) for parent, _, files in os.walk(path): for f in files: - decompress_file(os.path.join(parent, f)) + decompress_file(Path(parent, f)) def remove(path: str | Path, follow_symlink=False): @@ -149,10 +149,10 @@ def remove(path: str | Path, follow_symlink=False): path (str | Path): path to remove follow_symlink(bool): follow symlinks and removes whatever is in them """ - path = str(path) - if os.path.isfile(path): + path = Path(path) + if path.is_file(): os.remove(path) - elif os.path.islink(path): + elif path.is_symlink(): if follow_symlink: remove(os.readlink(path)) os.unlink(path) From f34f4fc5ac57b015aee73faadc11a83c12419c54 Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 11:56:43 -0700 Subject: [PATCH 04/16] more type hints --- monty/shutil.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/monty/shutil.py b/monty/shutil.py index 613bb9c15..a68cba847 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -39,7 +39,7 @@ def copy_r(src: str | Path, dst: str | Path) -> None: warnings.warn(f"Cannot copy {fpath} to itself") -def gzip_dir(path: str | Path, compresslevel=6): +def gzip_dir(path: str | Path, compresslevel: int = 6) -> None: """ Gzips all files in a directory. Note that this is different from shutil.make_archive, which creates a tar archive. The aim of this method @@ -62,7 +62,7 @@ def gzip_dir(path: str | Path, compresslevel=6): os.remove(full_f) -def compress_file(filepath: str | Path, compression="gz"): +def compress_file(filepath: str | Path, compression="gz") -> None: """ Compresses a file with the correct extension. Functions like standard Unix command line gzip and bzip2 in the sense that the original @@ -82,7 +82,7 @@ def compress_file(filepath: str | Path, compression="gz"): os.remove(filepath) -def compress_dir(path: str | Path, compression="gz"): +def compress_dir(path: str | Path, compression="gz") -> None: """ Recursively compresses all files in a directory. Note that this compresses all files singly, i.e., it does not create a tar archive. For @@ -123,7 +123,7 @@ def decompress_file(filepath: str | Path) -> str | None: return None -def decompress_dir(path: str | Path): +def decompress_dir(path: str | Path) -> None: """ Recursively decompresses all files in a directory. @@ -136,7 +136,7 @@ def decompress_dir(path: str | Path): decompress_file(Path(parent, f)) -def remove(path: str | Path, follow_symlink=False): +def remove(path: str | Path, follow_symlink=False) -> None: """ Implements a remove function that will delete files, folder trees and symlink trees. From a88461c0122bb761dd048e78724e7204d1b65f17 Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:04:01 -0700 Subject: [PATCH 05/16] Port os to pathlib --- monty/shutil.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monty/shutil.py b/monty/shutil.py index a68cba847..ef88db51c 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -55,7 +55,7 @@ def gzip_dir(path: str | Path, compresslevel: int = 6) -> None: for root, _, files in os.walk(path): for f in files: full_f = os.path.abspath(os.path.join(root, f)) - if not f.lower().endswith("gz") and not os.path.isdir(full_f): + if not f.suffix.lower() == ".gz" and not full_f.is_dir(): with open(full_f, "rb") as f_in, GzipFile(f"{full_f}.gz", "wb", compresslevel=compresslevel) as f_out: shutil.copyfileobj(f_in, f_out) shutil.copystat(full_f, f"{full_f}.gz") @@ -76,7 +76,7 @@ def compress_file(filepath: str | Path, compression="gz") -> None: filepath = Path(filepath) if compression not in ["gz", "bz2"]: raise ValueError("Supported compression formats are 'gz' and 'bz2'.") - if not filepath.lower().endswith(f".{compression}"): + if not filepath.suffix.lower() in (f".{compression}"): with open(filepath, "rb") as f_in, zopen(f"{filepath}.{compression}", "wb") as f_out: f_out.writelines(f_in) os.remove(filepath) @@ -96,7 +96,7 @@ def compress_dir(path: str | Path, compression="gz") -> None: path = Path(path) for parent, _, files in os.walk(path): for f in files: - compress_file(os.path.join(parent, f), compression=compression) + compress_file(Path(parent, f), compression=compression) def decompress_file(filepath: str | Path) -> str | None: @@ -155,6 +155,6 @@ def remove(path: str | Path, follow_symlink=False) -> None: elif path.is_symlink(): if follow_symlink: remove(os.readlink(path)) - os.unlink(path) + Path.unlink(path) else: shutil.rmtree(path) From b4b0a9c73f4a1842ae2fa2d54756668b9baab22c Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:08:34 -0700 Subject: [PATCH 06/16] More os to Path conversion --- monty/shutil.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/monty/shutil.py b/monty/shutil.py index ef88db51c..4c465b20e 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -111,10 +111,9 @@ def decompress_file(filepath: str | Path) -> str | None: str: The decompressed file path. """ filepath = Path(filepath) - toks = str(filepath).split(".") - file_ext = toks[-1].upper() - if file_ext in ["BZ2", "GZ", "Z"] and filepath.is_file(): - decompressed_file = ".".join(toks[0:-1]) + file_ext = filepath.suffix + if file_ext.lower() in [".bz2", ".gz", ".z"] and filepath.is_file(): + decompressed_file = Path(str(filepath).removesuffix(file_ext)) with zopen(filepath, "rb") as f_in, open(decompressed_file, "wb") as f_out: f_out.writelines(f_in) os.remove(filepath) From 7e4bf0aa780235d857796207b7f0fe04c3b23eda Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:10:46 -0700 Subject: [PATCH 07/16] include Literal type hint --- monty/shutil.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monty/shutil.py b/monty/shutil.py index 4c465b20e..a48950860 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -6,7 +6,7 @@ import warnings from gzip import GzipFile from pathlib import Path - +from typing import Literal from .io import zopen @@ -62,7 +62,7 @@ def gzip_dir(path: str | Path, compresslevel: int = 6) -> None: os.remove(full_f) -def compress_file(filepath: str | Path, compression="gz") -> None: +def compress_file(filepath: str | Path, compression: Literal["gz", "bz2"] ="gz") -> None: """ Compresses a file with the correct extension. Functions like standard Unix command line gzip and bzip2 in the sense that the original @@ -76,13 +76,13 @@ def compress_file(filepath: str | Path, compression="gz") -> None: filepath = Path(filepath) if compression not in ["gz", "bz2"]: raise ValueError("Supported compression formats are 'gz' and 'bz2'.") - if not filepath.suffix.lower() in (f".{compression}"): + if filepath.suffix.lower() != f".{compression}": with open(filepath, "rb") as f_in, zopen(f"{filepath}.{compression}", "wb") as f_out: f_out.writelines(f_in) os.remove(filepath) -def compress_dir(path: str | Path, compression="gz") -> None: +def compress_dir(path: str | Path, compression: Literal["gz", "bz2"] = "gz") -> None: """ Recursively compresses all files in a directory. Note that this compresses all files singly, i.e., it does not create a tar archive. For From 7ed2ae61731abc2e8a25977c4be8d4464fc6e2be Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Sep 2023 19:11:48 +0000 Subject: [PATCH 08/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monty/shutil.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monty/shutil.py b/monty/shutil.py index a48950860..450cfd990 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -7,6 +7,7 @@ from gzip import GzipFile from pathlib import Path from typing import Literal + from .io import zopen @@ -62,7 +63,7 @@ def gzip_dir(path: str | Path, compresslevel: int = 6) -> None: os.remove(full_f) -def compress_file(filepath: str | Path, compression: Literal["gz", "bz2"] ="gz") -> None: +def compress_file(filepath: str | Path, compression: Literal["gz", "bz2"] = "gz") -> None: """ Compresses a file with the correct extension. Functions like standard Unix command line gzip and bzip2 in the sense that the original From d93d97181d707cb47ca406b0c0960be337ca5025 Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:12:40 -0700 Subject: [PATCH 09/16] mypy --- monty/shutil.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monty/shutil.py b/monty/shutil.py index a48950860..227fc4e96 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -33,7 +33,7 @@ def copy_r(src: str | Path, dst: str | Path) -> None: fpath = Path(abssrc, f) if Path(fpath).is_file(): shutil.copy(fpath, absdst) - elif not absdst.startswith(fpath): + elif str(fpath) not in str(absdst): copy_r(fpath, Path(absdst, f)) else: warnings.warn(f"Cannot copy {fpath} to itself") @@ -62,7 +62,7 @@ def gzip_dir(path: str | Path, compresslevel: int = 6) -> None: os.remove(full_f) -def compress_file(filepath: str | Path, compression: Literal["gz", "bz2"] ="gz") -> None: +def compress_file(filepath: str | Path, compression: Literal["gz", "bz2"] = "gz") -> None: """ Compresses a file with the correct extension. Functions like standard Unix command line gzip and bzip2 in the sense that the original @@ -135,7 +135,7 @@ def decompress_dir(path: str | Path) -> None: decompress_file(Path(parent, f)) -def remove(path: str | Path, follow_symlink=False) -> None: +def remove(path: str | Path, follow_symlink: bool = False) -> None: """ Implements a remove function that will delete files, folder trees and symlink trees. From 1664951571cdb7f0fbe46297c4404f894fb5c56b Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:15:05 -0700 Subject: [PATCH 10/16] mypy fix --- monty/shutil.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monty/shutil.py b/monty/shutil.py index e798eb865..4364a2aed 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -55,7 +55,8 @@ def gzip_dir(path: str | Path, compresslevel: int = 6) -> None: path = Path(path) for root, _, files in os.walk(path): for f in files: - full_f = os.path.abspath(os.path.join(root, f)) + f = Path(f) + full_f = Path(root, f).resolve() if not f.suffix.lower() == ".gz" and not full_f.is_dir(): with open(full_f, "rb") as f_in, GzipFile(f"{full_f}.gz", "wb", compresslevel=compresslevel) as f_out: shutil.copyfileobj(f_in, f_out) From 367b081e5769005e9a7759bfac8f1f4f3d7bb678 Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:19:55 -0700 Subject: [PATCH 11/16] mypy fixes --- monty/shutil.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/monty/shutil.py b/monty/shutil.py index 4364a2aed..1c0f66d95 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -55,9 +55,8 @@ def gzip_dir(path: str | Path, compresslevel: int = 6) -> None: path = Path(path) for root, _, files in os.walk(path): for f in files: - f = Path(f) full_f = Path(root, f).resolve() - if not f.suffix.lower() == ".gz" and not full_f.is_dir(): + if not Path(f).suffix.lower() == ".gz" and not full_f.is_dir(): with open(full_f, "rb") as f_in, GzipFile(f"{full_f}.gz", "wb", compresslevel=compresslevel) as f_out: shutil.copyfileobj(f_in, f_out) shutil.copystat(full_f, f"{full_f}.gz") @@ -120,8 +119,7 @@ def decompress_file(filepath: str | Path) -> str | None: f_out.writelines(f_in) os.remove(filepath) - return decompressed_file - return None + return str(decompressed_file) def decompress_dir(path: str | Path) -> None: From bdd47f2ba8092d6e193e7666dfea69bb3142d7ae Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:22:04 -0700 Subject: [PATCH 12/16] remove unnecessary try/except --- monty/shutil.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/monty/shutil.py b/monty/shutil.py index 1c0f66d95..066fba89c 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -25,11 +25,7 @@ def copy_r(src: str | Path, dst: str | Path) -> None: dst = Path(dst) abssrc = src.resolve() absdst = dst.resolve() - try: - os.makedirs(absdst) - except OSError: - # If absdst exists, an OSError is raised. We ignore this error. - pass + os.makedirs(absdst, exist_ok=True) for f in os.listdir(abssrc): fpath = Path(abssrc, f) if Path(fpath).is_file(): From a1cc7aa6ec68b50c8175e574834e618f88ce7c64 Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:25:22 -0700 Subject: [PATCH 13/16] Satisfy mypy --- monty/shutil.py | 1 + 1 file changed, 1 insertion(+) diff --git a/monty/shutil.py b/monty/shutil.py index 066fba89c..69d07506b 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -95,6 +95,7 @@ def compress_dir(path: str | Path, compression: Literal["gz", "bz2"] = "gz") -> for f in files: compress_file(Path(parent, f), compression=compression) + return None def decompress_file(filepath: str | Path) -> str | None: """ From 2ab66a6d8b49f7fb47742b3a4da5c1b41422b11a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Sep 2023 19:25:35 +0000 Subject: [PATCH 14/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monty/shutil.py | 1 + 1 file changed, 1 insertion(+) diff --git a/monty/shutil.py b/monty/shutil.py index 69d07506b..d887ec030 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -97,6 +97,7 @@ def compress_dir(path: str | Path, compression: Literal["gz", "bz2"] = "gz") -> return None + def decompress_file(filepath: str | Path) -> str | None: """ Decompresses a file with the correct extension. Automatically detects From c465b26597e7a103f9f688763158031912e708f7 Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:26:11 -0700 Subject: [PATCH 15/16] Continue to satisfy mypy --- monty/shutil.py | 1 + 1 file changed, 1 insertion(+) diff --git a/monty/shutil.py b/monty/shutil.py index 69d07506b..b8333de5c 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -117,6 +117,7 @@ def decompress_file(filepath: str | Path) -> str | None: os.remove(filepath) return str(decompressed_file) + return None def decompress_dir(path: str | Path) -> None: From 501520740fe390cff6c8b066bcf663c5ffc967e8 Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Mon, 4 Sep 2023 12:30:50 -0700 Subject: [PATCH 16/16] pylint --- monty/shutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monty/shutil.py b/monty/shutil.py index a12028c18..45219eefb 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -52,7 +52,7 @@ def gzip_dir(path: str | Path, compresslevel: int = 6) -> None: for root, _, files in os.walk(path): for f in files: full_f = Path(root, f).resolve() - if not Path(f).suffix.lower() == ".gz" and not full_f.is_dir(): + if Path(f).suffix.lower() != ".gz" and not full_f.is_dir(): with open(full_f, "rb") as f_in, GzipFile(f"{full_f}.gz", "wb", compresslevel=compresslevel) as f_out: shutil.copyfileobj(f_in, f_out) shutil.copystat(full_f, f"{full_f}.gz")