// Copyright (c) 2010 Joe Moorhouse using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Threading; using IronPython.Hosting; using IronPython.Runtime; using Microsoft.Scripting.Hosting; using Microsoft.Scripting.Hosting.Providers; using Microsoft.Scripting.Hosting.Shell; namespace PythonConsoleControl { public delegate void ConsoleCreatedEventHandler(object sender, EventArgs e); /// /// Hosts the python console. /// public class PythonConsoleHost : ConsoleHost, IDisposable { Thread thread; PythonTextEditor textEditor; PythonConsole pythonConsole; public event ConsoleCreatedEventHandler ConsoleCreated; public PythonConsoleHost(PythonTextEditor textEditor) { this.textEditor = textEditor; } public PythonConsole Console { get { return pythonConsole; } } protected override Type Provider { get { return typeof(PythonContext); } } /// /// Runs the console host in its own thread. /// public void Run() { thread = new Thread(RunConsole); thread.IsBackground = true; thread.Start(); } public void Dispose() { if (pythonConsole != null) { pythonConsole.Dispose(); } if (thread != null) { thread.Join(); } } protected override CommandLine CreateCommandLine() { return new PythonCommandLine(); } protected override OptionsParser CreateOptionsParser() { return new PythonOptionsParser(); } /// /// After the engine is created the standard output is replaced with our custom Stream class so we /// can redirect the stdout to the text editor window. /// This can be done in this method since the Runtime object will have been created before this method /// is called. /// protected override IConsole CreateConsole(ScriptEngine engine, CommandLine commandLine, ConsoleOptions options) { var pythonOutputStream = new PythonOutputStream(textEditor); SetOutput(pythonOutputStream); pythonConsole = new PythonConsole(textEditor, commandLine); pythonOutputStream.TextWritten += pythonConsole.OutputWritten; if (ConsoleCreated != null) ConsoleCreated(this, EventArgs.Empty); return pythonConsole; } protected virtual void SetOutput(PythonOutputStream stream) { Runtime.IO.SetOutput(stream, Encoding.UTF8); } /// /// Runs the console. /// void RunConsole() { this.Run(new string[] { "-X:FullFrames" }); } protected override ScriptRuntimeSetup CreateRuntimeSetup() { ScriptRuntimeSetup srs = ScriptRuntimeSetup.ReadConfiguration(); foreach (var langSetup in srs.LanguageSetups) { if (langSetup.FileExtensions.Contains(".py")) { langSetup.Options["SearchPaths"] = new string[0]; } } return srs; } protected override void ParseHostOptions(string[] args) { // Python doesn't want any of the DLR base options. foreach (string s in args) { Options.IgnoredArgs.Add(s); } } protected override void ExecuteInternal() { if (PythonConfig.SearchPaths?.Any() ?? false) { var paths = Engine.GetSearchPaths().ToList(); paths.AddRange(PythonConfig.SearchPaths); Engine.SetSearchPaths(paths); } var pc = HostingHelpers.GetLanguageContext(Engine) as PythonContext; pc.SetModuleState(typeof(ScriptEngine), Engine); base.ExecuteInternal(); } } }