-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdissect_lib3.py
167 lines (154 loc) · 5.28 KB
/
dissect_lib3.py
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
# Manipulates a KiCad design to put rescue symbols into a library
# While pieces of this program might be reused, this specifically targets
# https://github.com/BerkeleyLab/Marble
# commit f815339cb69ce2841891022435b374af312367b9
# Expected command-line:
# rm kicad_libs/*; python scripts/dissect_lib3.py AMC_FMC_Carrier-PcbDoc-cache.lib
# It edits all .sch files in-place, creates libraries in kicad_libs/,
# and makes a new sym-lib-table file.
# You can set test_only (below) to True as a debugging aid.
# But really, you have to trust git, e.g., git reset --hard,
# to be comfortable developing and running this program.
from sys import argv
from glob import iglob
from os import rename
prefixes = [
"AMC_FMC_Carrier-PcbDoc-rescue",
"Analog_&_Interface",
"Capacitors_SMD",
"Capacitors_THD",
"Connector_Generic",
"Crystals_&_Oscillators",
"Diodes",
"Fuses",
"Inductors_SMD",
"LEDs_&_Displays",
"Logic",
"Miscellaneous",
"Optocouplers",
"Pads",
"Regulators",
"Resistors_SMD",
"SAMTEC",
"Standard_Logic",
"Switch",
"TYCO",
"Transistors",
"power",
"marble_misc"]
remap = {
"AMC_FMC_Carrier-PcbDoc-rescue": "marble_misc",
"FPGA_Xilinx_Kintex7": "marble_misc",
"MCU_ST_STM32F2": "marble_misc",
"Miscellaneous2": "Miscellaneous",
"SamacSys_Parts": "marble_misc",
"Sockets": "TYCO",
"power2": "power",
"Connector": "Connector_Generic"
}
map2 = {}
dest = None
outfile = None
test_only = False
# Almost the same as a search-and-replace
# Implicitly depends on the value of global map2
# Example modification:
# -L FPGA_Xilinx_Kintex7:XC7K160T-FFG676 U?
# +L marble_misc:XC7K160T-FFG676 U?
def process_schem(filen):
print("Processing " + filen)
ofile = filen + "2"
with open(filen, "r") as fdi:
with open(ofile, "w") as fdo:
for ll in fdi.readlines():
aa = ll.split()
if len(aa) == 3 and aa[0] == "L":
if aa[1] in map2:
aa[1] = map2[aa[1]]
ll = " ".join(aa) + "\n"
else:
print("map2 lookup failed for " + aa[1])
fdo.write(ll)
if not test_only:
rename(ofile, filen)
def select_dest(pp):
global dest, outfile
if pp != dest:
if outfile is not None:
outfile.close()
dest = pp
libname = "kicad_libs/%s.lib" % dest
try:
chk1 = open(libname, "r")
chk1.close()
# File already existed, so open as append, and don't put in header
# You probably want to rm kicad_libs/* before running this program
print("Extending " + pp)
outfile = open(libname, "a")
except IOError:
print("Creating " + pp)
outfile = open(libname, "w")
outfile.write("EESchema-LIBRARY Version 2.4\n")
outfile.write("#encoding utf-8\n")
outfile.write("#\n")
def kill_ending(s, t):
if s.endswith(t):
return True, s[0:-len(t)]
else:
return False, s
filen = argv[1] # AMC_FMC_Carrier-PcbDoc-cache.lib
print("test_only = " + str(test_only))
for ll in open(filen).readlines():
ll = ll.rstrip()
if ll[0:2] == "# ":
# print(ll)
# we need to determine
# lib1:part1 as found in the existing .sch files
# lib2:part2 as we want to rewrite the .sch files,
# and such that we will write part2 as an entry in kicad_libs/lib2.lib
part_name = ll[2:]
for pp in prefixes + sorted(remap.keys()):
if part_name.startswith(pp + "_"):
lib1, part1 = pp, part_name[len(pp)+1:]
break
else:
print("Unhandled name " + part_name)
break
lib2, part2 = lib1, part1
# if lib2 == "AMC_FMC_Carrier-PcbDoc-rescue":
# lib2 = "marble_misc"
if lib2 in remap:
lib2 = remap[lib2]
while True:
flag, part2 = kill_ending(part2, "-AMC_FMC_Carrier-PcbDoc-rescue")
if not flag:
break
flag, part2 = kill_ending(part2, "-powerMG")
flag, part2 = kill_ending(part2, "-power2")
flag, part2 = kill_ending(part2, "-powerMG2")
select_dest(lib2)
ll = "# " + part2
map2[lib1 + ":" + part1] = lib2 + ":" + part2
# this is the mapping that should apply to "^L " lines in *.sch
# print(" -e 's/" + lib1 + ":" + part1 + "/" + lib2 + ":" + part2 + "/' \\")
# print("old " + lib1 + ":" + part1)
if dest is not None and ll.startswith("DEF " + lib1 + "_"):
aa = ll.split()
ll = "DEF " + part2 + " " + " ".join(aa[2:])
elif dest is not None and ll.startswith("F1 "):
prj = "AMC_FMC_Carrier-PcbDoc-rescue"
ll = ll.replace(prj, '').strip('_-')
if outfile is not None:
outfile.write(ll + '\n')
# material headed for sym-lib-table
f1 = ' (lib (name %s)(type Legacy)(uri ${KIPRJMOD}/kicad_libs/%s.lib)(options "")(descr ""))\n'
with open("sym-lib-table-test", "w") as f2:
f2.write("(sym_lib_table\n")
for pp in prefixes:
f2.write(f1 % (pp, pp))
f2.write(")\n")
if not test_only:
rename("sym-lib-table-test", "sym-lib-table")
for ff in iglob("*.sch"):
process_schem(ff)
print("Done.")