48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
import os
|
|||
|
|
|
||
|
|
from .config import make_slug
|
||
|
|
|
||
|
|
|
||
|
|
def task_image_path(image_root, task, account=None, suffix="old", ext=".jpg"):
|
||
|
|
batch_id = _safe_component(_get(task, "batch_id"), "unknown_batch")
|
||
|
|
slug = _account_slug(account, task)
|
||
|
|
task_id = _safe_component(_get(task, "id"), "task")
|
||
|
|
item_id = _safe_component(_get(task, "item_id"), "item")
|
||
|
|
suffix = _safe_component(suffix, "image")
|
||
|
|
ext = str(ext or ".jpg")
|
||
|
|
if not ext.startswith("."):
|
||
|
|
ext = "." + ext
|
||
|
|
return os.path.abspath(
|
||
|
|
os.path.join(
|
||
|
|
str(image_root or "images"),
|
||
|
|
batch_id,
|
||
|
|
slug,
|
||
|
|
f"{task_id}_{item_id}_{suffix}{ext}",
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def _account_slug(account, task):
|
||
|
|
slug = _get(account, "slug")
|
||
|
|
if slug:
|
||
|
|
return _safe_component(slug, "unknown_account")
|
||
|
|
alias = _get(task, "alias") or _get(account, "alias") or _get(task, "account_name") or _get(account, "account_name")
|
||
|
|
if alias:
|
||
|
|
return _safe_component(make_slug(alias), "unknown_account")
|
||
|
|
return "unknown_account"
|
||
|
|
|
||
|
|
|
||
|
|
def _get(obj, name, default=None):
|
||
|
|
if obj is None:
|
||
|
|
return default
|
||
|
|
if isinstance(obj, dict):
|
||
|
|
return obj.get(name, default)
|
||
|
|
return getattr(obj, name, default)
|
||
|
|
|
||
|
|
|
||
|
|
def _safe_component(value, default):
|
||
|
|
text = str(value or "").strip()
|
||
|
|
if not text:
|
||
|
|
text = str(default)
|
||
|
|
safe = "".join(ch if ch.isalnum() or ch in "_-" else "_" for ch in text).strip("_")
|
||
|
|
return safe or str(default)
|