def InitDDE(self): # Do all the magic DDE handling. # Returns TRUE if we have pumped the arguments to our # remote DDE app, and we should terminate. try: from . import intpydde except ImportError: self.ddeServer = None intpydde = None if intpydde is not None: self.ddeServer = intpydde.DDEServer(self) self.ddeServer.Create("Pythonwin", intpydde.CBF_FAIL_SELFCONNECTIONS) try: # If there is an existing instance, pump the arguments to it. connection = self.MakeExistingDDEConnection() if connection is not None: if self.ProcessArgs(sys.argv, connection) is None: return 1 except: # It is too early to 'print' an exception - we # don't have stdout setup yet! win32ui.DisplayTraceback( sys.exc_info(), " - error in DDE conversation with Pythonwin")
def ProcessArgs(self, args, dde=None): # If we are going to talk to a remote app via DDE, then # activate it! if dde is not None: dde.Exec("self.Activate()") if len(args) and args[0] in ['/nodde', '/new']: del args[0] # already handled. if len(args) < 1 or not args[0]: # argv[0]=='' when started without args, just like Python.exe! return try: if args[0] and args[0][0] != '/': argStart = 0 argType = win32ui.GetProfileVal("Python", "Default Arg Type", "/edit").lower() else: argStart = 1 argType = args[0] if argStart >= len(args): raise TypeError("The command line requires an additional arg.") if argType == "/edit": # Load up the default application. if dde: fname = win32api.GetFullPathName(args[argStart]) dde.Exec("win32ui.GetApp().OpenDocumentFile(%s)" % (repr(fname))) else: win32ui.GetApp().OpenDocumentFile(args[argStart]) elif argType == "/rundlg": if dde: dde.Exec("from pywin.framework import scriptutils;scriptutils.RunScript('%s', '%s', 1)" % ( args[argStart], ' '.join(args[argStart + 1:]))) else: from . import scriptutils scriptutils.RunScript(args[argStart], ' '.join(args[argStart + 1:])) elif argType == "/run": if dde: dde.Exec("from pywin.framework import scriptutils;scriptutils.RunScript('%s', '%s', 0)" % ( args[argStart], ' '.join(args[argStart + 1:]))) else: from . import scriptutils scriptutils.RunScript(args[argStart], ' '.join(args[argStart + 1:]), 0) elif argType == "/app": raise RuntimeError("/app only supported for new instances of Pythonwin.exe") elif argType == '/dde': # Send arbitary command if dde is not None: dde.Exec(args[argStart]) else: win32ui.MessageBox("The /dde command can only be used\r\nwhen Pythonwin is already running") else: raise TypeError("Command line arguments not recognised") except: # too early for print anything. win32ui.DisplayTraceback(sys.exc_info(), " - error processing command line args")
def InitInstance(self): # Allow "/nodde" and "/new" to optimize this! if ("/nodde" not in sys.argv and "/new" not in sys.argv and "-nodde" not in sys.argv and "-new" not in sys.argv): if self.InitDDE(): return 1 # A remote DDE client is doing it for us! else: self.ddeServer = None win32ui.SetRegistryKey("Python %s" % ( sys.winver, )) # MFC automatically puts the main frame caption on! app.CApp.InitInstance(self) # Create the taskbar icon win32ui.CreateDebuggerThread() # Allow Pythonwin to host OCX controls. win32ui.EnableControlContainer() # Display the interactive window if the user wants it. from . import interact interact.CreateInteractiveWindowUserPreference() # Load the modules we use internally. self.LoadSystemModules() # Load additional module the user may want. self.LoadUserModules() # Load the ToolBar state near the end of the init process, as # there may be Toolbar IDs created by the user or other modules. # By now all these modules should be loaded, so all the toolbar IDs loaded. try: self.frame.LoadBarState("ToolbarDefault") except win32ui.error: # MFC sucks. It does essentially "GetDlgItem(x)->Something", so if the # toolbar with ID x does not exist, MFC crashes! Pythonwin has a trap for this # but I need to investigate more how to prevent it (AFAIK, ensuring all the # toolbars are created by now _should_ stop it!) pass # Finally process the command line arguments. try: self.ProcessArgs(sys.argv) except: # too early for printing anything. win32ui.DisplayTraceback(sys.exc_info(), " - error processing command line args")