-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub-lib-manage
executable file
·180 lines (144 loc) · 5.66 KB
/
sub-lib-manage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/python3
from enum import Enum
from pathlib import Path
import re
class LibTable:
def __init__(self, libs=None):
self.libs=[] if libs is None else libs
@classmethod
def from_file(cls, path):
c = cls()
if path.exists():
with open(path, "r") as f:
for line in f.readlines():
if line.strip().startswith("(lib "):
c.libs.append(line)
else:
print (f"Skipping non-existant library file {path}")
return c
@classmethod
def from_file_list_with_type(cls, dir_ls, type):
"Create LibTable from list of paths"
libs=[]
line_template = ' (lib (name {name})(type {type})(uri {uri})(options "")(descr ""))\n'
for path in dir_ls:
uri = "${KIPRJMOD}/" + str(path)
line = line_template.format(name=path.stem, type=type, uri=uri)
libs.append(line)
return cls(libs)
def combine(self, other):
self.libs += other.libs
def write_file_with_header(self, path, header):
with open(path, "w") as f:
f.write(header)
for lib in self.libs:
f.write(lib)
f.write(")")
def replace_var(self, var, replacement):
"In lib paths, replace all instances of var with replacement"
pattern = re.compile("\${" + var + "}")
self.libs = [pattern.sub(replacement, path) for path in self.libs]
class SymLibTable(LibTable):
def write_file(self, path="sym-lib-table"):
self.write_file_with_header(path, "(sym_lib_table\n")
@classmethod
def from_dir(cls, d):
return super().from_file_list_with_type(d.glob("*.lib"), "Legacy")
class FpLibTable(LibTable):
def write_file(self, path="fp-lib-table"):
self.write_file_with_header(path, "(fp_lib_table\n")
@classmethod
def from_dir(cls, d):
return super().from_file_list_with_type(d.glob("*.pretty"), "KiCad")
class LibTableConfig:
"Configuration for single lib table"
valid_fields = ["type", "path", "table", "pathvar"]
class InvalidConfigLine(Exception):
pass
def __init__(self, type, path, table, pathvar):
self.type = type
self.path = Path(path)
if table is None:
if type == "sym":
self.table_path = self.path / "sym-lib-table"
elif type == "fp":
self.table_path = self.path / "fp-lib-table"
else:
self.table_path = Path(table)
self.pathvar = pathvar
@staticmethod
def _get_field(text, field):
"Return named field from text, or None if not found"
regex = fr'\({field} "?([^\)"]*)"?\)'
search = re.search(regex, text)
if search is not None:
return search.groups()[0]
return None
@classmethod
def from_file_line(cls, line):
"(sublib (type) (path) (table) (pathvar))"
if line.strip().startswith("(sublib "):
entry = {}
for field_name in cls.valid_fields:
val = cls._get_field(line, field_name)
entry[field_name] = val
if entry["type"] == "sym" or entry["type"] == "fp":
return cls (**entry)
raise cls.InvalidConfigLine()
class Config:
def __init__(self):
self.tables = []
@classmethod
def from_file(cls, path):
c = cls()
with open(path, "r") as f:
for line in f.readlines():
try:
c.tables.append(LibTableConfig.from_file_line(line))
except LibTableConfig.InvalidConfigLine:
continue # Ignore invalid line
return c
def process_libs(config_file_path):
"Create lib tables from sub lib tables defined in given config file path"
config = Config.from_file(config_file_path)
root = config_file_path.parent
primary_tables = {"sym":SymLibTable(), "fp":FpLibTable()}
for conf in config.tables:
try:
primary = primary_tables[conf.type]
except KeyError:
print("Invalid lib table type!")
continue
# New classes will be same type as primary
cls = primary.__class__
if str(conf.table_path) == "AUTO":
sub_table = cls.from_dir(conf.path)
else:
table_path = root / conf.table_path
sub_table = cls.from_file(table_path)
if conf.pathvar is not None:
# Replace any instances of path variable with path within project
sub_table.replace_var(conf.pathvar, "${KIPRJMOD}/" + str(conf.path))
primary.combine(sub_table)
for typ, table in primary_tables.items():
table.write_file()
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser(description="Combine Kicad sub-library tables into primary library tables")
parser.add_argument("--config", "-c", type=Path, help="Config file describing footprint and symbol sub-libraries")
args=parser.parse_args()
if args.config is not None:
config_path = args.config
if not config_path.exists():
raise Exception(f"Config file {config_path} not found")
else:
# Walk up 3 directories, starting with current, looking for config
script_path = Path(__file__).resolve()
for depth in range(3):
config_path = script_path.parents[depth] / "sub-lib-config"
if config_path.exists():
print(f"Found config file at {config_path}")
break
if not config_path.exists():
raise Exception("No config file specified and none found")
process_libs(config_path)