|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
{ |
|
|
|
|
|
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(); |
|
|
} |
|
|
} |
|
|
} |
|
|
|