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 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)