Files
buryan-infra/ops/work/tools/build_attributes.py

147 lines
6.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Атрибуты устройств: реестр + извлечение значений из даташитов.
Идея владельца: любое значение из даташита (зум камеры, дальность захвата
звука в метрах, SPL...) — фильтруемый атрибут. Группы → «всплывающие» фильтры.
Выход:
attributes.yaml — реестр атрибутов (группа, единица, тип сравнения)
values.yaml — значения по устройствам с провенансом
"""
import glob
import os
import re
import sys
BASE = "/opt/work-hub/compat"
DEV_DIRS = ["/opt/work-hub/devices/yealink"]
# ── реестр атрибутов ──────────────────────────────────────────────
# id: (группа, название, единица, тип, паттерны)
ATTRS = {
"camera.zoom_optical": ("Камера", "Оптический зум", "×", "number",
[r"(?:optical|optica[l]?)\s*zoom[:\s]*(\d+(?:\.\d+)?)\s*[x×]",
r"(\d+(?:\.\d+)?)\s*[x×]\s*optical"]),
"camera.zoom_hybrid": ("Камера", "Гибридный зум", "×", "number",
[r"hybrid\s*zoom[:\s]*(\d+(?:\.\d+)?)",
r"(\d+(?:\.\d+)?)\s*[x×]\s*hybrid"]),
"camera.zoom_digital": ("Камера", "Цифровой зум", "×", "number",
[r"(\d+(?:\.\d+)?)\s*x\s*digital\s*zoom",
r"digital\s*zoom[:\s]*(\d+(?:\.\d+)?)"]),
"camera.fov_h": ("Камера", "Угол обзора по горизонтали", "°", "number",
[r"HFOV[:\s]*(\d+(?:\.\d+)?)", r"horizontal\s*FOV[:\s]*(\d+(?:\.\d+)?)"]),
"camera.fov_v": ("Камера", "Угол обзора по вертикали", "°", "number",
[r"VFOV[:\s]*(\d+(?:\.\d+)?)", r"vertical\s*FOV[:\s]*(\d+(?:\.\d+)?)"]),
"camera.max_resolution": ("Камера", "Максимальное разрешение", "px", "text",
[r"maximum\s*resolution[:\s]*([\d\*x×]+)"]),
"audio.pickup_m": ("Аудио", "Дальность захвата микрофона", "м", "number",
[r"(\d+(?:\.\d+)?)\s*[- ]?met(?:er|re)s?\s*pickup",
r"pickup\s*(?:distance|range)[:\s]*(\d+(?:\.\d+)?)\s*m",
r"(\d+(?:\.\d+)?)\s*m\s*pickup"]),
"audio.spl_max": ("Аудио", "Максимальное звуковое давление", "dB SPL", "number",
[r"(\d+)\s*dB\s*SPL"]),
"audio.mic_count": ("Аудио", "Микрофонных элементов", "шт", "number",
[r"(\d+)\s*(?:MEMS\s*)?microphone\s*array",
r"(\d+)\s*mic(?:rophone)?s?\s*array"]),
"audio.freq_min": ("Аудио", "Нижняя граница частот", "Гц", "number",
[r"(\d+)\s*[-\u2013]\s*\d+\s*k?Hz"]),
"network.poe": ("Сеть", "PoE-питание", "—", "bool",
[r"PoE", r"802\.3af", r"802\.3at"]),
"network.usb_c": ("Сеть", "USB-C", "—", "bool", [r"USB[-\s]?C"]),
"network.hdmi_out": ("Интерфейсы", "HDMI-выходов", "шт", "number",
[r"(\d+)\s*x\s*HDMI[-\s]?out", r"(\d+)\s*HDMI\s*out"]),
"network.hdmi_in": ("Интерфейсы", "HDMI-входов", "шт", "number",
[r"(\d+)\s*x\s*HDMI[-\s]?in"]),
}
def fix(s):
if s is None:
return ""
s = str(s)
for enc in ("cp1251", "cp866"):
try:
return s.encode(enc).decode("utf-8")
except Exception:
continue
return s
def load_datasheets():
"""device -> список строк спецификаций из разобранных даташитов."""
out = {}
for d in DEV_DIRS:
for f in glob.glob(os.path.join(d, "*.yaml")):
try:
import yaml
data = yaml.safe_load(open(f, encoding="utf-8"))["device"]
except Exception:
continue
model = data.get("model") or os.path.basename(f)
lines = []
for k, vals in (data.get("specs") or {}).items():
for v in vals:
lines.append("%s: %s" % (k, v))
out[model] = lines
return out
def extract(lines):
"""Ищет значения атрибутов в строках спецификаций."""
found = {}
blob = "\n".join(lines)
for aid, (group, name, unit, typ, pats) in ATTRS.items():
for pat in pats:
m = re.search(pat, blob, re.I)
if m:
if typ == "bool":
found[aid] = True
elif typ == "number":
try:
found[aid] = float(m.group(1))
except (ValueError, IndexError):
continue
else:
found[aid] = m.group(1) if m.groups() else m.group(0)
break
return found
def main():
import yaml
ds = load_datasheets()
print("даташитов:", len(ds))
values = []
stats = {aid: 0 for aid in ATTRS}
for model, lines in ds.items():
vals = extract(lines)
if not vals:
continue
for aid in vals:
stats[aid] += 1
values.append({"device": model, "attributes": vals,
"source": "datasheet (docs/work/datasheets)"})
reg = {"groups": sorted({v[0] for v in ATTRS.values()}),
"attributes": [{"id": aid, "group": g, "name": n, "unit": u, "type": t}
for aid, (g, n, u, t, _) in ATTRS.items()]}
os.makedirs(BASE, exist_ok=True)
with open(os.path.join(BASE, "attributes.yaml"), "w", encoding="utf-8") as f:
yaml.safe_dump(reg, f, allow_unicode=True, sort_keys=False, width=140)
with open(os.path.join(BASE, "values.yaml"), "w", encoding="utf-8") as f:
yaml.safe_dump({"vendor": "yealink", "devices": values},
f, allow_unicode=True, sort_keys=False, width=160)
print("устройств со значениями: %d" % len(values))
print("\nзаполненность атрибутов:")
for aid, n in sorted(stats.items(), key=lambda x: -x[1]):
g, name, unit, typ, _ = ATTRS[aid]
print(" %-24s %-34s %2d устройств" % (aid, name, n))
return 0
if __name__ == "__main__":
sys.exit(main())