-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathhelpers.py
More file actions
66 lines (51 loc) · 1.91 KB
/
helpers.py
File metadata and controls
66 lines (51 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json
from loguru import logger
def from_csv(value: str | None, separator: str = ",") -> list[str]:
if not value:
return []
parts = [part.strip() for part in value.split(separator)]
return [part for part in parts if part]
def serialize_inventory_tags(tags: list[str] | str | None) -> str | None:
if isinstance(tags, list):
return ",".join([tag for tag in tags if tag])
return tags
def inventory_tags_to_list(raw_tags: str | list[str] | None) -> list[str]:
if raw_tags is None:
return []
if isinstance(raw_tags, list):
return [tag.strip() for tag in raw_tags if tag and tag.strip()]
return [tag.strip() for tag in raw_tags.split(",") if tag and tag.strip()]
def inventory_tags_to_string(raw_tags: str | list[str] | None) -> str | None:
if raw_tags is None:
return None
if isinstance(raw_tags, str):
return raw_tags
return ",".join([tag for tag in raw_tags if tag])
def first_image(images: str | list[str] | None) -> str | None:
if not images:
return None
if isinstance(images, list):
return normalize_image(images[0]) if images else None
raw = str(images).strip()
if not raw:
return None
try:
parsed = json.loads(raw)
if isinstance(parsed, list) and parsed:
return normalize_image(parsed[0])
except Exception as exc:
logger.exception(f"Exception occurred while parsing image JSON: {exc}")
if "|||" in raw:
return normalize_image(raw.split("|||")[0])
if "," in raw:
return normalize_image(raw.split(",")[0])
return normalize_image(raw)
def normalize_image(val: str | None) -> str | None:
if not val:
return None
val = str(val).strip()
if not val:
return None
if val.startswith("http") or val.startswith("/api/") or val.startswith("data:"):
return val
return f"/api/v1/assets/{val}/thumbnail"