-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_obj.py
More file actions
1324 lines (1116 loc) · 52.1 KB
/
frame_obj.py
File metadata and controls
1324 lines (1116 loc) · 52.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frame_obj.py
from itertools import groupby, chain
from operator import itemgetter
from collections import defaultdict, deque, ChainMap
from db import connection
def asbool(x):
r'''Converts slot value (a python str) to a python bool.
'''
if x.lower() == 'true':
return True
if x.lower() == 'false':
return False
raise ValueError(f"{x!r} is not a legal boolean value")
def islist(x):
r'''True iff `x` is some kind of list.
'''
return isinstance(x, (slot_list, dynamic_slot_list, list, tuple))
def aslist(x):
r'''Makes sure that `x` is some kind of list.
If not, creates a list with `x` as the sole element.
'''
if islist(x):
return x
return [x]
class context:
r'''Only used for context for format values.
Makes keys case insensitive.
'''
def __init__(self, parent):
self.parent = parent
def __getitem__(self, name):
parent = self.parent
if name.lower() == 'frame':
assert parent is not None
#print(f"context[{name}] -> {parent}")
return parent
while parent is not None:
#print(f"context[{name}] checking {parent}")
try:
class_name = parent.get_slot('class_name')
except AttributeError:
pass
else:
#print(f"context[{name}] got {class_name!r}")
if class_name.lower() == name.lower():
#print(f"context[{name}] -> {parent}")
return parent
parent = getattr(parent, 'parent', None)
raise KeyError(name)
class version:
r'''This provides the high-level access to a frames database.
It pulls together the db connection used to access the database, the
user_id for the frames user making requests on this database, and a
sequence of version_names that collectively identify which specific
version of the database to present.
Methods:
- get_frame(frame_label)
The frame_label may be a frame id (as int or str), or frame_name.
Returns a frame object with all inherited slots.
- get_raw_frame(frame_label)
The frame_label may be a frame id (as int or str), or frame_name.
Returns a single frame (without inherited slots) in a raw format.
- frame_ids_with_slots(**slots)
Returns a set of frame_ids.
- delete_slot(slot_id)
- create_list(frame, name, values)
- update_slot(slot_id, value, slot_list_order=None, description=None)
Returns slot_id
- create_slot(frame_id, name, value, slot_list_order=None, description=None)
Returns a raw_slot (see get_raw_frame for what a "raw_slot" is).
- load_frame(slots)
`slots` is {name: value}.
Updates to the frame are done directly on the frame object.
'''
def __init__(self, db_conn, user_id, *version_names):
self.db_conn = db_conn
self.user_id = user_id
self.version_names = version_names
self.lookup_version_ids() # sets self.version_ids to set ids for names
# self.required_versions is the set of all versions (recursively)
# required by self
# self.required_map is {version_id: set of required_version_ids}
# for all version_ids in required_versions
self.required_versions, self.required_map = \
self.get_all_required_versions()
#print("version", self.required_versions, self.required_map)
# {frame_name.upper(): frame_id}
self.frame_names = {
value.upper(): frame_id
for frame_id, slot_id, value
in self.select_slot_ids_by_version("name = 'frame_name'")}
#print("frame_names", self.frame_names)
#print()
# [(parent_id, child_id)]
slots = [(frame_id, (int(value[1:])
if value[1:].isdigit()
else self.frame_names[value[1:].upper()]))
for frame_id, slot_id, value
in self.select_slot_ids_by_version(
"value LIKE '$%' "
"AND name != 'ako' AND name != 'isa'")]
# {frame_id: set(parent_frame_id)}
self.parent_links = {
child_id: {parent_id for parent_id, _ in parents}
for child_id, parents
in groupby(sorted(slots, key=itemgetter(1)), key=itemgetter(1))}
#print("parent_links", self.parent_links)
#print()
self.frame_ids = {} # {frame_id: frame}
def lookup_version_ids(self):
self.db_conn.execute("""SELECT version_id, name, status
FROM Version
WHERE name IN (::version_names)""",
version_names=self.version_names)
version_ids = []
self.frozen = True
names = {name.upper(): name for name in self.version_names}
for row in self.db_conn:
version_ids.append(row['version_id'])
if row['status'] == 'proposed':
self.frozen = False
del names[row['name'].upper()]
if names:
raise AssertionError(
f"Version names not found: {sorted(names.values())}")
self.version_ids = frozenset(version_ids)
def get_all_required_versions(self, seen=None, depth=0):
r'''Figures out all of the required version info.
Returns ({required_version_id}, {version_id: set(required_version_ids)})
'''
self.db_conn.execute("""
WITH RECURSIVE req(ver_id, req_ver_id)
AS ( SELECT version_id, required_version_id
FROM version_requires
WHERE version_id in (::version_ids)
UNION ALL
SELECT version_id, required_version_id
FROM version_requires
INNER JOIN req
WHERE version_id == req_ver_id
)
SELECT ver_id, req_ver_id FROM req
ORDER BY ver_id;""",
version_ids=self.version_ids)
required_map = {version_id: set(req_ver_id
for _, req_ver_id
in required_versions)
for version_id, required_versions
in groupby(self.db_conn, key=itemgetter(0))}
#print("required_map", required_map)
def fill_req(req_versions, remaining_req_versions):
for req_ver in remaining_req_versions:
if req_ver in required_map:
deeper_req_versions = \
required_map[req_ver].difference(req_versions)
req_versions.update(deeper_req_versions)
fill_req(req_versions, deeper_req_versions)
for ver_id, req_versions in required_map.items():
fill_req(req_versions, req_versions.copy())
all_required = set(self.version_ids)
for version_id, req_set in required_map.items():
all_required.add(version_id)
all_required.update(req_set)
return all_required, required_map
def frame_ids_with_slots(self, **slots):
r'''Searches for all frames that have all of the indicated slots.
Only does == matches. String compares are case insensitive.
Returns a set of frame_ids.
'''
slot_names = frozenset(k.upper() for k in slots.keys())
slot_names_with_ako = slot_names.union(['AKO', 'ISA'])
raw_frames = self.select_slots_by_version(
"name IN (::slot_names_with_ako)",
dict(slot_names_with_ako=slot_names_with_ako))
# {base_id: {derived_id}}
derived_map = defaultdict(set)
for (frame_id, name, slot_list_order), raw_slot in raw_frames.items():
if name in ('AKO', 'ISA'):
# FIX: what if raw_slot is a slot_list?
derived_map[raw_slot['value']].add(frame_id)
def frames_with_slot(slot_name, value):
r'''Slot_name is passed in uppercase.
'''
if isinstance(value, frame):
value = frame.frame_id
if isinstance(value, str):
value = value.upper()
for (frame_id, name, slot_list_order), slot in raw_frames.items():
if name == slot_name and \
value in ((slot['value'].upper()
if isinstance(slot['value'], str)
else slot['value']),
'*'):
yield frame_id
if slot_name != 'FRAME_NAME':
yield from spew_derived(frame_id, name, slot_list_order)
def spew_derived(frame_id, name, slot_list_order):
for d in derived_map[frame_id]:
if (d, name, slot_list_order) not in slots_found:
yield d
yield from spew_derived(d, name, slot_list_order)
found = [frozenset(frames_with_slot(slot_name.upper(), value))
for slot_name, value in slots.items()]
return found[0].intersection(*found[1:])
def get_raw_frame(self, frame_label):
r'''Reads one frame from the database.
frame_label can be either a frame_id (either int or str),
or a frame_name.
Only includes the proper slots to use for this set of versions.
Does not include inherited slots.
Returns frame_id, {(frame_id, name.upper(), slot_list_order): slot}
Where slot is dict with the following keys:
- frame_id
- slot_id
- name # value not upper cased
- slot_list_order
- description
- value # may be "<DELETED>"
'''
if isinstance(frame_label, int) or \
isinstance(frame_label, str) and frame_label.isdigit():
frame_id = int(frame_label)
else:
frame_id = self.frame_names[frame_label.upper()]
return (frame_id,
self.select_slots_by_version("frame_id = :frame_id",
frame_id=frame_id))
def select_slots_by_version(self, where_exp, **sql_params):
r'''Figures slots matching where_exp/sql_params that are best match to
my versions.
Returns {(frame_id, slot_name, slot_list_order): raw_slot}.
'''
matching_slot_ids = [slot_id
for frame_id, slot_id, value
in self.select_slot_ids_by_version(where_exp,
sql_params)]
self.db_conn.execute("""SELECT *
FROM Slot
WHERE slot_id IN (::slot_ids);""",
slot_ids=matching_slot_ids)
return {(row['frame_id'], row['name'].upper(), row['slot_list_order']):
dict(frame_id=row['frame_id'],
slot_id=row['slot_id'],
name=row['name'],
slot_list_order=row['slot_list_order'],
description=row['description'],
value=row['value'])
for row in self.db_conn}
def select_slot_ids_by_version(self, where_exp, sql_params={}):
r'''Finds matching slots that are best match to my versions.
Finds slots matching where_exp/sql_params.
Returns [(frame_id, slot_id, value)]
'''
self.db_conn.execute(f"""
WITH desired_slots(frame_id, name, slot_list_order, slot_id, value)
AS (SELECT frame_id, name, slot_list_order, slot_id, value
FROM Slot
WHERE {where_exp})
SELECT desired_slots.*, 1, version_id
FROM desired_slots
INNER JOIN Slot_versions USING (slot_id)
UNION ALL
-- Undesired slots that might be a better version match than the
-- desired ones (and, hence, hide the desired value)!
SELECT frame_id, name, slot_list_order, s.slot_id, NULL, 0, version_id
FROM desired_slots ds
INNER JOIN Slot s USING (frame_id, name, slot_list_order)
INNER JOIN Slot_versions v ON v.slot_id = s.slot_id
WHERE ds.slot_id != s.slot_id;""",
**sql_params)
return self.select_best_matches(self.db_conn)
def select_best_matches(self, raw_slot_rows):
r'''Selects desired slots that are the best match to my versions.
raw_slot_rows is (frame_id, name, slot_list_order, slot_id, value, desired,
version_id)
Returns [(frame_id, slot_id, value)]
'''
sorted_slots = sorted(raw_slot_rows,
key=lambda row: (row[0], row[1].upper(),
row[2], row[3]))
matching_slot_ids = []
# for each slot name:
for (frame_id, name, slot_list_order), slots \
in groupby(sorted_slots,
key=lambda row: (row[0], row[1].upper(), row[2])):
# [(slot_id, value, desired, version_ids_frozenset)]
matching_slots = []
# Gather slot_ids that have all of my required_versions.
for (slot_id, value, desired), versions \
in groupby(slots, key=itemgetter(3, 4, 5)):
version_ids = frozenset(v[6] for v in versions)
if version_ids.issubset(self.required_versions):
matching_slots.append((slot_id, value,
desired, version_ids))
#print("matching_slots", matching_slots)
# Find best match
if len(matching_slots) == 1:
# Only one slot_id found, it's the best match!
if matching_slots[0][2]: # desired
matching_slot_ids.append((frame_id, matching_slots[0][0],
matching_slots[0][1]))
elif matching_slots:
best_match = None # (slot_id, versions)
# Try each slot to see which one is the best match.
for slot_id, value, desired, versions in matching_slots:
#print("checking", slot_id, versions)
# Look for better match in other slots.
for slot_id2, _, _, versions2 in matching_slots:
if slot_id != slot_id2 and \
not self.better_fit(slot_id, versions,
slot_id2, versions2):
# nope, slot_id is not the best match!
break
else:
# No better fit, slot_id is the best one!
if best_match is not None:
# Conflict!
# How could this happen??
raise AssertionError(
"Impossible slot version conflict between "
f"{matching_slots}")
else:
best_match = (slot_id, value, desired, versions)
if best_match is not None:
# Best match found!
if best_match[2]: # desired
matching_slot_ids.append((frame_id, best_match[0],
best_match[1]))
else:
# None of the versions stands out as being better than all
# of the rest...
matches = ', '.join('{}{}'.format(s, list(v))
for s, _, _, v in matching_slots)
raise AssertionError(
f"Slot version conflict between {matches}")
return matching_slot_ids
def better_fit(self, slot_id, versions, other_slot_id, other_versions):
#print("better_fit", versions, other_versions)
if len(other_versions) > len(versions):
#print("better_fit -> False, len(other_versions) > len(versions)")
return False
num_better = 0
num_matches = 0
for v in versions:
for other_v in other_versions:
#print("checking", v, "against", other_v)
if v == other_v:
num_matches += 1
elif v in self.required_map and other_v in self.required_map[v]:
# v is better than other_v
num_better += 1
elif other_v in self.required_map and \
v in self.required_map[other_v]:
# other_v is better than v
return False
#print("better_fit: num_better", num_better, "num_matches", num_matches)
if num_better + num_matches < len(other_versions):
# There are some disjoint versions between the two sets of versions
return False
if num_matches == len(other_versions) == len(versions):
# The sets are the identical!
# FIX: Should this be an exception because these two slots will
# always fail each other?
return False
ans = num_better or len(versions) > len(other_versions)
#print("better_fit ->", ans, "final")
return ans
def get_frame(self, frame_label):
r'''Returns a frame object.
Does not include inherited slots.
'''
if isinstance(frame_label, int) or frame_label.isdigit():
frame_id = int(frame_label)
else:
frame_id = self.frame_names[frame_label.upper()]
if frame_id in self.frame_ids:
return self.frame_ids[frame_id]
return self.read_frame(frame_id)
def read_frame(self, frame_id):
r'''Reads a frame from the database.
'''
frame_id, raw_frame = self.get_raw_frame(frame_id)
new_frame = frame.from_raw_frame(frame_id, self, raw_frame)
self.frame_ids[frame_id] = new_frame
return new_frame
def lookup(self, frame_id, name):
r'''Does a lookup for a frame with frame.class_name == `name`.
This is called by the context.__getitem__ method, for use with format
values.
`name` must be lowercase.
If the `name` is not found in frame_id, then that frame's parents are
searched in a breadth-first order.
Raises KeyError if `name` not found.
'''
frame = self.frame_ids[frame_id]
if name == 'frame':
return frame
class_name = getattr(frame, 'class_name', None)
if class_name is not None and class_name.lower() == name:
return frame
# breadth-first search...
parents = deque(self.parent_links.get(frame_id, ()))
while parents:
try:
return self.lookup(parents.popleft(), name)
except KeyError:
parents.extend(self.parent_links.get(frame_id, ()))
raise KeyError(f"{name} in {frame.frame_label}")
def delete_slot(self, slot_id):
r'''Marks slot_id as deleted.
Doesn't return anything.
'''
# FIX: Do I need to check the slot's versions before doing this
# (vs. creating a new slot)?
self.db_conn.execute("""UPDATE Slot
SET value = '<DELETED>'
WHERE slot_id = :slot_id""",
slot_id=slot_id)
def create_list(self, frame, name, values):
r'''Creates a new set of slots, numbering slot_list_order from 1000 by 1.
Returns a slot_list of the newly created values.
'''
raw_slots = []
for slot_list_order, value in enumerate(values, 1000):
raw_slots.append(self.create_slot(frame.frame_id, name, value,
slot_list_order))
return slot_list(frame, name, raw_slots)
def update_slot(self, slot_id, value, slot_list_order=None, description=None):
r'''Returns slot_id (may have had to create a new one)
'''
if self.frozen:
raise AssertionError("Can not make changes to frozen versions")
self.db_conn.execute("""SELECT version_id FROM Slot_versions
WHERE slot_id = :slot_id""",
slot_id=slot_id)
slot_versions = frozenset(row[0] for row in self.db_conn)
if slot_versions == self.version_ids:
# Current slot_id is for this version! Update slot in place...
if isinstance(value, frame):
db_value = f"${value.frame_label}"
else:
db_value = value
self.db_conn.execute("""
UPDATE Slot
SET value = :value, slot_list_order = :slot_list_order
description = :description,
updated_user_id = :user_id,
updated_timestamp = datetime("now"),
WHERE slot_id = :slot_id""",
value=value, slot_list_order=slot_list_order, description=description,
slot_id=slot_id, user_id=self.user_id)
return slot_id
# Otherwise, create new slot for this version...
self.db_conn.execute("""SELECT frame_id, name
FROM Slot
WHERE slot_id = :slot_id""",
slot_id=slot_id)
frame_id, name = self.db_conn.fetchone()
raw_slot = self.create_slot(frame_id, name, slot_list_order, value,
description)
return raw_slot['slot_id']
def create_slot(self, frame_id, name, value, slot_list_order=None,
description=None):
r'''Returns a raw_slot (see get_raw_frame for what a "raw_slot" is).
'''
if self.frozen:
raise AssertionError("Can not make changes to frozen versions")
if isinstance(value, frame):
db_value = f"${value.frame_label}"
else:
db_value = str(value)
# Insert the new slot row
self.db_conn.execute("""
INSERT INTO Slot (frame_id, name, slot_list_order, value, description,
creation_user_id, creation_timestamp)
VALUES (:frame_id, :name, :slot_list_order, :value, :description,
:creation_user_id, datetime("now"));""",
frame_id=frame_id, name=name, slot_list_order=slot_list_order, value=db_value,
description=description, creation_user_id=self.user_id)
slot_id = self.db_conn.lastrowid
# Assign version_ids to new slot
for version_id in self.version_ids:
self.db_conn.execute("""
INSERT INTO Slot_versions (slot_id, version_id,
creation_user_id, creation_timestamp)
VALUES (:slot_id, :version_id,
:creation_user_id, datetime("now"));
""",
slot_id=slot_id, version_id=version_id,
creation_user_id=self.user_id)
return dict(frame_id=frame_id,
slot_id=slot_id,
name=name,
slot_list_order=slot_list_order,
description=description,
value=value)
def load_frame(self, slots, last_frame_id=None):
r'''Creates a new frame with the slots specified.
`slots` is {name: value}.
The slot_list_order of any list/tuple values are assigned starting at 1000.
`slot_list_order` is always incremented by 1.
Only called by load_yaml -> load_frames.
Returns frame_id, frame_label ("$<frame_id>" or "$<frame_name>") for
the new frame.
'''
# Figure out frame_id for new frame...
self.db_conn.execute("""SELECT frame_id FROM Slot
ORDER BY frame_id DESC
LIMIT 1""")
rows = self.db_conn.fetchall()
if rows:
assert len(rows) == 1
if last_frame_id is None:
frame_id = rows[0][0] + 1
else:
frame_id = max(rows[0][0], last_frame_id) + 1
else:
frame_id = 1
last_frame_id = frame_id
def unwrap_value_info(slot_name, value, version_obj=self,
current_index=None, slot_list_order_offset=None):
r'''Unwraps all nested dict objects (if any).
These could be "value_info" objects, or nested frames.
A "value_info" object may stand in for a single value. It will
always have a "value" key. (Thus, nested frames are not allowed
to have a "value" slot)...
This is a dict with the following keys (all but `value` are
optional):
- value -- the value it's standing in for (required)
- user_id -- the user_id to store in creation_user_id
- required_versions -- list of version names for slot_versions
- slot_list_order -- the offset to apply to slot_list_order
within a list
- slot_name -- actual slot name
(overrides `slot_name` param, allows
multiple values for different versions
stored under different (unique) keys)
- description -- defaults to None
Inner value_info objects override outer ones. FIX: Does this make
sense?
Returns slot_name, version_obj, slot_list_order_offset, value,
description
'''
nonlocal last_frame_id
new_offset = None
description = None
while isinstance(value, dict) and 'value' in value: # value info
user_id = None
required_versions = None
for key, info in value.items():
key_upper = key.upper()
if key_upper == 'VALUE_ORDER':
new_offset = info
elif key_upper == 'USER_ID':
user_id = info
elif key_upper == 'REQUIRED_VERSIONS':
required_versions = info
elif key_upper == 'SLOT_NAME':
slot_name = info
elif key_upper == 'DESCRIPTION':
description = info
elif key_upper == 'VALUE':
value = info
else:
raise AssertionError(
f"Unknown key, {key}, on slot {slot_name}")
if user_id is not None or required_versions is not None:
version_obj = self.db_conn.at_versions(
user_id or self.user_id,
*(required_versions
if required_versions is not None
else self.version_names))
if current_index is None and new_offset is not None:
raise AssertionError(
"slot_list_order not allowed on single-valued "
f"slot {slot_name}")
if isinstance(value, dict): # nested frame
last_frame_id, value = version_obj.load_frame(value,
last_frame_id)
if new_offset is not None:
return (slot_name, version_obj, new_offset - i, value,
description)
return (slot_name, version_obj, slot_list_order_offset, value,
description)
frame_label = f"${frame_id}"
for name, value in slots.items():
#print("create_frame", name, value)
slot_name, version_obj, _, value, description = \
unwrap_value_info(name, value)
if not islist(value):
if slot_name.upper() == 'FRAME_NAME':
frame_label = f"${value}"
version_obj.create_slot(frame_id, slot_name, value,
description=description)
else:
if slot_name.upper() in ("NAME", "AKO", "ISA", "FRAME_NAME",
"CLASS_NAME", "SPLICE"):
raise AssertionError(
f"{slot_name} slot not allowed to have "
"multiple values")
slot_list_order_offset = 1000
for i, v in enumerate(value):
new_name, this_version_obj, slot_list_order_offset, v, \
description = \
unwrap_value_info(slot_name, v, version_obj, i,
slot_list_order_offset)
if new_name != slot_name:
raise AssertionError(
"Not allowed to change slot name in "
f"multi-valued slot {name}")
this_version_obj.create_slot(frame_id, slot_name,
v, i + slot_list_order_offset,
description)
return frame_id, frame_label
class frame:
r'''Interface object for a frame.
The inheritance is done dynamically when the slots are accessed.
some_frame.slot_name -> value (may be a slot_list)
some_frame.get_raw_slot(slot_name) # Frame_slot row or slot_list
some_frame.get_slot_names() -> iterable of slot_names
'''
def __init__(self, frame_id, version_obj, raw_slots,
parent=None, splices=()):
self.frame_id = frame_id
self.version_obj = version_obj
# raw_slots is {slot_name.lower(): Frame_slot row or slot_list}
# raw_slots is shared by dynamic frames, and they see all changes.
self.raw_slots = raw_slots
self.parent = parent
self.splices = splices
@classmethod
def from_raw_data(cls, version_obj, frame_id, raw_data):
r'''Creates new frame from a list of Frame_slot rows.
The raw_data rows must be sorted by 'name', 'slot_list_order'.
'''
raw_slots = {}
new_frame = cls(frame_id, version_obj, raw_slots)
def make_value(slots_by_name):
first_slot = next(slots_by_name)
if first_slot['slot_list_order'] is None:
next_slot = next(slots_by_name, None)
if next_slot is not None:
raise AssertionError(
"Got slot_list_order of None in multi-valued slot: "
f"slot_id {first_slot['slot_id']}, "
f"next_slot {next_slot['slot_id']}")
return first_slot
return slot_list(new_frame, name, chain([first_slot],
slots_by_name))
for name, slots_by_name in groupby(raw_data, key=itemgetter('name')):
raw_slots[name.lower()] = make_value(slots_by_name)
return new_frame
@property
def frame_label(self):
return self.version_obj.lookup_frame_name(self.frame_id) \
or self.frame_id
def __repr__(self):
#if hasattr(self, 'name'):
# if hasattr(self, 'class_name'):
# return f"<{self.__class__.__name__}({self.frame_label}): " \
# f"{self.class_name}({self.name})>"
# return f"<{self.__class__.__name__}({self.frame_label}): " \
# f"{self.name}>"
if hasattr(self, 'class_name'):
return f"<{self.__class__.__name__}({self.frame_label}): " \
f"{self.class_name}>"
return f"<{self.__class__.__name__}({self.frame_label})>"
def print(self):
for name in sorted(self.get_slot_names()):
print(name, getattr(self, name))
def dump(self, indent=0):
for name in sorted(self.get_slot_names()):
value = self.get_slot(name, ignore_format_errors=True)
print(' ' * indent, end='')
if isinstance(value, frame) and name.lower() not in ('ako', 'isa'):
print(f"{name}:")
value.dump(indent + 2)
elif isinstance(value, dynamic_slot_list):
print(f"{name}: [")
need_sep = False
for v in value:
if need_sep:
print()
if isinstance(v, frame):
v.dump(indent + 2)
need_sep = True
else:
print(' ' * (indent + 2), v, ',', sep='')
need_sep = False
print(' ' * indent, "]", sep='')
else:
print(f"{name}:", repr(value))
def is_frozen(self):
return self.version_obj.is_frozen()
def add_context(self, parent, splices):
assert self.parent is None
return frame(self.frame_id, self.version_obj, self.raw_slots,
parent, splices)
def get_slot_names(self, seen_isa=False, seen_ako=False):
r'''Returns set of slot_names.
Includes inherited slots. Excludes deleted slots.
The returned names have been lowercased.
'''
# Get inherited 'AKO' slots:
if 'ako' in self.raw_slots:
ans = self.cook_raw_slot(self.get_raw_slot('ako')) \
.get_slot_names(seen_isa=seen_isa, seen_ako=True)
else:
ans = set()
# Get inherited 'ISA' slots: (these override inherited 'AKO' slots)
if not seen_isa and 'isa' in self.raw_slots:
ans.update(self.cook_raw_slot(self.get_raw_slot('isa'))
.get_slot_names(seen_isa=True, seen_ako=seen_ako))
# Add my slots: (these override inherited slots)
for slot_name, slot in self.raw_slots.items():
if not isinstance(slot, slot_list) and \
slot['value'].upper() == '<DELETED>':
ans.discard(slot_name)
else:
# Don't include 'FRAME_NAME' in inherited slots!
skip1 = slot_name == 'frame_name' and (seen_isa or seen_ako)
skip2 = slot_name == 'ako' and seen_isa
if not skip1 and not skip2:
ans.add(slot_name)
# Add spliced-in slots: (these override everything else!)
for slot_list_name, frame in self.splices:
for name, raw_slot in self.raw_slots.items():
if name not in ('frame_name', 'class_name', 'isa', 'ako',
'splice', slot_list_name):
if not isinstance(raw_slot, slot_list) and \
raw_slot['value'].upper() == '<DELETED>':
ans.discard(name)
else:
ans.add(name)
return ans
def __getattr__(self, slot_name):
return self.get_slot(slot_name)
def get_slot(self, slot_name, ignore_format_errors=False):
r'''`slot_name` can be any case (upper/lower).
Raises AttributeError if not found.
'''
return self.cook_raw_slot(self.get_raw_slot_inherited(slot_name),
ignore_format_errors=ignore_format_errors)
def get_raw_slot_inherited(self, slot_name, try_isa=True):
r'''Returns raw_slot, checking for inherited slots if necessary.
Raises AttributeError if not found.
'''
#print(f"{self.frame_id}.get_raw_slot_inherited({slot_name}, "
# f"{try_isa})")
try:
# Check my slots:
raw_slot = self.get_raw_slot(slot_name, deleted_is_error=False)
except AttributeError:
if slot_name.lower() != 'frame_name' and \
(slot_name.lower() != 'ako' or try_isa):
# Check inherited 'AKO' slots
if 'ako' in self.raw_slots:
ako = self.cook_raw_slot(self.get_raw_slot('ako'),
format_ok=False)
try:
return ako.get_raw_slot_inherited(slot_name, try_isa)
except AttributeError:
pass
# Check inherited 'ISA' slots
if try_isa and 'isa' in self.raw_slots:
isa = self.cook_raw_slot(self.get_raw_slot('isa'),
format_ok=False)
try:
return isa.get_raw_slot_inherited(slot_name,
try_isa=False)
except AttributeError:
pass
raise
if not isinstance(raw_slot, slot_list) and \
raw_slot['value'].upper() == '<DELETED>':
raise AttributeError(f"{self.frame_label}.{slot_name} deleted")
return raw_slot
def get_inherited_values(self, slot_name, try_isa=True):
r'''Get inherited values for slot_list.
Returns {slot_list_order: raw_slot}.
Does not include raw_slots in this frame itself.
'''
ans = {}
def fetch(slot, try_isa):
r'''Fetch elements inherited through `slot` link ('ISA', or 'AKO').
`slot` must be lowercase.
'''
#print("fetch", slot, try_isa, "len(ans)", ans)
if slot in self.raw_slots:
daddy = self.cook_raw_slot(self.get_raw_slot(slot),
format_ok=False)
#print("fetch got slot, daddy", daddy.frame_label)
ans.update(daddy.get_inherited_values(slot_name, try_isa))
#print("fetch after daddy.get_inherited_values, len(ans)",
# len(ans))
try:
daddy_list = daddy.get_raw_slot(slot_name,
deleted_is_error=False)
except AttributeError:
#print("fetch: daddy does not have slot", slot_name)
pass
else:
#print("fetch: daddy has slot", slot_name, daddy_list)
if not isinstance(daddy_list, slot_list):
ans.clear() # deletes all prior inherited values...
else:
for daddy_slot in daddy_list.iter_raw_slots():
if daddy_slot['value'].upper() == '<DELETED>':
if daddy_slot['slot_list_order'] in ans:
del ans[daddy_slot['slot_list_order']]
else:
ans[daddy_slot['slot_list_order']] = daddy_slot
if try_isa:
fetch('ISA', False)
fetch('AKO', True)
return ans
def get_raw_slot(self, slot_name, deleted_is_error=True):
r'''`slot_name` can be any case (upper/lower).
Does not use inheritance to find `slot_name`.
Does use spliced-in slots to find `slot_name`.
Raises AttributeError if not found.
'''
#print(f"{self.frame_id}.get_raw_slot({slot_name})")
# Check spliced-in slots:
if slot_name.lower() not in ('frame_name', 'class_name', 'isa', 'ako',
'splice'):
for slot_list_name, frame in self.splices:
if slot_name.lower() != slot_list_name.lower():
try:
return frame.get_my_raw_slot(slot_name)
except AttributeError:
pass
# Check my slots:
return self.get_my_raw_slot(slot_name, deleted_is_error)
def get_my_raw_slot(self, slot_name, deleted_is_error=True):
r'''`slot_name` can be any case (upper/lower).
Does not use inheritance to find `slot_name`.
Does not use spliced-in slots to find `slot_name`.
Raises AttributeError if not found.
'''
# Check my slots:
try:
ans = self.raw_slots[slot_name.lower()]
except KeyError:
pass
else:
if not deleted_is_error or isinstance(ans, slot_list) \
or ans['value'].upper() != '<DELETED>':
return ans
if slot_name.lower() != 'frame_name':
raise AttributeError(f"{self.frame_label}.{slot_name}")
else: