Mercurial > hg > orthanc
changeset 1184:4e9d517503ae
port to Python3
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 10 Oct 2014 09:13:48 +0200 |
parents | 6ef2c81581cd |
children | b17b6bd59747 |
files | Resources/Samples/Python/AutoClassify.py Resources/Samples/Python/ChangesLoop.py Resources/Samples/Python/DownloadAnonymized.py Resources/Samples/Python/HighPerformanceAutoRouting.py Resources/Samples/Python/RestToolbox.py |
diffstat | 5 files changed, 31 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/Resources/Samples/Python/AutoClassify.py Fri Oct 10 08:52:56 2014 +0200 +++ b/Resources/Samples/Python/AutoClassify.py Fri Oct 10 09:13:48 2014 +0200 @@ -23,6 +23,7 @@ import time import os import os.path +import sys import RestToolbox parser = argparse.ArgumentParser( @@ -44,7 +45,7 @@ def FixPath(p): - return p.encode('ascii', 'ignore').strip() + return p.encode('ascii', 'ignore').strip().decode() def GetTag(resource, tag): if ('MainDicomTags' in resource and @@ -70,7 +71,7 @@ p = os.path.join(args.target, FixPath(a), FixPath(b), FixPath(c)) f = os.path.join(p, FixPath(d)) - + # Copy the DICOM file to the target path print('Writing new DICOM file: %s' % f)
--- a/Resources/Samples/Python/ChangesLoop.py Fri Oct 10 08:52:56 2014 +0200 +++ b/Resources/Samples/Python/ChangesLoop.py Fri Oct 10 09:13:48 2014 +0200 @@ -54,7 +54,7 @@ # Remove the possible trailing characters due to DICOM padding patientName = patientName.strip() - print 'New instance received for patient "%s": "%s"' % (patientName, path) + print('New instance received for patient "%s": "%s"' % (patientName, path)) @@ -82,5 +82,5 @@ current = r['Last'] if r['Done']: - print "Everything has been processed: Waiting..." + print('Everything has been processed: Waiting...') time.sleep(1)
--- a/Resources/Samples/Python/DownloadAnonymized.py Fri Oct 10 08:52:56 2014 +0200 +++ b/Resources/Samples/Python/DownloadAnonymized.py Fri Oct 10 09:13:48 2014 +0200 @@ -42,7 +42,7 @@ if name.startswith('anonymized'): # Trigger the download - print 'Downloading %s' % name + print('Downloading %s' % name) zipContent = RestToolbox.DoGet('%s/patients/%s/archive' % (URL, patient)) f = open(os.path.join('/tmp', name + '.zip'), 'wb') f.write(zipContent)
--- a/Resources/Samples/Python/HighPerformanceAutoRouting.py Fri Oct 10 08:52:56 2014 +0200 +++ b/Resources/Samples/Python/HighPerformanceAutoRouting.py Fri Oct 10 09:13:48 2014 +0200 @@ -107,7 +107,7 @@ break if len(instances) > 0: - print 'Sending a packet of %d instances' % len(instances) + print('Sending a packet of %d instances' % len(instances)) start = time.time() # Send all the instances with a single DICOM connexion @@ -124,7 +124,7 @@ RestToolbox.DoDelete('%s/exports' % URL) end = time.time() - print 'The packet of %d instances has been sent in %d seconds' % (len(instances), end - start) + print('The packet of %d instances has been sent in %d seconds' % (len(instances), end - start)) # @@ -133,7 +133,7 @@ def PrintProgress(queue): while True: - print 'Current queue size: %d' % (queue.qsize()) + print('Current queue size: %d' % (queue.qsize())) time.sleep(1)
--- a/Resources/Samples/Python/RestToolbox.py Fri Oct 10 08:52:56 2014 +0200 +++ b/Resources/Samples/Python/RestToolbox.py Fri Oct 10 09:13:48 2014 +0200 @@ -18,10 +18,27 @@ import httplib2 import json -from urllib import urlencode +import sys + +if (sys.version_info >= (3, 0)): + from urllib.parse import urlencode +else: + from urllib import urlencode + _credentials = None + +def _DecodeJson(s): + try: + if (sys.version_info >= (3, 0)): + return json.loads(s.decode()) + else: + return json.loads(s) + except: + return s + + def SetCredentials(username, password): global _credentials _credentials = (username, password) @@ -42,12 +59,9 @@ if not (resp.status in [ 200 ]): raise Exception(resp.status) elif not interpretAsJson: - return content + return content.decode() else: - try: - return json.loads(content) - except: - return content + return _DecodeJson(content) def _DoPutOrPost(uri, method, data, contentType): @@ -72,10 +86,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(uri): @@ -86,10 +97,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(uri, data = {}, contentType = ''):