Mercurial > hg > orthanc
comparison OrthancServer/Resources/Samples/ImportDicomFiles/ImportDicomFiles.py @ 4748:4336642b8cff
ignore JSON files in sample Python upload scripts
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 08 Jul 2021 14:42:20 +0200 |
parents | d9473bd5ed43 |
children | 7053502fbf97 |
comparison
equal
deleted
inserted
replaced
4747:24dbf3f013d9 | 4748:4336642b8cff |
---|---|
17 # | 17 # |
18 # You should have received a copy of the GNU General Public License | 18 # You should have received a copy of the GNU General Public License |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | 20 |
21 | 21 |
22 import base64 | |
23 import httplib2 | |
24 import json | |
22 import os | 25 import os |
26 import os.path | |
23 import sys | 27 import sys |
24 import os.path | |
25 import httplib2 | |
26 import base64 | |
27 | 28 |
28 if len(sys.argv) != 4 and len(sys.argv) != 6: | 29 if len(sys.argv) != 4 and len(sys.argv) != 6: |
29 print(""" | 30 print(""" |
30 Sample script to recursively import in Orthanc all the DICOM files | 31 Sample script to recursively import in Orthanc all the DICOM files |
31 that are stored in some path. Please make sure that Orthanc is running | 32 that are stored in some path. Please make sure that Orthanc is running |
38 """ % (sys.argv[0], sys.argv[0], sys.argv[0])) | 39 """ % (sys.argv[0], sys.argv[0], sys.argv[0])) |
39 exit(-1) | 40 exit(-1) |
40 | 41 |
41 URL = 'http://%s:%d/instances' % (sys.argv[1], int(sys.argv[2])) | 42 URL = 'http://%s:%d/instances' % (sys.argv[1], int(sys.argv[2])) |
42 | 43 |
43 success_count = 0 | 44 dicom_count = 0 |
45 json_count = 0 | |
44 total_file_count = 0 | 46 total_file_count = 0 |
47 | |
48 | |
49 def IsJson(content): | |
50 try: | |
51 if (sys.version_info >= (3, 0)): | |
52 json.loads(content.decode()) | |
53 return True | |
54 else: | |
55 json.loads(content) | |
56 return True | |
57 except: | |
58 return False | |
45 | 59 |
46 | 60 |
47 # This function will upload a single file to Orthanc through the REST API | 61 # This function will upload a single file to Orthanc through the REST API |
48 def UploadFile(path): | 62 def UploadFile(path): |
49 global success_count | 63 global dicom_count |
64 global json_count | |
50 global total_file_count | 65 global total_file_count |
51 | 66 |
52 f = open(path, "rb") | 67 f = open(path, 'rb') |
53 content = f.read() | 68 content = f.read() |
54 f.close() | 69 f.close() |
55 total_file_count += 1 | 70 total_file_count += 1 |
56 | 71 |
72 sys.stdout.write('Importing %s' % path) | |
73 | |
74 if IsJson(content): | |
75 sys.stdout.write(' => ignored JSON file\n') | |
76 json_count += 1 | |
77 return | |
78 | |
57 try: | 79 try: |
58 sys.stdout.write("Importing %s" % path) | |
59 | |
60 h = httplib2.Http() | 80 h = httplib2.Http() |
61 | 81 |
62 headers = { 'content-type' : 'application/dicom' } | 82 headers = { 'content-type' : 'application/dicom' } |
63 | 83 |
64 if len(sys.argv) == 6: | 84 if len(sys.argv) == 6: |
71 # "Http.add_credentials()" method for Basic HTTP Access | 91 # "Http.add_credentials()" method for Basic HTTP Access |
72 # Authentication (for some weird reason, this method does | 92 # Authentication (for some weird reason, this method does |
73 # not always work) | 93 # not always work) |
74 # http://en.wikipedia.org/wiki/Basic_access_authentication | 94 # http://en.wikipedia.org/wiki/Basic_access_authentication |
75 creds_str = username + ':' + password | 95 creds_str = username + ':' + password |
76 creds_str_bytes = creds_str.encode("ascii") | 96 creds_str_bytes = creds_str.encode('ascii') |
77 creds_str_bytes_b64 = b'Basic ' + base64.b64encode(creds_str_bytes) | 97 creds_str_bytes_b64 = b'Basic ' + base64.b64encode(creds_str_bytes) |
78 headers['authorization'] = creds_str_bytes_b64.decode("ascii") | 98 headers['authorization'] = creds_str_bytes_b64.decode('ascii') |
79 | 99 |
80 resp, content = h.request(URL, 'POST', | 100 resp, content = h.request(URL, 'POST', |
81 body = content, | 101 body = content, |
82 headers = headers) | 102 headers = headers) |
83 | 103 |
84 if resp.status == 200: | 104 if resp.status == 200: |
85 sys.stdout.write(" => success\n") | 105 sys.stdout.write(' => success\n') |
86 success_count += 1 | 106 dicom_count += 1 |
87 else: | 107 else: |
88 sys.stdout.write(" => failure (Is it a DICOM file? Is there a password?)\n") | 108 sys.stdout.write(' => failure (Is it a DICOM file? Is there a password?)\n') |
89 | 109 |
90 except: | 110 except: |
91 type, value, traceback = sys.exc_info() | 111 type, value, traceback = sys.exc_info() |
92 sys.stderr.write(str(value)) | 112 sys.stderr.write(str(value)) |
93 sys.stdout.write(" => unable to connect (Is Orthanc running? Is there a password?)\n") | 113 sys.stdout.write(' => unable to connect (Is Orthanc running? Is there a password?)\n') |
94 | 114 |
95 | 115 |
96 if os.path.isfile(sys.argv[3]): | 116 if os.path.isfile(sys.argv[3]): |
97 # Upload a single file | 117 # Upload a single file |
98 UploadFile(sys.argv[3]) | 118 UploadFile(sys.argv[3]) |
100 # Recursively upload a directory | 120 # Recursively upload a directory |
101 for root, dirs, files in os.walk(sys.argv[3]): | 121 for root, dirs, files in os.walk(sys.argv[3]): |
102 for f in files: | 122 for f in files: |
103 UploadFile(os.path.join(root, f)) | 123 UploadFile(os.path.join(root, f)) |
104 | 124 |
105 if success_count == total_file_count: | 125 |
106 print("\nSummary: all %d DICOM file(s) have been imported successfully" % success_count) | 126 if dicom_count + json_count == total_file_count: |
127 print('\nSUCCESS: %d DICOM file(s) have been successfully imported' % dicom_count) | |
107 else: | 128 else: |
108 print("\nSummary: %d out of %d files have been imported successfully as DICOM instances" % (success_count, total_file_count)) | 129 print('\nWARNING: Only %d out of %d file(s) have been successfully imported as DICOM instance(s)' % (dicom_count, total_file_count - json_count)) |
130 | |
131 if json_count != 0: | |
132 print('NB: %d JSON file(s) have been ignored' % json_count) | |
133 | |
134 print('') |