Discussion:
how to determine if a module is running inside a wx app
schoenborno
2014-02-14 06:51:15 UTC
Permalink
One of the pubsub modules should print a warning when certain deprecated
features are used. When run in a wx app, the print should go to a wx
message box or such. Is there a way for a module to determine if it is
being loaded as part of wx app? Checking if path contains wx is not
sufficient (module can be run standalone even from wx/lib). Is there a
global that wx sets when it starts up?
Oliver
--
You received this message because you are subscribed to the Google Groups "wxPython-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxPython-dev+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Robin Dunn
2014-02-14 08:59:31 UTC
Permalink
Post by schoenborno
One of the pubsub modules should print a warning when certain deprecated
features are used. When run in a wx app, the print should go to a wx
message box or such. Is there a way for a module to determine if it is
being loaded as part of wx app? Checking if path contains wx is not
sufficient (module can be run standalone even from wx/lib). Is there a
global that wx sets when it starts up?
I would look at sys.modules, if there is an item for 'wx' then check if
wx.GetApp() is not None.
--
Robin Dunn
Software Craftsman
http://wxPython.org
--
You received this message because you are subscribed to the Google Groups "wxPython-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxPython-dev+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Chris Barker
2014-02-14 17:29:26 UTC
Permalink
Post by Robin Dunn
Post by schoenborno
One of the pubsub modules should print a warning when certain deprecated
features are used. When run in a wx app, the print should go to a wx
message box or such. Is there a way for a module to determine if it is
being loaded as part of wx app? Checking if path contains wx is not
sufficient (module can be run standalone even from wx/lib). Is there a
global that wx sets when it starts up?
I would look at sys.modules, if there is an item for 'wx' then check if
wx.GetApp() is not None.
"It's easier to ask forgiveness than permission" -- so:

try:
app = wx.GetApp()
if app is not None:
do_something_with_app
else:
raise AppNotStartedError
except NameError, AppNotStartedError:
do_something_else


Note, however that that's a bit klunky -- maybe better to use a logging
system or something, so there is a default implementation in pubsub, and wx
can override it with a wx version.

Maybe even use the python logging module, or the warnings module.

One more thought -- the Warning really shouldn't go to a GUI message
box anyway -- you want the developer to get the warning, not the end-user
-- I'd go with the standard library warnings module.

-Chris
Post by Robin Dunn
--
Robin Dunn
Software Craftsman
http://wxPython.org
--
You received this message because you are subscribed to the Google Groups
"wxPython-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

***@noaa.gov
--
You received this message because you are subscribed to the Google Groups "wxPython-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxPython-dev+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Robin Dunn
2014-02-16 01:26:25 UTC
Permalink
Post by schoenborno
One of the pubsub modules should print a warning when certain deprecated
features are used. When run in a wx app, the print should go to a wx
message box or such. Is there a way for a module to determine if it is
being loaded as part of wx app? Checking if path contains wx is not
sufficient (module can be run standalone even from wx/lib). Is there a
global that wx sets when it starts up?
I would look at sys.modules, if there is an item for 'wx' then check
if wx.GetApp() is not None.
app = wx.GetApp()
do_something_with_app
raise AppNotStartedError
do_something_else
Right, but if pubsib is being used from a non-wx app you wouldn't want
to have to import wx just to test if wx is being used.
--
Robin Dunn
Software Craftsman
http://wxPython.org
--
You received this message because you are subscribed to the Google Groups "wxPython-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxPython-dev+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
schoenborno
2014-02-19 05:46:52 UTC
Permalink
Post by Robin Dunn
Post by schoenborno
One of the pubsub modules should print a warning when certain
deprecated
features are used. When run in a wx app, the print should go to
a wx
Post by schoenborno
message box or such. Is there a way for a module to determine if it is
being loaded as part of wx app? Checking if path contains wx is
not
Post by schoenborno
sufficient (module can be run standalone even from wx/lib). Is there a
global that wx sets when it starts up?
I would look at sys.modules, if there is an item for 'wx' then check
if wx.GetApp() is not None.
app = wx.GetApp()
do_something_with_app
raise AppNotStartedError
do_something_else
Right, but if pubsib is being used from a non-wx app you wouldn't want
to have to import wx just to test if wx is being used.
--
Robin Dunn
Software Craftsman
http://wxPython.org
Actually it can't be done for my case: setuparg1.py prints warning about
deprecation during import, and I was thinking that message would not be
visible to developer importing from a wx app. But there is no guarantee
that import of setuparg1.py will occur after wx re-routed sys.stdout, could
be before, and the developer might start wx app with python instead of
pythonw, or even from a console, so the message may well be visible. And as
Chris pointed out, the message is for the developer, not the user.

The only sure way to get the developer's attention without affecting user
is to require that developer import some "override deprecation" module
prior to importing setuparg1, otherwise exception raised, something like
this:

from pubsub import allow_deprecated_setuparg1 # if omit this, then import
setuparg1 will raise
from pubsub import setuparg1 # insist use deprecated messaging protocol
from pubsub import pub # will now be setup for deprecated messaging protocol


The ugliness would be a good reminder for developer to migrate his/her
code! Any objections or gotchas to this strategy?
Oliver
--
You received this message because you are subscribed to the Google Groups "wxPython-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxPython-dev+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Chris Barker - NOAA Federal
2014-02-19 23:29:18 UTC
Permalink
Post by Chris Barker
app = wx.GetApp()
do_something_with_app
raise AppNotStartedError
do_something_else
Right, but if pubsib is being used from a non-wx app you wouldn't want to have to import wx just to test if wx is being used.
Exactly -- if wx was not already imported, then you'd get the NameError.

But I see your point--it would have to be imported into this module in
the it-is-a-wx-app case, so not so useful.

Never mind...

Chris
--
Robin Dunn
Software Craftsman
http://wxPython.org
--
You received this message because you are subscribed to the Google Groups "wxPython-dev" group.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "wxPython-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxPython-dev+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Loading...