]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/PyCrust.py
The truth is out there.
[wxWidgets.git] / wxPython / wx / py / PyCrust.py
1 """PyCrust is a python shell and namespace browser application."""
2
3 # The next two lines, and the other code below that makes use of
4 # ``__main__`` and ``original``, serve the purpose of cleaning up the
5 # main namespace to look as much as possible like the regular Python
6 # shell environment.
7 import __main__
8 original = __main__.__dict__.keys()
9
10 __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
11 __cvsid__ = "$Id$"
12 __revision__ = "$Revision$"[11:-2]
13
14 import wx
15
16
17 class App(wx.App):
18 """PyCrust standalone application."""
19
20 def OnInit(self):
21 import wx
22 wx.InitAllImageHandlers()
23 locals = __main__.__dict__
24 from crust import CrustFrame
25 self.frame = CrustFrame(locals=locals)
26 self.frame.SetSize((800, 600))
27 self.frame.Show()
28 self.SetTopWindow(self.frame)
29 return True
30
31 '''
32 The main() function needs to handle being imported, such as with the
33 pycrust script that wxPython installs:
34
35 #!/usr/bin/env python
36
37 from wx.py.PyCrust import main
38 main()
39 '''
40
41 def main():
42 """The main function for the PyCrust program."""
43 # Cleanup the main namespace, leaving the App class.
44 import __main__
45 md = __main__.__dict__
46 keepers = original
47 keepers.append('App')
48 for key in md.keys():
49 if key not in keepers:
50 del md[key]
51 # Create an application instance.
52 app = App(0)
53 # Mimic the contents of the standard Python shell's sys.path.
54 import sys
55 if sys.path[0]:
56 sys.path[0] = ''
57 # Add the application object to the sys module's namespace.
58 # This allows a shell user to do:
59 # >>> import sys
60 # >>> sys.app.whatever
61 sys.app = app
62 del sys
63 # Cleanup the main namespace some more.
64 if md.has_key('App') and md['App'] is App:
65 del md['App']
66 if md.has_key('__main__') and md['__main__'] is __main__:
67 del md['__main__']
68 # Start the wxPython event loop.
69 app.MainLoop()
70
71 if __name__ == '__main__':
72 main()