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
+9 -28
View File
@@ -32,9 +32,7 @@ ALBUM_MODE = os.environ.get("ALBUM_MODE", "root").lower()
ALBUM_SEPARATOR = os.environ.get("ALBUM_SEPARATOR", " - ")
PAGE_SIZE = int(os.environ.get("PAGE_SIZE", "1000"))
DRY_RUN = os.environ.get("DRY_RUN", "false").lower() == "true"
SYNC_INTERVAL_SECONDS = int(
os.environ.get("SYNC_INTERVAL_SECONDS", "1800")
)
SYNC_INTERVAL_SECONDS = int(os.environ.get("SYNC_INTERVAL_SECONDS", "1800"))
logging.basicConfig(
level=logging.INFO,
@@ -65,22 +63,17 @@ def check_configuration() -> None:
if missing:
raise RuntimeError(
"The following environment variables are missing: "
+ ", ".join(missing)
"The following environment variables are missing: " + ", ".join(missing)
)
if ALBUM_MODE not in {"root", "leaf", "relative"}:
raise RuntimeError(
"ALBUM_MODE must be root, leaf, or relative"
)
raise RuntimeError("ALBUM_MODE must be root, leaf, or relative")
if not EXTERNAL_ROOT.startswith("/"):
raise RuntimeError("EXTERNAL_ROOT must be an absolute path")
if SYNC_INTERVAL_SECONDS < 0:
raise RuntimeError(
"SYNC_INTERVAL_SECONDS must be zero or greater"
)
raise RuntimeError("SYNC_INTERVAL_SECONDS must be zero or greater")
def api_request(
@@ -100,8 +93,7 @@ def api_request(
if not response.ok:
raise RuntimeError(
f"{method} {url} failed: "
f"{response.status_code} {response.text}"
f"{method} {url} failed: {response.status_code} {response.text}"
)
if response.status_code == 204 or not response.content:
@@ -113,10 +105,7 @@ def api_request(
def get_albums() -> dict[str, dict[str, Any]]:
albums = api_request("GET", "/albums?isOwned=true")
return {
album["albumName"]: album
for album in albums
}
return {album["albumName"]: album for album in albums}
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]:
assets = search_assets(albumIds=[album_id])
return {
asset["id"]
for asset in assets
if asset.get("id")
}
return {asset["id"] for asset in assets if asset.get("id")}
def add_assets_to_album(
@@ -281,9 +266,7 @@ def synchronize() -> None:
# Immich v3 album responses no longer contain the asset list.
existing_asset_ids = get_album_asset_ids(album_id)
missing_asset_ids = sorted(
set(desired_asset_ids) - existing_asset_ids
)
missing_asset_ids = sorted(set(desired_asset_ids) - existing_asset_ids)
if missing_asset_ids:
logging.info(
@@ -310,9 +293,7 @@ def main() -> int:
except Exception:
if SYNC_INTERVAL_SECONDS == 0:
raise
logging.exception(
"Synchronization failed; the next run will retry"
)
logging.exception("Synchronization failed; the next run will retry")
if SYNC_INTERVAL_SECONDS == 0:
break