]>
Commit | Line | Data |
---|---|---|
1 | """PyShell is a python shell 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 | try: | |
17 | True | |
18 | except NameError: | |
19 | True = 1==1 | |
20 | False = 1==0 | |
21 | ||
22 | ||
23 | class App(wx.App): | |
24 | """PyShell standalone application.""" | |
25 | ||
26 | def OnInit(self): | |
27 | import wx | |
28 | wx.InitAllImageHandlers() | |
29 | locals = __main__.__dict__ | |
30 | from shell import ShellFrame | |
31 | self.frame = ShellFrame(locals=locals) | |
32 | self.frame.SetSize((750, 525)) | |
33 | self.frame.Show() | |
34 | self.SetTopWindow(self.frame) | |
35 | self.frame.shell.SetFocus() | |
36 | return True | |
37 | ||
38 | ''' | |
39 | The main() function needs to handle being imported, such as with the | |
40 | pyshell script that wxPython installs: | |
41 | ||
42 | #!/usr/bin/env python | |
43 | ||
44 | from wx.py.PyShell import main | |
45 | main() | |
46 | ''' | |
47 | ||
48 | def main(): | |
49 | """The main function for the PyShell program.""" | |
50 | # Cleanup the main namespace, leaving the App class. | |
51 | import __main__ | |
52 | md = __main__.__dict__ | |
53 | keepers = original | |
54 | keepers.append('App') | |
55 | for key in md.keys(): | |
56 | if key not in keepers: | |
57 | del md[key] | |
58 | # Create an application instance. | |
59 | app = App(0) | |
60 | # Cleanup the main namespace some more. | |
61 | if md.has_key('App') and md['App'] is App: | |
62 | del md['App'] | |
63 | if md.has_key('__main__') and md['__main__'] is __main__: | |
64 | del md['__main__'] | |
65 | # Mimic the contents of the standard Python shell's sys.path. | |
66 | import sys | |
67 | if sys.path[0]: | |
68 | sys.path[0] = '' | |
69 | # Add the application object to the sys module's namespace. | |
70 | # This allows a shell user to do: | |
71 | # >>> import sys | |
72 | # >>> sys.app.whatever | |
73 | sys.app = app | |
74 | del sys | |
75 | # Start the wxPython event loop. | |
76 | app.MainLoop() | |
77 | ||
78 | if __name__ == '__main__': | |
79 | main() |