Last active
April 1, 2024 03:19
-
-
Save aolkin/2bf11995ae18dc600da37a14110aa8c0 to your computer and use it in GitHub Desktop.
Convert TouchOSC layouts to and from raw XML
This file contains 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 sys, zipfile, base64 | |
import xml.etree.ElementTree, xml.dom.minidom | |
z = zipfile.ZipFile(sys.argv[1]) | |
f = z.open("index.xml") | |
tree = xml.etree.ElementTree.parse(f) | |
root = tree.getroot() | |
def decode_dict(d): | |
for k, v in d.items(): | |
d[k] = base64.b64decode(v).decode() if k in ("name", "osc_cs") else v | |
return d | |
for child in root: | |
decode_dict(child.attrib) | |
for c in child: | |
decode_dict(c.attrib) | |
rough_string = xml.etree.ElementTree.tostring(root, 'utf-8') | |
reparsed = xml.dom.minidom.parseString(rough_string) | |
of = open(sys.argv[1] + ".xml", "w") | |
of.write(reparsed.toprettyxml(indent="\t")) |
This file contains 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 sys, zipfile, base64 | |
import xml.etree.ElementTree | |
tree = xml.etree.ElementTree.parse(sys.argv[1]) | |
root = tree.getroot() | |
def encode_dict(d): | |
for k, v in d.items(): | |
d[k] = base64.b64encode(v.encode()).decode() if k in ("name", | |
"osc_cs") else v | |
return d | |
for child in root: | |
encode_dict(child.attrib) | |
for c in child: | |
encode_dict(c.attrib) | |
with zipfile.ZipFile(sys.argv[1].rstrip(".xml"), | |
"w", zipfile.ZIP_DEFLATED) as z: | |
with z.open("index.xml", "w") as f: | |
tree.write(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment