Mercurial > hg > orthanc
comparison Resources/Samples/ImportDicomFiles/ImportDicomFiles.py @ 733:29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Feb 2014 09:45:15 +0100 |
parents | 7000726bade7 |
children | 44382c8bcd15 |
comparison
equal
deleted
inserted
replaced
732:b79eda29896d | 733:29982cfc5009 |
---|---|
19 """ % (sys.argv[0], sys.argv[0], sys.argv[0])) | 19 """ % (sys.argv[0], sys.argv[0], sys.argv[0])) |
20 exit(-1) | 20 exit(-1) |
21 | 21 |
22 URL = 'http://%s:%d/instances' % (sys.argv[1], int(sys.argv[2])) | 22 URL = 'http://%s:%d/instances' % (sys.argv[1], int(sys.argv[2])) |
23 | 23 |
24 success = 0 | 24 success_count = 0 |
25 total_file_count = 0 | |
25 | 26 |
26 | 27 |
27 # This function will upload a single file to Orthanc through the REST API | 28 # This function will upload a single file to Orthanc through the REST API |
28 def UploadFile(path): | 29 def UploadFile(path): |
29 global success | 30 global success_count |
31 global total_file_count | |
30 | 32 |
31 f = open(path, "rb") | 33 f = open(path, "rb") |
32 content = f.read() | 34 content = f.read() |
33 f.close() | 35 f.close() |
36 total_file_count += 1 | |
34 | 37 |
35 try: | 38 try: |
36 sys.stdout.write("Importing %s" % path) | 39 sys.stdout.write("Importing %s" % path) |
37 | 40 |
38 h = httplib2.Http() | 41 h = httplib2.Http() |
49 # "Http.add_credentials()" method for Basic HTTP Access | 52 # "Http.add_credentials()" method for Basic HTTP Access |
50 # Authentication (for some weird reason, this method does | 53 # Authentication (for some weird reason, this method does |
51 # not always work) | 54 # not always work) |
52 # http://en.wikipedia.org/wiki/Basic_access_authentication | 55 # http://en.wikipedia.org/wiki/Basic_access_authentication |
53 headers['authorization'] = 'Basic ' + base64.b64encode(username + ':' + password) | 56 headers['authorization'] = 'Basic ' + base64.b64encode(username + ':' + password) |
54 | 57 |
55 resp, content = h.request(URL, 'POST', | 58 resp, content = h.request(URL, 'POST', |
56 body = content, | 59 body = content, |
57 headers = headers) | 60 headers = headers) |
58 | 61 |
59 if resp.status == 200: | 62 if resp.status == 200: |
60 sys.stdout.write(" => success\n") | 63 sys.stdout.write(" => success\n") |
61 success += 1 | 64 success_count += 1 |
62 else: | 65 else: |
63 sys.stdout.write(" => failure (Is it a DICOM file?)\n") | 66 sys.stdout.write(" => failure (Is it a DICOM file?)\n") |
64 | 67 |
65 except: | 68 except: |
66 sys.stdout.write(" => unable to connect (Is Orthanc running? Is there a password?)\n") | 69 sys.stdout.write(" => unable to connect (Is Orthanc running? Is there a password?)\n") |
72 else: | 75 else: |
73 # Recursively upload a directory | 76 # Recursively upload a directory |
74 for root, dirs, files in os.walk(sys.argv[3]): | 77 for root, dirs, files in os.walk(sys.argv[3]): |
75 for f in files: | 78 for f in files: |
76 UploadFile(os.path.join(root, f)) | 79 UploadFile(os.path.join(root, f)) |
77 | |
78 | 80 |
79 print("\nSummary: %d DICOM file(s) have been imported" % success) | 81 if success_count == total_file_count: |
82 print("\nSummary: all %d DICOM file(s) have been imported successfully" % success_count) | |
83 else: | |
84 print("\nSummary: %d out of %d files have been imported successfully as DICOM instances" % (success_count, total_file_count)) |