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
|
#!/usr/bin/env python
## Program: PypeServer
## Language: Python
## Copyright (c) Luca Antiga, David Steinman. All rights reserved.
## See LICENCE file for details.
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notices for more information.
from vmtk import pypes
import vtk
import time
import traceback
class OutputStream(object):
def __init__(self,textList):
self.textList = textList
def write(self, text):
self.textList.append(text)
def flush(self):
pass
def RunPypeProcess(arguments, inputStream=None, outputStream=None, logOn=True):
pipe = pypes.Pype()
pipe.ExitOnError = 0
if inputStream:
pipe.InputStream = inputStream
if outputStream:
pipe.OutputStream = outputStream
pipe.LogOn = logOn
pipe.LogOn = True
if type(arguments) in [str,unicode]:
pipe.SetArgumentsString(arguments)
else:
pipe.Arguments = arguments
try:
pipe.ParseArguments()
pipe.Execute()
except BaseException, e:
print traceback.format_exc()
del pipe
def PypeServer(queue, output, error, returnIfEmptyQueue=False):
def MessageCallback(o, e, m):
if not error:
return
error.append(m)
MessageCallback.CallDataType = 'string0'
vtk.vtkOutputWindow.GetInstance().AddObserver('ErrorEvent',MessageCallback)
vtk.vtkOutputWindow.GetInstance().AddObserver('WarningEvent',MessageCallback)
outputStream = None
if output != None:
outputStream = OutputStream(output)
if error != None:
errorStream = OutputStream(error)
ranOnce = False
while True:
try:
if queue:
arguments = queue.pop(0)
RunPypeProcess(arguments,outputStream=outputStream)
ranOnce = True
elif returnIfEmptyQueue:
if ranOnce:
return
else:
time.sleep(0.5)
except IOError, e:
print "Connection closed"
break
except KeyboardInterrupt, e:
print "Connection closed"
break
|