Skip to content

Instantly share code, notes, and snippets.

@gholker
Last active April 3, 2020 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gholker/cc978acc387a3d8fe52472182277f5e5 to your computer and use it in GitHub Desktop.
Save gholker/cc978acc387a3d8fe52472182277f5e5 to your computer and use it in GitHub Desktop.
import os
import sys
# Requires LXML. See: https://lxml.de/installation.html
from lxml import etree as ET
def check_file(path):
print(f'Checking file at path: {path}')
huge_tree_parser = ET.XMLParser(huge_tree=True)
tree = ET.parse(path, huge_tree_parser)
root = tree.getroot()
for section in root.findall('.//SECTION'):
sect_no = section.find('SECTNO').text
internal_sections = list(section.findall('.//SECTION'))
mismatched_sections = []
for internal_section in internal_sections:
internal_sect_no = internal_section.find('SECTNO').text
if internal_sect_no != sect_no:
mismatched_sections.append(internal_sect_no)
if mismatched_sections:
print(f'`{sect_no}` contains mismatched sections `{",".join(mismatched_sections)}`')
print(f'Check complete: {path}')
if __name__ == '__main__':
path = os.path.expanduser(sys.argv[1])
if os.path.isfile(path):
check_file(path)
else:
for dirpath, _, file_names in os.walk(path):
for f in file_names:
if f.endswith(".xml"):
check_file(os.path.join(dirpath, f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment