-
Notifications
You must be signed in to change notification settings - Fork 0
/
saobjects.py
595 lines (490 loc) · 20.3 KB
/
saobjects.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
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
593
594
595
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.sql import and_, or_
from sqlalchemy.types import Integer, String
from sqlalchemy.schema import Column, ForeignKey, ForeignKeyConstraint
from tabulator import Tabulator
meta = sa.MetaData()
def init_echo_session(dburi):
init_and_bind_engine(dburi)
meta.bind.echo = True
ses = orm.create_session()
ses.echo_uow = True
return ses
def init_and_bind_engine(dburi):
engine = sa.create_engine(dburi)
meta.bind = engine
meta.create_all()
net_table = sa.Table('nets', meta,
Column('unit', String(20), primary_key=True),
Column('num', Integer, primary_key=True),
Column('sig_desc', String(50)))
class Net(object):
def __init__(self, unit, sig_desc=None):
self.unit = unit
self.sig_desc = sig_desc
def __str__(self):
return "N-%s.%s" % (self.unit, self.num)
def __repr__(self):
return "<net %s>" % str(self)
class NetMapperExtension(orm.MapperExtension):
def before_insert(self, mapper, connection, instance):
num_sel = sa.select(
[sa.func.max(net_table.c.num).label('max')],
net_table.c.unit == instance.unit)
instance.num = num_sel.execute().fetchone().max or 0
instance.num += 1
def before_delete(self, mapper, connection, instance):
cdr_mapper = orm.class_mapper(Conductor)
[ cdr_mapper.delete_obj(cdr) for cdr in instance.linked_cdrs ]
conductor_table = sa.Table('conductors', meta,
Column('a_net_unit', String(20), nullable=False),
Column('a_net_num', Integer, nullable=False),
Column('b_net_unit', String(20), nullable=False),
Column('b_net_num', Integer, nullable=False),
Column('kind', String(20)),
Column('cable', Integer, primary_key=True),
Column('subcdr', Integer, primary_key=True),
ForeignKeyConstraint(
['a_net_unit', 'a_net_num'],
['nets.unit', 'nets.num']),
ForeignKeyConstraint(
['b_net_unit', 'b_net_num'],
['nets.unit', 'nets.num']))
class Conductor(object):
def __init__(self, a_net, b_net, kind='C', cable=None):
self.a_net = a_net
self.b_net = b_net
self.kind = kind
self.cable = cable
def __str__(self):
if self.subcdr and self.kind == 'C':
return "C-%s.%s" % (self.cable, self.subcdr)
elif self.subcdr:
return "C-%s.%s (%s)" % (self.cable, self.subcdr, self.kind)
elif self.kind == 'C':
return "C-%s" % (self.cable)
else:
return "C-%s (%s)" % (self.cable, self.kind)
def __repr__(self):
return "<conductor %s>" % str(self)
def swap_orientation(self):
self.a_net, self.b_net = self.b_net, self.a_net
class ConductorMapperExtension(orm.MapperExtension):
def before_insert(self, mapper, connection, instance):
if instance.cable == None:
cable_sel = sa.select(
[sa.func.max(conductor_table.c.cable).label('max')])
instance.cable = cable_sel.execute().fetchone().max or 0
instance.cable += 1
subcdr_sel = sa.select(
[sa.func.max(conductor_table.c.subcdr).label('max')],
conductor_table.c.cable == instance.cable)
instance.subcdr = subcdr_sel.execute().fetchone().max or 0
instance.subcdr += 1
def after_delete(self, mapper, connection, instance):
conductor_table.update(
and_(conductor_table.c.cable==instance.cable,
conductor_table.c.subcdr > instance.subcdr),
values={ conductor_table.c.subcdr:
conductor_table.c.subcdr - 1 }).execute()
pin_table = sa.Table('pins', meta,
Column('net_unit', String(20), nullable=False,
primary_key=True),
Column('net_num', Integer, nullable=False,
primary_key=True),
Column('conn', String(20), primary_key=True),
Column('desig', String(20), primary_key=True),
Column('sig_desc', String(50)),
ForeignKeyConstraint(
['net_unit', 'net_num'],
['nets.unit', 'nets.num']))
class Pin(object):
def __init__(self, conn, desig, net=None, sig_desc=None):
self.net = net
self.conn = conn
self.desig = desig
self.sig_desc = sig_desc
def __str__(self):
return "P-%s.%s.%s (%s)" % (
self.net and self.net.unit or None,
self.conn, self.desig, self.sig_desc)
def __repr__(self):
return "<pin %s>" % str(self)
net_to_cdr_join = or_(
and_(net_table.c.unit == conductor_table.c.a_net_unit,
net_table.c.num == conductor_table.c.a_net_num),
and_(net_table.c.unit == conductor_table.c.b_net_unit,
net_table.c.num == conductor_table.c.b_net_num))
orm.mapper(Net, net_table, properties = {
'linked_cdrs': orm.relation(Conductor,
primaryjoin = net_to_cdr_join,
order_by = [
conductor_table.c.cable,
conductor_table.c.subcdr ]
),
'linked_pins': orm.relation(Pin,
cascade = 'all, delete-orphan',
backref = orm.backref('net', lazy=False)
)
}, extension=NetMapperExtension())
orm.mapper(Conductor, conductor_table, properties = {
'a_net': orm.relation(Net,
primaryjoin = and_(
conductor_table.c.a_net_unit == net_table.c.unit,
conductor_table.c.a_net_num == net_table.c.num)
),
'b_net': orm.relation(Net,
primaryjoin = and_(
conductor_table.c.b_net_unit == net_table.c.unit,
conductor_table.c.b_net_num == net_table.c.num)
)
}, extension=ConductorMapperExtension())
pin_to_net_join = and_(
pin_table.c.net_unit == net_table.c.unit,
pin_table.c.net_num == net_table.c.num)
orm.mapper(Pin, pin_table)
class Interconnect(object):
def __init__(self, dburi='sqlite://', debug=True):
init_and_bind_engine(dburi)
self._debug = debug
self._ses = None
self._filter_units = None
self._link_filter = False
if debug:
meta.bind.echo = True
def _get_ses(self):
if not self._ses:
self._ses = orm.create_session()
self._ses.echo_uow = self._debug
return self._ses
def _close_ses(self):
if self._ses:
self._ses.close()
self._ses = None
def _pin_table_query(self):
s = self._get_ses()
pt_q = s.query(Pin).add_entity(Net).outerjoin('net')
if self._link_filter:
pt_q = pt_q.outerjoin('net', 'linked_cdrs').filter(
and_(conductor_table.c.a_net_unit.in_(self._filter_units),
conductor_table.c.b_net_unit.in_(self._filter_units)))
elif self._filter_units:
pt_q = pt_q.filter(
net_table.c.unit.in_(self._filter_units))
pt_q = pt_q.order_by(net_table.c.unit, net_table.c.num,
pin_table.c.conn, pin_table.c.desig)
return pt_q
def _cdr_table_query(self):
s = self._get_ses()
a_pins = orm.aliased(Pin)
b_pins = orm.aliased(Pin)
ct_q = s.query(Conductor, a_pins, b_pins).filter(
a_pins.net_unit==Conductor.a_net_unit,
a_pins.net_num==Conductor.a_net_num,
b_pins.net_unit==Conductor.b_net_unit,
b_pins.net_num==Conductor.b_net_num)
if self._filter_units:
oper = and_ if self._link_filter else or_
ct_q = ct_q.filter(oper(
a_pins.net_unit.in_(self._filter_units),
b_pins.net_unit.in_(self._filter_units)))
int_ = lambda exp: sa.cast(exp, sa.Integer)
ct_q = ct_q.order_by(
a_pins.net_unit, int_(a_pins.conn), a_pins.conn,
int_(a_pins.desig), a_pins.desig,
b_pins.net_unit, int_(b_pins.conn), b_pins.conn,
int_(b_pins.desig), b_pins.desig)
return ct_q
@property
def unit_display_filter(self):
return self._filter_units
@unit_display_filter.setter
def unit_display_filter(self, value):
self._filter_units = value
def toggle_link_filter(self):
self._link_filter = not self._link_filter
return self._link_filter
def conn_tables(self, file, show_pcids=True):
if self._link_filter:
print "*** Link Filter is ON ***"
pin_tables_q = self._pin_table_query()
pin_tables_result = list(pin_tables_q)
output_lines = ['Unit Net Conn Pin Signal Cdrs Ref Net PCID'.split()]
prev_output_line = [None] * 7
pcid = 0
for index in range(len(pin_tables_result)):
cur_pin, cur_net = pin_tables_result[index]
unblanked_output_line = [
cur_net.unit,
('.%s' % str(cur_net.num)),
cur_pin.conn,
cur_pin.desig,
cur_pin.sig_desc or '',
'',
'',
pcid]
output_line = list(unblanked_output_line)
pcid += 1
for field in range(5):
if output_line[field] == prev_output_line[field]:
output_line[field] = ''
elif field != 1:
break
prev_output_line = unblanked_output_line
if len(output_line[0]):
output_lines.append([''] * 7)
output_lines.append(output_line)
try:
if cur_net == pin_tables_result[index + 1][1]:
continue
except IndexError:
pass
for cdr in cur_net.linked_cdrs:
output_line[5] = cdr
if cur_net == cdr.a_net:
output_line[6] = cdr.b_net
else:
output_line[6] = cdr.a_net
output_line = [''] * 7
output_lines.append(output_line)
if cur_net.linked_cdrs:
del output_lines[-1]
if show_pcids:
t = Tabulator(10, 5, 6, 4, 20, 12, 16, 4)
else:
t = Tabulator(10, 5, 6, 4, 20, 12, 16, 0)
for line in output_lines:
file.write(t(*line) + '\n')
def cdr_table(self, file):
if self._link_filter:
print "*** Link Filter is ON ***"
cdr_table_q = self._cdr_table_query()
cdr_table_result = list(cdr_table_q)
output_lines = ['Unit Conn Pin [/] Cdr [/] Pin Conn Unit'.split()]
output_lines.append(['' for _ in output_lines[0]])
prev_output_line = [None] * 9
for index in range(len(cdr_table_result)):
cur_cdr, cur_a_pin, cur_b_pin = cdr_table_result[index]
unblanked_output_line = [
cur_a_pin.net_unit,
cur_a_pin.conn,
cur_a_pin.desig,
'[ ]',
str(cur_cdr),
'[ ]',
cur_b_pin.desig,
cur_b_pin.conn,
cur_b_pin.net_unit ]
output_line = list(unblanked_output_line)
for fieldset in [[4], [0, 1, 2, 3], [8, 7, 6, 5]]:
for field in fieldset:
if output_line[field] == prev_output_line[field]:
output_line[field] = ''
else:
break
prev_output_line = unblanked_output_line
output_lines.append(output_line)
t = Tabulator(12, 6, 4, 5, 6, 5, 4, 6, 12)
for line in output_lines:
file.write(t(*line) + '\n')
def add_cdr(self, a_unit, a_conn, a_pin,
b_unit, b_conn, b_pin, cable=None):
s = self._get_ses()
def find_or_create_pin_net(unit, conn, pin):
q = s.query(Pin).add_entity(Net).join('net') \
.filter(net_table.c.unit == unit) \
.filter(pin_table.c.conn == conn) \
.filter(pin_table.c.desig == pin)
if q.count():
the_net = list(q)[0][1]
print " - pin %s/%s/%s exists on net %s" % (
unit, conn, pin, repr(the_net))
return the_net
else:
the_net = Net(unit)
the_pin = Pin(conn, pin)
the_net.linked_pins.append(the_pin)
print " - creating pin %s/%s/%s, net %s" % (
unit, conn, pin, repr(the_net))
s.add(the_net)
return the_net
pin_a_net = find_or_create_pin_net(a_unit, a_conn, a_pin)
pin_b_net = find_or_create_pin_net(b_unit, b_conn, b_pin)
new_cdr = Conductor(pin_a_net, pin_b_net, cable=cable)
s.add(new_cdr)
s.flush()
print pin_a_net
print pin_b_net
print new_cdr
self._close_ses()
def add_pin(self, unit, conn, pin, net_num):
s = self._get_ses()
existing_net = s.query(Net).filter(net_table.c.unit == unit) \
.filter(net_table.c.num == net_num)[0]
the_pin = Pin(conn, pin)
existing_net.linked_pins.append(the_pin)
s.flush()
self._close_ses()
def del_cdr(self, cable, subcdr):
self._close_ses()
conductor_table.delete(and_(conductor_table.c.cable == cable,
conductor_table.c.subcdr == subcdr)).execute()
def desc_pin(self, pin_cid, desc):
pin = self._pin_table_query()[pin_cid][0]
pin.sig_desc = desc
self._get_ses().flush()
self._close_ses()
def del_pin(self, pin_cid):
pin = self._pin_table_query()[pin_cid][0]
s = self._get_ses()
s.delete(pin)
s.flush()
self._close_ses()
def rename_pin(self, pin_cid, conn, desig):
pin = self._pin_table_query()[pin_cid][0]
pin.conn = conn
pin.desig = desig
self._get_ses().flush()
self._close_ses()
def del_net(self, unit, net_num):
s = self._get_ses()
net = s.query(Net).get([unit, net_num])
s.delete(net)
s.flush()
self._close_ses()
def complete_unit(self, prefix):
compl_r = sa.select([net_table.c.unit],
whereclause=net_table.c.unit.like(prefix + '%'),
order_by=[net_table.c.unit], distinct=True).execute()
return [ r[0] + ' ' for r in compl_r ]
def complete_conn(self, unit, prefix):
pn_j = sa.join(pin_table, net_table, onclause=pin_to_net_join)
compl_r = sa.select([pin_table.c.conn],
whereclause=and_(
net_table.c.unit==unit,
pin_table.c.conn.like(prefix +'%')
),
from_obj=[pn_j],
order_by=[pin_table.c.conn], distinct=True).execute()
return [ r[0] + ' ' for r in compl_r ]
def renumber_nets(self, unit):
conn = meta.bind.connect()
trans = conn.begin()
try:
nets_sorted_j = sa.outerjoin(net_table, pin_table,
onclause = pin_to_net_join)
nets_sorted_q = sa.select(
[net_table.c.unit, net_table.c.num,
sa.func.min(pin_table.c.conn)
.label('lexical_conn_sort'),
sa.func.min(sa.cast(pin_table.c.conn, sa.Integer))
.label('numeric_conn_sort'),
sa.func.min(pin_table.c.desig)
.label('lexical_pin_sort'),
sa.func.min(sa.cast(pin_table.c.desig, sa.Integer))
.label('numeric_pin_sort')],
from_obj = [nets_sorted_j],
whereclause = net_table.c.unit==unit,
group_by = [net_table.c.unit, net_table.c.num],
order_by = [net_table.c.unit,
'numeric_conn_sort', 'lexical_conn_sort',
'numeric_pin_sort', 'lexical_pin_sort'])
nets_sorted = list(conn.execute(nets_sorted_q))
# This is a marginal hack to avoid having to do more magic to keep
# from breaking PK uniqueness constraints. Update each reference to
# the old net number with the negation of the new net number...
for index in range(len(nets_sorted)):
new_num = index + 1
print nets_sorted[index], '->', new_num
conn.execute(net_table.update(
and_(net_table.c.unit==nets_sorted[index].unit,
net_table.c.num==nets_sorted[index].num),
{'num': -new_num}))
conn.execute(pin_table.update(
and_(pin_table.c.net_unit==nets_sorted[index].unit,
pin_table.c.net_num==nets_sorted[index].num),
{'net_num': -new_num}))
conn.execute(conductor_table.update(
and_(conductor_table.c.a_net_unit==nets_sorted[index].unit,
conductor_table.c.a_net_num==nets_sorted[index].num),
{'a_net_num': -new_num}))
conn.execute(conductor_table.update(
and_(conductor_table.c.b_net_unit==nets_sorted[index].unit,
conductor_table.c.b_net_num==nets_sorted[index].num),
{'b_net_num': -new_num}))
# ...then bulk-update all net number references to the absolute
# value of themselves.
conn.execute(net_table.update(
net_table.c.num<0,
{'num': sa.func.abs(net_table.c.num)}))
conn.execute(pin_table.update(
pin_table.c.net_num<0,
{'net_num': sa.func.abs(pin_table.c.net_num)}))
conn.execute(conductor_table.update(
conductor_table.c.a_net_num<0,
{'a_net_num': sa.func.abs(conductor_table.c.a_net_num)}))
conn.execute(conductor_table.update(
conductor_table.c.b_net_num<0,
{'b_net_num': sa.func.abs(conductor_table.c.b_net_num)}))
trans.commit()
except:
trans.rollback()
raise
finally:
conn.close()
self._close_ses()
def reorient_cdrs(self):
conn = meta.bind.connect()
try:
net_cdr_j = sa.outerjoin(net_table, conductor_table,
onclause=net_to_cdr_join)
unit_fan_order_q = sa.select(
[net_table.c.unit,
sa.func.count(conductor_table.c.cable)
.label('cdr_count')],
from_obj=[net_cdr_j],
group_by=[net_table.c.unit],
order_by=[sa.asc('cdr_count'), net_table.c.unit])
unit_fan_order = [r[0] for r in conn.execute(unit_fan_order_q)]
finally:
conn.close()
s = self._get_ses()
s.begin()
try:
for unit in unit_fan_order:
b_cdrs = (s.query(Conductor)
.options(orm.joinedload('a_net'),
orm.joinedload('b_net'))
.filter(Conductor.b_net_unit==unit))
for cdr in b_cdrs:
cdr.swap_orientation()
s.flush()
s.commit()
except:
s.rollback()
raise
finally:
self._close_ses()
def set_cdr_kind(self, cable, subcdr, new_kind):
s = self._get_ses()
cdr = s.query(Conductor).get([cable, subcdr])
cdr.kind = new_kind
s.update(cdr)
s.flush()
self._close_ses()
def set_cdr_sub(self, cable, subcdr, new_subcdr):
self._close_ses()
cdrs = conductor_table.select(
and_(conductor_table.c.cable == cable,
conductor_table.c.subcdr == new_subcdr)
).count().execute()
if list(cdrs)[0][0]:
raise RuntimeError(
'The cable has a conductor with that ID already.')
conductor_table.update(
values={conductor_table.c.subcdr: new_subcdr},
whereclause=and_(
conductor_table.c.subcdr == subcdr,
conductor_table.c.cable == cable)
).execute()