mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 19:33:55 +01:00
Ninth batch of the 250-line sweep. Every Python file in the repo is now under cap (over-cap count 25 -> 18; the 18 remaining are all Dart). _foodbank.py 365 -> 210 (+ _foodbank_search, _foodbank_rebuild) test_averages.py 351 -> 222 (+ test_averages_report.py) _estimator.py 325 -> 137 (+ _estimator_off.py) _budget.py 323 -> 227 (+ _budget_biometrics, _budget_derived) test_foodbank.py 311 -> 117 (+ test_foodbank_rebuild.py) _averages.py 298 -> 174 (+ _averages_periods, _averages_report) test_budget_history.py 288 -> 162 (+ test_budget_history_io.py) The split boundaries are constrained by which module owns each conftest-patched state path, not by line count. BUDGET_FILE stays in _budget, FOOD_BANK_FILE in _foodbank: the derived/search/rebuild halves reach the file only through their parent's private helpers. Two of the new modules had copied FOOD_BANK_FILE into their headers as an incidental import, which is exactly the leak the guard is for -- both cleaned. test_state_redirect.py now covers all five redirected paths (FOOD_LOG_FILE, BUDGET_FILE, BUDGET_HISTORY_FILE, FOOD_BANK_FILE, MANUAL_BANK_FILE) rather than just the food log, in both directions: only the owning module may name each constant, and each redirect demonstrably moves a real write off the live path. Two rewires fail-closed rather than silently: `_budget.budget_weight` and `_estimator.requests` were module-attribute references no import rewrite touches, and both raised AttributeError immediately. The latter appeared only after ruff removed the now-unused `requests` import from _estimator -- the import was load-bearing solely as a patch target. Verified: 707 Python tests / 100% branch coverage, 5 layout-fits tests under xvfb, check_patch_targets 0, pre-commit clean, ~/.local/share/diet_guard byte-identical. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
"""Values derived from the stored budget record.
|
|
|
|
Split out of :mod:`._budget` to keep both files under the repo's 250-line
|
|
limit. Nothing here opens a file: both helpers read through
|
|
:func:`diet_guard._budget._read_record`, so ``BUDGET_FILE`` stays named by
|
|
:mod:`._budget` alone -- which is what ``tests/conftest.py`` redirects, and
|
|
what ``test_state_redirect.py`` enforces.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from diet_guard._budget import PROTEIN_G_PER_KG, BudgetError, _read_record
|
|
|
|
__all__ = ["budget_weight", "protein_target_g"]
|
|
|
|
|
|
def protein_target_g() -> float | None:
|
|
"""Return the daily protein target in grams, or None if it cannot be derived.
|
|
|
|
Derived from the stored body weight at :data:`PROTEIN_G_PER_KG`. Returns
|
|
None -- rather than raising -- whenever the target is simply unavailable
|
|
(no budget set, a pre-v2 record without weight, or a corrupt file), so
|
|
the dashboard can show calories and quietly omit the protein line.
|
|
|
|
Returns:
|
|
The protein target in grams, or None when weight is unknown.
|
|
"""
|
|
try:
|
|
weight = budget_weight()
|
|
except BudgetError:
|
|
return None
|
|
if weight is None:
|
|
return None
|
|
return round(weight * PROTEIN_G_PER_KG, 1)
|
|
|
|
|
|
def budget_weight() -> float | None:
|
|
"""Return the body weight stored with the budget, or None if unavailable.
|
|
|
|
Returns:
|
|
The stored weight in kg, or None for a pre-v2 (budget-only) record.
|
|
|
|
Raises:
|
|
BudgetNotInitializedError: If no budget has been set yet.
|
|
BudgetFileCorruptError: If the file exists but cannot be parsed.
|
|
"""
|
|
record = _read_record()
|
|
value = record.get("w")
|
|
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
return None
|
|
return float(value)
|