#!/usr/bin/env python3 """Извлечение портов из даташитов (v2): надёжный разбор строк Interface/Ports. Умеет разбирать склеенные строки вида: "Interface -1 x 1000Mbps RJ45 port -1 x USB 2.0 Type C -1 x PD power port" Выдаёт: устройство -> {in: [...], out: [...], evidence: [...]} """ import glob import os import re import sys BASE = "/opt/work-hub/compat" DEV = "/opt/work-hub/devices/yealink" CONNECTORS = [ (r"usb[\s-]?c|type[\s-]?c", "USB-C"), (r"usb\s*3|usb\s*2|usb\s*type|usb", "USB"), (r"hdmi", "HDMI"), (r"vch", "VCH"), (r"rj[\s-]?45|ethernet|1000mbps|lan", "LAN"), (r"displayport|\bdp\b", "DP"), (r"3\.5\s*mm|line[\s-]?in|line[\s-]?out|line", "3.5mm"), (r"rj[\s-]?9", "RJ-9"), (r"phoenix|gpio|rs232|rs-232", "PHOENIX"), (r"ops slot|ops", "OPS"), (r"sd card|microsd", "SD"), ] SPLIT = re.compile(r"(?=\d+\s*[x×*]\s*[A-Za-zА-Яа-я0-9])") def canon(desc): d = desc.lower() for pat, name in CONNECTORS: if re.search(pat, d): return name return None def direction(desc): d = desc.lower() if re.search(r"\bout(put)?\b", d): return "out" if re.search(r"\bin(put)?\b", d) or "video input" in d: return "in" if "device interface" in d or "device port" in d: return "in" return None def parse_text(text): ins, outs, unknown = [], [], [] for chunk in SPLIT.split(text): m = re.match(r"(\d+)\s*[x×*]\s*(.+)", chunk.strip()) if not m: continue n = int(m.group(1)) if not (1 <= n <= 16): continue desc = m.group(2) c = canon(desc) if not c: continue d = direction(desc) if d == "out": outs += [c] * n elif d == "in": ins += [c] * n else: unknown += [c] * n return ins, outs, unknown def main(): import yaml result = {} 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() if not model: continue lines = [] for k, vals in (d.get("specs") or {}).items(): lines += [str(v) for v in vals] # строки, где вообще есть упоминание портов cand = [ln for ln in lines if re.search(r"\d+\s*[x×*]|interface|port", ln, re.I)] ins, outs, unk = [], [], [] for ln in cand: a, b, c = parse_text(ln) ins += a outs += b unk += c if not (ins or outs or unk): continue # неизвестные по умолчанию считаем входами ins += unk def dedupe(seq): from collections import Counter return [k for k, _ in Counter(seq).most_common()] entry = {"in": dedupe(ins)[:6], "out": dedupe(outs)[:6]} prev = result.get(model) score = len(entry["in"]) + len(entry["out"]) if prev and prev["_score"] >= score: continue entry["_score"] = score entry["evidence"] = cand[:3] result[model] = entry for v in result.values(): v.pop("_score", None) path = os.path.join(BASE, "interfaces.yaml") with open(path, "w", encoding="utf-8") as f: yaml.safe_dump({"source": "даташиты (регекс по строкам Interface/Ports)", "devices": [{"device": k, **v} for k, v in sorted(result.items())]}, f, allow_unicode=True, sort_keys=False, width=190) print("устройств с портами:", len(result)) for k in ("VCH51 Pro", "WPP30", "VCH51", "AP08", "UVC86", "UVC86 E2", "MeetingEye 500", "CTP25", "MSpeaker Pro", "CM50"): v = result.get(k) print(" %-16s in=%-22s out=%s" % (k, ",".join(v["in"]) if v else "нет данных", ",".join(v["out"]) if v else "")) return 0 if __name__ == "__main__": sys.exit(main())