diff --git a/pyproject.toml b/pyproject.toml index b74308d..f361a18 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,7 +51,7 @@ select = [ "SIM", # Common simplification rules "TID", # Some good import practices "TC", # Enforce importing certain types in a TYPE_CHECKING block - # "PTH", # Use pathlib instead of os.path + "PTH", # Use pathlib instead of os.path "NPY", # Some numpy-specific things ] diff --git a/src/emulsion/config.py b/src/emulsion/config.py index c1875ba..a0c49de 100644 --- a/src/emulsion/config.py +++ b/src/emulsion/config.py @@ -1,5 +1,6 @@ import copy import os +from pathlib import Path import toml @@ -56,14 +57,14 @@ DEFAULT_CONFIG = { def get_config_path(): xdg_config_home = os.environ.get( - 'XDG_CONFIG_HOME', os.path.expanduser('~/.config') + 'XDG_CONFIG_HOME', Path('~/.config').expanduser() ) - return os.path.join(xdg_config_home, 'emulsion', 'config.toml') + return Path(xdg_config_home) / 'emulsion' / 'config.toml' class ConfigLoader: def __init__(self, path=None): - self.path = path or get_config_path() + self.path: Path = path or get_config_path() self.config = copy.deepcopy(DEFAULT_CONFIG) def load(self): @@ -71,7 +72,7 @@ class ConfigLoader: Loads the config from disk and merges it into the defaults. Returns the full config dictionary. """ - if os.path.isfile(self.path): + if self.path.is_file(): try: user_config = toml.load(self.path) self._merge(self.config, user_config) @@ -110,13 +111,13 @@ class ConfigLoader: # doesn't do that. # Ensure directory exists - os.makedirs(os.path.dirname(self.path), exist_ok=True) + self.path.parent.mkdir(parents=True) # We probably only want to write if it doesn't exist, to avoid # clobbering. - if os.path.exists(self.path): + if self.path.exists: return False - with open(self.path, "w", encoding="utf-8") as f: + with self.path.open(encoding='utf-8') as f: toml.dump(current_defaults, f) return True diff --git a/src/emulsion/executor.py b/src/emulsion/executor.py index 3af52b2..468ca1d 100644 --- a/src/emulsion/executor.py +++ b/src/emulsion/executor.py @@ -1,8 +1,8 @@ import datetime -import os import shlex import subprocess from concurrent.futures import ThreadPoolExecutor, as_completed +from pathlib import Path from alive_progress import alive_bar @@ -27,7 +27,7 @@ class Executor: # Filter supported files valid_files = [ f for f in files - if os.path.splitext(f)[1].lower() in extensions + if Path(f).suffix in extensions ] if not valid_files: @@ -109,7 +109,7 @@ class Executor: # If sidecar doesn't exist, we need to tell ExifTool to read from source # and write to the new sidecar file. - if not os.path.exists(target_path): + if not Path(target_path).exists(): return target_path, original_file return target_path, None diff --git a/src/emulsion/main.py b/src/emulsion/main.py index 96115f9..b310e8f 100644 --- a/src/emulsion/main.py +++ b/src/emulsion/main.py @@ -151,7 +151,7 @@ def main(): # Handle Initialization if args.init_config: - if os.path.exists(loader.path): + if loader.path.exists(): print( f'Config file already exists at {loader.path}. Not ' 'overwriting.'