forked from ObjectProfile/PythonBridge
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPBApplication.class.st
More file actions
432 lines (361 loc) · 10.1 KB
/
PBApplication.class.st
File metadata and controls
432 lines (361 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
Class {
#name : #PBApplication,
#superclass : #Object,
#instVars : [
'communicationHandler',
'processHandler',
'loggingHandler',
'executionHandler',
'settings'
],
#classVars : [
'DebugMode'
],
#classInstVars : [
'uniqueInstance'
],
#category : #'PythonBridge-Global'
}
{ #category : #accessing }
PBApplication class >> baseApplication [
^ PBApplication
]
{ #category : #debug }
PBApplication class >> debugMode [
^ DebugMode ifNil: [ false ]
]
{ #category : #debug }
PBApplication class >> debugMode: bool [
^ DebugMode := bool
]
{ #category : #'start-stop' }
PBApplication class >> do: aBlockClosure [
| retVal |
self isRunning ifTrue: [ ^ aBlockClosure value ].
[
self start.
retVal := aBlockClosure cull: self uniqueInstance.
self uniqueInstance commandFactory << nil.
self uniqueInstance commandFactory send waitForValue. ] ensure: [ self stop ].
^ retVal
]
{ #category : #initialization }
PBApplication class >> initialize [
SessionManager default registerToolClassNamed: self name
]
{ #category : #testing }
PBApplication class >> isRunning [
^ uniqueInstance notNil and: [ uniqueInstance isRunning ]
]
{ #category : #'start-stop' }
PBApplication class >> killAll [
"Stop ALL PBApplication instances and kill any server processes that may still be running."
| allInstances firstApplication wd proc error |
allInstances := self allInstances.
allInstances ifEmpty: [ ^ self ].
firstApplication := allInstances
detect: #isRunning
ifNone: [ allInstances first ].
allInstances do: #stop.
wd := firstApplication workingDirectory.
proc := PBUnixSubprocess new command: (wd / 'killpbservers.sh') fullName.
proc runAndWait.
proc isSuccess ifFalse:
[ error:= PBPythonProcessError new
application: firstApplication;
process: proc.
error signal ].
self assert: proc stderr trimBoth isEmpty.
^ proc stdout
]
{ #category : #testing }
PBApplication class >> module [
^ self uniqueInstance module
]
{ #category : #'pipenv setup' }
PBApplication class >> platform [
^ PBPlatform current
]
{ #category : #'python hooks file' }
PBApplication class >> repositoryFileReference [
self deprecated: 'This should be run against a PBApplication instance that knows its runtime directory'.
"^ self platform runtimeFolderForApplication: self"
]
{ #category : #instructions }
PBApplication class >> resetUniqueInstance [
uniqueInstance ifNotNil: [
[ uniqueInstance stop ]
on: Error
do: [ PBPlatform current uiManager inform: 'Error on reseting unique instance.' ]. ].
uniqueInstance := nil
]
{ #category : #accessing }
PBApplication class >> runtimeDirectory [
"This is the directory basename where the runtime files are located"
^ 'PythonBridgeRuntime'
]
{ #category : #instructions }
PBApplication class >> send: obj [
self assert: self isRunning.
^ self uniqueInstance send: obj
]
{ #category : #accessing }
PBApplication class >> shutdown: isImageQuitting [
isImageQuitting ifTrue: [ self stop ]
]
{ #category : #'start-stop' }
PBApplication class >> start [
self isRunning ifTrue: [ Error signal: 'Keras already running.' ].
self stop.
uniqueInstance := self new.
PBCF reset.
uniqueInstance commandFactory reset.
uniqueInstance start
]
{ #category : #'start-stop' }
PBApplication class >> stop [
uniqueInstance ifNotNil: [
uniqueInstance stop. ].
uniqueInstance := nil
]
{ #category : #testing }
PBApplication class >> test [
self assert: (self do: [ :app |
app commandFactory << (P3GBinaryOperator new
left: 1;
right: 2;
operator: $+;
yourself).
app commandFactory send waitForValue
]) = 3
]
{ #category : #instructions }
PBApplication class >> uniqueInstance [
uniqueInstance ifNil: [ self start ].
^ uniqueInstance
]
{ #category : #accessing }
PBApplication class >> uniqueInstance: anObject [
uniqueInstance := anObject
]
{ #category : #utils }
PBApplication >> addMapping: pythonType to: pythonFunction [
self commandFactory
send: (#bridge_globals asP3GI => #msg_service => #addMapping
callWith: (Array with: pythonType with: pythonFunction))
]
{ #category : #accessing }
PBApplication >> commandFactory [
^ PBCF
]
{ #category : #accessing }
PBApplication >> communicationHandler [
^ communicationHandler
]
{ #category : #accessing }
PBApplication >> errorMessage [
"Fetch the error message from the stderr stream, from the processhander"
^ self serverStatus
]
{ #category : #accessing }
PBApplication >> executionHandler [
^ executionHandler
]
{ #category : #ui }
PBApplication >> gtSettingsFor: aView [
<gtView>
^ aView forward
title: 'Settings';
priority: 60;
object: [ settings ];
view: #gtSettingsFor:
]
{ #category : #ui }
PBApplication >> gtStatusFor: aView [
<gtView>
^ aView textEditor
title: 'Status';
priority: 50;
text: [ self serverStatus asRopedText glamorousCodeFont ]
]
{ #category : #handlers }
PBApplication >> handlers [
^ Array
with: loggingHandler
with: communicationHandler
with: processHandler
with: executionHandler
]
{ #category : #initialization }
PBApplication >> initialize [
super initialize.
settings := PBPlatform current defaultSettings.
self initializeHandlers.
]
{ #category : #initialization }
PBApplication >> initializeHandlers [
loggingHandler := PBLoggingHandler application: self.
communicationHandler := PBCommunicationHandler application: self.
processHandler := PBPythonHandler application: self.
executionHandler := PBExecutionHandler application: self
]
{ #category : #utils }
PBApplication >> installModule: aString [
"Install the supplied module using `pipenv run pip install aString`"
PBPharoPipenvProcess new installModule: aString in: self
]
{ #category : #testing }
PBApplication >> isPythonReady [
"Ensures python webserver is ready for receiving commands"
^ self communicationHandler isPythonReady
]
{ #category : #testing }
PBApplication >> isRunning [
^ self handlers allSatisfy: #isRunning
]
{ #category : #logging }
PBApplication >> log: aLog [
self loggingHandler log: aLog
]
{ #category : #accessing }
PBApplication >> loggingHandler [
^ loggingHandler
]
{ #category : #accessing }
PBApplication >> module [
self subclassResponsibility
]
{ #category : #'instance creation' }
PBApplication >> newCommandFactory [
^ self executionHandler newCommandFactory
]
{ #category : #'instance creation' }
PBApplication >> newCommandStringFactory [
^ self executionHandler newCommandStringFactory
]
{ #category : #instructions }
PBApplication >> notifyDebuggerPaused: aDebugger [
"Notify all promises that the server debugger has paused in case the Bloc UI process is waiting on one of the promises."
self executionHandler notifyDebuggerPaused: aDebugger.
]
{ #category : #accessing }
PBApplication >> pipenvPath [
"Answer the pipenvPath to be used for this application"
^ settings pipenvPath ifNil: [ processHandler pipenvPath ]
]
{ #category : #accessing }
PBApplication >> postInitialization [
]
{ #category : #printing }
PBApplication >> printOn: aStream [
aStream
<< self class name;
<< '('.
self isRunning ifFalse:
[ aStream << 'not running)'.
^ self ].
aStream
print: settings pharoSocketAddress;
<< ', ';
print: settings pythonSocketAddress;
<< ')'
]
{ #category : #accessing }
PBApplication >> processHandler [
^ processHandler
]
{ #category : #utils }
PBApplication >> registerObject: aPythonObject [
self executionHandler registerObject: aPythonObject
]
{ #category : #accessing }
PBApplication >> runtimeDirectory [
"This is the directory basename where the runtime files are located"
^ self class runtimeDirectory
]
{ #category : #instructions }
PBApplication >> send: aCommand [
self isRunning ifFalse: [ Error signal: 'Bridge not running.' ].
^ executionHandler sendCommand: aCommand
]
{ #category : #'start-stop' }
PBApplication >> serverStatus [
"Answer a string describing the environment and server status.
Mostly for problem solving."
^ String streamContents: [ :stream |
self serverStatusOn: stream process: processHandler ]
]
{ #category : #'start-stop' }
PBApplication >> serverStatusOn: aStream process: process [
"Write the environment and server status on the supplied stream.
Mostly for problem solving."
| serverPathString |
serverPathString := [ self pipenvPath isExecutable
ifTrue: [ self pipenvPath fullName ]
ifFalse: [ self pipenvPath fullName, ': doesn''t exist or isn''t executable, it should be in the PATH' ] ]
on: Error
do: [ '<Unable to determine server path. It should be in the PATH>' ].
aStream
<< 'Running: ';
print: self isRunning; cr;
<< 'pipenv: ';
<< serverPathString; cr;
<< 'Working directory: ';
<< self workingDirectory fullName; cr;
<< 'Server running: ';
print: process isRunning; cr.
process hasProcess ifFalse: [ ^ self ].
process isRunning ifFalse:
[ aStream
<< 'exit status: ';
print: process exitStatusInterpreter exitStatus; cr ].
aStream
cr; cr;
<< 'Stdout:'; cr;
<< process stdout; cr;
cr; cr;
<< 'Stderr:'; cr;
<< process stderr.
]
{ #category : #accessing }
PBApplication >> settings [
^ settings
]
{ #category : #accessing }
PBApplication >> settings: anObject [
settings := anObject
]
{ #category : #'start-stop' }
PBApplication >> start [
self handlers do: #start.
self waitInitialization.
self postInitialization.
self newCommandFactory send.
]
{ #category : #'start-stop' }
PBApplication >> stop [
self handlers select: #notNil thenDo: #stop
]
{ #category : #private }
PBApplication >> waitInitialization [
"Timeout of 10 seconds and try every 0.5 seconds"
| error |
1 to: 10 by: 0.5 do: [ :t |
self isPythonReady
ifTrue: [ ^ self ]
ifFalse: [ (Delay forMilliseconds: 500) wait ] ].
"Store the error in a temporary variable since the debugger sometimes hides the parent stack frames."
error := PBPythonProcessError new
messageText: 'Python application initialization failed!';
application: self.
error signal.
"
Print the result of executing the following line:
self errorMessage
"
]
{ #category : #accessing }
PBApplication >> workingDirectory [
^ settings workingDirectory ifNil:
[ FileLocator imageDirectory / self runtimeDirectory ]
]