diff --git a/FreePIE.Console/ConsoleHost.cs b/FreePIE.Console/ConsoleHost.cs index ed55687a..698ceb8b 100644 --- a/FreePIE.Console/ConsoleHost.cs +++ b/FreePIE.Console/ConsoleHost.cs @@ -55,7 +55,8 @@ public void Start(string[] args) persistanceManager.Load(); System.Console.WriteLine("Starting script parser"); - scriptEngine.Start(script); + + scriptEngine.Start(script, args[0]); waitUntilStopped.WaitOne(); } catch (Exception e) diff --git a/FreePIE.Core/Common/FileSystem.cs b/FreePIE.Core/Common/FileSystem.cs index c08ecef5..76bcd2a0 100644 --- a/FreePIE.Core/Common/FileSystem.cs +++ b/FreePIE.Core/Common/FileSystem.cs @@ -40,6 +40,11 @@ public string GetFilename(string path) return Path.GetFileName(path); } + public string GetDirectoryName(string path) + { + return Path.GetDirectoryName(path); + } + public void Delete(string path) { File.Delete(path); diff --git a/FreePIE.Core/Common/IFileSystem.cs b/FreePIE.Core/Common/IFileSystem.cs index 97b92d02..7c610fea 100644 --- a/FreePIE.Core/Common/IFileSystem.cs +++ b/FreePIE.Core/Common/IFileSystem.cs @@ -14,5 +14,6 @@ public interface IFileSystem string GetFilename(string path); void Delete(string path); void CreateDirectory(string path); + string GetDirectoryName(string path); } } \ No newline at end of file diff --git a/FreePIE.Core/ScriptEngine/IScriptEngine.cs b/FreePIE.Core/ScriptEngine/IScriptEngine.cs index bf2e01f2..106e0713 100644 --- a/FreePIE.Core/ScriptEngine/IScriptEngine.cs +++ b/FreePIE.Core/ScriptEngine/IScriptEngine.cs @@ -4,7 +4,7 @@ namespace FreePIE.Core.ScriptEngine { public interface IScriptEngine { - void Start(string script); + void Start(string script, string scriptPath = null); void Stop(); } -} \ No newline at end of file +} diff --git a/FreePIE.Core/ScriptEngine/Python/PythonScriptEngine.cs b/FreePIE.Core/ScriptEngine/Python/PythonScriptEngine.cs index ba493cd2..b92b6db7 100644 --- a/FreePIE.Core/ScriptEngine/Python/PythonScriptEngine.cs +++ b/FreePIE.Core/ScriptEngine/Python/PythonScriptEngine.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.IO; using System.Linq; using System.Reflection; using System.Text; @@ -11,20 +10,15 @@ using FreePIE.Core.Common.Extensions; using FreePIE.Core.Contracts; using FreePIE.Core.Model.Events; -using FreePIE.Core.Persistence; using FreePIE.Core.Persistence.Paths; using FreePIE.Core.ScriptEngine.Globals; using FreePIE.Core.ScriptEngine.ThreadTiming; -using IronPython; -using IronPython.Compiler; using IronPython.Hosting; using IronPython.Modules; using IronPython.Runtime.Operations; using Microsoft.Scripting; using Microsoft.Scripting.Hosting; -using Microsoft.Scripting.Hosting.Providers; -using Microsoft.Scripting.Runtime; -using IronPython.Runtime.Exceptions; +using Microsoft.Scripting.Utils; namespace FreePIE.Core.ScriptEngine.Python { @@ -80,6 +74,7 @@ public class PythonScriptEngine : IScriptEngine private readonly IThreadTimingFactory threadTimingFactory; private readonly IPaths paths; private readonly ILog log; + private readonly IFileSystem fileSystem; private static Microsoft.Scripting.Hosting.ScriptEngine engine; private IEnumerable usedPlugins; private InterlockableBool stopRequested; @@ -105,7 +100,8 @@ public PythonScriptEngine( IEventAggregator eventAggregator, IThreadTimingFactory threadTimingFactory, IPaths paths, - ILog log) + ILog log, + IFileSystem fileSystem) { this.parser = parser; this.globalProviders = globalProviders; @@ -113,9 +109,10 @@ public PythonScriptEngine( this.threadTimingFactory = threadTimingFactory; this.paths = paths; this.log = log; + this.fileSystem = fileSystem; } - public void Start(string script) + public void Start(string script, string scriptPath = null) { thread = new Thread(obj1 => { @@ -148,7 +145,8 @@ public void Start(string script) pluginStarted.Wait(); - Engine.SetSearchPaths(GetPythonPaths()); + var additionalPaths = new [] { scriptPath }.Where(p => !string.IsNullOrEmpty(p)).Select(p => fileSystem.GetDirectoryName(p)); + Engine.SetSearchPaths(GetPythonPaths(additionalPaths)); script = PreProcessScript(script, usedGlobalEnums, globals); }, logToFile: true); @@ -179,9 +177,12 @@ void RunLoop(string script, ScriptScope scope) }); } - ICollection GetPythonPaths() + ICollection GetPythonPaths(IEnumerable additionalPaths) { - return new Collection { paths.GetApplicationPath("pylib") }; + var pythonPaths = new Collection { paths.GetApplicationPath("pylib") }; + pythonPaths.AddRange(additionalPaths); + + return pythonPaths; } private void CatchThreadAbortedException(Action func) diff --git a/FreePIE.GUI/Views/Main/Menu/MainMenuViewModel.cs b/FreePIE.GUI/Views/Main/Menu/MainMenuViewModel.cs index 47b89c8a..d8998f0a 100644 --- a/FreePIE.GUI/Views/Main/Menu/MainMenuViewModel.cs +++ b/FreePIE.GUI/Views/Main/Menu/MainMenuViewModel.cs @@ -173,7 +173,8 @@ public void RunScript() PublishScriptStateChange(); currentScriptEngine = scriptEngineFactory(); - currentScriptEngine.Start(activeDocument.FileContent); + + currentScriptEngine.Start(activeDocument.FileContent, activeDocument.FilePath); } public void StopScript()