File indexing completed on 2024-05-05 03:51:20

0001 #!/usr/bin/env python3
0002 # SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003 # SPDX-License-Identifier: LGPL-2.0-or-later
0004 
0005 import argparse
0006 import xml.etree.ElementTree as ET
0007 
0008 parser = argparse.ArgumentParser(description='Normalize OSM data in XML format for easier comparison')
0009 parser.add_argument('osm', nargs='+', help='OSM file to normalize', type=str)
0010 arguments = parser.parse_args()
0011 
0012 attrExclusionList = [ 'generator', 'timestamp', 'uid', 'user', 'visible', 'changeset', 'version' ]
0013 
0014 def processRecursive(node):
0015     if not isinstance(node.tag, str):
0016         return
0017 
0018     # drop undesired attributes
0019     for attr in attrExclusionList:
0020         if attr in node.attrib:
0021             del node.attrib[attr]
0022 
0023     # sort tags
0024     node[:] = sorted(node, key=lambda node: node.get('k', ''))
0025 
0026     for child in node:
0027         processRecursive(child)
0028 
0029 for f in arguments.osm:
0030     tree = ET.parse(f)
0031     root = tree.getroot()
0032     processRecursive(root)
0033     ET.indent(tree)
0034     tree.write(f)