Files
buryan-infra/ops/work/tools/parse_matrix_extra.py

66 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Парсит остальные листы матрицы: прошивки и Audio Input/Output."""
import os
import re
import sys
OUT = os.environ.get("COMPAT_OUT", "/opt/work-hub/compat")
def fix(s):
if s is None:
return ""
s = str(s)
for enc in ("cp1251", "cp866"):
try:
return s.encode(enc).decode("utf-8")
except Exception:
continue
return s
def main():
import openpyxl
import yaml
path = sys.argv[1] if len(sys.argv) > 1 else "/opt/work-hub/compat-src/yealink-matrix.xlsx"
wb = openpyxl.load_workbook(path, data_only=True)
# --- прошивки ---
for name in wb.sheetnames:
if "irmware" not in name:
continue
ws = wb[name]
rows = []
for r in ws.iter_rows(values_only=True):
cells = [fix(c).strip() for c in r if c is not None]
if cells:
rows.append(cells)
os.makedirs(os.path.join(OUT, "matrix"), exist_ok=True)
with open(os.path.join(OUT, "matrix", "_firmware.yaml"), "w", encoding="utf-8") as f:
yaml.safe_dump({"source": {"file": os.path.basename(path), "sheet": name},
"rows": rows}, f, allow_unicode=True, sort_keys=False, width=160)
print("прошивки: строк %d -> _firmware.yaml" % len(rows))
# --- audio I/O ---
for name in wb.sheetnames:
if "udio" not in name:
continue
ws = wb[name]
rows = []
for r in ws.iter_rows(values_only=True):
cells = [fix(c).strip() for c in r]
if any(cells):
rows.append([c for c in cells if c])
with open(os.path.join(OUT, "matrix", "_audio_io.yaml"), "w", encoding="utf-8") as f:
yaml.safe_dump({"source": {"file": os.path.basename(path), "sheet": name},
"rows": rows}, f, allow_unicode=True, sort_keys=False, width=200)
print("audio I/O: строк %d -> _audio_io.yaml" % len(rows))
print(" примеры:")
for r in rows[:6]:
print(" ", " | ".join(c[:28] for c in r[:4]))
return 0
if __name__ == "__main__":
sys.exit(main())