Skip to content

Instantly share code, notes, and snippets.

@jonathanrobie
Created May 10, 2024 12:32
Show Gist options
  • Select an option

  • Save jonathanrobie/160b9524d3dc503fe3b430d59d79e4ce to your computer and use it in GitHub Desktop.

Select an option

Save jonathanrobie/160b9524d3dc503fe3b430d59d79e4ce to your computer and use it in GitHub Desktop.
import os
import xml.etree.ElementTree as ET
import json
def parse_xml_file(file_path):
""" Parse an XML file and return needed metadata as a dictionary. """
tree = ET.parse(file_path)
root = tree.getroot()
data = {
'Language': root.find('Language').text if root.find('Language') is not None else '',
'FullName': root.find('FullName').text if root.find('FullName') is not None else '',
'Copyright': root.find('Copyright').text if root.find('Copyright') is not None else '',
'Name': root.find('Name').text if root.find('Name') is not None else '',
'subdirectory': os.path.dirname(file_path)
}
return data
def create_indexed_dictionaries(base_path):
""" Create dictionaries indexed by language, fullname, and name from Settings.xml files. """
language_dict = {}
fullname_dict = {}
name_dict = {}
# Walk through the directory and find all Settings.xml files
for root, dirs, files in os.walk(base_path):
for file in files:
if file == 'Settings.xml':
full_path = os.path.join(root, file)
metadata = parse_xml_file(full_path)
language_dict[metadata['Language']] = metadata
fullname_dict[metadata['FullName']] = metadata
name_dict[metadata['Name']] = metadata
return {
'ByLanguage': language_dict,
'ByFullName': fullname_dict,
'ByName': name_dict
}
# Directory containing the subdirectories with Settings.xml
base_directory = '.'
# Create dictionaries and print them as JSON
indexed_data = create_indexed_dictionaries(base_directory)
print(json.dumps(indexed_data, indent=4, ensure_ascii=False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment