Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/ImportDicomFiles/OrthancImport.py @ 5185:0ea402b4d901
upgrade to year 2023
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 25 Mar 2023 12:27:21 +0100 |
parents | 17c91e054636 |
children | 514fd39f87a8 |
rev | line source |
---|---|
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 # Orthanc - A Lightweight, RESTful DICOM Store |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 # Department, University Hospital of Liege, Belgium |
5185
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4973
diff
changeset
|
6 # Copyright (C) 2017-2023 Osimis S.A., Belgium |
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4973
diff
changeset
|
7 # Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 # |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 # This program is free software: you can redistribute it and/or |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 # modify it under the terms of the GNU General Public License as |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 # published by the Free Software Foundation, either version 3 of the |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 # License, or (at your option) any later version. |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 # |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 # This program is distributed in the hope that it will be useful, but |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 # WITHOUT ANY WARRANTY; without even the implied warranty of |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 # General Public License for more details. |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 # |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 # You should have received a copy of the GNU General Public License |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 import argparse |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 import bz2 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 import gzip |
4748
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
26 import json |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 import os |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 import requests |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 import sys |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 import tarfile |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 import zipfile |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 from requests.auth import HTTPBasicAuth |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 parser = argparse.ArgumentParser(description = 'Command-line tool to import files or archives into Orthanc.') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 parser.add_argument('--url', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 default = 'http://localhost:8042', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 help = 'URL to the REST API of the Orthanc server') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 parser.add_argument('--username', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 default = 'orthanc', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 help = 'Username to the REST API') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 parser.add_argument('--password', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 default = 'orthanc', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 help = 'Password to the REST API') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 parser.add_argument('--force', help = 'Do not warn the user about deletion', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 action = 'store_true') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 parser.add_argument('--clear', help = 'Remove the content of the Orthanc database', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 action = 'store_true') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 parser.add_argument('--verbose', help = 'Be verbose', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 action = 'store_true') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 parser.add_argument('--ignore-errors', help = 'Do not stop if encountering non-DICOM files', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 action = 'store_true') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 parser.add_argument('files', metavar = 'N', nargs = '*', |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 help = 'Files to import') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 args = parser.parse_args() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 if args.clear and not args.force: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 print(""" |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 WARNING: This script will remove all the content of your |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 Orthanc instance running on %s! |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 |
4972
cde17b68d73e
linor fix in OrthancImport.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
66 Are you sure ["yes" to go on]?""" % args.url) |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 if sys.stdin.readline().strip() != 'yes': |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 print('Aborting...') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 exit(0) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 IMPORTED_STUDIES = set() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 COUNT_ERROR = 0 |
4748
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
76 COUNT_DICOM = 0 |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
77 COUNT_JSON = 0 |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
78 |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
79 |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
80 def IsJson(content): |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
81 try: |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
82 if (sys.version_info >= (3, 0)): |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
83 json.loads(content.decode()) |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
84 return True |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
85 else: |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
86 json.loads(content) |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
87 return True |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
88 except: |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
89 return False |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
90 |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
91 |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 def UploadBuffer(dicom): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 global IMPORTED_STUDIES |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 global COUNT_ERROR |
4748
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
95 global COUNT_DICOM |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
96 global COUNT_JSON |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
97 |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
98 if IsJson(dicom): |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
99 COUNT_JSON += 1 |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
100 return |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
101 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
102 auth = HTTPBasicAuth(args.username, args.password) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
103 r = requests.post('%s/instances' % args.url, auth = auth, data = dicom) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
104 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
105 try: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
106 r.raise_for_status() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
107 except: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
108 COUNT_ERROR += 1 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
109 if args.ignore_errors: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 if args.verbose: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
111 print(' not a valid DICOM file, ignoring it') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 return |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
113 else: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
114 raise |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
115 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
116 info = r.json() |
4748
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
117 COUNT_DICOM += 1 |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
118 |
4973
17c91e054636
minor improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4972
diff
changeset
|
119 if (isinstance(info, dict) and |
17c91e054636
minor improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4972
diff
changeset
|
120 not info['ParentStudy'] in IMPORTED_STUDIES): |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
121 IMPORTED_STUDIES.add(info['ParentStudy']) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
122 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
123 r2 = requests.get('%s/instances/%s/tags?short' % (args.url, info['ID']), |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
124 auth = auth) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
125 r2.raise_for_status() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
126 tags = r2.json() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
127 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
128 print('') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
129 print('New imported study:') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
130 print(' Orthanc ID of the patient: %s' % info['ParentPatient']) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
131 print(' Orthanc ID of the study: %s' % info['ParentStudy']) |
4450
9bf2f9e0af47
support empty PatientID in OrthancImport.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
132 print(' DICOM Patient ID: %s' % ( |
9bf2f9e0af47
support empty PatientID in OrthancImport.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
133 tags['0010,0020'] if '0010,0020' in tags else '(empty)')) |
9bf2f9e0af47
support empty PatientID in OrthancImport.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
134 print(' DICOM Study Instance UID: %s' % ( |
9bf2f9e0af47
support empty PatientID in OrthancImport.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
135 tags['0020,000d'] if '0020,000d' in tags else '(empty)')) |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
136 print('') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
137 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
138 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
139 def UploadFile(path): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
140 with open(path, 'rb') as f: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
141 dicom = f.read() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
142 if args.verbose: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
143 print('Uploading: %s (%dMB)' % (path, len(dicom) / (1024 * 1024))) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
144 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
145 UploadBuffer(dicom) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
146 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
147 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
148 def UploadBzip2(path): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
149 with bz2.BZ2File(path, 'rb') as f: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
150 dicom = f.read() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
151 if args.verbose: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
152 print('Uploading: %s (%dMB)' % (path, len(dicom) / (1024 * 1024))) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
153 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
154 UploadBuffer(dicom) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
155 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
156 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
157 def UploadGzip(path): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
158 with gzip.open(path, 'rb') as f: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
159 dicom = f.read() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
160 if args.verbose: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
161 print('Uploading: %s (%dMB)' % (path, len(dicom) / (1024 * 1024))) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
162 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
163 UploadBuffer(dicom) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
164 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
165 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
166 def UploadTar(path, decoder): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
167 if args.verbose: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
168 print('Uncompressing tar archive: %s' % path) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
169 with tarfile.open(path, decoder) as tar: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
170 for item in tar: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
171 if item.isreg(): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
172 f = tar.extractfile(item) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
173 dicom = f.read() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
174 f.close() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
175 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
176 if args.verbose: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
177 print('Uploading: %s (%dMB)' % (item.name, len(dicom) / (1024 * 1024))) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
178 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
179 UploadBuffer(dicom) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
180 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
181 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
182 def UploadZip(path): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
183 if args.verbose: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
184 print('Uncompressing ZIP archive: %s' % path) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
185 with zipfile.ZipFile(path, 'r') as zip: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
186 for item in zip.infolist(): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
187 # WARNING - "item.is_dir()" would be better, but is not available in Python 2.7 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
188 if item.file_size > 0: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
189 dicom = zip.read(item.filename) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
190 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
191 if args.verbose: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
192 print('Uploading: %s (%dMB)' % (item.filename, len(dicom) / (1024 * 1024))) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
193 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
194 UploadBuffer(dicom) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
195 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
196 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
197 def DecodeFile(path): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
198 extension = os.path.splitext(path) [1] |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
199 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
200 if path.endswith('.tar.bz2'): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
201 UploadTar(path, 'r:bz2') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
202 elif path.endswith('.tar.gz'): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
203 UploadTar(path, 'r:gz') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
204 elif extension == '.zip': |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
205 UploadZip(path) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
206 elif extension == '.tar': |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
207 UploadTar(path, 'r') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
208 elif extension == '.bz2': |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
209 UploadBzip2(path) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
210 elif extension == '.gz': |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
211 UploadGzip(path) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
212 else: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
213 UploadFile(path) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
214 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
215 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
216 if args.clear: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
217 print('Removing the content of Orthanc') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
218 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
219 auth = HTTPBasicAuth(args.username, args.password) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
220 r = requests.get('%s/studies' % args.url, auth = auth) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
221 r.raise_for_status() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
222 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
223 print(' %d studies are being removed...' % len(r.json())) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
224 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
225 for study in r.json(): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
226 requests.delete('%s/studies/%s' % (args.url, study), auth = auth).raise_for_status() |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
227 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
228 print('Orthanc is now empty') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
229 print('') |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
230 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
231 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
232 for path in args.files: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
233 if os.path.isfile(path): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
234 DecodeFile(path) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
235 elif os.path.isdir(path): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
236 for root, dirs, files in os.walk(path): |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
237 for name in files: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
238 DecodeFile(os.path.join(root, name)) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
239 else: |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
240 raise Exception('Missing file or directory: %s' % path) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
241 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
242 |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
243 print('') |
4748
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
244 |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
245 if COUNT_ERROR == 0: |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
246 print('SUCCESS:') |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
247 else: |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
248 print('WARNING:') |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
249 |
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
250 print(' %d DICOM instances properly imported' % COUNT_DICOM) |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
251 print(' %d DICOM studies properly imported' % len(IMPORTED_STUDIES)) |
4748
4336642b8cff
ignore JSON files in sample Python upload scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4450
diff
changeset
|
252 print(' %d JSON files ignored' % COUNT_JSON) |
4337
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
253 print(' Error in %d files' % COUNT_ERROR) |
7707fa761b71
OrthancImport.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
254 print('') |