Switch to pathlib

This commit is contained in:
Alexander Wainwright
2025-12-27 13:01:20 +10:00
parent 6c00b8e733
commit 35e6410b2b
4 changed files with 13 additions and 12 deletions

View File

@@ -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
]

View File

@@ -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

View File

@@ -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

View File

@@ -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.'