public Engine()
        {
            var options = new Dictionary<string, object>();
            options["DivisionOptions"] = PythonDivisionOptions.New;
            _history = new NullStream();
            _output = new EventRaisingStreamWriter(_history);
            _output.StringWritten += _output_StringWritten;
            _engine = Python.CreateEngine(options);
            _engine.Runtime.IO.SetOutput(_history, _output);
            _engine.Runtime.IO.SetErrorOutput(_history, _output);
            _scope = _engine.CreateScope();
            foreach (var t in _pluggable)
            {
                _scope.SetVariable(t.Name, DynamicHelpers.GetPythonTypeFromType(t));
            }
            _scope.SetVariable("CalculatorFunctions", DynamicHelpers.GetPythonTypeFromType(typeof(CalculatorFunctions)));
            _functioncache = new Dictionary<string, string>();
            //hidden functions
            _functioncache.Add("Var", "CalculatorFunctions.Var");
            _functioncache.Add("FncList", "CalculatorFunctions.FncList");
            _functioncache.Add("RegFunction", "CalculatorFunctions.RegFunction");

            foreach (var f in _functions)
            {
                if (_functioncache.ContainsKey(f.Name)) continue;
                _functioncache.Add(f.Name, f.FullName);
            }
            LoadBitops();
        }
Exemple #2
0
        /// <summary>
        /// Executes the supplied Python script
        /// </summary>
        /// <param name="script">
        /// The string of the Python script that is to be executed
        /// </param>
        public override void Execute(string script)
        {
            try
            {
                // Check if the script should be stopped
                this.CancellationToken.ThrowIfCancellationRequested();

                this.ScriptingProxy.ScriptingPanelViewModel = this;

                var sourceCode = this.Engine.CreateScriptSourceFromString(script, SourceCodeKind.AutoDetect);

                using (var memoryStream = new MemoryStream())
                {
                    var streamWriter = new EventRaisingStreamWriter(memoryStream);

                    streamWriter.StringWritten += OnMemoryStreamChanged;
                    this.Engine.Runtime.IO.SetOutput(memoryStream, streamWriter);
                    sourceCode.Execute(this.Scope);
                }
            }
            catch (ThreadAbortException tae)
            {
                // If a ThreadAbortException occured but the user didn't choose to stop the script
                if (!(tae.ExceptionState is KeyboardInterruptException))
                {
                    Application.Current.Dispatcher.Invoke(
                        DispatcherPriority.Input,
                        new Action(() => this.OutputTerminal.AppendText(string.Format("\nAn error occured during the execution of the script !\nError: {0}\n", tae.Message))));
                }

                // The abortion of the thread is cancelled to avoid the loss of data, the cancelletation token is checked and will throw an exception to cancel the task properly if necessary
                Thread.ResetAbort();
                this.Engine.Runtime.Shutdown();
                this.CancellationToken.ThrowIfCancellationRequested();
            }
            // If CancellationRequested
            catch (OperationCanceledException)
            {
                throw new OperationCanceledException(this.CancellationToken);
            }
            // Other kinds of exceptions
            catch (Exception ex)
            {
                Application.Current.Dispatcher.Invoke(
                    DispatcherPriority.Input,
                    new Action(() => this.OutputTerminal.AppendText(string.Format("\nAn error occured during the execution of the script !\nError: {0}\n", ex.Message))));
            }
            finally
            {
                this.UpdateScopeVariables();
            }
        }
Exemple #3
0
 protected virtual void Dispose(bool native)
 {
     if (_outp != null)
     {
         _outp.Dispose();
         _outp = null;
     }
     if (_history != null)
     {
         _history.Dispose();
         _history = null;
     }
 }
        private void TSB_RunScript_Click(object sender, EventArgs e)
        {
            TB_Output.Text = "";

            ScriptEngine scriptEngine = Python.CreateEngine();
            ScriptScope  pyScope      = scriptEngine.CreateScope();

            MemoryStream             ms       = new MemoryStream();
            EventRaisingStreamWriter outputWr = new EventRaisingStreamWriter(ms);

            outputWr.StringWritten += new EventHandler <MyEvtArgs <string> >(sWr_StringWritten);
            scriptEngine.Runtime.IO.SetOutput(ms, outputWr);

            //Set path
            string        path    = Assembly.GetExecutingAssembly().Location;
            string        rootDir = Directory.GetParent(path).FullName;
            List <string> paths   = new List <string>();

            paths.Add(rootDir);
            paths.Add(Path.Combine(rootDir, "Lib"));
            scriptEngine.SetSearchPaths(paths.ToArray());

            pyScope.SetVariable("mipy", frmMain.CurrentWin);

            TextEditorControl editor = ActiveEditor;

            if (editor != null)
            {
                string code = editor.Text;
                if (code.Trim() == "")
                {
                    this.Cursor = Cursors.Default;
                    return;
                }

                this.Cursor = Cursors.WaitCursor;
                ScriptSource source = scriptEngine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
                try
                {
                    source.Execute(pyScope);
                }
                catch (Exception exception)
                {
                    //MessageBox.Show(scriptEngine.GetService<ExceptionOperations>().FormatException(exception), "Error",MessageBoxButtons.OK);
                    TB_Output.Text += scriptEngine.GetService <ExceptionOperations>().FormatException(exception);
                }
            }

            this.Cursor = Cursors.Default;
        }
Exemple #5
0
        public PythonCore(IConsole term)
        {
            Dictionary <String, Object> options = new Dictionary <string, object>();

            options["DivisionOptions"] = PythonDivisionOptions.New;
            _history             = new NullStream();
            _outp                = new EventRaisingStreamWriter(_history);
            _terminal            = term;
            _outp.StringWritten += new EventHandler <MyEvtArgs <string> >(_outp_StringWritten);
            _engine              = Python.CreateEngine(options);
            _engine.Runtime.IO.SetOutput(_history, _outp);
            _engine.Runtime.IO.SetErrorOutput(_history, _outp);
            _scope         = _engine.CreateScope();
            _snytax        = new PythonSystax();
            _snytax.Engine = _engine;
            TrigMode       = TrigMode.Deg;
        }
Exemple #6
0
        private void Execute()
        {
            Save();

            LogBox.Clear();
            LogBox.AppendText(DateTime.Now.ToString() + "\n");
            MemoryStream             ms       = new MemoryStream();
            EventRaisingStreamWriter outputWr = new EventRaisingStreamWriter(ms);

            outputWr.StringWritten += sWr_StringWritten;

            var o = MainForm.QHScriptEngine.Runtime.IO.OutputStream;

            MainForm.QHScriptEngine.Runtime.IO.SetOutput(ms, outputWr);
            var scope = HackContext.CreateScriptScope(MainForm.QHScriptEngine);

            MainForm.QHScriptEngine.Execute(CodeView.Text, scope);
            MainForm.QHScriptEngine.Runtime.IO.SetOutput(o, Encoding.UTF8);

            void sWr_StringWritten(object sd, OnWrittenEventArgs <string> ev)
            {
                LogBox.AppendText(ev.Value);
            }
        }
        private void ConfigureScriptEngine()
        {
            MemoryStream ms = new MemoryStream();

            EventRaisingStreamWriter outputWriter = new EventRaisingStreamWriter(ms);
            outputWriter.StringWritten += new EventHandler<MyEvtArgs<string>>(sWr_StringWritten);

            scriptEngine.Runtime.IO.SetOutput(ms, outputWriter);
        }
 protected virtual void Dispose(bool native)
 {
     if (_output != null)
     {
         _output.Dispose();
         _output = null;
     }
     if (_history != null)
     {
         _history.Dispose();
         _history = null;
     }
 }