Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/ChangesLoop.py @ 5854:65f8c6dfba50 find-refactoring tip
cleanup
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Mon, 04 Nov 2024 17:51:46 +0100 |
parents | f7adfb22e20e |
children |
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 |
5640
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
6 # Copyright (C) 2017-2023 Osimis S.A., Belgium |
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
7 # Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
8 # Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
9 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
10 # 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
|
11 # 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
|
12 # 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
|
13 # 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
|
14 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
15 # 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
|
16 # 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
|
17 # 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
|
18 # General Public License for more details. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
19 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
20 # 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
|
21 # 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
|
22 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
23 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
24 |
340 | 25 import time |
26 import sys | |
27 import RestToolbox | |
28 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
29 |
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 ## Print help message |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
32 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
33 |
340 | 34 if len(sys.argv) != 3: |
35 print(""" | |
36 Sample script that continuously monitors the arrival of new DICOM | |
37 images into Orthanc (through the Changes API). | |
38 | |
39 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
|
40 For instance: %s 127.0.0.1 8042 |
340 | 41 """ % (sys.argv[0], sys.argv[0])) |
42 exit(-1) | |
43 | |
44 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2])) | |
45 | |
46 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
47 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
48 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
49 ## 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
|
50 ## received. |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
51 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
52 |
340 | 53 def NewInstanceReceived(path): |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
54 global URL |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
55 patientName = RestToolbox.DoGet(URL + path + '/content/PatientName') |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
56 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
57 # Remove the possible trailing characters due to DICOM padding |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
58 patientName = patientName.strip() |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
59 |
1184 | 60 print('New instance received for patient "%s": "%s"' % (patientName, path)) |
340 | 61 |
62 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
63 |
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 ## Main loop that listens to the changes API. |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
66 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
67 |
340 | 68 current = 0 |
69 while True: | |
70 r = RestToolbox.DoGet(URL + '/changes', { | |
71 'since' : current, | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
72 'limit' : 4 # Retrieve at most 4 changes at once |
340 | 73 }) |
74 | |
75 for change in r['Changes']: | |
3340 | 76 # We are only interested in the arrival of new instances |
340 | 77 if change['ChangeType'] == 'NewInstance': |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
78 # Call the callback function |
340 | 79 path = change['Path'] |
80 NewInstanceReceived(path) | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
81 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
82 # Delete the instance once it has been discovered |
340 | 83 RestToolbox.DoDelete(URL + path) |
84 | |
85 current = r['Last'] | |
86 | |
87 if r['Done']: | |
1184 | 88 print('Everything has been processed: Waiting...') |
340 | 89 time.sleep(1) |