|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Drawing; |
4 | 4 | using System.Windows.Forms; |
| 5 | +using System.Linq; |
5 | 6 | using GhPython.DocReplacement; |
6 | 7 | using GhPython.Properties; |
7 | 8 | using Grasshopper.Kernel; |
@@ -347,30 +348,38 @@ protected override void SafeSolveInstance(IGH_DataAccess DA) |
347 | 348 | SetFormErrorOrClearIt(DA, m_py_output); |
348 | 349 | } |
349 | 350 |
|
350 | | - private bool AddLocalPath(out string location) |
| 351 | + private bool AddLocalPath(out string path) |
351 | 352 | { |
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; } |
354 | 355 |
|
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; } |
357 | 358 |
|
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)); |
364 | 362 |
|
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; |
367 | 366 | } |
368 | 367 |
|
369 | 368 | private void RemoveLocalPath(string location) |
370 | 369 | { |
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; |
374 | 383 | } |
375 | 384 |
|
376 | 385 | private void AddErrorNicely(StringList sw, Exception ex) |
|
0 commit comments