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.
Viewer for pyui files: intercept warnings
-
I'm busy to develop a file explorer, starting from File Picker of @omz.
I use a an ui.Webview to display some files extensions, or an ui.TextView to display some text files.
But for .pyui files, I use an ui.load_view for which I force the frame where I want it as a subview of my main view.
For certain .pyui files, I get some normal warning messages like "could not bind action" because my script is a general browser and all buttons actions of all .pyui files are obviously not defined.
Is it possible to intercept these messages? -
You shouldd peruse the source code for the ui module, as this will give you some ideas.
First, you could copy the source to view_from_dict into your own module, and customize it as you see fit.
Tyat particular warning comes from
_bind_action
. So you could monkey patchui._bind_action
to not issue that warning, or replace the code that writes to sys.stderr with a warning, which then can be suppressed using thewarnings
module.Or you could temporarily reassign sys.stderr, though that would make it hard to see real errors in your code. though if you wrap your code in a try/catcg where you assign it back before rethrowing, you might be ok.
-
Alternatively you could pre-read the .pyui file looking for all occurrences of
action=
and create doNothing functions for each action before calling ui.load(). -
@ccc I understand your proposal but my script would be general and only show a .pyui file.
I don't know by advance which file I could read, thus I can't imagine to create a doNothing function for each action.
As I only want to intercept these warnings, I'ld prefer the @jonb solution. I'll try to understand it...I really don't know how we write a monkey patch to replace the standard ui._bind_action, but I'll try
But anyway, as usual, thanks to you both, THE guru's...I understand I can replace the ui.py by mine but not how to replace only one def by mine which would "override" the original one, if it's possible.
Sorry, I even didn't know these words "monkey patch" but I have Googlized it and found, thus thanks, this should be the solution.
-
And it works.
I've so much to learn in Python...all these features I knew in all other languages I've used in my long life 😭