startup.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # startup.py
  2. #
  3. "The main application startup code for PythonWin."
  4. #
  5. # This does the basic command line handling.
  6. # Keep this as short as possible, cos error output is only redirected if
  7. # this runs OK. Errors in imported modules are much better - the messages go somewhere (not any more :-)
  8. import sys
  9. import win32ui
  10. # You may wish to redirect error output somewhere useful if you have startup errors.
  11. # eg, 'import win32traceutil' will do this for you.
  12. # import win32traceutil # Just uncomment this line to see error output!
  13. # An old class I used to use - generally only useful if Pythonwin is running under MSVC
  14. #class DebugOutput:
  15. # softspace=1
  16. # def write(self,message):
  17. # win32ui.OutputDebug(message)
  18. #sys.stderr=sys.stdout=DebugOutput()
  19. # To fix a problem with Pythonwin when started from the Pythonwin directory,
  20. # we update the pywin path to ensure it is absolute.
  21. # If it is indeed relative, it will be relative to our current directory.
  22. # If its already absolute, then this will have no affect.
  23. import pywin, pywin.framework
  24. pywin.__path__[0] = win32ui.FullPath(pywin.__path__[0])
  25. pywin.framework.__path__[0] = win32ui.FullPath(pywin.framework.__path__[0])
  26. # make a few wierd sys values. This is so later we can clobber sys.argv to trick
  27. # scripts when running under a GUI environment.
  28. moduleName = "pywin.framework.intpyapp"
  29. sys.appargvoffset = 0
  30. sys.appargv = sys.argv[:]
  31. # Must check for /app param here.
  32. if len(sys.argv)>=2 and sys.argv[0].lower()=='/app':
  33. import cmdline
  34. moduleName = cmdline.FixArgFileName(sys.argv[1])
  35. sys.appargvoffset = 2
  36. newargv=sys.argv[sys.appargvoffset:]
  37. # newargv.insert(0, sys.argv[0])
  38. sys.argv = newargv
  39. # Import the application module.
  40. __import__(moduleName)
  41. try:
  42. win32ui.GetApp()._obj_
  43. # This worked - an app already exists - do nothing more
  44. except (AttributeError, win32ui.error):
  45. # This means either no app object exists at all, or the one
  46. # that does exist does not have a Python class (ie, was created
  47. # by the host .EXE). In this case, we do the "old style" init...
  48. import app
  49. if app.AppBuilder is None:
  50. raise TypeError("No application object has been registered")
  51. app.App = app.AppBuilder()