139 lines
3.8 KiB
Python
139 lines
3.8 KiB
Python
"""Label field positions (mm / pt). Stored in data/calibration.json."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from copy import deepcopy
|
|
from pathlib import Path
|
|
|
|
# Matches prior label_print.css defaults (screen + print).
|
|
DEFAULTS = {
|
|
'batch': {
|
|
'top': 63.0,
|
|
'left': 30.2,
|
|
'font': 7.0,
|
|
'bottom_top': 43.3,
|
|
'right_left': 22.2,
|
|
},
|
|
'date': {
|
|
'top': 63.0,
|
|
'left': 67.4,
|
|
'font': 6.0,
|
|
'bottom_top': 43.3,
|
|
'bottom_left': 70.4,
|
|
},
|
|
'next': {
|
|
'top': 68.5,
|
|
'left': 84.2,
|
|
'font': 7.0,
|
|
'bottom_top': 48.8,
|
|
'bottom_left': 87.2,
|
|
},
|
|
'weight': {
|
|
'top': 68.5,
|
|
'left': 37.3,
|
|
'font': 7.0,
|
|
'bottom_top': 48.8,
|
|
'right_left': 29.3,
|
|
},
|
|
}
|
|
|
|
FIELDS = ('batch', 'date', 'next', 'weight')
|
|
|
|
|
|
def _data_dir() -> Path:
|
|
db_env = os.environ.get('DB_PATH')
|
|
if db_env:
|
|
return Path(db_env).parent
|
|
try:
|
|
from django.conf import settings
|
|
return Path(settings.BASE_DIR) / 'data'
|
|
except Exception:
|
|
return Path(__file__).resolve().parents[1] / 'data'
|
|
|
|
|
|
def calibration_path() -> Path:
|
|
return _data_dir() / 'calibration.json'
|
|
|
|
|
|
def _merge(base: dict, overlay: dict) -> dict:
|
|
out = deepcopy(base)
|
|
for field, vals in (overlay or {}).items():
|
|
if field not in out or not isinstance(vals, dict):
|
|
continue
|
|
for key, val in vals.items():
|
|
if key in out[field] and isinstance(val, (int, float)):
|
|
out[field][key] = float(val)
|
|
return out
|
|
|
|
|
|
def load_calibration() -> dict:
|
|
path = calibration_path()
|
|
if not path.is_file():
|
|
return deepcopy(DEFAULTS)
|
|
try:
|
|
with path.open(encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
except (OSError, json.JSONDecodeError):
|
|
return deepcopy(DEFAULTS)
|
|
return _merge(DEFAULTS, data)
|
|
|
|
|
|
def for_api(cal: dict) -> dict:
|
|
"""Ensure optional keys are present (null) for API schema."""
|
|
out = {}
|
|
for field in FIELDS:
|
|
v = cal[field]
|
|
out[field] = {
|
|
'top': v['top'],
|
|
'left': v['left'],
|
|
'font': v['font'],
|
|
'bottom_top': v['bottom_top'],
|
|
'bottom_left': v.get('bottom_left'),
|
|
'right_left': v.get('right_left'),
|
|
}
|
|
return out
|
|
|
|
|
|
def save_calibration(data: dict) -> dict:
|
|
merged = _merge(DEFAULTS, data)
|
|
path = calibration_path()
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
tmp = path.with_suffix('.tmp')
|
|
with tmp.open('w', encoding='utf-8') as f:
|
|
json.dump(merged, f, indent=2)
|
|
f.write('\n')
|
|
tmp.replace(path)
|
|
return merged
|
|
|
|
|
|
def css_vars(cal: dict | None = None) -> str:
|
|
"""Inline :root block for print + calibrate pages."""
|
|
cal = cal or load_calibration()
|
|
lines = [':root {']
|
|
for field in FIELDS:
|
|
v = cal[field]
|
|
lines.append(f' --{field}-top: {v["top"]}mm;')
|
|
lines.append(f' --{field}-left: {v["left"]}mm;')
|
|
lines.append(f' --{field}-font: {v["font"]}pt;')
|
|
lines.append(f' --{field}-bottom-top: {v["bottom_top"]}mm;')
|
|
if 'bottom_left' in v:
|
|
lines.append(f' --{field}-bottom-left: {v["bottom_left"]}mm;')
|
|
if 'right_left' in v:
|
|
lines.append(f' --{field}-right-left: {v["right_left"]}mm;')
|
|
lines.append('}')
|
|
return '\n'.join(lines)
|
|
|
|
|
|
# ponytail: assert-based check; upgrade to pytest if suite grows
|
|
if __name__ == '__main__':
|
|
css = css_vars(DEFAULTS)
|
|
assert '--batch-top: 63.0mm;' in css
|
|
assert '--date-bottom-left: 70.4mm;' in css
|
|
assert '--weight-right-left: 29.3mm;' in css
|
|
merged = _merge(DEFAULTS, {'batch': {'top': 64, 'bogus': 1}})
|
|
assert merged['batch']['top'] == 64.0
|
|
assert 'bogus' not in merged['batch']
|
|
assert merged['date']['left'] == DEFAULTS['date']['left']
|
|
print('ok')
|