codekingpro/portable-devtools
115k
1# startup.py2#3"The main application startup code for PythonWin."4 5#6# This does the basic command line handling.7 8# Keep this as short as possible, cos error output is only redirected if9# this runs OK. Errors in imported modules are much better - the messages go somewhere (not any more :-)10 11import os12import sys13 14import win32api15import win32ui16 17if not sys.argv:18 # Initialize sys.argv from commandline. When sys.argv is empty list (19 # different from [''] meaning "no cmd line arguments" ), then C20 # bootstrapping or another method of invocation failed to initialize21 # sys.argv and it will be done here. ( This was a workaround for a bug in22 # win32ui but is retained for other situations. )23 argv = win32api.CommandLineToArgv(win32api.GetCommandLine())24 sys.argv = argv[1:]25 if os.getcwd() not in sys.path and "." not in sys.path:26 sys.path.insert(0, os.getcwd())27 28# You may wish to redirect error output somewhere useful if you have startup errors.29# eg, 'import win32traceutil' will do this for you.30# import win32traceutil # Just uncomment this line to see error output!31 32# An old class I used to use - generally only useful if Pythonwin is running under MSVC33# class DebugOutput:34# softspace=135# def write(self,message):36# win32ui.OutputDebug(message)37# sys.stderr=sys.stdout=DebugOutput()38 39# To fix a problem with Pythonwin when started from the Pythonwin directory,40# we update the pywin path to ensure it is absolute.41# If it is indeed relative, it will be relative to our current directory.42# If its already absolute, then this will have no affect.43import pywin44import pywin.framework45 46pywin.__path__[0] = win32ui.FullPath(pywin.__path__[0])47pywin.framework.__path__[0] = win32ui.FullPath(pywin.framework.__path__[0])48 49# make a few wierd sys values. This is so later we can clobber sys.argv to trick50# scripts when running under a GUI environment.51 52moduleName = "pywin.framework.intpyapp"53sys.appargvoffset = 054sys.appargv = sys.argv[:]55# Must check for /app param here.56if len(sys.argv) >= 2 and sys.argv[0].lower() in ("/app", "-app"):57 from . import cmdline58 59 moduleName = cmdline.FixArgFileName(sys.argv[1])60 sys.appargvoffset = 261 newargv = sys.argv[sys.appargvoffset :]62 # newargv.insert(0, sys.argv[0])63 sys.argv = newargv64 65# Import the application module.66__import__(moduleName)67 68try:69 win32ui.GetApp()._obj_70 # This worked - an app already exists - do nothing more71except (AttributeError, win32ui.error):72 # This means either no app object exists at all, or the one73 # that does exist does not have a Python class (ie, was created74 # by the host .EXE). In this case, we do the "old style" init...75 from . import app76 77 if app.AppBuilder is None:78 raise TypeError("No application object has been registered")79 80 app.App = app.AppBuilder()81 