1#!/usr/bin/env python 2"""PySlicesShell is a python shell application.""" 3 4# The next two lines, and the other code below that makes use of 5# ``__main__`` and ``original``, serve the purpose of cleaning up the 6# main namespace to look as much as possible like the regular Python 7# shell environment. 8import __main__ 9original = list(__main__.__dict__.keys()) 10 11__author__ = "Patrick K. O'Brien <pobrien@orbtech.com>" 12 13import wx 14import os 15 16class App(wx.App): 17 """PySlicesShell standalone application.""" 18 19 def __init__(self, filename=None): 20 self.filename = filename 21 import wx 22 wx.App.__init__(self, redirect=False) 23 24 def OnInit(self): 25 import os 26 import wx 27 from wx import py 28 29 self.SetAppName("pysliceshell") 30 confDir = wx.StandardPaths.Get().GetUserDataDir() 31 if not os.path.exists(confDir): 32 os.mkdir(confDir) 33 fileName = os.path.join(confDir, 'config') 34 self.config = wx.FileConfig(localFilename=fileName) 35 self.config.SetRecordDefaults(True) 36 37 self.frame = py.sliceshell.SlicesShellFrame(config=self.config, 38 dataDir=confDir, 39 filename=self.filename) 40 self.frame.Show() 41 self.SetTopWindow(self.frame) 42 return True 43 44''' 45The main() function needs to handle being imported, such as with the 46pyshell script that wxPython installs: 47 48 #!/usr/bin/env python 49 50 from wx.py.PySlicesShell import main 51 main() 52''' 53 54def main(filename=None): 55 """The main function for the PySlicesShell program.""" 56 # Cleanup the main namespace, leaving the App class. 57 import sys 58 if not filename and len(sys.argv) > 1: 59 filename = sys.argv[1] 60 if filename: 61 filename = os.path.realpath(filename) 62 63 import __main__ 64 md = __main__.__dict__ 65 keepers = original 66 keepers.append('App') 67 keepers.append('filename') 68 for key in list(md.keys()): 69 if key not in keepers: 70 del md[key] 71 # Create an application instance. 72 app = App(filename=filename) 73 # Cleanup the main namespace some more. 74 if 'App' in md and md['App'] is App: 75 del md['App'] 76 if 'filename' in md and md['filename'] is filename: 77 del md['filename'] 78 if '__main__' in md and md['__main__'] is __main__: 79 del md['__main__'] 80 # Mimic the contents of the standard Python shell's sys.path. 81 import sys 82 if sys.path[0]: 83 sys.path[0] = '' 84 # Add the application object to the sys module's namespace. 85 # This allows a shell user to do: 86 # >>> import sys 87 # >>> sys.app.whatever 88 sys.app = app 89 del sys 90 # Start the wxPython event loop. 91 app.MainLoop() 92 93if __name__ == '__main__': 94 main() 95