예제 #1
0
        //-///////////////////////////////////////////////////////////////////////
        //-///////////////////////////////////////////////////////////////////////

        private void RunScript(string fileName, bool loadLayoutForScript, bool mayKeepValues)
        {
            List <SavedAttrValue> savedAttrValues = null;

            if (mayKeepValues)
            {
                if (_scriptFileName != null)
                {
                    if (Misc.AreFileNamesEqual(fileName, _scriptFileName))
                    {
                        savedAttrValues = _pydoodleModule.GetSavedAttrValues();
                    }
                }
            }

            SaveStateForScript();

            StopScript();

            //
            if (loadLayoutForScript)
            {
                LoadLayoutForScript(fileName);
            }

            LoadStateForScript(fileName);

            // Initialise basic script stuff.
            Dictionary <string, object> options = new Dictionary <string, object>();

            options["Debug"] = true;

            _scriptEngine = Python.CreateEngine(options);

            Stream       tmp       = new TextPanel.WriteStream(_textPanel);
            StreamWriter tmpWriter = new StreamWriter(tmp);

            _scriptEngine.Runtime.IO.SetOutput(tmp, tmpWriter);
            _scriptEngine.Runtime.IO.SetErrorOutput(tmp, tmpWriter);

            _objectOperations = _scriptEngine.CreateOperations();

            // Add pydoodle module.
            _pydoodleModule = new pydoodleModule(_scriptEngine, this, savedAttrValues);

            // Reset panels.
            _tweaksPanel.Reset();
            _textPanel.ClearText();

            // Fiddle with python module search paths. Replace "." with the path to the loaded script.
            string filePath = Misc.GetPathDirectoryName(fileName);

            if (filePath != null)
            {
                List <string> paths = new List <string>(_scriptEngine.GetSearchPaths());

                paths.Remove(".");
                paths.Insert(0, filePath);

                _scriptEngine.SetSearchPaths(paths);
            }

            //
            _main.OnRecentFileUsed(fileName);

            // go!
            _scriptFileName = fileName;
            _scriptScope    = _scriptEngine.CreateScope();
            _scriptSource   = _scriptEngine.CreateScriptSourceFromFile(_scriptFileName);
            _scriptState    = ScriptState.RunGlobal;

            TickPython(null);

            List <string> moduleFileNames = Misc.GetModuleFileNames(_scriptEngine);

            // If it's broken already, the module file names list might not
            // include the module that threw the exception. So, try to find that
            // from the exception. Also, always add the script file name.
            if (_scriptState == ScriptState.Borked)
            {
                foreach (DynamicStackFrame stackFrame in Misc.GetScriptExceptionDynamicStackFrames(_scriptException))
                {
                    moduleFileNames.Add(stackFrame.GetFileName());
                }

                moduleFileNames.Add(_scriptFileName);
            }

            // Watch for further changes.
            _fileChangeWatcher          = new FileChangeWatcher(moduleFileNames);
            _fileChangeWatcher.Changed += this.HandleFileChangeWatcherChanged;
        }