The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

22 lines
779 B

  1. #!/usr/bin/env python3
  2. import json
  3. import sys
  4. import xml.etree.ElementTree
  5. root = xml.etree.ElementTree.fromstring(sys.stdin.read())
  6. assert root.tag == 'files'
  7. for file in root:
  8. assert file.tag == 'file'
  9. attributes = file.attrib
  10. childrenTags = [child.tag for child in file]
  11. assert sorted(childrenTags) == sorted(set(childrenTags)), 'duplicate children'
  12. children = {child.tag: child.text for child in file}
  13. assert not any(k in children for k in attributes), 'attribute found in children'
  14. assert not any(k in attributes for k in children), 'child found in attributes'
  15. assert 'name' in attributes, 'malformed file without name attribute'
  16. o = {'name': attributes['name']}
  17. del attributes['name']
  18. o.update(sorted({**attributes, **children}.items()))
  19. print(json.dumps(o))