Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
[Share Code] the start of a pyui key inspector...
-
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')
-
json_str = f.read() root_list = json.loads(json_str) # can be rewritten... root_list = json.load(f)
-
@ccc, thanks again! You should be paid more :)
# 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 NOT 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 not os.path.splitext(pyui_path)[1]: pyui_path += '.pyui' # make sure the file exists! if not os.path.isfile(pyui_path): # return None for now, but maybe should raise # an error return None 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: return root_list[0].get(key, 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: return node['attributes'].get(key, 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')
-
@omz, look I know you have bigger fish to fry at the moment. But appears if say a ui.label has a justification of ui.ALIGN_CENTER, in the pyui it's stored as 'center' rather than ui.ALIGN_CENTER.
A small detail, but I think worth reporting. -
why not just load the pyui and interrogate it directly?
-
@JonB, I understand what you mean. But let's say you only want the frame size of the pyui file ahead of time. (This is all I started out to achieve). But , it just seems like a huge over kill if you just want a attribute such as the view frame to do a call to ui.loadview
Look, I understand in a way it is splitting hairs. The processors are so fast and memory is so big that today it does not really matter.
I think I am stuck in the past. In another way, I think it's ok. A lot of People scoff at high level languages because you can get away with murder and write shitty code. Not really apparent to the coder until he/she has to maintain it , especially commercially. Personally I think the way people look down on high level languages is crap. Can write good and bad code in any language.
When I write here, I have no need to write the word script Vrs code. It's all code. Up to the coder how elegant he or she is or is not!
And look, I am far from being elegant, currently I am crap. But I want to be elegant. The language doesn't define me, I define myself about how I use the language.I know you know this already. Sometimes I am being anal though. But I think for a good reason. If I was not like that when I was younger, I would not be sitting retired in thailand, well it can be anywhere in the world, but I choose here.
I hope the young guys here also take that approach. Being anal I mean about getting to the bottom of things.
Lol, so that's why I don't just want to load in the view and query it :)
-
in that case, you need to read through the load_view code, which can be found in the ui module, so you can learn the "language" of pyuis. there are some quirks you have to deal with, such as the way colors are represented, etc.
-
@JonB , I agree. I have been looking at ui.py
But to be honest, I don't get it all. But as each day passes, I understand a little more each day. Maybe I will be dead before I get anywhere , but it's still a fun journey. But I am sure a lot of these quirks are things that omz did in his early development of the product, and he has to spread his time about what he focuses on.
Personally, I find it hard to comprehend he has written Pythonista. He calls himself an indie programmer, well there needs to be more of them.
I honestly can't wait to meet him. I am a smart guy, but in different ways. But I feel the same about you and ccc also. I can see you guys are very talented.
Oh shit, I am a groupie :) I don't care. Love meeting and talking with exceptional people!