convert hcp xml into json for easier processing
This commit is contained in:
103
xml/hcpxml2json
Executable file
103
xml/hcpxml2json
Executable file
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env python3
|
||||
# Convert the featuremap and devicedescription XML files into a single JSON
|
||||
# this collapses the XML entities and duplicates some things, but makes for
|
||||
# easier parsing later
|
||||
#
|
||||
# Program groups are ignored for now
|
||||
#
|
||||
|
||||
import sys
|
||||
import xml.etree.ElementTree as ET
|
||||
import json
|
||||
|
||||
# the feature file has features, errors, and enums
|
||||
# for now the ordering is hardcoded
|
||||
featuremapping = ET.parse(sys.argv[1]).getroot()
|
||||
description = ET.parse(sys.argv[2]).getroot()
|
||||
|
||||
#####################
|
||||
#
|
||||
# Parse the feature file
|
||||
#
|
||||
|
||||
features = {}
|
||||
errors = {}
|
||||
enums = {}
|
||||
|
||||
# Features
|
||||
for child in featuremapping[1]: #.iter('feature'):
|
||||
uid = int(child.attrib["refUID"], 16)
|
||||
name = child.text
|
||||
features[uid] = name
|
||||
|
||||
# Errors
|
||||
for child in featuremapping[2]:
|
||||
uid = int(child.attrib["refEID"], 16)
|
||||
name = child.text
|
||||
errors[uid] = name
|
||||
|
||||
# Enums
|
||||
for child in featuremapping[3]:
|
||||
uid = int(child.attrib["refENID"], 16)
|
||||
enum_name = child.attrib["enumKey"]
|
||||
values = {}
|
||||
for v in child:
|
||||
value = int(v.attrib["refValue"])
|
||||
name = v.text
|
||||
values[value] = name
|
||||
enums[uid] = {
|
||||
"name": enum_name,
|
||||
"values": values,
|
||||
}
|
||||
|
||||
#####################
|
||||
#
|
||||
# Parse the description file
|
||||
#
|
||||
|
||||
def parse_xml_list(entries):
|
||||
parsed = {}
|
||||
|
||||
for el in entries:
|
||||
# not sure how to parse refCID and refDID
|
||||
uid = int(el.attrib["uid"], 16)
|
||||
data = {
|
||||
"name": features[uid],
|
||||
}
|
||||
|
||||
for key in el.attrib:
|
||||
data[key] = el.attrib[key]
|
||||
|
||||
# clean up
|
||||
del data["uid"]
|
||||
|
||||
if "enumerationType" in el.attrib:
|
||||
del data["enumerationType"]
|
||||
enum_id = int(el.attrib["enumerationType"], 16)
|
||||
data["values"] = enums[enum_id]["values"]
|
||||
|
||||
parsed[uid] = data
|
||||
|
||||
return parsed
|
||||
|
||||
def parse_machine_description(entries):
|
||||
description = {}
|
||||
|
||||
for el in entries:
|
||||
prefix, has_namespace, tag = el.tag.partition('}')
|
||||
if tag != "pairableDeviceTypes":
|
||||
description[tag] = el.text
|
||||
|
||||
return description
|
||||
|
||||
machine = {
|
||||
"description": parse_machine_description(description[3]),
|
||||
"status": parse_xml_list(description[4]),
|
||||
"settings": parse_xml_list(description[5]),
|
||||
"events": parse_xml_list(description[6]),
|
||||
"commands": parse_xml_list(description[7]),
|
||||
"options": parse_xml_list(description[8]),
|
||||
"errors": errors,
|
||||
}
|
||||
|
||||
print(json.dumps(machine))
|
||||
Reference in New Issue
Block a user