DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
jsonify.py
Go to the documentation of this file.
1import conffwk
2import json
3from logging import getLogger
4import sys
5from rich import print
6log = getLogger('daqconf.jsonify')
7
8
9def hash_function(obj):
10 # I guess we could get ObjectId from MongoDB
11 return hash(f'{obj.id}@{obj.className()}')
12
13
14def convert_to_dict(db, obj):
15 dal_dict = {
16 "__type": obj.className(),
17 "_id": {
18 "$oid": hash_function(obj), # Borrowing from MongoDB
19 }
20 }
21
22 for attribute_name, attribute_value in db.attributes(obj.className(), all=True).items():
23 dal_dict[attribute_name] = getattr(obj, attribute_name)
24
25 for relation_name, relation_value in db.relations(obj.className(), all=True).items():
26 relation_object = getattr(obj, relation_name, None)
27
28 if relation_object is None:
29 dal_dict[relation_name] = None
30
31 elif not relation_value.get('multivalue', False):
32 dal_dict[relation_name] = {
33 # "$ref": "run-registry"
34 '$id': hash_function(relation_object),
35 }
36
37 else:
38 dal_dict[relation_name] = [
39 {
40 "$id": hash_function(one_relation_object)
41 # "$ref": "run-registry"
42 }
43 for one_relation_object in relation_object
44 ]
45
46 return dict(sorted(dal_dict.items()))
47
48
49def jsonify_xml_data(oksfile, output):
50
51 sys.setrecursionlimit(10000)
52
53 log.info(f"JSonifying database \'{oksfile}\' to \'{output}\'.")
54
55 log.debug("Reading database")
56 db = conffwk.Configuration("oksconflibs:" + oksfile)
57
58 dals = db.get_all_dals()
59 the_big_dict = {}
60
61 for dal_str in dals:
62 dal = dals[dal_str]
63 key_name = f"{dal.id}@{dal.className()}"
64 log.debug(f"Processing DAL {key_name}")
65 if key_name in the_big_dict:
66 log.error(f"Duplicate DAL id {key_name}")
67 continue
68
69 dal_dict = convert_to_dict(db, dal)
70 the_big_dict[key_name] = dal_dict
71
72 with open(output, 'w') as f:
73 json.dump(dict(sorted(the_big_dict.items())), f, indent=4)
74
hash_function(obj)
Definition jsonify.py:9
convert_to_dict(db, obj)
Definition jsonify.py:14
jsonify_xml_data(oksfile, output)
Definition jsonify.py:49