Skip to content

Commit

Permalink
Merge pull request #22 from faustomilletari/tempdir-is-a-directory
Browse files Browse the repository at this point in the history
Store temporary files in unique directories
  • Loading branch information
faustomilletari committed Jun 10, 2018
2 parents b9dc0db + c94c9ed commit 72ab080
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions tomaat/server/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import base64
import traceback
import numpy as np
import shutil

try:
# For Python 3.0 and later
Expand Down Expand Up @@ -166,7 +167,7 @@ def make_error_response(self, message):
response = {'type': 'PlainText', 'content': message, 'label': 'Error!'}
return json.dumps(response)

def parse_request(self, request):
def parse_request(self, request, savepath):
"""
This function takes in the content of the client message and creates a dictionary containing data.
The service interface, that was specified in the input_interface dictionary specified at init,
Expand All @@ -176,8 +177,6 @@ def parse_request(self, request):
:return: dict containing data that can be fed to the pre-processing, inference, post-processing pipeline
"""

savepath = tempfile.gettempdir()

data = {}

for element in self.input_interface:
Expand All @@ -186,7 +185,7 @@ def parse_request(self, request):
if element['type'] == 'volume':
uid = uuid.uuid4()

mha_file = str(uid) + '.mha'
mha_file = str(uid).replace('-', '') + '.mha'

tmp_filename_mha = os.path.join(savepath, mha_file)

Expand All @@ -213,7 +212,7 @@ def parse_request(self, request):

return data

def make_response(self, data):
def make_response(self, data, savepath):
"""
This function takes in the post-processed results of inference and creates a message for the client.
The message is created according to the directives specified in the output_interface dictionary passed
Expand All @@ -223,16 +222,14 @@ def make_response(self, data):
"""
message = []

savepath = tempfile.gettempdir()

for element in self.output_interface:
type = element['type']
field = element['field']

if type == 'LabelVolume':
uid = uuid.uuid4()

mha_seg = str(uid) + '_seg.mha'
mha_seg = str(uid).replace('-', '') + '_seg.mha'
tmp_label_volume = os.path.join(savepath, mha_seg)

writer = sitk.ImageFileWriter()
Expand All @@ -252,7 +249,7 @@ def make_response(self, data):

uid = uuid.uuid4()

vtk_mesh = str(uid) + '_seg.vtk'
vtk_mesh = str(uid).replace('-', '') + '_seg.vtk'
tmp_vtk_mesh = os.path.join(savepath, vtk_mesh)

writer = vtk.vtkPolyDataWriter()
Expand All @@ -279,8 +276,12 @@ def make_response(self, data):
return json.dumps(message)

def received_data_handler(self, request):
savepath = os.path.join(tempfile.gettempdir(), str(uuid.uuid4()).replace('-', ''))

os.mkdir(savepath)

try:
data = self.parse_request(request)
data = self.parse_request(request, savepath)
except:
traceback.print_exc()
logger.error('Server-side ERROR during request parsing')
Expand All @@ -294,12 +295,14 @@ def received_data_handler(self, request):
return self.make_error_response('Server-side ERROR during processing')

try:
response = self.make_response(transformed_result)
response = self.make_response(transformed_result, savepath)
except:
traceback.print_exc()
logger.error('Server-side ERROR during response message creation')
return self.make_error_response('Server-side ERROR during response message creation')

shutil.rmtree(savepath)

return response

def run(self):
Expand Down

0 comments on commit 72ab080

Please sign in to comment.