Skip to content

Commit c774db3

Browse files
Apply suggestions from code review
Co-authored-by: glopesdev <[email protected]>
1 parent 254e8f5 commit c774db3

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/Bonsai.Scripting.Python/RuntimeManager.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,38 @@ internal RuntimeManager(string pythonHome, string scriptPath, IObserver<RuntimeM
4343
disposable => disposable.Subject)
4444
.Take(1);
4545

46-
internal static bool IsEmbeddedResourcePath(string path)
46+
static bool IsEmbeddedResourcePath(string path)
4747
{
4848
var separatorIndex = path.IndexOf(AssemblySeparator);
4949
return separatorIndex >= 0 && !SystemPath.IsPathRooted(path);
5050
}
5151

52-
internal static string GetEmbeddedPythonCode(string path)
52+
static string ReadAllText(string path)
5353
{
54-
var nameElements = path.Split(new[] { AssemblySeparator }, 2);
55-
if (string.IsNullOrEmpty(nameElements[0]))
54+
if (IsEmbeddedResourcePath(path))
5655
{
57-
throw new InvalidOperationException(
58-
"The embedded resource path \"" + path +
59-
"\" must be qualified with a valid assembly name.");
60-
}
56+
var nameElements = path.Split(new[] { AssemblySeparator }, 2);
57+
if (string.IsNullOrEmpty(nameElements[0]))
58+
{
59+
throw new InvalidOperationException(
60+
"The embedded resource path \"" + path +
61+
"\" must be qualified with a valid assembly name.");
62+
}
6163

62-
var assembly = Assembly.Load(nameElements[0]);
63-
var resourceName = string.Join(ExpressionHelper.MemberSeparator, nameElements);
64-
using var resourceStream = assembly.GetManifestResourceStream(resourceName);
65-
if (resourceStream == null)
66-
{
67-
throw new InvalidOperationException(
68-
"The specified embedded resource \"" + nameElements[1] +
69-
"\" was not found in assembly \"" + nameElements[0] + "\"");
64+
var assembly = Assembly.Load(nameElements[0]);
65+
var resourceName = string.Join(ExpressionHelper.MemberSeparator, nameElements);
66+
using var resourceStream = assembly.GetManifestResourceStream(resourceName);
67+
if (resourceStream == null)
68+
{
69+
throw new InvalidOperationException(
70+
"The specified embedded resource \"" + nameElements[1] +
71+
"\" was not found in assembly \"" + nameElements[0] + "\"");
72+
}
73+
74+
using var reader = new StreamReader(resourceStream);
75+
return reader.ReadToEnd();
7076
}
71-
using var reader = new StreamReader(resourceStream);
72-
var code = reader.ReadToEnd();
73-
return code;
77+
else return File.ReadAllText(path);
7478
}
7579

7680
internal static DynamicModule CreateModule(string name = "", string scriptPath = "")
@@ -82,7 +86,7 @@ internal static DynamicModule CreateModule(string name = "", string scriptPath =
8286
{
8387
try
8488
{
85-
var code = IsEmbeddedResourcePath(scriptPath) ? GetEmbeddedPythonCode(scriptPath) : File.ReadAllText(scriptPath);
89+
var code = ReadAllText(scriptPath);
8690
module.Exec(code);
8791
}
8892
catch (Exception)

0 commit comments

Comments
 (0)