113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
||
"""Извлечение официальных таблиц совместимости Yealink.
|
||
|
||
Формат в руководствах:
|
||
Accessories Compatibility
|
||
1. Compatibility of <Device> with accessories
|
||
Device | Accessories | Max Qty | Note
|
||
|
||
Пишет ops/work/compat/<vendor>/<device>.yaml с провенансом.
|
||
"""
|
||
import os
|
||
import re
|
||
import subprocess
|
||
import sys
|
||
|
||
OUT = os.environ.get("COMPAT_OUT", "/opt/work-hub/compat")
|
||
|
||
ROLE_MAP = [
|
||
(r"^CPW\d|^VCM\d|^CP960|^CP700|^CP900|^MSpeaker|^CM\d|^CS\d", "audio"),
|
||
(r"^CTP\d", "touch"),
|
||
(r"^UVC\d", "camera"),
|
||
(r"^VCH\d|^WPP\d|^WF\d|^DD1\d", "hub_or_sharing"),
|
||
(r"^VCR\d", "remote"),
|
||
(r"^MVC|^MCore", "kit"),
|
||
]
|
||
|
||
|
||
def role_of(model):
|
||
for pat, role in ROLE_MAP:
|
||
if re.match(pat, model, re.I):
|
||
return role
|
||
return "other"
|
||
|
||
|
||
def text_of(pdf):
|
||
r = subprocess.run(["pdftotext", "-layout", pdf, "-"],
|
||
capture_output=True, text=True, timeout=180)
|
||
return r.stdout or ""
|
||
|
||
|
||
def parse_guide(text, source):
|
||
"""Возвращает список устройств с их аксессуарами."""
|
||
lines = text.splitlines()
|
||
devices = []
|
||
cur_dev = None
|
||
section = None
|
||
expect_header = False
|
||
|
||
for i, ln in enumerate(lines):
|
||
s = ln.strip()
|
||
m = re.match(r"^\d+\.\s*Compatibility of (.+?) with accessories", s, re.I)
|
||
if m:
|
||
cur_dev = {"device": m.group(1).strip(), "accessories": [], "source": source}
|
||
devices.append(cur_dev)
|
||
expect_header = True
|
||
continue
|
||
if not cur_dev:
|
||
continue
|
||
if re.search(r"Device\s+Accessories\s+Max Qty\s+Note", s):
|
||
expect_header = False
|
||
continue
|
||
if expect_header:
|
||
continue
|
||
|
||
# строки вида: " Audio Device CPW90-DECT 4 Note..."
|
||
m2 = re.match(r"^\s*(Audio Device|Other Accessories|Other|Camera|Accessories)?\s*"
|
||
r"([A-Z][A-Za-z0-9][\w\-/.]*)\s+(\d+|Unlimited|Nonsupport|N/A|\-)\s*(.*)$", ln)
|
||
if m2:
|
||
cat, model, qty, note = m2.group(1), m2.group(2), m2.group(3), m2.group(4).strip()
|
||
if model.lower() in ("device", "accessories"):
|
||
continue
|
||
try:
|
||
q = int(qty)
|
||
except ValueError:
|
||
q = 0 if qty.lower().startswith("nonsupport") else None
|
||
cur_dev["accessories"].append({
|
||
"model": model, "role": role_of(model), "max_qty": q,
|
||
"note": re.sub(r"\s{2,}", " ", note)[:200] or None,
|
||
})
|
||
return [d for d in devices if d["accessories"]]
|
||
|
||
|
||
def main():
|
||
if len(sys.argv) < 2:
|
||
print("использование: parse_compat.py <pdf> [<pdf> ...]")
|
||
return 1
|
||
import yaml
|
||
os.makedirs(OUT, exist_ok=True)
|
||
total_dev = total_acc = 0
|
||
for pdf in sys.argv[1:]:
|
||
text = text_of(pdf)
|
||
src = os.path.basename(pdf)
|
||
devs = parse_guide(text, src)
|
||
for d in devs:
|
||
safe = re.sub(r"[^A-Za-z0-9._-]+", "_", d["device"])[:60]
|
||
path = os.path.join(OUT, "yealink", safe + ".yaml")
|
||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||
with open(path, "w", encoding="utf-8") as f:
|
||
yaml.safe_dump({"compatibility": {
|
||
"vendor": "yealink", "device": d["device"],
|
||
"source": {"file": src, "kind": "official_guide", "verified": True},
|
||
"accessories": d["accessories"]}},
|
||
f, allow_unicode=True, sort_keys=False, width=160)
|
||
total_dev += 1
|
||
total_acc += len(d["accessories"])
|
||
print(" %-28s аксессуаров: %2d" % (d["device"][:28], len(d["accessories"])))
|
||
print("\nустройств: %d | рёбер совместимости: %d" % (total_dev, total_acc))
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|