#!/usr/bin/env python3 """Парсит «Rooms Accessories Compatibility Lists» (строки=аксессуары, столбцы=хосты) и добавляет рёбра в граф — закрывает пробел A40/A50. """ import glob import os import re import sys BASE = "/opt/work-hub/compat" OUT = BASE 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 parse_qty(note): m = re.search(r"up to\s*(\d+)", note, re.I) return int(m.group(1)) if m else None def main(): import yaml files = glob.glob(os.path.join(BASE, "raw", "rooms-accessories-compatibility-lists-*.yaml")) if not files: print("нет файла rooms-accessories-compatibility-lists") return 1 d = yaml.safe_load(open(files[0], encoding="utf-8")) edges = [] hosts = set() for table in d.get("tables") or []: if not table: continue header = [fix(c).strip() for c in table[0]] for row in table[1:]: if not row: continue item = fix(row[0]).strip() if not item: continue for i, cell in enumerate(row[1:], start=1): host = header[i] if i < len(header) else "" host = re.sub(r"\s+", " ", fix(host)).strip() if not host: continue c = fix(cell).strip() if not c: continue supported = bool(re.search(r"[√✓✔]", c)) if not supported and re.search(r"[×✗✘]", c): supported = False elif not supported: continue hosts.add(host) note = re.sub(r"^[√✓✔×✗✘\s]+", "", c)[:200] edges.append({"host": host, "item": item, "supported": supported, "max_qty": parse_qty(note), "condition": note or None}) print("хостов:", len(hosts)) for h in sorted(hosts): n = sum(1 for e in edges if e["host"] == h) sup = sum(1 for e in edges if e["host"] == h and e["supported"]) print(" %-24s рёбер %2d (поддержано %2d)" % (h[:24], n, sup)) # сохраняем как отдельный источник path = os.path.join(OUT, "accessories_by_host.yaml") with open(path, "w", encoding="utf-8") as f: yaml.safe_dump({"source": {"file": os.path.basename(files[0]), "kind": "official_support_page", "url": "https://support.yealink.com/docs/a50/rooms-accessories-compatibility-lists/f4c068aafd024b338340ddb628f1c0f5", "verified": True}, "edges": edges}, f, allow_unicode=True, sort_keys=False, width=200) print("\nсохранено:", path) print("всего рёбер:", len(edges)) # показываем A40/A50 for want in ("MeetingBar A40", "MeetingBar A50"): rows = [e for e in edges if e["host"] == want and e["supported"]] print("\n%s: поддержано %d" % (want, len(rows))) for e in rows[:12]: print(" %-16s до %-4s %s" % (e["item"], e["max_qty"] or "—", (e["condition"] or "")[:40])) return 0 if __name__ == "__main__": sys.exit(main())