-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from DUNE-DAQ/plasorak/jsonifiy
Add a `jsonify-xml` utility
- Loading branch information
Showing
7 changed files
with
170 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import conffwk | ||
import json | ||
from logging import getLogger | ||
import sys | ||
from rich import print | ||
log = getLogger('daqconf.jsonify') | ||
|
||
|
||
def hash_function(obj): | ||
# I guess we could get ObjectId from MongoDB | ||
return hash(f'{obj.id}@{obj.className()}') | ||
|
||
|
||
def convert_to_dict(db, obj): | ||
dal_dict = { | ||
"__type": obj.className(), | ||
"_id": { | ||
"$oid": hash_function(obj), # Borrowing from MongoDB | ||
} | ||
} | ||
|
||
for attribute_name, attribute_value in db.attributes(obj.className(), all=True).items(): | ||
dal_dict[attribute_name] = getattr(obj, attribute_name) | ||
|
||
for relation_name, relation_value in db.relations(obj.className(), all=True).items(): | ||
relation_object = getattr(obj, relation_name, None) | ||
|
||
if relation_object is None: | ||
dal_dict[relation_name] = None | ||
|
||
elif not relation_value.get('multivalue', False): | ||
dal_dict[relation_name] = { | ||
# "$ref": "run-registry" | ||
'$id': hash_function(relation_object), | ||
} | ||
|
||
else: | ||
dal_dict[relation_name] = [ | ||
{ | ||
"$id": hash_function(one_relation_object) | ||
# "$ref": "run-registry" | ||
} | ||
for one_relation_object in relation_object | ||
] | ||
|
||
return dict(sorted(dal_dict.items())) | ||
|
||
|
||
def jsonify_xml_data(oksfile, output): | ||
|
||
sys.setrecursionlimit(10000) | ||
|
||
log.info(f"JSonifying database \'{oksfile}\' to \'{output}\'.") | ||
|
||
log.debug("Reading database") | ||
db = conffwk.Configuration("oksconflibs:" + oksfile) | ||
|
||
dals = db.get_all_dals() | ||
the_big_dict = {} | ||
|
||
for dal_str in dals: | ||
dal = dals[dal_str] | ||
key_name = f"{dal.id}@{dal.className()}" | ||
log.debug(f"Processing DAL {key_name}") | ||
if key_name in the_big_dict: | ||
log.error(f"Duplicate DAL id {key_name}") | ||
continue | ||
|
||
dal_dict = convert_to_dict(db, dal) | ||
the_big_dict[key_name] = dal_dict | ||
|
||
with open(output, 'w') as f: | ||
json.dump(dict(sorted(the_big_dict.items())), f, indent=4) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
#!/bin/env python3 | ||
import click | ||
from daqconf.consolidate import consolidate_db | ||
from daqconf.utils import log_levels, setup_logging | ||
|
||
@click.command() | ||
@click.option('--oksfile', '-i', help='Input database to read') | ||
@click.option('--log-level', '-l', help='Log level', default='INFO', type=click.Choice(log_levels, case_sensitive=False)) | ||
@click.argument('output_file') | ||
def consolidate(oksfile, output_file): | ||
consolidate_db(oksfile, output_file) | ||
def consolidate(oksfile, output_file, log_level): | ||
setup_logging(log_level) | ||
consolidate_db(oksfile, output_file) | ||
|
||
if __name__ == '__main__': | ||
consolidate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
#!/bin/env python3 | ||
import click | ||
from daqconf.consolidate import consolidate_files | ||
from daqconf.utils import log_levels, setup_logging | ||
|
||
@click.command() | ||
@click.option('--oksfile', '-i', help='Input database(s) to read', multiple=True) | ||
@click.option('--log-level', '-l', help='Log level', default='INFO', type=click.Choice(log_levels, case_sensitive=False)) | ||
@click.argument('output_file') | ||
def consolidate(oksfile, output_file): | ||
consolidate_files(output_file, *oksfile) | ||
def consolidate(oksfile, output_file, log_level): | ||
setup_logging(log_level) | ||
consolidate_files(output_file, *oksfile) | ||
|
||
if __name__ == '__main__': | ||
consolidate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/env python3 | ||
import click | ||
from daqconf.jsonify import jsonify_xml_data | ||
from daqconf.utils import log_levels, setup_logging | ||
|
||
@click.command() | ||
@click.option('--oksfile', '-i', help='Input database to read') | ||
@click.option('--log-level', '-l', help='Log level', default='INFO', type=click.Choice(log_levels, case_sensitive=False)) | ||
@click.argument('output_file') | ||
def jsonify_xml(oksfile, output_file, log_level): | ||
setup_logging(log_level) | ||
jsonify_xml_data(oksfile, output_file) | ||
|
||
if __name__ == '__main__': | ||
jsonify_xml() |