import json from config_utils import collect_layout_tokens, load_datasource from factory import load_json, build_table, table_token_key, iter_table_items, PAGES_DIR def collect_tokens(cfg): tokens = collect_layout_tokens(cfg) vlans = cfg.get('vlans', []) vlan_names = [v.get('name', '') for v in vlans] res_ips_by_vlan, res_hosts_by_vlan = {}, {} for v in vlans: vn = v.get('name', '') if not vn: continue vlan_res = [r for r in cfg.get('dhcp_reservations', []) if r.get('vlan') == vn] res_ips_by_vlan[vn] = [r['ip'] for r in vlan_res if r.get('ip') and r['ip'] != 'dynamic'] res_hosts_by_vlan[vn] = [r['hostname'] for r in vlan_res if r.get('hostname')] filter_opts = '' + ''.join( f'' for n in vlan_names ) tokens['VLAN_FILTER_OPTIONS'] = filter_opts tokens['VLAN_NAMES_AS_OPTIONS'] = json.dumps([{'value': n, 'label': n} for n in vlan_names]) tokens['VLAN_SUBNET_INFO_JSON'] = json.dumps({ v.get('name', ''): {'subnet': v.get('subnet', ''), 'prefix': v.get('subnet_mask', 0)} for v in vlans if v.get('name') and v.get('subnet') }) tokens['RESERVATION_IPS_BY_VLAN_JSON'] = json.dumps(res_ips_by_vlan) tokens['RESERVATION_HOSTNAMES_BY_VLAN_JSON'] = json.dumps(res_hosts_by_vlan) content = load_json(f'{PAGES_DIR}/dhcpreservations/content.json') for table_item in iter_table_items(content.get('items', [])): ds = table_item.get('datasource', '') tokens[table_token_key(ds)] = build_table(table_item, tokens, load_datasource(ds)) return tokens