Mercurial > hg > orthanc
annotate Resources/Samples/Python/ChangesLoop.py @ 1340:0ad4773f28b3
new Python sample
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 03 Apr 2015 13:31:00 +0200 |
parents | 6e7e5ed91c2d |
children | b1291df2f780 |
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 |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1184
diff
changeset
|
4 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1184
diff
changeset
|
5 # Department, University Hospital of Liege, Belgium |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
6 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
7 # 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
|
8 # 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
|
9 # 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
|
10 # 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
|
11 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
12 # 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
|
13 # 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
|
14 # 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
|
15 # General Public License for more details. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
16 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
17 # 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
|
18 # 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
|
19 |
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 |
340 | 22 import time |
23 import sys | |
24 import RestToolbox | |
25 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
26 |
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 ## Print help message |
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 |
340 | 31 if len(sys.argv) != 3: |
32 print(""" | |
33 Sample script that continuously monitors the arrival of new DICOM | |
34 images into Orthanc (through the Changes API). | |
35 | |
36 Usage: %s [hostname] [HTTP port] | |
37 For instance: %s localhost 8042 | |
38 """ % (sys.argv[0], sys.argv[0])) | |
39 exit(-1) | |
40 | |
41 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2])) | |
42 | |
43 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
44 |
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 ## 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
|
47 ## received. |
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 |
340 | 50 def NewInstanceReceived(path): |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
51 global URL |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
52 patientName = RestToolbox.DoGet(URL + path + '/content/PatientName') |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
53 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
54 # Remove the possible trailing characters due to DICOM padding |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
55 patientName = patientName.strip() |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
56 |
1184 | 57 print('New instance received for patient "%s": "%s"' % (patientName, path)) |
340 | 58 |
59 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
60 |
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 ## Main loop that listens to the changes API. |
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 |
340 | 65 current = 0 |
66 while True: | |
67 r = RestToolbox.DoGet(URL + '/changes', { | |
68 'since' : current, | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
69 'limit' : 4 # Retrieve at most 4 changes at once |
340 | 70 }) |
71 | |
72 for change in r['Changes']: | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
73 # We are only interested interested in the arrival of new instances |
340 | 74 if change['ChangeType'] == 'NewInstance': |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
75 # Call the callback function |
340 | 76 path = change['Path'] |
77 NewInstanceReceived(path) | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
78 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
79 # Delete the instance once it has been discovered |
340 | 80 RestToolbox.DoDelete(URL + path) |
81 | |
82 current = r['Last'] | |
83 | |
84 if r['Done']: | |
1184 | 85 print('Everything has been processed: Waiting...') |
340 | 86 time.sleep(1) |