forked from mergeos-bounties/PlantGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
57 lines (41 loc) · 1.62 KB
/
Copy pathloader.py
File metadata and controls
57 lines (41 loc) · 1.62 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
from __future__ import annotations
import json
from pathlib import Path
from plantguide.config import SAMPLES_DIR, SPECIES_DIR
def list_species_files(directory: Path | None = None) -> list[Path]:
root = directory or SPECIES_DIR
if not root.exists():
return []
return sorted(root.glob("*.json"))
def list_sample_files(directory: Path | None = None) -> list[Path]:
root = directory or SAMPLES_DIR
if not root.exists():
return []
return sorted(root.glob("*.json"))
def load_json(path: Path) -> dict:
return json.loads(path.read_text(encoding="utf-8"))
def load_species(path: Path) -> dict:
payload = load_json(path)
payload.setdefault("id", path.stem)
payload.setdefault("common_name", path.stem.replace("_", " ").title())
payload.setdefault("scientific_name", payload.get("id", "").replace("_", " ").title())
payload.setdefault("tags", [])
payload.setdefault("care", {})
return payload
def load_sample(path: Path) -> dict:
payload = load_json(path)
payload.setdefault("id", path.stem)
payload.setdefault("tags", [])
payload.setdefault("expected_species", None)
return payload
def load_species_catalog() -> list[dict]:
return [load_species(p) for p in list_species_files()]
def get_species_by_id(species_id: str) -> dict | None:
key = species_id.strip().lower().replace(" ", "_")
for species in load_species_catalog():
sid = str(species.get("id", "")).lower()
if sid == key:
return species
if str(species.get("common_name", "")).lower().replace(" ", "_") == key:
return species
return None