Files
buryan-infra/ops/work/tools/build_matrix_xlsx.py
T

163 lines
6.3 KiB
Python
Raw 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
"""Сводная матрица атрибутов из ВСЕХ даташитов Yealink -> XLSX с вкладками.
Листы: Сводка, Камера, Аудио, Сеть, Интерфейсы, Физика, Источники.
Каждая ячейка = значение из конкретного даташита (с провенансом на листе «Источники»).
"""
import glob
import os
import re
import sys
SRC = "/opt/work-hub/datasheets-src/Datasheet/Yealink"
OUT = "/opt/work-hub/compat"
ATTR_FILE = os.path.join(OUT, "attributes.yaml")
GROUP_SHEETS = {
"Камера": ["camera.zoom_optical", "camera.zoom_hybrid", "camera.zoom_digital",
"camera.fov_h", "camera.fov_v", "camera.presets", "camera.pan_range",
"camera.tilt_range", "camera.dof_min", "camera.max_resolution"],
"Аудио": ["audio.pickup_m", "audio.spl_max", "audio.mic_count", "audio.snr",
"audio.freq_min", "audio.freq_max_khz", "audio.expansion_mics"],
"Сеть": ["network.poe", "network.usb_c", "network.wifi", "network.bluetooth",
"network.ethernet_mbps"],
"Интерфейсы": ["ports.hdmi_out", "ports.hdmi_in", "ports.usb", "ports.vch"],
"Физика": ["physical.weight_kg", "physical.temp_min"],
}
def clean_name(n):
n = re.sub(r"\s*\(\d+\)?\s*$", "", str(n or "")).strip()
n = re.sub(r"\s+", " ", n)
return n
def main():
import yaml
ds_base = os.environ.get("DS_BASE", SRC)
pdfs = []
for root, _, files in os.walk(ds_base):
for f in files:
if f.lower().endswith(".pdf"):
pdfs.append(os.path.join(root, f))
print("PDF найдено:", len(pdfs))
# берём только те, где есть слово datasheet/product introduction/specification
cand = [p for p in pdfs if re.search(r"datasheet|product\s*introduction|specification",
os.path.basename(p), re.I)]
print("кандидатов в даташиты:", len(cand))
# парсим атрибуты из уже разобранных YAML (быстро)
parsed = {}
for f in glob.glob("/opt/work-hub/devices/yealink/*.yaml"):
try:
d = yaml.safe_load(open(f, encoding="utf-8"))["device"]
except Exception:
continue
name = clean_name(d.get("model"))
lines = []
for k, vals in (d.get("specs") or {}).items():
lines += ["%s: %s" % (k, v) for v in vals]
parsed[name] = lines
print("разобранных даташитов (YAML):", len(parsed))
# реестр атрибутов
reg = yaml.safe_load(open(ATTR_FILE, encoding="utf-8"))
attrs = {a["id"]: a for a in reg.get("attributes") or []}
# значения
vals = {}
for dev, lines in parsed.items():
blob = "\n".join(lines)
got = {}
for aid, a in attrs.items():
from build_attributes2 import ATTRS, PLAUSIBLE, norm_units # noqa
if aid not in ATTRS:
continue
g, n, u, typ, pats = ATTRS[aid]
for pat in pats:
m = re.search(pat, blob, re.I)
if not m:
continue
if typ == "bool":
got[aid] = True
elif typ == "number":
try:
v = float(m.group(1))
except (ValueError, IndexError):
continue
lo, hi = PLAUSIBLE.get(aid, (-1e9, 1e9))
if not (lo <= v <= hi):
continue
got[aid] = norm_units(aid, v, blob)
else:
got[aid] = m.group(1) if m.groups() else m.group(0)
break
if got:
dev = clean_name(dev)
if dev in vals:
for k, v in got.items():
vals[dev].setdefault(k, v)
else:
vals[dev] = got
print("устройств со значениями:", len(vals))
# XLSX
import openpyxl
from openpyxl.styles import Font, Alignment, PatternFill
wb = openpyxl.Workbook()
hdr_fill = PatternFill("solid", fgColor="1F4E78")
hdr_font = Font(color="FFFFFF", bold=True)
# сводка
ws = wb.active
ws.title = "Сводка"
ws.append(["Устройство"] + [a["name"] for a in attrs.values()] + ["Заполнено"])
keep = {d: v for d, v in vals.items() if len(v) >= 2}
print("устройств в матрице (>=2 значений):", len(keep))
for dev in sorted(keep):
row = [dev] + [keep[dev].get(aid, "") for aid in attrs] + [len(keep[dev])]
ws.append(row)
for c in ws[1]:
c.fill = hdr_fill
c.font = hdr_font
ws.freeze_panes = "B2"
# листы по группам
for sheet, aids in GROUP_SHEETS.items():
w = wb.create_sheet(sheet)
cols = [a for a in aids if a in attrs]
w.append(["Устройство"] + ["%s (%s)" % (attrs[a]["name"], attrs[a]["unit"])
if attrs[a]["unit"] != "—" else attrs[a]["name"]
for a in cols])
for dev in sorted(keep):
w.append([dev] + [keep[dev].get(a, "") for a in cols])
for c in w[1]:
c.fill = hdr_fill
c.font = hdr_font
w.freeze_panes = "B2"
for col in w.columns:
w.column_dimensions[col[0].column_letter].width = 22
# источники
ws2 = wb.create_sheet("Источники")
ws2.append(["Устройство", "Файл даташита", "Статус"])
for dev in sorted(parsed):
cd = clean_name(dev)
ws2.append([cd, "Work/Datasheet/Yealink/<модель>/*.pdf",
"значения извлечены" if cd in keep else "нет значений/мало"])
for c in ws2[1]:
c.fill = hdr_fill
c.font = hdr_font
path = os.path.join(OUT, "yealink-attributes-matrix.xlsx")
wb.save(path)
print("XLSX сохранён:", path)
print("листов:", len(wb.sheetnames), "->", wb.sheetnames)
return 0
if __name__ == "__main__":
sys.path.insert(0, "/root/buryan-infra/ops/work/tools")
sys.exit(main())