|
| 1 | + |
| 2 | +from geosx_xml_tools.attribute_coverage import parse_schema |
| 3 | +from geosx_xml_tools.xml_formatter import format_file |
| 4 | +from lxml import etree as ElementTree |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +import argparse |
| 8 | + |
| 9 | + |
| 10 | +def check_redundancy_level(local_schema, node, whitelist=['component']): |
| 11 | + """Check xml redundancy at the current level |
| 12 | +
|
| 13 | + @arg local_schema dict containing schema definitions |
| 14 | + @arg node current xml node |
| 15 | + """ |
| 16 | + node_is_required = 0 |
| 17 | + for ka in node.attrib.keys(): |
| 18 | + if (ka in whitelist): |
| 19 | + node_is_required += 1 |
| 20 | + elif (ka not in local_schema['attributes']): |
| 21 | + node_is_required += 1 |
| 22 | + elif ('default' not in local_schema['attributes'][ka]): |
| 23 | + node_is_required += 1 |
| 24 | + elif (node.get(ka) != local_schema['attributes'][ka]['default']): |
| 25 | + node_is_required += 1 |
| 26 | + else: |
| 27 | + node.attrib.pop(ka) |
| 28 | + |
| 29 | + for child in node: |
| 30 | + # Comments will not appear in the schema |
| 31 | + if child.tag in local_schema['children']: |
| 32 | + child_is_required = check_redundancy_level(local_schema['children'][child.tag], |
| 33 | + child) |
| 34 | + node_is_required += child_is_required |
| 35 | + if not child_is_required: |
| 36 | + node.remove(child) |
| 37 | + |
| 38 | + return node_is_required |
| 39 | + |
| 40 | + |
| 41 | +def check_xml_redundancy(schema, fname): |
| 42 | + """Check redundancy in an xml file |
| 43 | +
|
| 44 | + @arg schema dict containing schema definitions |
| 45 | + @arg fname name of the target file |
| 46 | + """ |
| 47 | + xml_tree = ElementTree.parse(fname) |
| 48 | + xml_root = xml_tree.getroot() |
| 49 | + check_redundancy_level(schema['Problem'], xml_root) |
| 50 | + xml_tree.write(fname) |
| 51 | + format_file(fname) |
| 52 | + |
| 53 | + |
| 54 | +def process_xml_files(geosx_root): |
| 55 | + """Test for xml redundancy |
| 56 | +
|
| 57 | + @arg geosx_root GEOSX root directory |
| 58 | + """ |
| 59 | + |
| 60 | + # Parse the schema |
| 61 | + geosx_root = os.path.expanduser(geosx_root) |
| 62 | + schema_fname = '%ssrc/coreComponents/schema/schema.xsd' % (geosx_root) |
| 63 | + schema = parse_schema(schema_fname) |
| 64 | + |
| 65 | + # Find all xml files, collect their attributes |
| 66 | + for folder in ['src', 'examples']: |
| 67 | + print(folder) |
| 68 | + xml_files = Path(os.path.join(geosx_root, folder)).rglob('*.xml') |
| 69 | + for f in xml_files: |
| 70 | + print(' %s' % (str(f))) |
| 71 | + check_xml_redundancy(schema, str(f)) |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + """Entry point for the xml attribute usage test script |
| 76 | +
|
| 77 | + @arg -r/--root GEOSX root directory |
| 78 | + """ |
| 79 | + |
| 80 | + # Parse the user arguments |
| 81 | + parser = argparse.ArgumentParser() |
| 82 | + parser.add_argument('-r', '--root', type=str, help='GEOSX root', default='') |
| 83 | + args = parser.parse_args() |
| 84 | + |
| 85 | + # Parse the xml files |
| 86 | + process_xml_files(args.root) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
| 91 | + |
| 92 | + |
| 93 | + |
0 commit comments