Skip to content

Instantly share code, notes, and snippets.

@tilacog
Created April 17, 2018 17:52
Show Gist options
  • Save tilacog/9e9fd3bb54fceab4a9982e888b97ad18 to your computer and use it in GitHub Desktop.
Save tilacog/9e9fd3bb54fceab4a9982e888b97ad18 to your computer and use it in GitHub Desktop.
pretty print specification files
import sys
from xml.etree.ElementTree import iterparse
from types import SimpleNamespace
def show(text, depth, spacer=' '):
print(spacer * depth + text)
def main(spec_file_path):
state = SimpleNamespace(depth=0, records=[])
parser = iterparse(spec_file_path, events=('start', 'end'))
for (event, node) in parser:
if event == 'start' and node.tag == 'registro':
state.records.append(node)
state.depth += 1
show(node.attrib['id'] + ':', state.depth)
if event == 'end' and node.tag == 'registro':
state.records.pop()
state.depth -= 1
if event == 'start' and node.tag == 'campo':
# TODO: print current record before field name, to make it grep'pable
text = node.attrib['id']
if text == 'REG':
continue
show(text, state.depth +1)
if __name__ == '__main__':
spec_file = sys.argv[1]
main(spec_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment