From 3a0e23f813f35700f084a01e529daf3e4b8476bf Mon Sep 17 00:00:00 2001 From: Seppl Date: Thu, 25 Apr 2024 00:46:01 +0200 Subject: [PATCH] change to kiutils --- exporter.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/exporter.py b/exporter.py index c5d92ab..b802dda 100644 --- a/exporter.py +++ b/exporter.py @@ -1,7 +1,7 @@ import argparse import re import pprint as ppr -# from kiutils.libraries import LibTable # using own parser for easier env var substitution +from kiutils.libraries import LibTable # using own parser for easier env var substitution from kiutils.symbol import SymbolLib @@ -28,20 +28,23 @@ def main(): libs = table_to_dict(filename) with open(args.output, 'w+') as f: - for name, path in libs.items(): - if libs['lib_table_type'] == 'sym_lib_table': + for name, path in libs['libs'].items(): + if libs['type'] == 'sym_lib_table': ent = symbols_to_list(path) pass pass def table_to_dict(filename): - with open(filename, 'r') as f: - lib_table = f.read() + lib = LibTable.from_file(filename) + + # with open(filename, 'r') as f: + # lib_table = f.read() + check_env = '\n'.join([l.uri for l in lib.libs]) # check for env fields p = r'\${.*}' - m = re.findall(p, lib_table) + m = re.findall(p, check_env) envs = {env: None for env in m} # ask for env field values @@ -49,21 +52,16 @@ def table_to_dict(filename): env_cleaned = env.strip('$').strip('}').strip('{') envs[env] = input(f'Please enter the path for {env_cleaned}: ') + # and build pattern + pattern = '|'.join(sorted(re.escape(k) for k in envs)) + # replace envs - for env, val in envs.items(): - lib_table = re.sub(fr'\{env}', val, lib_table) + for l in lib.libs: + l.uri = re.sub(pattern, lambda m: envs[m.group(0)], l.uri) - libs = {} - p = r'name "([^"]*)".*type "([^"]*)".*uri "([^"]*)"' - for line in lib_table.splitlines(): - m = re.search(p, line) - if not m: continue - if m[2] == 'KiCad': - libs[m[1]] = m[3] - - libs['lib_table_type'] = lib_table.splitlines()[0][1:] + # TODO: remove power symbols and not on board symbols - return libs + return lib def symbols_to_list(path): sym = SymbolLib.from_file(path)