comparison Resources/Samples/Python/ChangesLoop.py @ 341:b51c67f28b33

documentation of the sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 17 Jan 2013 16:01:58 +0100
parents 61f6a3d66b85
children 44382c8bcd15
comparison
equal deleted inserted replaced
340:61f6a3d66b85 341:b51c67f28b33
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import time 3 import time
4 import sys 4 import sys
5 import RestToolbox 5 import RestToolbox
6
7
8 ##
9 ## Print help message
10 ##
6 11
7 if len(sys.argv) != 3: 12 if len(sys.argv) != 3:
8 print(""" 13 print("""
9 Sample script that continuously monitors the arrival of new DICOM 14 Sample script that continuously monitors the arrival of new DICOM
10 images into Orthanc (through the Changes API). 15 images into Orthanc (through the Changes API).
15 exit(-1) 20 exit(-1)
16 21
17 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2])) 22 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2]))
18 23
19 24
25
26 ##
27 ## The following function is called each time a new instance is
28 ## received.
29 ##
30
20 def NewInstanceReceived(path): 31 def NewInstanceReceived(path):
21 print 'New instance received: "%s"' % path 32 global URL
33 patientName = RestToolbox.DoGet(URL + path + '/content/PatientName')
34
35 # Remove the possible trailing characters due to DICOM padding
36 patientName = patientName.strip()
37
38 print 'New instance received for patient "%s": "%s"' % (patientName, path)
22 39
23 40
24 URL = 'http://localhost:8042' 41
42 ##
43 ## Main loop that listens to the changes API.
44 ##
45
25 current = 0 46 current = 0
26 while True: 47 while True:
27 r = RestToolbox.DoGet(URL + '/changes', { 48 r = RestToolbox.DoGet(URL + '/changes', {
28 'since' : current, 49 'since' : current,
29 'limit' : 4 50 'limit' : 4 # Retrieve at most 4 changes at once
30 }) 51 })
31 52
32 for change in r['Changes']: 53 for change in r['Changes']:
54 # We are only interested interested in the arrival of new instances
33 if change['ChangeType'] == 'NewInstance': 55 if change['ChangeType'] == 'NewInstance':
56 # Call the callback function
34 path = change['Path'] 57 path = change['Path']
35 NewInstanceReceived(path) 58 NewInstanceReceived(path)
59
60 # Delete the instance once it has been discovered
36 RestToolbox.DoDelete(URL + path) 61 RestToolbox.DoDelete(URL + path)
37 62
38 current = r['Last'] 63 current = r['Last']
39 64
40 if r['Done']: 65 if r['Done']: