startup.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 os
  10. import win32api
  11. import win32ui
  12. if not sys.argv:
  13. # Initialize sys.argv from commandline. When sys.argv is empty list (
  14. # different from [''] meaning "no cmd line arguments" ), then C
  15. # bootstrapping or another method of invocation failed to initialize
  16. # sys.argv and it will be done here. ( This was a workaround for a bug in
  17. # win32ui but is retained for other situations. )
  18. argv = win32api.CommandLineToArgv(win32api.GetCommandLine())
  19. sys.argv = argv[1:]
  20. if os.getcwd() not in sys.path and '.' not in sys.path:
  21. sys.path.insert(0, os.getcwd())
  22. # You may wish to redirect error output somewhere useful if you have startup errors.
  23. # eg, 'import win32traceutil' will do this for you.
  24. # import win32traceutil # Just uncomment this line to see error output!
  25. # An old class I used to use - generally only useful if Pythonwin is running under MSVC
  26. #class DebugOutput:
  27. # softspace=1
  28. # def write(self,message):
  29. # win32ui.OutputDebug(message)
  30. #sys.stderr=sys.stdout=DebugOutput()
  31. # To fix a problem with Pythonwin when started from the Pythonwin directory,
  32. # we update the pywin path to ensure it is absolute.
  33. # If it is indeed relative, it will be relative to our current directory.
  34. # If its already absolute, then this will have no affect.
  35. import pywin, pywin.framework
  36. pywin.__path__[0] = win32ui.FullPath(pywin.__path__[0])
  37. pywin.framework.__path__[0] = win32ui.FullPath(pywin.framework.__path__[0])
  38. # make a few wierd sys values. This is so later we can clobber sys.argv to trick
  39. # scripts when running under a GUI environment.
  40. moduleName = "pywin.framework.intpyapp"
  41. sys.appargvoffset = 0
  42. sys.appargv = sys.argv[:]
  43. # Must check for /app param here.
  44. if len(sys.argv) >= 2 and sys.argv[0].lower() in ('/app', '-app'):
  45. from . import cmdline
  46. moduleName = cmdline.FixArgFileName(sys.argv[1])
  47. sys.appargvoffset = 2
  48. newargv=sys.argv[sys.appargvoffset:]
  49. # newargv.insert(0, sys.argv[0])
  50. sys.argv = newargv
  51. # Import the application module.
  52. __import__(moduleName)
  53. try:
  54. win32ui.GetApp()._obj_
  55. # This worked - an app already exists - do nothing more
  56. except (AttributeError, win32ui.error):
  57. # This means either no app object exists at all, or the one
  58. # that does exist does not have a Python class (ie, was created
  59. # by the host .EXE). In this case, we do the "old style" init...
  60. from . import app
  61. if app.AppBuilder is None:
  62. raise TypeError("No application object has been registered")
  63. app.App = app.AppBuilder()