Mercurial > hg > orthanc
comparison OrthancServer/Resources/Samples/ImportDicomFiles/OrthancImport.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 | 9bf2f9e0af47 |
children | 7053502fbf97 |
comparison
equal
deleted
inserted
replaced
4747:24dbf3f013d9 | 4748:4336642b8cff |
---|---|
20 | 20 |
21 | 21 |
22 import argparse | 22 import argparse |
23 import bz2 | 23 import bz2 |
24 import gzip | 24 import gzip |
25 import json | |
25 import os | 26 import os |
26 import requests | 27 import requests |
27 import sys | 28 import sys |
28 import tarfile | 29 import tarfile |
29 import zipfile | 30 import zipfile |
69 | 70 |
70 | 71 |
71 | 72 |
72 IMPORTED_STUDIES = set() | 73 IMPORTED_STUDIES = set() |
73 COUNT_ERROR = 0 | 74 COUNT_ERROR = 0 |
74 COUNT_SUCCESS = 0 | 75 COUNT_DICOM = 0 |
75 | 76 COUNT_JSON = 0 |
77 | |
78 | |
79 def IsJson(content): | |
80 try: | |
81 if (sys.version_info >= (3, 0)): | |
82 json.loads(content.decode()) | |
83 return True | |
84 else: | |
85 json.loads(content) | |
86 return True | |
87 except: | |
88 return False | |
89 | |
90 | |
76 def UploadBuffer(dicom): | 91 def UploadBuffer(dicom): |
77 global IMPORTED_STUDIES | 92 global IMPORTED_STUDIES |
78 global COUNT_ERROR | 93 global COUNT_ERROR |
79 global COUNT_SUCCESS | 94 global COUNT_DICOM |
95 global COUNT_JSON | |
96 | |
97 if IsJson(dicom): | |
98 COUNT_JSON += 1 | |
99 return | |
80 | 100 |
81 auth = HTTPBasicAuth(args.username, args.password) | 101 auth = HTTPBasicAuth(args.username, args.password) |
82 r = requests.post('%s/instances' % args.url, auth = auth, data = dicom) | 102 r = requests.post('%s/instances' % args.url, auth = auth, data = dicom) |
83 | 103 |
84 try: | 104 try: |
91 return | 111 return |
92 else: | 112 else: |
93 raise | 113 raise |
94 | 114 |
95 info = r.json() | 115 info = r.json() |
96 COUNT_SUCCESS += 1 | 116 COUNT_DICOM += 1 |
97 | 117 |
98 if not info['ParentStudy'] in IMPORTED_STUDIES: | 118 if not info['ParentStudy'] in IMPORTED_STUDIES: |
99 IMPORTED_STUDIES.add(info['ParentStudy']) | 119 IMPORTED_STUDIES.add(info['ParentStudy']) |
100 | 120 |
101 r2 = requests.get('%s/instances/%s/tags?short' % (args.url, info['ID']), | 121 r2 = requests.get('%s/instances/%s/tags?short' % (args.url, info['ID']), |
217 else: | 237 else: |
218 raise Exception('Missing file or directory: %s' % path) | 238 raise Exception('Missing file or directory: %s' % path) |
219 | 239 |
220 | 240 |
221 print('') | 241 print('') |
222 print('Status:') | 242 |
223 print(' %d DICOM instances properly imported' % COUNT_SUCCESS) | 243 if COUNT_ERROR == 0: |
244 print('SUCCESS:') | |
245 else: | |
246 print('WARNING:') | |
247 | |
248 print(' %d DICOM instances properly imported' % COUNT_DICOM) | |
224 print(' %d DICOM studies properly imported' % len(IMPORTED_STUDIES)) | 249 print(' %d DICOM studies properly imported' % len(IMPORTED_STUDIES)) |
250 print(' %d JSON files ignored' % COUNT_JSON) | |
225 print(' Error in %d files' % COUNT_ERROR) | 251 print(' Error in %d files' % COUNT_ERROR) |
226 print('') | 252 print('') |