"""Amazon inputs a person types once and several Apps read and write: FBA capacity. Command Center and Restock both read and write `fba_capacity` here, so neither App imports the other (rule 13). The value moved out of `restock_planner_settings`. Until this document holds the key, a read falls back to Restock's typed value, and `seed_fba_capacity` copies it here once. """ from __future__ import annotations import datetime as dt import math import re KEY = "amazon_inputs" #: Where Restock kept the typed capacity before it moved here. Read only, never written. LEGACY_KEY = "restock_planner_settings" FIELD = "fba_capacity" UNITS = ("cu ft", "cu m") CUBIC_FEET_PER_CUBIC_METER = 35.3146667 def volume_unit(value): """`cu ft` or `cu m` for a volume unit word, else None.""" unit = re.sub(r"[^a-z0-9]", "", str(value or "").lower()) return {"cuft": "cu ft", "cubicfeet": "cu ft", "ft3": "cu ft", "cubicfoot": "cu ft", "cum": "cu m", "cubicmeters": "cu m", "cubicmetres": "cu m", "cubicmeter": "cu m", "cubicmetre": "cu m", "m3": "cu m", "cbm": "cu m"}.get(unit) def convert(value, unit, to): """`value` in volume unit `unit` expressed in `to`; None when either unit is unknown.""" src, dst = volume_unit(unit), volume_unit(to) if value is None or src is None or dst is None: return None if src == dst: return float(value) return float(value) * CUBIC_FEET_PER_CUBIC_METER if dst == "cu ft" else float(value) / CUBIC_FEET_PER_CUBIC_METER def clean_capacity(value): """A typed capacity `{value, unit}` with a finite value at or above 0, or None. Raises ValueError.""" if value is None: return None if not isinstance(value, dict) or set(value) != {"value", "unit"}: raise ValueError("Enter FBA capacity with a value and volume unit.") raw, unit = value["value"], volume_unit(value["unit"]) try: number = None if isinstance(raw, bool) else float(raw) except (TypeError, ValueError): number = None if number is None or not math.isfinite(number) or number < 0 or not unit: raise ValueError("Enter a finite FBA capacity at or above 0 in cu ft or cu m.") return {"value": number, "unit": unit} def _doc(st, key): value = st.get(key) return value if isinstance(value, dict) else {} def _legacy(st): try: return clean_capacity(_doc(st, LEGACY_KEY).get(FIELD)) except ValueError: return None def fba_capacity(st): """The one typed FBA capacity, or None. Reads only: it never writes the store.""" doc = _doc(st, KEY) if FIELD not in doc: return _legacy(st) try: return clean_capacity(doc.get(FIELD)) except ValueError: return None def set_fba_capacity(st, value, *, by=""): """Save a typed capacity (None clears it). Returns the stored value.""" clean = clean_capacity(value) stamp = dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds") def _apply(doc): doc = doc if isinstance(doc, dict) else {} doc[FIELD] = clean doc["fbaCapacitySetBy"] = str(by or "") or None doc["fbaCapacitySetAt"] = stamp return doc st.update(KEY, _apply) return clean def seed_fba_capacity(st): """Copy Restock's typed value here once. Returns True when this call wrote the seed.""" if FIELD in _doc(st, KEY): return False legacy = _legacy(st) wrote = [] def _apply(doc): doc = doc if isinstance(doc, dict) else {} wrote.clear() if FIELD not in doc: doc[FIELD] = legacy doc["fbaCapacitySeededFrom"] = LEGACY_KEY wrote.append(True) return doc st.update(KEY, _apply) return bool(wrote)