89 lines
2.1 KiB
Python
89 lines
2.1 KiB
Python
import argparse
|
|
import re
|
|
import pprint as ppr
|
|
from kiutils.libraries import LibTable # using own parser for easier env var substitution
|
|
from kiutils.symbol import SymbolLib
|
|
|
|
|
|
printer = ppr.PrettyPrinter(width=900)
|
|
pprint = printer.pprint
|
|
|
|
debug = True
|
|
|
|
|
|
def parse_inputs():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('input', type=str, help='input file')
|
|
parser.add_argument('output', type=str, help='output file')
|
|
|
|
args = parser.parse_args()
|
|
return args
|
|
|
|
|
|
def main():
|
|
args = parse_inputs()
|
|
|
|
filename = args.input
|
|
|
|
libs = table_to_dict(filename)
|
|
|
|
with open(args.output, 'w+') as f:
|
|
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):
|
|
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, check_env)
|
|
envs = {env: None for env in m}
|
|
|
|
# ask for env field values
|
|
for env, _ in envs.items():
|
|
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 l in lib.libs:
|
|
l.uri = re.sub(pattern, lambda m: envs[m.group(0)], l.uri)
|
|
|
|
# TODO: remove power symbols and not on board symbols
|
|
|
|
return lib
|
|
|
|
def symbols_to_list(path):
|
|
sym = SymbolLib.from_file(path)
|
|
|
|
symlst = [s.entryName for s in sym.symbols if (s.inBom and not s.isPower)]
|
|
|
|
pass
|
|
|
|
# with open(path, 'r') as sym_f:
|
|
# symbols = sym_f.read()
|
|
|
|
# symlst = []
|
|
|
|
# p = r'symbol "([^"]*)"'
|
|
# level = 0
|
|
# for line in symbols.splitlines():
|
|
# level += line.count('(') - line.count(')')
|
|
# name = re.search(p, line)
|
|
# if name and level == 2:
|
|
# symlst.append(name[1])
|
|
# # print(f' {level:2} |\t{line}')
|
|
pass
|
|
|
|
if __name__ == '__main__':
|
|
main() |