-
Notifications
You must be signed in to change notification settings - Fork 1
/
impart.py
executable file
·351 lines (306 loc) · 11.8 KB
/
impart.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python3
# coding: utf-8
# Builds local KiCad "legacy" component libraries from downloaded
# component zipfiles. Currently assembles just symbols and
# footptints. Tested with KiCad 6.0.7.
#
# Web sources:
#
# octopart: https://octopart.com/ or https://eeconcierge.com/
# samacsys: https://componentsearchengine.com/
# ultralibrarian: https://www.ultralibrarian.com/
# snapeda: https://www.snapeda.com/
from mydirs import SRC, LIB # *CONFIGURE ME*
import argparse
import clipboard
import re
import readline
import shutil
import signal
import zipfile
PRJ = {0: 'octopart', 1: 'samacsys', 2: 'ultralibrarian', 3: 'snapeda'}
def Signal(signum, stack):
raise UserWarning('CTRL-C')
def Xinput(prompt):
# extended input to allow Emacs input backspace
reply = input(prompt)
index = reply.find('~') + 1
return reply[index:]
class Pretext:
"""input() with inserted text"""
def __init__(self, pretext):
self._pretext = pretext
readline.set_completer(lambda: None)
readline.set_pre_input_hook(self.insert)
clipboard.copy('')
def __call__(self, prompt):
reply = Xinput(prompt + (': ' if self._pretext else ' [=clipboard]: '))
if reply == '':
text = clipboard.paste()
if text:
clipboard.copy('')
self._pretext = text.replace('\n', ' ')
reply = Xinput(prompt + ': ')
readline.set_pre_input_hook(None)
return reply.strip()
def insert(self):
readline.insert_text(self._pretext)
readline.redisplay()
class Select:
"""input() from select completions """
def __init__(self, select):
self._select = select
readline.set_completer(self.complete)
readline.set_pre_input_hook(None)
def __call__(self, prompt):
reply = Xinput(prompt)
readline.set_completer(lambda: None)
return reply.strip()
def complete(self, text, state):
if state == 0:
if text:
self._pre = [s for s in self._select
if s and s.startswith(text)]
else:
self._pre = self._select[:]
try:
echo = self._pre[state]
except IndexError:
echo = None
return echo
class Catch(Exception):
def __init__(self, value):
self.catch = value
super().__init__(self)
def Zipper(root, suffix):
"""return zipfile.Path starting with root ending with suffix"""
def zipper(parent):
if parent.name.endswith(suffix):
raise Catch(parent)
elif parent.is_dir():
for child in parent.iterdir():
zipper(child)
try:
zipper(root)
except Catch as e:
return e.catch
return None
def Impart(zip):
"""zip is a pathlib.Path to import the symbol from"""
if not zipfile.is_zipfile(zip):
return None
device = zip.name[:-4]
eec = Pretext(device)('Generic device name')
if eec == '':
return None
with zipfile.ZipFile(zip) as zf:
root = zipfile.Path(zf)
while True:
desc = root / 'eec.dcm'
symb = root / 'eec.lib'
food = root / 'eec.pretty'
if desc.exists() and symb.exists() and food.exists():
prj = 0 # OCTOPART
break
dir = Zipper(root, 'KiCad')
if dir:
desc = Zipper(dir, '.dcm')
symb = Zipper(dir, '.lib')
food = dir
assert desc and symb, 'Not in samacsys format'
prj = 1 # SAMACSYS
break
dir = root / 'KiCAD'
if dir.exists():
desc = Zipper(dir, '.dcm')
symb = Zipper(dir, '.lib')
food = Zipper(dir, '.pretty')
assert symb and food, 'Not in ultralibrarian format'
prj = 2 # ULTRALIBRARIAN
break
symb = Zipper(root, '.lib')
if symb:
desc = Zipper(root, '.dcm')
food = root
prj = 3 # SNAPEDA
break
assert False, 'Unknown library zipfile'
txt = desc.read_text().splitlines() if desc else [
'#', '# ' + device, '#', '$CMP ' + eec, 'D', 'F', '$ENDCMP']
print('Adding', eec, 'to', PRJ[prj])
stx = None
etx = None
hsh = None
for no, tx in enumerate(txt):
if stx is None:
if tx.startswith('#'):
if tx.strip() == '#' and hsh is None:
hsh = no # header start
elif tx.startswith('$CMP '):
t = tx[5:].strip()
if not t.startswith(eec):
return 'Unexpected device in', desc.name
txt[no] = tx.replace(t, eec, 1)
stx = no
else:
hsh = None
elif etx is None:
if tx.startswith('$CMP '):
return 'Multiple devices in', desc.name
elif tx.startswith('$ENDCMP'):
etx = no + 1
elif tx.startswith('D'):
t = tx[2:].strip()
dsc = Pretext(t)('Device description')
if dsc:
txt[no] = 'D ' + dsc
elif tx.startswith('F'):
t = tx[2:].strip()
url = Pretext(t)('Datasheet URL')
if url:
txt[no] = 'F ' + url
if etx is None:
return eec, 'not found in', desc.name
rd_dcm = LIB / (PRJ[prj] + '.dcm')
wr_dcm = LIB / (PRJ[prj] + '.dcm~')
update = updated = False
with rd_dcm.open('rt') as rf:
with wr_dcm.open('wt') as wf:
for tx in rf:
if re.match('# *end ', tx, re.IGNORECASE):
if not updated:
wf.write('\n'.join(txt[stx if hsh is None else hsh:
etx]) + '\n')
wf.write(tx)
break
elif tx.startswith('$CMP '):
t = tx[5:].strip()
if t.startswith(eec):
yes = Pretext('Yes')(
eec + ' in ' + rd_dcm.name + ', replace it ? ')
update = yes and 'yes'.startswith(yes.lower())
if not update:
return 'OK:', eec, 'already in', rd_dcm.name
wf.write('\n'.join(txt[stx:etx]) + '\n')
updated = True
else:
wf.write(tx)
elif update:
if tx.startswith('$ENDCMP'):
update = False
else:
wf.write(tx)
txt = symb.read_text().splitlines()
stx = None
etx = None
hsh = None
for no, tx in enumerate(txt):
if stx is None:
if tx.startswith('#'):
if tx.strip() == '#' and hsh is None:
hsh = no # header start
elif tx.startswith('DEF '):
t = tx.split()[1]
if not t.startswith(eec):
return 'Unexpected device in', symb.name
txt[no] = tx.replace(t, eec, 1)
stx = no
else:
hsh = None
elif etx is None:
if tx.startswith('ENDDEF'):
etx = no + 1
elif tx.startswith('F1 '):
txt[no] = tx.replace(device, eec, 1)
elif tx.startswith('DEF '):
return 'Multiple devices in', symb.name
if etx is None:
return device, 'not found in', symb.name
rd_lib = LIB / (PRJ[prj] + '.lib')
wr_lib = LIB / (PRJ[prj] + '.lib~')
update = updated = False
with rd_lib.open('rt') as rf:
with wr_lib.open('wt') as wf:
for tx in rf:
if re.match('# *end ', tx, re.IGNORECASE):
if not updated:
wf.write('\n'.join(txt[stx if hsh is None else hsh:
etx]) + '\n')
wf.write(tx)
break
elif tx.startswith('DEF '):
t = tx.split()[1]
if t.startswith(eec):
yes = Pretext('Yes')(
eec + ' in ' + rd_lib.name + ', replace it ? ')
update = yes and 'yes'.startswith(yes.lower())
if not update:
return 'OK:', eec, 'already in', rd_lib
wf.write('\n'.join(txt[stx:etx]) + '\n')
updated = True
else:
wf.write(tx)
elif update:
if tx.startswith('ENDDEF'):
update = False
else:
wf.write(tx)
pretty = 0
for rd in food.iterdir():
if rd.name.endswith('.kicad_mod') or rd.name.endswith('.mod'):
pretty += 1
txt = rd.read_text()
with (LIB / (PRJ[prj] + '.pretty') / rd.name).open('wt') as wr:
wr.write(txt)
print('footprints:', pretty)
wr_dcm.replace(rd_dcm)
wr_lib.replace(rd_lib)
return 'OK:',
if __name__ == '__main__':
parser = argparse.ArgumentParser(
epilog='Note, empty input: invites clipboard content, if available.')
parser.add_argument('--init', action='store_true',
help='initialize library')
parser.add_argument('--zap', action='store_true',
help='delete source zipfile after assembly')
arg = parser.parse_args()
signal.signal(signal.SIGINT, Signal)
readline.set_completer_delims('\t')
readline.parse_and_bind('tab: complete')
readline.set_auto_history(False)
try:
if arg.init:
libras = list(PRJ.values())
while libras:
libra = Select(libras)('Erase/Initialize which library? ')
if libra == '':
break
assert libra in libras, 'Unknown library'
dcm = LIB / (libra + '.dcm')
with dcm.open('wt') as dcmf:
dcmf.writelines(['EESchema-DOCLIB Version 2.0\n',
'#End Doc Library\n'])
dcm.chmod(0o660)
lib = LIB / (libra + '.lib')
with lib.open('wt') as libf:
libf.writelines(['EESchema-LIBRARY Version 2.4\n',
'#encoding utf-8\n',
'#End Library\n'])
lib.chmod(0o660)
pcb = LIB / (libra + '.pretty')
shutil.rmtree(pcb, ignore_errors=True)
pcb.mkdir(mode=0o770, parents=False, exist_ok=False)
libras.remove(libra)
while True:
zips = [zip.name for zip in SRC.glob('*.zip')]
zip = SRC / Select(zips)('Library zip file: ')
response = Impart(zip)
if response:
print(*response)
if arg.zap and response[0] == 'OK:':
zip.unlink()
except EOFError:
print('EOF')
except Exception as e:
print(*e.args)
exit(0)