-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwoopChecker.py
More file actions
592 lines (475 loc) · 20.1 KB
/
SwoopChecker.py
File metadata and controls
592 lines (475 loc) · 20.1 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import binascii
import operator
import re
from inspect import getframeinfo, stack
import importlib
import Swoop
from intersect import doIntersect, Point
from Bunch import Bunch
def mm_to_mil(mm):
return 39.3701 * mm
def mil_to_mm(mil):
return mil/39.3701
def mm_to_inch(mm):
return mm/25.4
def inch_to_mm(inch):
return inch * 25.4
checker_options = Bunch(
silkscreen_min_size = 0.9,
silkscreen_ratio = 8,
silkscreen_fonts = ['vector'],
green_led_pin = "PG0(DIG3)",
red_led_pin = "PG1(DIG1)",
green_led_resistor= "SMD-2012-0805-330",
red_led_resistor = "SMD-2012-0805-330",
max_clock_net_turns = 11,
high_current_min_width = mil_to_mm(10),
rf_min_width = mil_to_mm(40),
expected_regulated_decoupling_caps=7,
expected_battery_decoupling_caps=2,#5,
high_current_net_class_names=["HIGH_CURRENT", "HIGHCURRENT","HighAmps", "HIGH-I"],
power_net_class_names=["PWR", "pwr"],
power_and_ground_names = ["VCC", "VDD", "3V3", "+3V3", "GND", "BAT_GND", "3V", "VBAT", "5V"],
symbols_that_need_no_name = ["FRAME_B_L", "DOCFIELD"],
ground_device_sets_names = ["GND", "BAT_GND"],
power_device_set_names = ["3V", "VCC", "V3", "VBAT", "+3V3"]
)
html_output = True
def output_format(p, type=None):
if isinstance(p, str):
name = p
else:
name = p.get_name()
if html_output:
if isinstance(p, Swoop.Part) or type == "part":
return "<span class='swoop-lint-part'>{}</span>".format(name)
elif isinstance(p, Swoop.Element) or type == "part":
return "<span class='swoop-lint-part'>{}</span>".format(name)
elif isinstance(p, Swoop.Net) or isinstance(p, Swoop.Signal) or type == "net":
return "<span class='swoop-lint-net'>{}</span>".format(name)
elif isinstance(p, Swoop.Pin) or type == "pin":
return "<span class='swoop-lint-pin'>{}</span>".format(name)
else:
return name
else:
return name
class ChainLink(object):
def __init__(self):
super(object, self).__init__()
class Pin(ChainLink):
def __init__(self, name=None, select=None, description=None):
super(ChainLink, self).__init__()
self.name = listify(name)
self.select = select
self.description = description
def __str__(self):
if html_output:
return self.html()
else:
return "Pin({})".format(self.get_string())
def get_string(self):
if self.name:
return "|".join(self.name)
elif self.select:
return "{}".format(self.description if self.description else "custom")
else:
return "??"
def html(self):
return "<span class='swoop-lint-pin'>{}</span>".format(self.get_string())
def match(self, pin):
return all([not self.name or pin.get_pin() in self.name,
not self.select or self.select(pin)])
def listify(l):
if l is None:
return None
return l if isinstance(l, list) else [l]
class Part(ChainLink):
def __init__(self, name=None, longname=None, deviceset=None, device=None, select=None, part=None, description=None, value=None):
super(ChainLink, self).__init__()
self.name = listify(name)
self.deviceset = listify(deviceset)
self.device = listify(device)
self.longname = listify(longname)
self.part = listify(part)
self.description = description
self.select = select
self.value = listify(value)
def get_string(self):
if self.name:
return "|".join(self.name)
elif self.deviceset:
return "|".join(self.deviceset)
elif self.device:
return "|".join(self.device)
elif self.longname:
return "|".join(self.longname)
elif self.select:
return self.description if self.description else "custom"
elif self.part:
return "|".join([x.get_name() for x in self.part])
else:
return "??"
def __str__(self):
if html_output:
return self.html()
else:
return "Part({})".format(self.get_string())
def html(self):
return "<span class='swoop-lint-part'>{}</span>".format(self.get_string())
def match(self, part):
return all([not self.part or part in self.part,
not self.longname or part.get_deviceset() + part.get_device() in self.longname,
not self.deviceset or part.get_deviceset() in self.deviceset,
not self.device or part.get_device() in self.device,
not self.select or self.select(part),
not self.value or (part.get_value() and part.get_value().upper() in map(lambda x:x.upper(), self.value))])
class Net(ChainLink):
def __init__(self, name=None,select=None, net=None, description=None):
super(ChainLink, self).__init__()
self.name = name
self.select = select
self.net = net
self.description = description
def get_string(self):
if self.name:
return "{}".format(self.name)
elif self.net:
return "{}".format(self.net.get_name())
elif self.select:
return "{}".format(self.description if self.description else "custom")
else:
return "??"
def __str__(self):
if html_output:
return self.html()
else:
return "Net({})".format(self.get_string())
def html(self):
return "<span class='swoop-lint-net'>{}</span>".format(self.get_string())
def match(self, net):
return all([not self.name or self.name == net.get_name(),
not self.select or self.select(net),
not self.net or self.net == net])
def str_pattern(path=(), pattern=()):
if path:
path_str = ":".join(map(lambda x: x.get_name(), path))
else:
path_str = ""
if pattern:
if html_output:
pat_str = '<div class="swoop-lint-pattern"> '+ "".join(map(str, pattern)) + '</div>'
#print pat_str
else:
pat_str = " ".join(map(str, pattern))
else:
pat_str = ""
if path:
return "{}: {}".format(path_str, pat_str)
else:
return pat_str
class FailedMatchException(Exception):
pass
class CheckerContext(object):
def __init__(self):
pass
class Checker(object):
def __init__(self, errors, fix, context=None, sch=None, brd=None, lbrs=None, options=None):
assert isinstance(errors, ErrorCollector)
self.errors = errors
self.sch = sch
self.brd = brd
self.lbrs = lbrs if lbrs else []
self.fix = fix
self.ctx = context or CheckerContext()
if options is None:
self.options = {}
else:
self.options = options
def do_check(self):
pass
def check(self):
self.do_check()
return (self.errors, self.ctx)
def error(self, message, inexcusable=False):
caller = getframeinfo(stack()[1][0])
self.errors.record_error(self.sch, message, note="{}:{}".format(caller.filename.split("/")[-1], caller.lineno), inexcusable=inexcusable)
def info(self, message):
caller = getframeinfo(stack()[1][0])
self.errors.record_info(self.sch, message, note="{}:{}".format(caller.filename.split("/")[-1], caller.lineno))
def warn(self, message, inexcusable=False):
caller = getframeinfo(stack()[1][0])
self.errors.record_warning(self.sch, message, note="{}:{}".format(caller.filename.split("/")[-1], caller.lineno), inexcusable=inexcusable)
def do_match(self, path, pattern, solutions):
if len(pattern) == 0:
solutions.append(path)
return
tail = path[-1]
if isinstance(tail, Swoop.Part):
if len(pattern) == 2:
pin_link = pattern[0]
assert isinstance(pin_link, Pin)
net_link = pattern[1]
assert isinstance(net_link, Net), "{}".format(net_link)
nets = (Swoop.From(self.sch)
.get_sheets()
.get_nets()
.filtered_by(lambda x: net_link.match(x))
.get_segments()
.get_pinrefs()
.filtered_by(lambda x: pin_link.match(x) and x.get_part() == tail.get_name())
.get_parent().get_parent())
for n in nets:
self.do_match(path + [n], pattern[2:], solutions)
elif len(pattern) > 2:
initial_pin_link = pattern[0]
assert isinstance(initial_pin_link, Pin)
net_link = pattern[1]
assert isinstance(net_link, Net)
terminal_pin_link = pattern[2]
assert isinstance(terminal_pin_link, Pin)
part_link = pattern[3]
assert isinstance(part_link, Part), "Bad pattern: {}".format(str_pattern(pattern=pattern))
nets = (Swoop.From(self.sch)
.get_sheets()
.get_nets()
.filtered_by(lambda x: net_link.match(x))
.get_segments()
.get_pinrefs()
.filtered_by(lambda x: x.get_part() == tail.get_name())
.get_parent()
.get_parent()
.unique())
for n in nets:
refs = (Swoop.From(n)
.get_segments()
.get_pinrefs())
initial_pin_matches = refs.filtered_by(lambda x: initial_pin_link.match(x) and x.get_part() == tail.get_name())
terminal_pin_matches = refs.filtered_by(lambda x: terminal_pin_link.match(x))
if (len(initial_pin_matches)):
parts = map(lambda x: self.sch.get_part(x), terminal_pin_matches.get_part())
for p in parts:
if part_link.match(p) and p != tail:
self.do_match(path + [n, p], pattern[4:], solutions)
else:
raise Exception("Couldn't process pattern {}".format(str_pattern(path, pattern)))
else:
raise Exception("Patterns must start with a part.")
def match(self, pattern):
solutions = []
for i in range(0,len(pattern)):
if isinstance(pattern[i], Swoop.Part):
pattern[i] = Part(part=pattern[i])
elif isinstance(pattern[i], Swoop.Net):
pattern[i] = Net(net=pattern[i])
link = pattern[0]
if isinstance(link, Part):
options = Swoop.From(self.sch).get_parts().filtered_by(lambda x: link.match(x))
elif isinstance(link, Net):
options = Swoop.From(self.sch).get_sheets().get_nets().filtered_by(lambda x: link.match(x))
else:
raise Exception("First link in pattern must be Net or Part: {}".format(link))
for o in options:
path=[o]
self.do_match(path, pattern[1:], solutions)
return solutions
def match_one(self, pattern, error="", warning=""):
r = self.match(pattern)
if len(r) != 1:
if error or warning:
s = (error + warning + "<br/>Searching for {pp}<br/>Found {c} matching paths, but should have found 1. Here are the matches (if any):<br/> {matches}").format(c=len(r), pattern=pattern, pp=str_pattern(pattern=pattern), matches="<br/>".join(map(str_pattern,r)))
if not html_output:
s = s.replace("<br/>", "\n\t")
if error:
self.error(s)
if warning:
self.warn(s)
return [None] * len(filter(lambda x: isinstance(x, Net) or isinstance(x, Part), pattern))
else:
raise FailedMatchException(u"Wanted one match, got {}: {} {}".format(len(r), str_pattern(pattern=pattern), "\n".join(map(str_pattern,r))))
else:
#print "Pattern {} matched on {}".format(str_pattern(pattern=pattern), str_pattern(path=r[0]))
return r[0]
def match_none(self, pattern, error):
r = self.match(pattern)
if len(r) != 0:
if error:
#self.error((error + "({pattern} found {path})").format(pattern=str_pattern(pattern=pattern),path=str_pattern(path=r[0])))
self.error((error + " {pattern} found {path}").format(pattern=str_pattern(pattern=pattern),path=str_pattern(path=r[0])))
return [None] * len(filter(lambda x: isinstance(x, Net) or isinstance(x, Part), pattern))
else:
raise FailedMatchException(u"Too many matches: {} {}".format(str_pattern(pattern=pattern), "\n".join(map(str_pattern,r))))
else:
return []
def signal_length(self, name):
return Swoop.From(self.brd.get_signal(name)).get_wires().get_length().reduce(operator.add,0)
def check_intersections(self, routed_wires):
if not routed_wires:
return
t = routed_wires[0]
if isinstance(t.get_parent(), Swoop.Segment):
get_net = lambda x: x.get_parent().get_parent()
else:
get_net = lambda x: x.get_parent()
wires = enumerate(sorted(routed_wires))
for (i, w1) in wires:
w1x1, w1y1, w1x2, w1y2 = w1.get_points()
for (j, w2) in wires:
if j <= i:
continue
if get_net(w1) is not get_net(w2) and w1.get_layer() == w2.get_layer():
w2x1, w2y1, w2x2, w2y2 = w2.get_points()
if doIntersect(Point(w1x1, w1y1),
Point(w1x2, w1y2),
Point(w2x1, w2y1),
Point(w2x2, w2y2)):
self.error(
"The segment of {w1} from ({w1x1}, {w1y1}) to ({w1x2}, {w1y2}) intersects with the segment of {w2} from ({w2x1}, {w2y1}) to ({w2x2}, {w2y2}).".format(
w1=output_format(get_net(w1)),
w1x1=w1x1,
w1y1=w1y1,
w1x2=w1x2,
w1y2=w1y2,
w2x1=w2x1,
w2y1=w2y1,
w2x2=w2x2,
w2y2=w2y2,
w2=output_format(get_net(w2))))
def is_aligned(self, d, grid):
x = round(d / grid, 0)
return round(x * grid, 2) == round(d, 2)
class NoopChecker(Checker):
def do_check(self):
if self.sch:
self.info("Examined schematic {}".format(self.sch.get_filename()))
if self.brd:
self.info("Examined board {}".format(self.brd.get_filename()))
for l in self.lbrs:
self.info("Examined library {}".format(l.get_filename()))
class CheckerSequence(Checker):
def __init__(self, checkers, *args, **kwargs):
super(CheckerSequence, self).__init__(*args, **kwargs)
self.checkers = checkers
def do_check(self):
for Checker in self.checkers:
Checker(sch=self.sch, errors=self.errors, context=self.ctx, brd=self.brd, lbrs=self.lbrs, fix=self.fix).check()
def CheckSet(checkers):
class C(CheckerSequence):
def __init__(self, *args, **kwargs):
super(C, self).__init__(checkers, *args, **kwargs)
return C
def count_pins(part):
gates = part.find_deviceset().get_gates()
if len(gates) == 0:
return 0
else:
return len(gates[0].find_symbol().get_pins())
def bounding_box(items):
if isinstance(items, Swoop.From):
items = items.unpack()
elif not isinstance(items, list):
items = [items]
bounds_points = reduce(lambda x,y: x+y, map(lambda x:x.get_bounds_points(), items), [])
xs = map(lambda a: a[0], bounds_points)
ys = map(lambda a: a[1], bounds_points)
return (min(*xs), min(*ys), max(*xs), max(*ys))
def bounding_box_size(items):
x1, y1, x2, y2 = bounding_box(items)
return abs(x2 - x1), abs(y2 - y1)
def bounding_box_area(items):
x,y = bounding_box_size(items)
return x * y
class Error(object):
def __init__(self, path, error, level, index, context="", excused=False, inexcusable=False):
self.path = path
self.error = error
self.level = level
self.index = index
self.excused = excused
self.context = context
self.inexcusable = inexcusable
def _asdict(self):
return dict(path=self.path,
error=self.error,
level=self.level,
index=self.index,
excused=self.excused,
context=self.context,
inexcusable=self.inexcusable)
def get_full_string(self):
return self.path + self.error
def render_message(self):
return u"{}: {} -- {}".format(self.level, self.path, self.error)
def __str__(self):
return u"{} ({}:{})".format(self.render_message(), self.context, self.hash())
def hash(self):
hash_message = self.render_message()
hash_message = re.sub("[^:].*/", "", hash_message) # Trim path to file, if present
return "{:08X}".format(abs(binascii.crc32(hash_message)))
pretty_print_map={"Deviceset": "Device",
"Device": "Variant"
}
class NestedError(object):
def __init__(self, ec, efp):
self.ec = ec
if isinstance(efp, str):
ec.push_path(efp)
self.needs_pop = True
elif hasattr(efp, "get_name"):
if efp.__class__.__name__ in pretty_print_map:
t = pretty_print_map[efp.__class__.__name__]
else:
t = efp.__class__.__name__
ec.push_path(u"{}".format(efp.get_name()))
self.needs_pop = True
else:
self.needs_pop = False
def __enter__(self):
return self.ec
def __exit__(self, exc_type, exc_val, exc_tb):
if self.needs_pop:
self.ec.pop_path()
class ErrorCollector(object):
def __init__(self):
self.errors=[]
self.path=[]
def load_json(self, json):
for i in json:
self.errors.append(Error(**i))
def dump_json(self):
return map(lambda x:x._asdict(), self.errors)
def push_path(self, path):
self.path.append(path)
def pop_path(self):
self.path = self.path[:-1]
def record(self, efp, error, context="", level=None, inexcusable=False):
if not level:
level = "Error"
if hasattr(efp, "get_name"):
self.errors.append(Error(u"{}:{}".format(u":".join(self.path), efp.get_name()), error, level=level, index=len(self.errors), context=context, inexcusable=inexcusable))
elif isinstance(efp, str):
self.errors.append(Error(u"{}:{}".format(u":".join(self.path), efp), error, level=level, index=len(self.errors), context=context, inexcusable=inexcusable))
else:
self.errors.append(Error(u":".join(self.path), error, level, index=len(self.errors), context=context, inexcusable=inexcusable))
def record_error(self, efp,error, note="", inexcusable=False):
return self.record(efp,
error,
note,
None,
inexcusable=inexcusable)
def record_warning(self, efp,error, note="", inexcusable=False):
return self.record(efp,
error,
note,
"Warning",
inexcusable=inexcusable)
def record_info(self, efp,error, note=""):
return self.record(efp,
error,
note,
"Info")
def get_errors(self):
return self.errors
def nest(self, efp):
return NestedError(self, efp)
def filter_by_hash(self, approved_errors):
self.errors = [e for e in self.errors if e.hash() not in approved_errors]