Files

149 lines
5.7 KiB
Python

#!/usr/bin/env python3
"""Извлечение таблиц совместимости Yealink через pdfplumber (финальный подход).
Находит страницы с заголовком "Accessories Compatibility" и берёт таблицы
с колонками Device | Accessories | Max Qty | Note.
"""
import os
import re
import sys
OUT = os.environ.get("COMPAT_OUT", "/opt/work-hub/compat")
def role_of(m):
for pat, role in [
(r"^CPW\d|^VCM\d|^CP9\d\d|^CP7\d\d|^MSpeaker|^CM\d|^CS\d|^BT\d|Dect", "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"),
]:
if re.match(pat, m, re.I):
return role
return "other"
def clean(x):
if x is None:
return ""
return re.sub(r"\s+", " ", str(x)).strip()
JUNK_RE = re.compile(
r"^(Recommended solution|Name|Custom Key|Mute Key|Device|Accessories|"
r"\u2022|Note|Model|Description)", re.I)
def is_junk(model):
"""Отсекаем не-аксессуары: длинные фразы, заголовки других таблиц, маркеры."""
if not model or JUNK_RE.match(model):
return True
if model.startswith("\u2022") or model.startswith("-"):
return True
if len(model) > 26: # длинная фраза, а не модель
return True
if len(model.split()) > 3: # «VCS device: VC800 Pickup device»
return True
if ":" in model:
return True
return False
def parse(pdf, plumber):
devices = {}
cur = None
with plumber.open(pdf) as doc:
for page in doc.pages:
txt = page.extract_text() or ""
# определяем устройство на странице
m = re.search(r"Compatibility of (.+?) with accessories", txt, re.I)
if m:
cur = m.group(1).strip()
devices.setdefault(cur, [])
if not cur:
continue
if "Accessories Compatibility" not in txt and "Max Qty" not in txt:
continue
for table in page.extract_tables() or []: # MAXQTY_GATE
head = " ".join(clean(c) for c in (table[0] if table else [])).lower()
if "max" not in head and "qty" not in head:
continue
for row in table:
cells = [clean(c) for c in (row or [])]
if len(cells) < 3:
continue
model, qty, note = cells[1] if len(cells) > 3 else cells[1], None, ""
# типовой порядок: [None/cat, model, qty, note]
if len(cells) >= 4:
model, qty, note = cells[1], cells[2], " ".join(cells[3:])
else:
model, qty = cells[1], cells[2]
note = " ".join(cells[3:]) if len(cells) > 3 else ""
if is_junk(model):
continue
try:
q = int(qty)
except (TypeError, ValueError):
q = 0 if str(qty).lower().startswith("nonsupport") else None
devices[cur].append({"model": model, "role": role_of(model),
"max_qty": q, "note": note[:200] or None})
return devices
def dedupe(acc):
"""DEDUPE: убираем повторы модели внутри одного устройства (multi-page таблицы)."""
seen, out = {}, []
for a in acc:
key = a["model"].lower().replace(" ", "")
if key in seen:
prev = seen[key]
# оставляем максимальный лимит и самую длинную заметку
if a["max_qty"] and (prev["max_qty"] is None or a["max_qty"] > prev["max_qty"]):
prev["max_qty"] = a["max_qty"]
if a["note"] and len(a["note"]) > len(prev["note"] or ""):
prev["note"] = a["note"]
else:
seen[key] = dict(a)
out.append(seen[key])
return out
def main():
import pdfplumber
import yaml
if len(sys.argv) < 2:
print("использование: parse_compat3.py <pdf> ...")
return 1
merged = {}
for pdf in sys.argv[1:]:
devs = parse(pdf, pdfplumber)
for name, acc in devs.items():
acc = dedupe(acc)
if not acc:
continue
key = name.lower()
if key in merged:
have = {a["model"] for a in merged[key]["accessories"]}
merged[key]["accessories"] += [a for a in acc if a["model"] not in have]
else:
merged[key] = {"device": name, "accessories": acc,
"source": os.path.basename(pdf)}
total = 0
for key, d in sorted(merged.items()):
safe = re.sub(r"[^A-Za-z0-9._-]+", "_", d["device"])[:60]
os.makedirs(os.path.join(OUT, "yealink"), exist_ok=True)
with open(os.path.join(OUT, "yealink", safe + ".yaml"), "w", encoding="utf-8") as f:
yaml.safe_dump({"compatibility": {
"vendor": "yealink", "device": d["device"],
"source": {"file": d["source"], "kind": "official_guide", "verified": True},
"accessories": d["accessories"]}}, f,
allow_unicode=True, sort_keys=False, width=160)
total += len(d["accessories"])
print(" %-24s аксессуаров: %2d" % (d["device"][:24], len(d["accessories"])))
print("\nустройств: %d | рёбер: %d" % (len(merged), total))
if __name__ == "__main__":
sys.exit(main())