From 4c3cdc2ab2c7182fff51d986285a526d9e7e7568 Mon Sep 17 00:00:00 2001 From: Seppl Date: Thu, 25 Apr 2024 00:29:08 +0200 Subject: [PATCH] first commit --- .gitignore | 3 ++ .vscode/launch.json | 17 +++++++++ README.md | 8 +++- exporter.py | 91 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json create mode 100644 exporter.py diff --git a/.gitignore b/.gitignore index 5d381cc..3a41b85 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,6 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +fp-lib-table +sym-lib-table +symbols.txt diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..edb3f0e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "exporter sym-lib", + "type": "debugpy", + "request": "launch", + "program": "exporter.py", + "args": [".\\sym-lib-table", ".\\symbols.txt"], + "console": "integratedTerminal", + "justMyCode": false + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index e8150d8..fb79140 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # kicad-symbol-exporter -exports a list of all available symbols and footprints in the format `LIBRARY:PART` to one text file each \ No newline at end of file +exports a list of all available symbols/footprints in the format `LIBRARY:PART` to a text file + +## usage +``` +python fp-lib-table footprints.txt +python sym-lib-table symbols.txt +``` \ No newline at end of file diff --git a/exporter.py b/exporter.py new file mode 100644 index 0000000..c5d92ab --- /dev/null +++ b/exporter.py @@ -0,0 +1,91 @@ +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.items(): + if libs['lib_table_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() + + # check for env fields + p = r'\${.*}' + m = re.findall(p, lib_table) + 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}: ') + + # replace envs + for env, val in envs.items(): + lib_table = re.sub(fr'\{env}', val, lib_table) + + 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:] + + return libs + +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() \ No newline at end of file