change to kiutils

This commit is contained in:
2024-04-25 00:46:01 +02:00
parent 4c3cdc2ab2
commit 3a0e23f813

View File

@@ -1,7 +1,7 @@
import argparse import argparse
import re import re
import pprint as ppr 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 from kiutils.symbol import SymbolLib
@@ -28,20 +28,23 @@ def main():
libs = table_to_dict(filename) libs = table_to_dict(filename)
with open(args.output, 'w+') as f: with open(args.output, 'w+') as f:
for name, path in libs.items(): for name, path in libs['libs'].items():
if libs['lib_table_type'] == 'sym_lib_table': if libs['type'] == 'sym_lib_table':
ent = symbols_to_list(path) ent = symbols_to_list(path)
pass pass
pass pass
def table_to_dict(filename): def table_to_dict(filename):
with open(filename, 'r') as f: lib = LibTable.from_file(filename)
lib_table = f.read()
# 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 # check for env fields
p = r'\${.*}' p = r'\${.*}'
m = re.findall(p, lib_table) m = re.findall(p, check_env)
envs = {env: None for env in m} envs = {env: None for env in m}
# ask for env field values # ask for env field values
@@ -49,21 +52,16 @@ def table_to_dict(filename):
env_cleaned = env.strip('$').strip('}').strip('{') env_cleaned = env.strip('$').strip('}').strip('{')
envs[env] = input(f'Please enter the path for {env_cleaned}: ') 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 # replace envs
for env, val in envs.items(): for l in lib.libs:
lib_table = re.sub(fr'\{env}', val, lib_table) l.uri = re.sub(pattern, lambda m: envs[m.group(0)], l.uri)
libs = {} # TODO: remove power symbols and not on board symbols
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:] return lib
return libs
def symbols_to_list(path): def symbols_to_list(path):
sym = SymbolLib.from_file(path) sym = SymbolLib.from_file(path)