]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/wx/py/PyShell.py
Factory some of wxFilterClassFactory into a base class.
[wxWidgets.git] / wxPython / wx / py / PyShell.py
... / ...
CommitLineData
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.
7import __main__
8original = __main__.__dict__.keys()
9
10__author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
11__cvsid__ = "$Id$"
12__revision__ = "$Revision$"[11:-2]
13
14import wx
15import os
16
17class App(wx.App):
18 """PyShell standalone application."""
19
20 def OnInit(self):
21 import os
22 import wx
23 from wx import py
24
25 self.SetAppName("pyshell")
26 confDir = wx.StandardPaths.Get().GetUserDataDir()
27 if not os.path.exists(confDir):
28 os.mkdir(confDir)
29 fileName = os.path.join(confDir, 'config')
30 self.config = wx.FileConfig(localFilename=fileName)
31 self.config.SetRecordDefaults(True)
32
33 self.frame = py.shell.ShellFrame(config=self.config, dataDir=confDir)
34 self.frame.Show()
35 self.SetTopWindow(self.frame)
36 return True
37
38'''
39The main() function needs to handle being imported, such as with the
40pyshell script that wxPython installs:
41
42 #!/usr/bin/env python
43
44 from wx.py.PyShell import main
45 main()
46'''
47
48def 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
78if __name__ == '__main__':
79 main()