Files

94 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""Комплектация и ограничения из даташитов Yealink.
Ищет секции «Package includes», «Package Contents», «Optional package»
и ограничения вида «not suitable for», «is not compatible with».
"""
import glob
import os
import re
import sys
BASE = "/opt/work-hub/compat"
DEV = "/opt/work-hub/devices/yealink"
def clean(s):
s = re.sub(r"\s+", " ", str(s)).strip()
return s
def split_items(text):
"""' - CP50 - DC power adapter ... - 7.5m Ethernet cable' -> список."""
parts = re.split(r"\s*[-–—]\s*", text)
out = []
for p in parts:
p = clean(p)
p = re.sub(r"^\*?\s*\d*\s*", "", p)
if len(p) < 3:
continue
if re.search(r"package|includes|content", p, re.I) and len(p) < 30:
continue
out.append(p)
return out[:12]
def main():
import yaml
pkg, limits = {}, {}
for f in glob.glob(os.path.join(DEV, "*.yaml")):
try:
d = yaml.safe_load(open(f, encoding="utf-8"))["device"]
except Exception:
continue
model = re.sub(r"\s*\(\d+\)?\s*$", "", d.get("model") or "").strip()
lines = []
for k, vals in (d.get("specs") or {}).items():
for v in vals:
lines.append(clean("%s: %s" % (k, v)) if k else clean(v))
blob = "\n".join(lines)
# комплектация
m = re.search(r"(?:Package includes|Package Contents?|Package Features?)[:\s]*(.{10,600})",
blob, re.I | re.S)
if m:
items = split_items(m.group(1))
if items:
pkg[model] = {"items": items,
"evidence": clean(m.group(0))[:260]}
# ограничения
found = []
for pat in (r"[^.\n]{0,120}not suitable for[^.\n]{0,120}",
r"[^.\n]{0,120}(?:is not compatible with|are not compatible with)[^.\n]{0,120}",
r"[^.\n]{0,120}cannot be (?:used|connected)[^.\n]{0,120}",
r"[^.\n]{0,120}only compatible with[^.\n]{0,120}"):
for mm in re.finditer(pat, blob, re.I):
t = clean(mm.group(0))
if 15 < len(t) < 240:
found.append(t)
if found:
limits[model] = sorted(set(found))[:6]
with open(os.path.join(BASE, "package.yaml"), "w", encoding="utf-8") as f:
yaml.safe_dump({"source": "даташиты Yealink, разделы Package",
"devices": [{"device": k, **v} for k, v in sorted(pkg.items())]},
f, allow_unicode=True, sort_keys=False, width=200)
with open(os.path.join(BASE, "limitations.yaml"), "w", encoding="utf-8") as f:
yaml.safe_dump({"source": "даташиты Yealink, ограничения",
"devices": [{"device": k, "limits": v} for k, v in sorted(limits.items())]},
f, allow_unicode=True, sort_keys=False, width=200)
print("комплектация найдена у:", len(pkg))
for k, v in list(pkg.items())[:8]:
print(" %-20s %s" % (k[:20], "; ".join(v["items"][:5])[:110]))
print("\nограничения найдены у:", len(limits))
for k, v in list(limits.items())[:6]:
print(" %-20s %s" % (k[:20], v[0][:110]))
print("\nсохранено: package.yaml, limitations.yaml")
return 0
if __name__ == "__main__":
sys.exit(main())