I just wrote this code because I wanted the size of a pyui file's view without having to literally load the view (object creation, memory allocation etc) It does of course load the files contents and converts it into a list of dicts. (Just copied omz's code).
Anyway, useful for me. But not complete. Also, no extensive testing
# coding: utf-8
# the begining of a pyui inspector.
# maybe many already exists. just copied from ui.py
# for reading the file. then try to return a value.
# if object_name == None, assumes you are looking
# the parent view.
# if object_name, then looks through the nodes for
# a ui element of that name. if found, it looks to
# see if the key requested is present.
# for the moment, if what you are looking for is not
# found, None is returned, not sure about best
# practices, should an error be raised etc...
# i say its only a start.
# ITS NO RECURSIVE ON NODES
# i just needed the view size of a pyui file. i didnt
# want to have the view's objects created to get the # size! but i thought, easy to expand it a little and # share it.
# again, this could have been written a million times
# already. but this sort of access reminds me of the
# old resource manager on the mac! was such a simple
# idea, but worked so well.
def get_pyui_attr(pyui_path, object_name , key):
import os, json
if len(os.path.splitext(pyui_path)[1]) == 0:
# The '.pyui' extension can be omitted, but if there is an extension (e.g. '.json'), it should be kept.
pyui_path += '.pyui'
with open(pyui_path) as f:
json_str = f.read()
root_list = json.loads(json_str)
# if object == None, we are in the view/root
if not object_name:
# looking in the root
if root_list[0].has_key(key):
return root_list[0][key]
else:
return None
# look through the nodes for a ui element named
# object_name. not recursive at the moment.
for node in root_list[0]['nodes']:
if node['attributes']['name'] == object_name:
if node.has_key(key):
return node[key]
else:
return None
return None
# returns the frame of my pyui file called 'mycell1'
# has to read the file, convert with json into a list
# of dicts, but no object creation!
print get_pyui_attr('mycell1', None, 'frame')