# HG changeset patch # User Sebastien Jodogne # Date 1412925228 -7200 # Node ID 4e9d517503aeec074c6950fd7c0608e5480077cb # Parent 6ef2c81581cd8b1189853bc08855d9557a9503aa port to Python3 diff -r 6ef2c81581cd -r 4e9d517503ae Resources/Samples/Python/AutoClassify.py --- 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) diff -r 6ef2c81581cd -r 4e9d517503ae Resources/Samples/Python/ChangesLoop.py --- 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) diff -r 6ef2c81581cd -r 4e9d517503ae Resources/Samples/Python/DownloadAnonymized.py --- 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) diff -r 6ef2c81581cd -r 4e9d517503ae Resources/Samples/Python/HighPerformanceAutoRouting.py --- 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) diff -r 6ef2c81581cd -r 4e9d517503ae Resources/Samples/Python/RestToolbox.py --- 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 = ''):