Mercurial > hg > orthanc
annotate Resources/Samples/Python/ChangesLoop.py @ 445:7816aaa5db17
merge
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 17 May 2013 17:32:16 +0200 |
parents | b51c67f28b33 |
children | 44382c8bcd15 |
rev | line source |
---|---|
340 | 1 #!/usr/bin/python |
2 | |
3 import time | |
4 import sys | |
5 import RestToolbox | |
6 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
7 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
8 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
9 ## Print help message |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
10 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
11 |
340 | 12 if len(sys.argv) != 3: |
13 print(""" | |
14 Sample script that continuously monitors the arrival of new DICOM | |
15 images into Orthanc (through the Changes API). | |
16 | |
17 Usage: %s [hostname] [HTTP port] | |
18 For instance: %s localhost 8042 | |
19 """ % (sys.argv[0], sys.argv[0])) | |
20 exit(-1) | |
21 | |
22 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2])) | |
23 | |
24 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
25 |
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 ## 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
|
28 ## received. |
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 def NewInstanceReceived(path): |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
32 global URL |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
33 patientName = RestToolbox.DoGet(URL + path + '/content/PatientName') |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
34 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
35 # Remove the possible trailing characters due to DICOM padding |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
36 patientName = patientName.strip() |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
37 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
38 print 'New instance received for patient "%s": "%s"' % (patientName, path) |
340 | 39 |
40 | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
41 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
42 ## |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
43 ## Main loop that listens to the changes API. |
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 |
340 | 46 current = 0 |
47 while True: | |
48 r = RestToolbox.DoGet(URL + '/changes', { | |
49 'since' : current, | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
50 'limit' : 4 # Retrieve at most 4 changes at once |
340 | 51 }) |
52 | |
53 for change in r['Changes']: | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
54 # We are only interested interested in the arrival of new instances |
340 | 55 if change['ChangeType'] == 'NewInstance': |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
56 # Call the callback function |
340 | 57 path = change['Path'] |
58 NewInstanceReceived(path) | |
341
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
59 |
b51c67f28b33
documentation of the sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
340
diff
changeset
|
60 # Delete the instance once it has been discovered |
340 | 61 RestToolbox.DoDelete(URL + path) |
62 | |
63 current = r['Last'] | |
64 | |
65 if r['Done']: | |
66 print "Everything has been processed: Waiting..." | |
67 time.sleep(1) |