Last active
March 28, 2022 07:35
-
-
Save dlinsley/38be3e97f09ad2ca114ba7566f910106 to your computer and use it in GitHub Desktop.
Parse strings from an exported vRO Configuration Element with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import json | |
import sys | |
import xml.etree.ElementTree as ET | |
parser = argparse.ArgumentParser('Parse strings from exported vRO Configuration Element XML') | |
parser.add_argument('FILE',help='ConfigElement XML file path') | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument('-n','--name',dest='vroAttribute',help='Attribute Name to parse') | |
group.add_argument('--json',dest='jsonOut',action='store_true',help='Parse Configuration Element as a JSON object') | |
cliArgs = parser.parse_args() | |
tree = ET.parse(cliArgs.FILE) | |
vroAttribute = cliArgs.vroAttribute | |
root = tree.getroot() | |
ourConfig = {} | |
for attr in root.iter('att'): | |
attributeName = attr.get('name') | |
attributeType = attr.get('type') | |
if attributeType == 'Array/string': | |
encodedValues = attr.find('value').text | |
encodedValues = encodedValues.replace('"','\\"') | |
encodedValues = encodedValues.replace('#;#string#','","') | |
encodedValues = encodedValues.replace('#}#','"]') | |
jsonValues = encodedValues.replace('#{#string#','["') | |
ourConfig[attributeName] = json.loads(jsonValues) | |
elif attributeType == 'string': | |
ourConfig[attributeName] = attr.find('value').text | |
if cliArgs.jsonOut: | |
print(json.dumps(ourConfig)) | |
else: | |
if isinstance(ourConfig[vroAttribute],list): | |
for attr in ourConfig[vroAttribute]: | |
print(attr) | |
else: | |
print(ourConfig[vroAttribute]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment