From b1469773d1951dc706076cce61a82b3236225685 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Mon, 14 Nov 2022 10:55:33 +0000 Subject: [PATCH] Add extension folders to IronPython search path --- .../Bonsai.Scripting.IronPython.Design.csproj | 2 +- .../Bonsai.Scripting.IronPython.csproj | 2 +- Bonsai.Scripting.IronPython/PythonEngine.cs | 25 +++++++++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Bonsai.Scripting.IronPython.Design/Bonsai.Scripting.IronPython.Design.csproj b/Bonsai.Scripting.IronPython.Design/Bonsai.Scripting.IronPython.Design.csproj index 1c60d5666..a957273f4 100644 --- a/Bonsai.Scripting.IronPython.Design/Bonsai.Scripting.IronPython.Design.csproj +++ b/Bonsai.Scripting.IronPython.Design/Bonsai.Scripting.IronPython.Design.csproj @@ -6,7 +6,7 @@ Bonsai Rx Scripting Iron Python Design true net462 - 2.7.0 + 2.7.1 diff --git a/Bonsai.Scripting.IronPython/Bonsai.Scripting.IronPython.csproj b/Bonsai.Scripting.IronPython/Bonsai.Scripting.IronPython.csproj index 0575d7a14..ba49cf568 100644 --- a/Bonsai.Scripting.IronPython/Bonsai.Scripting.IronPython.csproj +++ b/Bonsai.Scripting.IronPython/Bonsai.Scripting.IronPython.csproj @@ -5,7 +5,7 @@ Bonsai Scripting Library containing IronPython scripting infrastructure for Bonsai. Bonsai Rx Scripting Iron Python net462 - 2.7.0 + 2.7.1 diff --git a/Bonsai.Scripting.IronPython/PythonEngine.cs b/Bonsai.Scripting.IronPython/PythonEngine.cs index 245a3a60d..917a80609 100644 --- a/Bonsai.Scripting.IronPython/PythonEngine.cs +++ b/Bonsai.Scripting.IronPython/PythonEngine.cs @@ -1,4 +1,6 @@ using Microsoft.Scripting.Hosting; +using System; +using System.Collections.Generic; using System.IO; using System.Reflection; @@ -6,17 +8,30 @@ namespace Bonsai.Scripting.IronPython { static class PythonEngine { + const string ExtensionsPath = "Extensions"; + const string RepositoryPath = "Packages"; + internal static ScriptEngine Create() { - var engine = global::IronPython.Hosting.Python.CreateEngine(); - var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - var libPath = Directory.GetDirectories(Path.Combine(basePath, "../../../"), "IronPython.StdLib.*"); + var searchPaths = new List(); + var editorPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); + var editorExtensionsPath = Path.Combine(editorPath, ExtensionsPath); + var editorRepositoryPath = Path.Combine(editorPath, RepositoryPath); + var customExtensionsPath = Path.Combine(Environment.CurrentDirectory, ExtensionsPath); + searchPaths.Add(customExtensionsPath); + searchPaths.Add(editorExtensionsPath); + + var libPath = Directory.GetDirectories(editorRepositoryPath, "IronPython.StdLib.*"); if (libPath.Length == 1) { - var lib = Path.Combine(libPath[0], $"content/Lib"); + var lib = Path.Combine(libPath[0], "content", "Lib"); var sitePackages = Path.Combine(lib, "site-packages"); - engine.SetSearchPaths(new[] { lib, sitePackages }); + searchPaths.Add(lib); + searchPaths.Add(sitePackages); } + + var engine = global::IronPython.Hosting.Python.CreateEngine(); + engine.SetSearchPaths(searchPaths); return engine; } }