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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 import time
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 import sys
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 import RestToolbox
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 if len(sys.argv) != 3:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 print("""
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 Sample script that continuously monitors the arrival of new DICOM
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 images into Orthanc (through the Changes API).
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 Usage: %s [hostname] [HTTP port]
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 For instance: %s localhost 8042
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 """ % (sys.argv[0], sys.argv[0]))
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 exit(-1)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2]))
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 current = 0
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 while True:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 r = RestToolbox.DoGet(URL + '/changes', {
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 })
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 path = change['Path']
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 RestToolbox.DoDelete(URL + path)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 current = r['Last']
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 if r['Done']:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 print "Everything has been processed: Waiting..."
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 time.sleep(1)