public ScriptEngine GetEngine() { if (_engine != null) return _engine; _engine = Python.CreateEngine(); _engine.Runtime.IO.SetOutput(_stream, Encoding.UTF8); _engine.Runtime.IO.SetErrorOutput(_stream, Encoding.UTF8); string path = Environment.CurrentDirectory; string ironPythonLibPath = string.Format(@"{0}\IronPythonLib.zip", path); var paths = _engine.GetSearchPaths() as List<string> ?? new List<string>(); paths.Add(path); paths.Add(ironPythonLibPath); path = Environment.GetEnvironmentVariable("IRONPYTHONPATH"); if (!string.IsNullOrEmpty(path)) { var pathStrings = path.Split(';'); paths.AddRange(pathStrings.Where(p => p.Length > 0)); } _engine.SetSearchPaths(paths.ToArray()); return _engine; } private void GetPythonVarsInfo(ScriptScope scope) { _varList.Clear(); var items = scope.GetItems(); foreach (var item in items) { _varList.Add(new VarValue { VarName = item.Key, Value = item.Value }); } valueListView.ItemsSource = _varList; } private void OnExecuteButtonClick(object sender, ItemClickEventArgs e) { string outPutString = string.Empty; outPutString = "*************************************" + "Excute Date: " + DateTime.Now.ToLocalTime().ToString(CultureInfo.InvariantCulture); ExeceutePython(document, outPutString); TabControl.SelectedIndex = 2; } private void ExeceutePython(EditorDocument document, string outPutString) { ScriptEngine engine = GetEngine(); string script = document.Text; ScriptSource source = engine.CreateScriptSourceFromString(script); ScriptScope scope = _engine.CreateScope(); try { source.Compile(); OutputTextBox.AppendText(outPutString + Environment.NewLine); var result = source.Execute(scope); if (result != null) { OutputTextBox.AppendText(engine.Operations.Format(result)); } OutputTextBox.AppendText(Environment.NewLine); GetPythonVarsInfo(scope); } catch (Exception ex) { var eo = engine.GetService<ExceptionOperations>(); var eoString = eo.FormatException(ex); OutputTextBox.AppendText(eoString); return; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)