Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PYUIhelper.py
import ui, editor, clipboard
import base64, bz2, json, textwrap
_pyui_str = \
'''
QlpoOTFBWSZTWZism/sAAR7fgFUQUOd/0D/AFY6/r9/KMAF5qwwSiTTQp7Qnok09QaAbU9
R6QeoMaj0g0Co8piANMg0ADQAAAEiiaCnppHqb1Q9QaHqAAAA02pISuSGAc+YNeVsQiyii
ARtlFRalIpEJFIAb6AkKoBQXb4wKWSUP3cy89WpMI2EGmUIw6n+lY6hsxid+BnkQjkkCJK
Skaqimig1ViFXva0dCqXpfnYnCEGlwivUhQZideAUAwYLSOZnTFfMwhEKJgFoykQWBXRik
HXLEAKGJ1aIKFD54dSXBdBanX1TzzvhLKTXyAJfUj446sEp09NktmFO1PdwGg69VeWzZty
S6GFlE33uYSJkJucRC4rkjGkSNYGBEkbEkg79GTVIL85jCCrBo+eJqipvoCQtkQxpSfgnG
ji/GzMRjWQLbTUFqoqFmbqzQ1grXnIFCaQfXTzPpB7K14Z5WrJk0Vod15hMUiapm5zKFVX
JbCiWLQU3VqasMwVDKz6eI05BHlxl9m0/cX+LuSKcKEhMVk39g
'''
# PYUI file Utilities
def decode_pyui_str(pyui_str):
# reverse the encoding of the pyui_str
#
s = bz2.decompress(base64.b64decode(pyui_str))
return s.decode('utf-8')
def is_encoded(pyui_str):
# a simple check. if the str 'attributes' is not in the pyui_str,
# it must be encoded or invalid. really!
if 'attributes' not in pyui_str:
return True
return False
def write_pyui_str_to_file(fn, pyui_str):
# write a pyui_str (a json str) to a file.
# normally your fn (file) should have the ext (.pyui) if you want to
# view the file in the Designer. Of course this only works as long as
# the string written is not encoded.
if is_encoded(pyui_str):
the_str = decode_pyui_str(pyui_str)
else:
the_str = pyui_str
try:
with open(fn , 'w') as file:
file.write(the_str)
except OSError as e:
print('OSError - {}, {}'.format(e.errno, e.strerror))
def get_custom_class_from_pyui_str(pyui_str):
# a single puropse, given a pyui_str to return the 'custom_class'
# value from the root view.
if is_encoded(pyui_str):
the_str = decode_pyui_str(pyui_str)
else:
the_str = pyui_str
root_list = json.loads(the_str)
cc = root_list[0]['attributes']['custom_class']
return cc
def custom_class_from_pyui_file(fn):
with open(fn, encoding='utf-8') as file:
json_str = file.read()
return get_custom_class_from_pyui_str(json_str)
def pyui_encode(fn, to_clipboard = True):
#with open(fn, 'rb') as file:
with open(fn, 'rb') as file:
pyui_str = file.read()
compressed = base64.b64encode(bz2.compress(pyui_str)).decode('utf-8')
encoded = '\n'.join(textwrap.wrap(compressed, 70))
if to_clipboard:
clipboard.set(encoded)
return encoded
# end PYUI file Utilities
class PYUILoader(ui.View):
def WrapInstance(obj):
class Wrapper(obj.__class__):
def __new__(cls):
return obj
return Wrapper
def __init__(self, data_ref, *args, **kwargs):
bindings = globals().copy()
bindings[self.__class__.__name__]=self.WrapInstance()
# decide if we are passed a string or a filename
if self._is_file_name(data_ref):
ui.load_view(data_ref, bindings)
else:
ui.load_view_str(self.pyui_decode(data_ref), bindings)
# call after so our kwargs modify attrs
super().__init__(*args, **kwargs)
def _is_file_name(self, data_ref):
# rough as hell... refine later
if len(data_ref) > 50:
return False
else:
return True
def pyui_decode(self, str):
# reverse the encoding of the pyui_str
s = bz2.decompress(base64.b64decode(str))
return s.decode('utf-8')
if __name__ == '__main__':
# try to show an example case. its not that easy to explain. Not
# that is diffict to do. just a little disjointed.
'''
1. create your pyui file in the Designer
1a. create class sublassing PYUILoader, as below....
TestClass(PYUILoader)
2. in the pyui file.
'''
class TestClass(PYUILoader):
def __init__(self, pyui_fn, *args, **kwargs):
super().__init__(pyui_fn, *args, **kwargs)
class DrawCircle(ui.View):
'''
DrawCircle
this class maybe looks a little stange.its never referred to
in code.
in the pyui file, there is a custom view control and its Custom View Class is set to DrawCircle. then it calls this class to be the view.
There is another anmoly in the draw method. the statement self.draw_color. its not defined here in the code.
Its a custom attribute of the view control that points to this class. the custom attr looks like this -
{'draw_color':'deeppink'}
So
'''
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def draw(self):
s = ui.Path.oval(*self.bounds.inset(5, 5))
ui.set_color(self.draw_color)
s.fill()
#fn = _pyui_str
#fn = 't.pyui'
fn = _pyui_str
tc = TestClass(fn, bg_color='lightyellow')
#print(dir(mc))
tc.present('sheet', animated=False)
#pyui_encode('t.pyui' )
#mc.pyui_str_to_file('crap.pyui', _pyui_str)
#write_pyui_str_to_file('crap2.pyui', _pyui_str)
#print(mc.custom_class_from_pyui_str(_pyui_str, encoded = True))
#print(custom_class_from_pyui_file('control_pyui.pyui'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment