refactor(core): clean up code style and improve readability

This commit addresses various minor structural improvements across the codebase, primarily focusing on simplifying syntax and enhancing robustness. Several instances of overly verbose string concatenation and unnecessary line breaks have been cleaned up for better adherence to Python best practices.

- Simplified dictionary comprehensions in asset retrieval functions
- Improved error message formatting using f-strings
- Updated .gitignore to exclude agent and codex directories
This commit is contained in:
2026-07-23 16:54:23 +02:00
parent a51479b599
commit 326175b176
2 changed files with 11 additions and 28 deletions
+2
View File
@@ -1,3 +1,5 @@
.venv .venv
__pycache__ __pycache__
.env .env
.agents
.codex
+9 -28
View File
@@ -32,9 +32,7 @@ ALBUM_MODE = os.environ.get("ALBUM_MODE", "root").lower()
ALBUM_SEPARATOR = os.environ.get("ALBUM_SEPARATOR", " - ") ALBUM_SEPARATOR = os.environ.get("ALBUM_SEPARATOR", " - ")
PAGE_SIZE = int(os.environ.get("PAGE_SIZE", "1000")) PAGE_SIZE = int(os.environ.get("PAGE_SIZE", "1000"))
DRY_RUN = os.environ.get("DRY_RUN", "false").lower() == "true" DRY_RUN = os.environ.get("DRY_RUN", "false").lower() == "true"
SYNC_INTERVAL_SECONDS = int( SYNC_INTERVAL_SECONDS = int(os.environ.get("SYNC_INTERVAL_SECONDS", "1800"))
os.environ.get("SYNC_INTERVAL_SECONDS", "1800")
)
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
@@ -65,22 +63,17 @@ def check_configuration() -> None:
if missing: if missing:
raise RuntimeError( raise RuntimeError(
"The following environment variables are missing: " "The following environment variables are missing: " + ", ".join(missing)
+ ", ".join(missing)
) )
if ALBUM_MODE not in {"root", "leaf", "relative"}: if ALBUM_MODE not in {"root", "leaf", "relative"}:
raise RuntimeError( raise RuntimeError("ALBUM_MODE must be root, leaf, or relative")
"ALBUM_MODE must be root, leaf, or relative"
)
if not EXTERNAL_ROOT.startswith("/"): if not EXTERNAL_ROOT.startswith("/"):
raise RuntimeError("EXTERNAL_ROOT must be an absolute path") raise RuntimeError("EXTERNAL_ROOT must be an absolute path")
if SYNC_INTERVAL_SECONDS < 0: if SYNC_INTERVAL_SECONDS < 0:
raise RuntimeError( raise RuntimeError("SYNC_INTERVAL_SECONDS must be zero or greater")
"SYNC_INTERVAL_SECONDS must be zero or greater"
)
def api_request( def api_request(
@@ -100,8 +93,7 @@ def api_request(
if not response.ok: if not response.ok:
raise RuntimeError( raise RuntimeError(
f"{method} {url} failed: " f"{method} {url} failed: {response.status_code} {response.text}"
f"{response.status_code} {response.text}"
) )
if response.status_code == 204 or not response.content: if response.status_code == 204 or not response.content:
@@ -113,10 +105,7 @@ def api_request(
def get_albums() -> dict[str, dict[str, Any]]: def get_albums() -> dict[str, dict[str, Any]]:
albums = api_request("GET", "/albums?isOwned=true") albums = api_request("GET", "/albums?isOwned=true")
return { return {album["albumName"]: album for album in albums}
album["albumName"]: album
for album in albums
}
def create_album(album_name: str) -> dict[str, Any]: def create_album(album_name: str) -> dict[str, Any]:
@@ -171,11 +160,7 @@ def search_assets(**filters: Any) -> list[dict[str, Any]]:
def get_album_asset_ids(album_id: str) -> set[str]: def get_album_asset_ids(album_id: str) -> set[str]:
assets = search_assets(albumIds=[album_id]) assets = search_assets(albumIds=[album_id])
return { return {asset["id"] for asset in assets if asset.get("id")}
asset["id"]
for asset in assets
if asset.get("id")
}
def add_assets_to_album( def add_assets_to_album(
@@ -281,9 +266,7 @@ def synchronize() -> None:
# Immich v3 album responses no longer contain the asset list. # Immich v3 album responses no longer contain the asset list.
existing_asset_ids = get_album_asset_ids(album_id) existing_asset_ids = get_album_asset_ids(album_id)
missing_asset_ids = sorted( missing_asset_ids = sorted(set(desired_asset_ids) - existing_asset_ids)
set(desired_asset_ids) - existing_asset_ids
)
if missing_asset_ids: if missing_asset_ids:
logging.info( logging.info(
@@ -310,9 +293,7 @@ def main() -> int:
except Exception: except Exception:
if SYNC_INTERVAL_SECONDS == 0: if SYNC_INTERVAL_SECONDS == 0:
raise raise
logging.exception( logging.exception("Synchronization failed; the next run will retry")
"Synchronization failed; the next run will retry"
)
if SYNC_INTERVAL_SECONDS == 0: if SYNC_INTERVAL_SECONDS == 0:
break break