Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit 1ab72f0

Browse files
committed
attempting to tackle issue #52
1 parent d19bb76 commit 1ab72f0

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

Component/ScriptingAncestorComponent.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Drawing;
44
using System.Windows.Forms;
5+
using System.Linq;
56
using GhPython.DocReplacement;
67
using GhPython.Properties;
78
using Grasshopper.Kernel;
@@ -347,30 +348,38 @@ protected override void SafeSolveInstance(IGH_DataAccess DA)
347348
SetFormErrorOrClearIt(DA, m_py_output);
348349
}
349350

350-
private bool AddLocalPath(out string location)
351+
private bool AddLocalPath(out string path)
351352
{
352-
location = m_document.Path;
353-
if (string.IsNullOrWhiteSpace(location)) return false;
353+
string probe_location = m_document.Path;
354+
if (string.IsNullOrWhiteSpace(probe_location)) { path = null; return false; }
354355

355-
location = Path.GetDirectoryName(location);
356-
if (!Directory.Exists(location)) return false;
356+
probe_location = Path.GetDirectoryName(probe_location);
357+
if (!Directory.Exists(probe_location)) { path = null; return false; }
357358

358-
var added = m_py.EvaluateExpression(
359-
@"import sys",
360-
string.Format("(sys.path.insert(0,r\"{0}\") or True) if r\"{0}\" not in sys.path else False",
361-
location)
362-
);
363-
m_py.RemoveVariable("sys");
359+
var sys_path = GetSysPath();
360+
var there_was = sys_path.OfType<string>().Any(s =>
361+
string.Equals(s, probe_location, StringComparison.InvariantCultureIgnoreCase));
364362

365-
if (!(added is bool)) return false;
366-
return (bool)added;
363+
if (!there_was) sys_path.Insert(0, probe_location);
364+
path = probe_location;
365+
return !there_was;
367366
}
368367

369368
private void RemoveLocalPath(string location)
370369
{
371-
var added = m_py.EvaluateExpression(@"import sys
372-
if r""" + location + @""" in sys.path: sys.path.remove(""" + location + @""")
373-
del sys", "True");
370+
var sys_path = GetSysPath();
371+
if (sys_path.Count > 0 &&
372+
string.Equals(sys_path[0] as string, location, StringComparison.InvariantCultureIgnoreCase))
373+
{
374+
sys_path.RemoveAt(0);
375+
}
376+
}
377+
378+
private System.Collections.IList GetSysPath()
379+
{
380+
var sys_path = (System.Collections.IList)m_py.EvaluateExpression("import sys", "sys.path");
381+
m_py.RemoveVariable("sys");
382+
return sys_path;
374383
}
375384

376385
private void AddErrorNicely(StringList sw, Exception ex)

0 commit comments

Comments
 (0)