_core_ex.py
Go to the documentation of this file.
00001 #----------------------------------------------------------------------------
00002 
00003 # Use Python's bool constants if available, make some if not
00004 try:
00005     True
00006 except NameError:
00007     __builtins__.True = 1==1
00008     __builtins__.False = 1==0
00009     def bool(value): return not not value
00010     __builtins__.bool = bool
00011 
00012 
00013 
00014 # workarounds for bad wxRTTI names
00015 __wxPyPtrTypeMap['wxGauge95']    = 'wxGauge'
00016 __wxPyPtrTypeMap['wxSlider95']   = 'wxSlider'
00017 __wxPyPtrTypeMap['wxStatusBar95']   = 'wxStatusBar'
00018 
00019 
00020 #----------------------------------------------------------------------------
00021 # Load version numbers from __version__...  Ensure that major and minor
00022 # versions are the same for both wxPython and wxWidgets.
00023 
00024 from __version__ import *
00025 __version__ = VERSION_STRING
00026 
00027 assert MAJOR_VERSION == _core_.MAJOR_VERSION, "wxPython/wxWidgets version mismatch"
00028 assert MINOR_VERSION == _core_.MINOR_VERSION, "wxPython/wxWidgets version mismatch"
00029 if RELEASE_VERSION != _core_.RELEASE_VERSION:
00030     import warnings
00031     warnings.warn("wxPython/wxWidgets release number mismatch")
00032 
00033 
00034 def version():
00035     """Returns a string containing version and port info"""
00036     ctype = wx.USE_UNICODE and 'unicode' or 'ansi'
00037     if wx.Platform == '__WXMSW__':
00038         port = 'msw'
00039     elif wx.Platform == '__WXMAC__':
00040         port = 'mac'
00041     elif wx.Platform == '__WXGTK__':
00042         port = 'gtk'
00043         if 'gtk2' in wx.PlatformInfo:
00044             port = 'gtk2'
00045     else:
00046         port = '?'
00047 
00048     return "%s (%s-%s)" % (wx.VERSION_STRING, port, ctype)
00049                        
00050     
00051 #----------------------------------------------------------------------------
00052 
00053 # Set wxPython's default string<-->unicode conversion encoding from
00054 # the locale, but only if Python's default hasn't been changed.  (We
00055 # assume that if the user has customized it already then that is the
00056 # encoding we need to use as well.)
00057 #
00058 # The encoding selected here is used when string or unicode objects
00059 # need to be converted in order to pass them to wxWidgets.  Please be
00060 # aware that the default encoding within the same locale may be
00061 # slightly different on different platforms.  For example, please see
00062 # http://www.alanwood.net/demos/charsetdiffs.html for differences
00063 # between the common latin/roman encodings.
00064 
00065 default = _sys.getdefaultencoding()
00066 if default == 'ascii':
00067     import locale
00068     import codecs
00069     try:
00070         if hasattr(locale, 'getpreferredencoding'):
00071             default = locale.getpreferredencoding()
00072         else:
00073             default = locale.getdefaultlocale()[1]
00074         codecs.lookup(default)
00075     except (ValueError, LookupError, TypeError):
00076         default = _sys.getdefaultencoding()
00077     del locale
00078     del codecs
00079 if default:
00080     wx.SetDefaultPyEncoding(default)
00081 del default
00082 
00083 #----------------------------------------------------------------------------
00084 
00085 class PyDeadObjectError(AttributeError):
00086     pass
00087 
00088 class _wxPyDeadObject(object):
00089     """
00090     Instances of wx objects that are OOR capable will have their __class__
00091     changed to this class when the C++ object is deleted.  This should help
00092     prevent crashes due to referencing a bogus C++ pointer.
00093     """
00094     reprStr = "wxPython wrapper for DELETED %s object! (The C++ object no longer exists.)"
00095     attrStr = "The C++ part of the %s object has been deleted, attribute access no longer allowed."
00096 
00097     def __repr__(self):
00098         if not hasattr(self, "_name"):
00099             self._name = "[unknown]"
00100         return self.reprStr % self._name
00101 
00102     def __getattr__(self, *args):
00103         if not hasattr(self, "_name"):
00104             self._name = "[unknown]"
00105         raise PyDeadObjectError(self.attrStr % self._name)
00106 
00107     def __nonzero__(self):
00108         return 0
00109 
00110 
00111 
00112 class PyUnbornObjectError(AttributeError):
00113     pass
00114 
00115 class _wxPyUnbornObject(object):
00116     """
00117     Some stock objects are created when the wx._core module is
00118     imported, but their C++ instance is not created until the wx.App
00119     object is created and initialized.  These object instances will
00120     temporarily have their __class__ changed to this class so an
00121     exception will be raised if they are used before the C++ instance
00122     is ready.
00123     """
00124 
00125     reprStr = "wxPython wrapper for UNBORN object! (The C++ object is not initialized yet.)"
00126     attrStr = "The C++ part of this object has not been initialized, attribute access not allowed."
00127 
00128     def __repr__(self):
00129         #if not hasattr(self, "_name"):
00130         #    self._name = "[unknown]"
00131         return self.reprStr #% self._name
00132 
00133     def __getattr__(self, *args):
00134         #if not hasattr(self, "_name"):
00135         #    self._name = "[unknown]"
00136         raise PyUnbornObjectError(self.attrStr) # % self._name )
00137 
00138     def __nonzero__(self):
00139         return 0
00140 
00141 
00142 #----------------------------------------------------------------------------
00143 
00144 def CallAfter(callable, *args, **kw):
00145     """
00146     Call the specified function after the current and pending event
00147     handlers have been completed.  This is also good for making GUI
00148     method calls from non-GUI threads.  Any extra positional or
00149     keyword args are passed on to the callable when it is called.
00150 
00151     :see: `wx.CallLater`
00152     """
00153     app = wx.GetApp()
00154     assert app is not None, 'No wx.App created yet'
00155 
00156     if not hasattr(app, "_CallAfterId"):
00157         app._CallAfterId = wx.NewEventType()
00158         app.Connect(-1, -1, app._CallAfterId,
00159                     lambda event: event.callable(*event.args, **event.kw) )
00160     evt = wx.PyEvent()
00161     evt.SetEventType(app._CallAfterId)
00162     evt.callable = callable
00163     evt.args = args
00164     evt.kw = kw
00165     wx.PostEvent(app, evt)
00166 
00167 #----------------------------------------------------------------------------
00168 
00169 
00170 class CallLater:
00171     """
00172     A convenience class for `wx.Timer`, that calls the given callable
00173     object once after the given amount of milliseconds, passing any
00174     positional or keyword args.  The return value of the callable is
00175     availbale after it has been run with the `GetResult` method.
00176 
00177     If you don't need to get the return value or restart the timer
00178     then there is no need to hold a reference to this object.  It will
00179     hold a reference to itself while the timer is running (the timer
00180     has a reference to self.Notify) but the cycle will be broken when
00181     the timer completes, automatically cleaning up the wx.CallLater
00182     object.
00183 
00184     :see: `wx.CallAfter`
00185     """
00186     def __init__(self, millis, callable, *args, **kwargs):
00187         self.millis = millis
00188         self.callable = callable
00189         self.SetArgs(*args, **kwargs)
00190         self.runCount = 0
00191         self.running = False
00192         self.hasRun = False
00193         self.result = None
00194         self.timer = None
00195         self.Start()
00196 
00197     def __del__(self):
00198         self.Stop()
00199 
00200 
00201     def Start(self, millis=None, *args, **kwargs):
00202         """
00203         (Re)start the timer
00204         """
00205         self.hasRun = False
00206         if millis is not None:
00207             self.millis = millis
00208         if args or kwargs:
00209             self.SetArgs(*args, **kwargs)
00210         self.Stop()
00211         self.timer = wx.PyTimer(self.Notify)
00212         self.timer.Start(self.millis, wx.TIMER_ONE_SHOT)
00213         self.running = True
00214     Restart = Start
00215 
00216 
00217     def Stop(self):
00218         """
00219         Stop and destroy the timer.
00220         """
00221         if self.timer is not None:
00222             self.timer.Stop()
00223             self.timer = None
00224 
00225 
00226     def GetInterval(self):
00227         if self.timer is not None:
00228             return self.timer.GetInterval()
00229         else:
00230             return 0
00231 
00232 
00233     def IsRunning(self):
00234         return self.timer is not None and self.timer.IsRunning()
00235 
00236 
00237     def SetArgs(self, *args, **kwargs):
00238         """
00239         (Re)set the args passed to the callable object.  This is
00240         useful in conjunction with Restart if you want to schedule a
00241         new call to the same callable object but with different
00242         parameters.
00243         """
00244         self.args = args
00245         self.kwargs = kwargs
00246 
00247 
00248     def HasRun(self):
00249         return self.hasRun
00250 
00251     def GetResult(self):
00252         return self.result
00253 
00254     def Notify(self):
00255         """
00256         The timer has expired so call the callable.
00257         """
00258         if self.callable and getattr(self.callable, 'im_self', True):
00259             self.runCount += 1
00260             self.running = False
00261             self.result = self.callable(*self.args, **self.kwargs)
00262         self.hasRun = True
00263         if not self.running:
00264             # if it wasn't restarted, then cleanup
00265             wx.CallAfter(self.Stop)
00266 
00267     Interval = property(GetInterval)
00268     Result = property(GetResult)
00269 
00270 
00271 class FutureCall(CallLater):
00272     """A compatibility alias for `CallLater`."""
00273 
00274 #----------------------------------------------------------------------------
00275 # Control which items in this module should be documented by epydoc.
00276 # We allow only classes and functions, which will help reduce the size
00277 # of the docs by filtering out the zillions of constants, EVT objects,
00278 # and etc that don't make much sense by themselves, but are instead
00279 # documented (or will be) as part of the classes/functions/methods
00280 # where they should be used.
00281 
00282 class __DocFilter:
00283     """
00284     A filter for epydoc that only allows non-Ptr classes and
00285     functions, in order to reduce the clutter in the API docs.
00286     """
00287     def __init__(self, globals):
00288         self._globals = globals
00289         
00290     def __call__(self, name):
00291         import types
00292         obj = self._globals.get(name, None)
00293 
00294         # only document classes and function
00295         if type(obj) not in [type, types.ClassType, types.FunctionType, types.BuiltinFunctionType]:
00296             return False
00297 
00298         # skip other things that are private or will be documented as part of somethign else
00299         if name.startswith('_') or name.startswith('EVT') or name.endswith('_swigregister')  or name.endswith('Ptr') :
00300             return False
00301 
00302         # skip functions that are duplicates of static functions in a class
00303         if name.find('_') != -1:
00304             cls = self._globals.get(name.split('_')[0], None)
00305             methname = name.split('_')[1]
00306             if hasattr(cls, methname) and type(getattr(cls, methname)) is types.FunctionType:
00307                 return False
00308             
00309         return True
00310 
00311 #----------------------------------------------------------------------------
00312 #----------------------------------------------------------------------------
00313 
00314 # Import other modules in this package that should show up in the
00315 # "core" wx namespace
00316 from _gdi import *
00317 from _windows import *
00318 from _controls import *
00319 from _misc import *
00320 
00321 #----------------------------------------------------------------------------
00322 #----------------------------------------------------------------------------


wxpython_swig_interface
Author(s):
autogenerated on Mon Oct 6 2014 07:25:55