private void RunScript(string scriptName) { var engine = Python.CreateEngine(); using (var stream = new ScriptOutputStream( s => { this.AppendToScriptOutput(s); App.Current.Dispatcher.BeginInvoke(new Action(() => this.OnPropertyChanged("ScriptOutput"))); }, Encoding.UTF8)) { engine.Runtime.IO.SetOutput(stream, Encoding.UTF8); var scriptSource = engine.CreateScriptSourceFromFile(scriptName); try { scriptSource.Execute(); } catch (SyntaxErrorException e) { this.AppendToScriptOutput("Syntax error (line {0}, column {1}): {2}", e.Line, e.Column, e.Message); App.Current.Dispatcher.BeginInvoke(new Action(() => this.OnPropertyChanged("ScriptOutput"))); } } }
private void CmdRunScript_Click(object sender, EventArgs e) { string scriptBody; if (radInputFromForm.Checked) { scriptBody = txtInputScript.Text; } else if (radInputFromFile.Checked) { var inputFilePath = txtInputScriptPath.Text; if (!File.Exists(inputFilePath)) { MessageBox.Show( "The file '" + inputFilePath + "' does not exist.", Application.ProductName); txtInputScriptPath.Focus(); txtInputScriptPath.SelectAll(); return; } try { using (var reader = new StreamReader(inputFilePath)) { scriptBody = reader.ReadToEnd(); } } catch (IOException ex) { MessageBox.Show( "An exception was caught while opening '" + inputFilePath + "':\r\n\r\n" + ex.Message + "\r\n\r\n" + ex.StackTrace, Application.ProductName); return; } } else { throw new NotImplementedException("The option for executing scripts has not been implemented."); } // run our script and print the output txtOutput.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":\r\n"; using (var outStream = new ScriptOutputStream(txtOutput)) { ExecuteScript(scriptBody, outStream); } txtOutput.Text += "\r\n"; // scroll to the end of the output txtOutput.SelectionLength = 0; txtOutput.SelectionStart = txtOutput.Text.Length - 1; txtOutput.ScrollToCaret(); }