Mercurial > hg > orthanc-tests
diff Tests/Toolbox.py @ 83:3f2170efa8d2
patches for python3
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 23 Jun 2016 18:04:59 +0200 |
parents | 97acfdf0dbce |
children | 2af6c0fb850d |
line wrap: on
line diff
--- a/Tests/Toolbox.py Tue Jun 21 16:33:42 2016 +0200 +++ b/Tests/Toolbox.py Thu Jun 23 18:04:59 2016 +0200 @@ -26,18 +26,40 @@ import signal import subprocess import threading +import sys import time import zipfile from PIL import Image -from urllib import urlencode + +if (sys.version_info >= (3, 0)): + from urllib.parse import urlencode + from io import StringIO + from io import BytesIO + +else: + from urllib import urlencode + + # http://stackoverflow.com/a/1313868/881731 + try: + from cStringIO import StringIO + except: + from StringIO import StringIO -# http://stackoverflow.com/a/1313868/881731 -try: - from cStringIO import StringIO -except: - from StringIO import StringIO +def _DecodeJson(s): + t = s + + if (sys.version_info >= (3, 0)): + try: + t = s.decode() + except: + pass + + try: + return json.loads(t) + except: + return t def DefineOrthanc(server = 'localhost', @@ -90,17 +112,14 @@ if not (resp.status in [ 200 ]): raise Exception(resp.status) else: - try: - return json.loads(content) - except: - return content + return _DecodeJson(content) def _DoPutOrPost(orthanc, uri, method, data, contentType, headers): http = httplib2.Http() http.follow_redirects = False _SetupCredentials(orthanc, http) - if isinstance(data, str): + if isinstance(data, (str, bytearray, bytes)): body = data if len(contentType) != 0: headers['content-type'] = contentType @@ -116,10 +135,7 @@ if not (resp.status in [ 200, 302 ]): raise Exception(resp.status) else: - try: - return json.loads(content) - except: - return content + return _DecodeJson(content) def DoDelete(orthanc, uri): http = httplib2.Http() @@ -130,10 +146,7 @@ if not (resp.status in [ 200 ]): raise Exception(resp.status) else: - try: - return json.loads(content) - except: - return content + return _DecodeJson(content) def DoPut(orthanc, uri, data = {}, contentType = ''): return _DoPutOrPost(orthanc, uri, 'PUT', data, contentType, {}) @@ -145,9 +158,9 @@ return os.path.join(os.path.dirname(__file__), '..', 'Database', filename) def UploadInstance(orthanc, filename): - f = open(GetDatabasePath(filename), 'rb') - d = f.read() - f.close() + with open(GetDatabasePath(filename), 'rb') as f: + d = f.read() + return DoPost(orthanc, '/instances', d, 'application/dicom') def UploadFolder(orthanc, path): @@ -174,12 +187,19 @@ def GetImage(orthanc, uri, headers = {}): # http://www.pythonware.com/library/pil/handbook/introduction.htm data = DoGet(orthanc, uri, headers = headers) - return Image.open(StringIO(data)) + if (sys.version_info >= (3, 0)): + return Image.open(BytesIO(data)) + else: + return Image.open(StringIO(data)) def GetArchive(orthanc, uri): # http://stackoverflow.com/a/1313868/881731 s = DoGet(orthanc, uri) - return zipfile.ZipFile(StringIO(s), "r") + + if (sys.version_info >= (3, 0)): + return zipfile.ZipFile(BytesIO(s), "r") + else: + return zipfile.ZipFile(StringIO(s), "r") def IsDefinedInLua(orthanc, name): s = DoPost(orthanc, '/tools/execute-script', 'print(type(%s))' % name, 'application/lua') @@ -224,7 +244,7 @@ os._exit(-1) stop_event.wait(0.1) - print 'Stopping the external command' + print('Stopping the external command') external.terminate() external.communicate() # Wait for the command to stop