add support for sym lib tables
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -163,3 +163,4 @@ cython_debug/
|
||||
fp-lib-table
|
||||
sym-lib-table
|
||||
symbols.txt
|
||||
envs.toml
|
||||
|
||||
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -9,7 +9,7 @@
|
||||
"type": "debugpy",
|
||||
"request": "launch",
|
||||
"program": "exporter.py",
|
||||
"args": [".\\sym-lib-table", ".\\symbols.txt"],
|
||||
"args": [".\\sym-lib-table", ".\\symbols.txt", "-e", ".\\envs.toml"],
|
||||
"console": "integratedTerminal",
|
||||
"justMyCode": false
|
||||
}
|
||||
|
||||
2
envs.toml.template
Normal file
2
envs.toml.template
Normal file
@@ -0,0 +1,2 @@
|
||||
KICAD8_3RD_PARTY = '...\KiCad\8.0\3rdparty'
|
||||
KICAD8_SYMBOL_DIR = '...\KiCad\8.0\share\kicad\symbols\'
|
||||
91
exporter.py
91
exporter.py
@@ -1,8 +1,11 @@
|
||||
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
|
||||
from kiutils.symbol import SymbolLib
|
||||
import tomli
|
||||
import tomli_w
|
||||
import pathlib
|
||||
|
||||
|
||||
printer = ppr.PrettyPrinter(width=900)
|
||||
@@ -15,6 +18,8 @@ def parse_inputs():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('input', type=str, help='input file')
|
||||
parser.add_argument('output', type=str, help='output file')
|
||||
parser.add_argument('-e', type=str, help='file containing replacements for the env vars (format: ENV = path)')
|
||||
parser.add_argument('-s', action='store_true', default=False, help='save env vars to env.toml')
|
||||
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
@@ -25,41 +30,54 @@ def main():
|
||||
|
||||
filename = args.input
|
||||
|
||||
libs = table_to_dict(filename)
|
||||
envs = load_envs(args)
|
||||
|
||||
libs = table_to_dict(filename, envs)
|
||||
|
||||
if args.s:
|
||||
with open('envs.toml', "wb") as f:
|
||||
tomli_w.dump(envs, f)
|
||||
|
||||
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
|
||||
|
||||
for lib in libs.libs:
|
||||
if libs.type == 'sym_lib_table':
|
||||
write_syms_to_file(f, lib)
|
||||
if libs.type == 'fp_lib_table':
|
||||
pass
|
||||
# TODO
|
||||
pass
|
||||
|
||||
def table_to_dict(filename):
|
||||
def write_syms_to_file(f, lib):
|
||||
if lib.type == 'KiCad':
|
||||
syms = symbols_to_list(lib.uri)
|
||||
if syms:
|
||||
for sym in syms:
|
||||
print(f'{lib.name}:{sym}')
|
||||
f.write(f'{lib.name}:{sym}\n')
|
||||
|
||||
def load_envs(args):
|
||||
if args.e:
|
||||
with open(args.e, 'rb') as settings:
|
||||
envs = tomli.load(settings)
|
||||
for k, v in envs.items():
|
||||
envs[k] = pathlib.Path(v).as_posix()
|
||||
return envs
|
||||
else:
|
||||
return None
|
||||
|
||||
def table_to_dict(filename, envs = None):
|
||||
p = r'\${(.*)}'
|
||||
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
|
||||
m = re.search(p, l.uri)
|
||||
if m: #found env
|
||||
try:
|
||||
l.uri = re.sub(p, fr'{envs[m.group(1)]}', l.uri) #replace it
|
||||
except KeyError:
|
||||
envs[m.group(1)] = pathlib.Path(input(f'Please enter the path for {m.group(1)}: ')).as_posix() # if not found, ask for replacement value
|
||||
l.uri = re.sub(p, envs[m.group(1)], l.uri) # and replace it
|
||||
|
||||
return lib
|
||||
|
||||
@@ -68,22 +86,7 @@ def symbols_to_list(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
|
||||
return symlst
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user