Skip to content

Commit 6ec78d2

Browse files
committed
Internalize implementation details
1 parent 871fc3c commit 6ec78d2

11 files changed

+518
-530
lines changed

Bonsai.Scripting/Bonsai.Scripting.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<PackageTags>Bonsai Rx Scripting</PackageTags>
77
<UseWindowsForms>true</UseWindowsForms>
88
<TargetFramework>net462</TargetFramework>
9-
<Version>2.6.1</Version>
9+
<Version>2.7.0</Version>
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<PackageReference Include="IronPython" Version="2.7.5" />

Bonsai.Scripting/ExpressionScriptEditorDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Bonsai.Scripting
77
{
8-
public partial class ExpressionScriptEditorDialog : Form
8+
internal partial class ExpressionScriptEditorDialog : Form
99
{
1010
public ExpressionScriptEditorDialog()
1111
{
Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
1-
using System;
2-
using System.Linq;
3-
using System.ComponentModel;
4-
using System.Reactive.Linq;
5-
6-
namespace Bonsai.Scripting
7-
{
8-
[Obsolete]
9-
[DefaultProperty("Script")]
10-
[WorkflowElementCategory(ElementCategory.Condition)]
11-
[Description("A Python script used to determine which elements of the input sequence are accepted.")]
12-
public class PythonCondition : Combinator
13-
{
14-
public PythonCondition()
15-
{
16-
Script = "def process(value):\n return True";
17-
}
18-
19-
[Editor("Bonsai.Scripting.PythonScriptEditor, Bonsai.Scripting", DesignTypes.UITypeEditor)]
20-
[Description("The script that determines the criteria for the condition.")]
21-
public string Script { get; set; }
22-
23-
public override IObservable<TSource> Process<TSource>(IObservable<TSource> source)
24-
{
25-
return Observable.Defer(() =>
26-
{
27-
var engine = PythonEngine.Create();
28-
var scope = engine.CreateScope();
29-
engine.Execute(Script, scope);
30-
31-
object condition;
32-
PythonProcessor<TSource, bool> processor;
33-
if (PythonHelper.TryGetClass(scope, "Condition", out condition))
34-
{
35-
processor = new PythonProcessor<TSource, bool>(engine.Operations, condition);
36-
}
37-
else processor = new PythonProcessor<TSource, bool>(scope);
38-
39-
if (processor.Load != null)
40-
{
41-
processor.Load();
42-
}
43-
44-
var result = source.Where(processor.Process);
45-
if (processor.Unload != null)
46-
{
47-
result = result.Finally(processor.Unload);
48-
}
49-
50-
return result;
51-
});
52-
}
53-
}
54-
}
1+
using System;
2+
using System.Linq;
3+
using System.ComponentModel;
4+
using System.Reactive.Linq;
5+
6+
namespace Bonsai.Scripting
7+
{
8+
[Obsolete]
9+
[DefaultProperty("Script")]
10+
[WorkflowElementCategory(ElementCategory.Condition)]
11+
[Description("A Python script used to determine which elements of the input sequence are accepted.")]
12+
public class PythonCondition : Combinator
13+
{
14+
public PythonCondition()
15+
{
16+
Script = "def process(value):\n return True";
17+
}
18+
19+
[Editor("Bonsai.Scripting.PythonScriptEditor, Bonsai.Scripting", DesignTypes.UITypeEditor)]
20+
[Description("The script that determines the criteria for the condition.")]
21+
public string Script { get; set; }
22+
23+
public override IObservable<TSource> Process<TSource>(IObservable<TSource> source)
24+
{
25+
return Observable.Defer(() =>
26+
{
27+
var engine = PythonEngine.Create();
28+
var scope = engine.CreateScope();
29+
engine.Execute(Script, scope);
30+
31+
PythonProcessor<TSource, bool> processor;
32+
if (PythonHelper.TryGetClass(scope, "Condition", out object condition))
33+
{
34+
processor = new PythonProcessor<TSource, bool>(engine.Operations, condition);
35+
}
36+
else processor = new PythonProcessor<TSource, bool>(scope);
37+
38+
if (processor.Load != null)
39+
{
40+
processor.Load();
41+
}
42+
43+
var result = source.Where(processor.Process);
44+
if (processor.Unload != null)
45+
{
46+
result = result.Finally(processor.Unload);
47+
}
48+
49+
return result;
50+
});
51+
}
52+
}
53+
}

Bonsai.Scripting/PythonHelper.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ static class PythonHelper
1616

1717
internal static bool TryGetClass(ScriptScope scope, string className, out object pythonClass)
1818
{
19-
object variable;
20-
if (scope.TryGetVariable<object>(className, out variable))
19+
if (scope.TryGetVariable(className, out object variable))
2120
{
2221
pythonClass = variable as OldClass;
2322
if (pythonClass != null)
@@ -35,9 +34,8 @@ internal static bool TryGetClass(ScriptScope scope, string className, out object
3534

3635
internal static Type GetOutputType(ObjectOperations op, object pythonClass, string methodName)
3736
{
38-
object returnType;
3937
var function = (PythonFunction)op.GetMember<Method>(pythonClass, methodName).__func__;
40-
if (function.func_dict.TryGetValue(PythonHelper.ReturnTypeAttribute, out returnType))
38+
if (function.func_dict.TryGetValue(ReturnTypeAttribute, out object returnType))
4139
{
4240
return (Type)returnType;
4341
}
@@ -47,16 +45,14 @@ internal static Type GetOutputType(ObjectOperations op, object pythonClass, stri
4745

4846
internal static Type GetOutputType(ScriptScope scope, string functionName)
4947
{
50-
object returnType;
5148
var function = scope.GetVariable<PythonFunction>(functionName);
52-
if (function.func_dict.TryGetValue(PythonHelper.ReturnTypeAttribute, out returnType))
49+
if (function.func_dict.TryGetValue(ReturnTypeAttribute, out object returnType))
5350
{
5451
return (Type)returnType;
5552
}
5653
else
5754
{
58-
Func<Type> getOutputType;
59-
if (scope.TryGetVariable<Func<Type>>("getOutputType", out getOutputType))
55+
if (scope.TryGetVariable("getOutputType", out Func<Type> getOutputType))
6056
{
6157
return getOutputType();
6258
}

Bonsai.Scripting/PythonProcessor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ class PythonProcessor<TSource, TResult>
77
{
88
internal PythonProcessor(ScriptScope scope)
99
{
10-
Action load, unload;
11-
scope.TryGetVariable<Action>(PythonHelper.LoadFunction, out load);
12-
scope.TryGetVariable<Action>(PythonHelper.UnloadFunction, out unload);
10+
scope.TryGetVariable(PythonHelper.LoadFunction, out Action load);
11+
scope.TryGetVariable(PythonHelper.UnloadFunction, out Action unload);
1312
Process = scope.GetVariable<Func<TSource, TResult>>(PythonHelper.ProcessFunction);
1413
Load = load;
1514
Unload = unload;

Bonsai.Scripting/PythonScriptEditorDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Bonsai.Scripting
77
{
8-
public partial class PythonScriptEditorDialog : Form
8+
internal partial class PythonScriptEditorDialog : Form
99
{
1010
public PythonScriptEditorDialog()
1111
{

0 commit comments

Comments
 (0)