Mercurial > hg > orthanc
annotate Resources/Samples/Python/ChangesLoop.py @ 2575:a874bdbbfa11 jobs
news
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 09 May 2018 17:57:21 +0200 |
parents | 878b59270859 |
children | 4e43e67f8ecf |
rev | line source |
---|---|
340 | 1 #!/usr/bin/python |
2 | |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
3 # Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1184
diff
changeset
|
5 # Department, University Hospital of Liege, Belgium |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
6 # Copyright (C) 2017-2018 Osimis S.A., Belgium |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
7 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
8 # This program is free software: you can redistribute it and/or |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
9 # modify it under the terms of the GNU General Public License as |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
10 # published by the Free Software Foundation, either version 3 of the |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
11 # License, or (at your option) any later version. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
12 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
13 # This program is distributed in the hope that it will be useful, but |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
14 # WITHOUT ANY WARRANTY; without even the implied warranty of |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
16 # General Public License for more details. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
17 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
18 # You should have received a copy of the GNU General Public License |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
20 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
21 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
22 |
340 | 23 import time |
24 import sys | |
25 import RestToolbox | |
26 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
27 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
28 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
29 ## Print help message |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
30 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
31 |
340 | 32 if len(sys.argv) != 3: |
33 print(""" | |
34 Sample script that continuously monitors the arrival of new DICOM | |
35 images into Orthanc (through the Changes API). | |
36 | |
37 Usage: %s [hostname] [HTTP port] | |
2032
65b1ce7cb84f
Replaced "localhost" by "127.0.0.1", as it might impact performance on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
38 For instance: %s 127.0.0.1 8042 |
340 | 39 """ % (sys.argv[0], sys.argv[0])) |
40 exit(-1) | |
41 | |
42 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2])) | |
43 | |
44 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
45 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
46 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
47 ## The following function is called each time a new instance is |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
48 ## received. |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
49 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
50 |
340 | 51 def NewInstanceReceived(path): |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
52 global URL |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
53 patientName = RestToolbox.DoGet(URL + path + '/content/PatientName') |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
54 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
55 # Remove the possible trailing characters due to DICOM padding |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
56 patientName = patientName.strip() |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
57 |
1184 | 58 print('New instance received for patient "%s": "%s"' % (patientName, path)) |
340 | 59 |
60 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
61 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
62 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
63 ## Main loop that listens to the changes API. |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
64 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
65 |
340 | 66 current = 0 |
67 while True: | |
68 r = RestToolbox.DoGet(URL + '/changes', { | |
69 'since' : current, | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
70 'limit' : 4 # Retrieve at most 4 changes at once |
340 | 71 }) |
72 | |
73 for change in r['Changes']: | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
74 # We are only interested interested in the arrival of new instances |
340 | 75 if change['ChangeType'] == 'NewInstance': |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
76 # Call the callback function |
340 | 77 path = change['Path'] |
78 NewInstanceReceived(path) | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
79 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
80 # Delete the instance once it has been discovered |
340 | 81 RestToolbox.DoDelete(URL + path) |
82 | |
83 current = r['Last'] | |
84 | |
85 if r['Done']: | |
1184 | 86 print('Everything has been processed: Waiting...') |
340 | 87 time.sleep(1) |