Skip to content

Instantly share code, notes, and snippets.

@dave-britten
Created September 16, 2015 13:48
Show Gist options
  • Save dave-britten/5b2d1c66f5fc042a6b2c to your computer and use it in GitHub Desktop.
Save dave-britten/5b2d1c66f5fc042a6b2c to your computer and use it in GitHub Desktop.
PYUITest.py
#Attempt to load a .pyui file and generate code that would produce the same results.
#Note: this doesn't actually work yet.
from __future__ import print_function
infilename = raw_input("Input File?")
#infilename = "RegexSandbox.pyui"
outfilename = infilename + ".py"
f = open(infilename, "r")
vs = f.read()
f.close()
vs = vs.replace("true", "True").replace("false", "False")
outfile = open(outfilename, "w")
def explode(o, p):
objname = o["attributes"]["name"]
objclass = o["class"]
print("\t\t#" + "".join(["{} \"{}\"/".format(n[1], n[0]) for n in p]) + objclass + " \"" + objname + "\"", file=outfile)
print("\t\to = ui." + objclass + "()", file=outfile)
for a in o["attributes"]:
if a in ["enabled", "content_width", "content_height", "uuid"]:
continue
elif a == "alignment":
val = ["ui.ALIGN_CENTER", "ui.ALIGN_JUSTIFIED", "ui.ALIGN_LEFT", "ui.ALIGN_NATURAL", "ui.ALIGN_RIGHT"][["center", "jusitified", "left", "natural", "right"].index(o["attributes"][a])]
else:
val = repr(o["attributes"][a])
print("\t\to.{} = {}".format(a, val), file=outfile)
if objclass == "ScrollView":
print("\t\to.content_size = ({}, {})".format(o["attributes"]["content_width"], o["attributes"]["content_height"]), file=outfile)
print("\t\tself" + "".join(["[\"" + n[0] + "\"]" for n in p]) + ".add_subview(o)", file=outfile)
print("", file=outfile)
p = list(p)
p.append((objname, objclass))
for n in o["nodes"]:
explode(n, p)
vw = eval(vs)
classname = vw[0]["attributes"]["custom_class"]
print("import ui", file=outfile)
print("class " + classname + "(ui.View):", file=outfile)
print("\tdef __init__(self):", file=outfile)
attr = vw[0]["attributes"]
for a in attr:
if a in {"custom_class"}:
continue
print("\t\t" + "self." + a + " = " + repr(attr[a]), file=outfile)
print("", file=outfile)
for n in vw[0]["nodes"]:
explode(n, [])
print("", file=outfile)
print("vw = " + classname + "()", file=outfile)
print("vw.present(\"sheet\")", file=outfile)
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment