78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
||
"""Извлечение габаритов (W x D x H мм) и добавочных атрибутов.
|
||
|
||
Форматы в даташитах:
|
||
400 x 300 x 150 mm
|
||
Dimensions: 400×300×150mm
|
||
(400 W) x (300 D) x (150 H) mm
|
||
"""
|
||
import glob
|
||
import os
|
||
import re
|
||
import sys
|
||
|
||
BASE = "/opt/work-hub/compat"
|
||
|
||
|
||
def fix(s):
|
||
return "" if s is None else str(s)
|
||
|
||
|
||
def parse_dimensions(text):
|
||
"""Возвращает (ширина, глубина, высота) в мм или None."""
|
||
t = fix(text).replace("\u00d7", "x").replace("\u2715", "x")
|
||
pats = [
|
||
# три числа перед mm
|
||
r"(\d+(?:\.\d+)?)\s*(?:mm)?\s*[xXх]\s*(\d+(?:\.\d+)?)\s*(?:mm)?\s*[xXх]\s*(\d+(?:\.\d+)?)\s*mm",
|
||
r"dimensions?[:\s]*(\d+(?:\.\d+)?)\s*[xX]\s*(\d+(?:\.\d+)?)\s*[xX]\s*(\d+(?:\.\d+)?)",
|
||
# с подписями W/D/H
|
||
r"(\d+(?:\.\d+)?)\s*(?:mm)?\s*W\s*[xX]\s*(\d+(?:\.\d+)?)\s*(?:mm)?\s*D\s*[xX]\s*(\d+(?:\.\d+)?)\s*(?:mm)?\s*H",
|
||
]
|
||
for p in pats:
|
||
m = re.search(p, t, re.I)
|
||
if m:
|
||
try:
|
||
a, b, c = (float(m.group(1)), float(m.group(2)), float(m.group(3)))
|
||
except ValueError:
|
||
continue
|
||
# отбрасываем неправдоподобное (мм: 10..3000)
|
||
if all(10 <= v <= 3000 for v in (a, b, c)):
|
||
return a, b, c
|
||
return None
|
||
|
||
|
||
def main():
|
||
import yaml
|
||
ds_files = glob.glob("/opt/work-hub/devices/yealink/*.yaml")
|
||
had, got = 0, 0
|
||
out = {}
|
||
for f in ds_files:
|
||
try:
|
||
d = yaml.safe_load(open(f, encoding="utf-8"))["device"]
|
||
except Exception:
|
||
continue
|
||
name = re.sub(r"\s*\(\d+\)?\s*$", "", d.get("model") or "").strip()
|
||
lines = []
|
||
for k, vals in (d.get("specs") or {}).items():
|
||
lines += ["%s: %s" % (k, v) for v in vals]
|
||
blob = "\n".join(lines)
|
||
had += 1
|
||
dim = parse_dimensions(blob)
|
||
if dim:
|
||
got += 1
|
||
out[name] = {"width_mm": dim[0], "depth_mm": dim[1], "height_mm": dim[2]}
|
||
print("даташитов проверено: %d" % had)
|
||
print("габариты найдены у: %d (%.0f %%)" % (got, 100.0 * got / max(had, 1)))
|
||
for n, v in list(out.items())[:12]:
|
||
print(" %-30s %g x %g x %g мм" % (n[:30], v["width_mm"], v["depth_mm"], v["height_mm"]))
|
||
path = os.path.join(BASE, "dimensions.yaml")
|
||
with open(path, "w", encoding="utf-8") as f:
|
||
yaml.safe_dump({"vendor": "yealink", "devices": out}, f,
|
||
allow_unicode=True, sort_keys=False, width=140)
|
||
print("\nсохранено:", path)
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|