| 56 | |
| 57 | # tag::SCHEDULE2_LOAD[] |
| 58 | def load(path=JSON_PATH): |
| 59 | records = {} |
| 60 | with open(path) as fp: |
| 61 | raw_data = json.load(fp) |
| 62 | for collection, raw_records in raw_data['Schedule'].items(): |
| 63 | record_type = collection[:-1] # <1> |
| 64 | cls_name = record_type.capitalize() # <2> |
| 65 | cls = globals().get(cls_name, Record) # <3> |
| 66 | if inspect.isclass(cls) and issubclass(cls, Record): # <4> |
| 67 | factory = cls # <5> |
| 68 | else: |
| 69 | factory = Record # <6> |
| 70 | for raw_record in raw_records: # <7> |
| 71 | key = f'{record_type}.{raw_record["serial"]}' |
| 72 | records[key] = factory(**raw_record) # <8> |
| 73 | return records |
| 74 | # end::SCHEDULE2_LOAD[] |