96 lines
3.3 KiB
Python
Executable File
96 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""Сводит подтверждённые LLM-данные в каталог Комбайна (values.yaml).
|
||
|
||
Правила:
|
||
* не перезаписываем уже существующие значения;
|
||
* добавляем только подтверждённое (verified.yaml);
|
||
* каждая добавка фиксируется в proposals/applied.yaml с цитатой;
|
||
* сопоставление имён — по коду модели (A30 = MeetingBar A30).
|
||
"""
|
||
import re
|
||
import shutil
|
||
import sys
|
||
import time
|
||
|
||
import yaml
|
||
|
||
BASE = "/opt/work-hub/compat"
|
||
VAL = BASE + "/values.yaml"
|
||
VER = BASE + "/proposals/verified.yaml"
|
||
APP = BASE + "/proposals/applied.yaml"
|
||
|
||
|
||
def norm(s):
|
||
s = str(s or "").lower()
|
||
s = re.sub(r"\(.*?\)", " ", s)
|
||
s = re.sub(r"[^a-z0-9]+", " ", s)
|
||
return " ".join(s.split())
|
||
|
||
|
||
def code(s):
|
||
"""Код модели: a30, bh72, vch51pro"""
|
||
n = norm(s)
|
||
toks = [t for t in n.split() if re.search(r"\d", t)]
|
||
if toks:
|
||
return toks[-1]
|
||
return n.replace(" ", "")
|
||
|
||
|
||
def main():
|
||
dry = "--dry" in sys.argv
|
||
vals = yaml.safe_load(open(VAL, encoding="utf-8")) or {}
|
||
ver = yaml.safe_load(open(VER, encoding="utf-8")) or {}
|
||
devs = vals.setdefault("devices", [])
|
||
|
||
by_code = {}
|
||
for d in devs:
|
||
by_code.setdefault(code(d.get("device")), d)
|
||
|
||
props = ver.get("proposals") or []
|
||
added, skipped_present, unmatched = [], 0, []
|
||
for p in props:
|
||
name = p.get("device") or ""
|
||
target = by_code.get(code(name))
|
||
if target is None:
|
||
unmatched.append(name)
|
||
continue
|
||
attrs = target.setdefault("attributes", {})
|
||
for k, v in (p.get("attributes") or {}).items():
|
||
if k in attrs:
|
||
skipped_present += 1
|
||
continue
|
||
attrs[k] = v
|
||
added.append({"device": target.get("device"), "attr": k, "value": v,
|
||
"citation": (p.get("citations") or [{}])[0].get("url", ""),
|
||
"source": "datasheet-llm"})
|
||
|
||
print(" устройств в каталоге: %d" % len(devs))
|
||
print(" предложений проверенных: %d (%d устройств)" % (len(props), ver.get("devices")))
|
||
print(" добавлено атрибутов: %d" % len(added))
|
||
print(" уже были (не тронуты): %d" % skipped_present)
|
||
print(" не сопоставлено устройств: %d %s" % (len(unmatched), unmatched[:5]))
|
||
for a in added[:8]:
|
||
print(" %-22s %-24s = %s" % (a["device"], a["attr"], a["value"]))
|
||
|
||
if dry:
|
||
print("\n сухой прогон — файл не изменён")
|
||
return
|
||
|
||
if added:
|
||
shutil.copy(VAL, VAL + ".bak-llm-" + time.strftime("%Y%m%d-%H%M"))
|
||
io.open(VAL, "w", encoding="utf-8").write(
|
||
yaml.safe_dump(vals, allow_unicode=True, sort_keys=False))
|
||
old = []
|
||
try:
|
||
old = (yaml.safe_load(open(APP, encoding="utf-8")) or {}).get("applied") or []
|
||
except Exception:
|
||
pass
|
||
io.open(APP, "w", encoding="utf-8").write(yaml.safe_dump(
|
||
{"updated": time.strftime("%Y-%m-%dT%H:%M"), "applied": old + added},
|
||
allow_unicode=True, sort_keys=False))
|
||
print("\n записано в values.yaml, аудит: applied.yaml")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|