Skip to content

Commit ba96617

Browse files
committed
Add PythonCommandStringFactory
1 parent 83059d4 commit ba96617

8 files changed

+202
-1
lines changed

src/PythonBridge/PBApplication.class.st

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ PBApplication >> newCommandFactory [
210210
^ self executionHandler newCommandFactory
211211
]
212212

213+
{ #category : #'instance creation' }
214+
PBApplication >> newCommandStringFactory [
215+
^ self executionHandler newCommandStringFactory
216+
]
217+
213218
{ #category : #instructions }
214219
PBApplication >> notifyDebuggerPaused: aDebugger [
215220
"Notify all promises that the server debugger has paused in case the Bloc UI process is waiting on one of the promises."

src/PythonBridge/PBCommand.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ PBCommand >> promise: anObject [
8989
promise := anObject
9090
]
9191

92-
{ #category : #'as yet unclassified' }
92+
{ #category : #accessing }
9393
PBCommand >> pythonCode [
9494
| py3CodeStream |
9595
py3CodeStream := String new writeStream.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Class {
2+
#name : #PBCommandString,
3+
#superclass : #PBCommand,
4+
#category : #'PythonBridge-Execution'
5+
}
6+
7+
{ #category : #accessing }
8+
PBCommandString >> pythonCode [
9+
10+
^ instructions
11+
12+
]
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"
2+
PBCommandStringFactory is variant of the command factory that takes two strings to build the set of statements:
3+
4+
- script: a python script that will be evaluated on the remote.
5+
- resultExpression: a single python expression whose value will be returned.
6+
7+
Note that PBCommandStringFactory no longer uses the instructions instance variable from ${class:name=PBCommandFactory}$.
8+
9+
Example usage:
10+
11+
[[[
12+
self application newCommandStringFactory
13+
script:
14+
'a = 4
15+
b = 3';
16+
resultExpression: 'a + b';
17+
sendAndWait
18+
]]]
19+
"
20+
Class {
21+
#name : #PBCommandStringFactory,
22+
#superclass : #PBCommandFactory,
23+
#instVars : [
24+
'script',
25+
'resultExpression'
26+
],
27+
#category : #'PythonBridge-Execution'
28+
}
29+
30+
{ #category : #streaming }
31+
PBCommandStringFactory >> << anObject [
32+
33+
self error: 'PBCommandStringFactory expects a single python script'
34+
]
35+
36+
{ #category : #api }
37+
PBCommandStringFactory >> addAllBindings: aDictionary [
38+
"Add the supplied key / value pairs as bindings to the receiver"
39+
40+
aDictionary keysAndValuesDo: [ :key :value |
41+
self bindingAt: key put: value ]
42+
]
43+
44+
{ #category : #streaming }
45+
PBCommandStringFactory >> append: aPythonStatement [
46+
47+
self error: 'PBCommandStringFactory expects a single python script'
48+
]
49+
50+
{ #category : #accessing }
51+
PBCommandStringFactory >> instructionsWithNotifyAtEnd [
52+
53+
^ String streamContents: [ :stream |
54+
stream
55+
<< script withUnixLineEndings;
56+
lf;
57+
<< 'notify(';
58+
<< resultExpression;
59+
<< ', ';
60+
print: command id;
61+
<< ')' ].
62+
63+
]
64+
65+
{ #category : #initialization }
66+
PBCommandStringFactory >> reset [
67+
script := ''.
68+
resultExpression := #None.
69+
bindingsDictionary := Dictionary new.
70+
observers := OrderedCollection new.
71+
command := PBCommandString new.
72+
transformBlock := #yourself
73+
]
74+
75+
{ #category : #accessing }
76+
PBCommandStringFactory >> resultExpression [
77+
<return: #String>
78+
79+
^ resultExpression
80+
]
81+
82+
{ #category : #accessing }
83+
PBCommandStringFactory >> resultExpression: aString [
84+
85+
resultExpression := aString
86+
]
87+
88+
{ #category : #accessing }
89+
PBCommandStringFactory >> script [
90+
<return: #String>
91+
92+
^ script
93+
]
94+
95+
{ #category : #accessing }
96+
PBCommandStringFactory >> script: aString [
97+
"Set the Python script to be executed on the remote"
98+
99+
script := aString
100+
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Class {
2+
#name : #PBCommandStringFactoryTest,
3+
#superclass : #TestCase,
4+
#instVars : [
5+
'factory'
6+
],
7+
#category : #'PythonBridge-Tests'
8+
}
9+
10+
{ #category : #initialization }
11+
PBCommandStringFactoryTest >> setUp [
12+
factory := PBCommandStringFactory new
13+
]
14+
15+
{ #category : #tests }
16+
PBCommandStringFactoryTest >> testBuildEmptyCommand [
17+
| command |
18+
command := factory buildCommand.
19+
self assert: command class equals: PBCommandString.
20+
self assert: (command pythonCode trimLeft beginsWith: 'notify(None,').
21+
self assert: command bindings isEmpty
22+
]
23+
24+
{ #category : #tests }
25+
PBCommandStringFactoryTest >> testBuildFullCommand [
26+
| command |
27+
28+
factory script: 'foo'.
29+
factory bindingAt: #myVar put: 3.
30+
command := factory buildCommand.
31+
self assert: command pythonCode lines first equals: 'foo'.
32+
self assert: (command bindingsDictionary at: #myVar) equals: 3
33+
]
34+
35+
{ #category : #tests }
36+
PBCommandStringFactoryTest >> testBuildWithObservers [
37+
| command |
38+
factory observerFromCallback: [ 32 ].
39+
command := factory buildCommand.
40+
self assert: command observers first callback value equals: 32.
41+
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Class {
2+
#name : #PBCommandStringTest,
3+
#superclass : #TestCase,
4+
#category : #'PythonBridge-Tests'
5+
}
6+
7+
{ #category : #tests }
8+
PBCommandStringTest >> testNewCommandHasEmtptyBindings [
9+
| command |
10+
command := PBCommandString new.
11+
self assert: command id isString.
12+
self assert: command id notEmpty.
13+
self assert: command bindings equals: #().
14+
]
15+
16+
{ #category : #tests }
17+
PBCommandStringTest >> testNotValidBindingsKeyNotString [
18+
| command |
19+
command := PBCommandString new.
20+
command bindings: { 33 -> 'bar' }.
21+
self deny: command isValid
22+
]
23+
24+
{ #category : #tests }
25+
PBCommandStringTest >> testNotValidBindingsNotAssoc [
26+
| command |
27+
command := PBCommandString new.
28+
command bindings: { '33' -> 'bar'. 'foo' }.
29+
self deny: command isValid
30+
]

src/PythonBridge/PBExecutionHandler.class.st

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ PBExecutionHandler >> newCommandFactory [
6767
yourself
6868
]
6969

70+
{ #category : #'as yet unclassified' }
71+
PBExecutionHandler >> newCommandStringFactory [
72+
^ PBCommandStringFactory new
73+
application: self application;
74+
yourself
75+
]
76+
7077
{ #category : #handlers }
7178
PBExecutionHandler >> notifyCallbackHandler: msg [
7279
| val serialization |

src/PythonBridge/PBObject.class.st

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ PBObject >> newCommandFactory [
112112
^ self application newCommandFactory
113113
]
114114

115+
{ #category : #'instance creation' }
116+
PBObject >> newCommandStringFactory [
117+
118+
^ self application newCommandFactory
119+
]
120+
115121
{ #category : #'instance creation' }
116122
PBObject >> newPythonVar [
117123
^ self class newPythonVar

0 commit comments

Comments
 (0)