"""Code-name pairs from the hidden SAP BW (BEx) sheets inside the budget law annex files. The printed annex tables show line names only; the hidden ZQZBC_* sheets carry the code next to the name. Input: source//P01.XLSX, P02.XLSX, P03.XLSX, P05.XLSX, P11.XLSX Output: source/kodi/codepairs.json (name -> [codes]) Usage: python tools/kodi_no_sap.py [year] (default 2026) """ import sys, os, re, json, collections TOOLS = os.path.dirname(os.path.abspath(__file__)) ROOT = os.environ.get('BUDGET_ROOT', os.path.dirname(TOOLS)) sys.path.insert(0, TOOLS) from rd import sheets year = sys.argv[1] if len(sys.argv) > 1 else '2026' pairs = collections.OrderedDict() for f in ('P01.XLSX', 'P02.XLSX', 'P05.XLSX', 'P03.XLSX', 'P11.XLSX'): for name, rows in sheets(os.path.join(ROOT, 'source', year, f)): if not name.startswith('ZQZ'): continue for r in rows: for i in range(len(r) - 1): c, nm = str(r[i]), r[i + 1] if re.fullmatch(r'[A-Z]{0,3}\d[\dA-Z]*|[A-Z]{1,5}\d?T?|\d{4,5}', c) and isinstance(nm, str) and len(nm) > 3 and not nm.startswith('Nosaukums'): pairs.setdefault(nm.strip(), set()).add(c) json.dump({k: sorted(v) for k, v in pairs.items()}, open(os.path.join(ROOT, 'source', 'kodi', 'codepairs.json'), 'w', encoding='utf-8'), ensure_ascii=False, indent=0) print('pairs', len(pairs))