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