comparison Tests/Toolbox.py @ 13:7b69a561f4d3

cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 18 Jun 2015 16:22:44 +0200
parents 5f73008bb873
children 2a29bcff60a7
comparison
equal deleted inserted replaced
12:15c166240dfb 13:7b69a561f4d3
31 31
32 from PIL import Image 32 from PIL import Image
33 from urllib import urlencode 33 from urllib import urlencode
34 34
35 35
36 HERE = os.path.dirname(__file__)
37
38
39 # http://stackoverflow.com/a/1313868/881731 36 # http://stackoverflow.com/a/1313868/881731
40 try: 37 try:
41 from cStringIO import StringIO 38 from cStringIO import StringIO
42 except: 39 except:
43 from StringIO import StringIO 40 from StringIO import StringIO
44 41
45 42
46 def DefineOrthanc(url = 'http://localhost:8042', 43 def DefineOrthanc(server = 'localhost',
44 restPort = 8042,
47 username = None, 45 username = None,
48 password = None, 46 password = None,
49 aet = 'ORTHANC', 47 aet = 'ORTHANC',
50 dicomPort = 4242): 48 dicomPort = 4242):
51 m = re.match(r'(http|https)://([^:]+):([^@]+)@([^@]+)', url) 49 #m = re.match(r'(http|https)://([^:]+):([^@]+)@([^@]+)', url)
52 if m != None: 50 #if m != None:
53 url = m.groups()[0] + '://' + m.groups()[3] 51 # url = m.groups()[0] + '://' + m.groups()[3]
54 username = m.groups()[1] 52 # username = m.groups()[1]
55 password = m.groups()[2] 53 # password = m.groups()[2]
56 54
57 if not url.endswith('/'): 55 #if not url.endswith('/'):
58 url += '/' 56 # url += '/'
59 57
60 return { 58 return {
61 'Url' : url, 59 'Server' : server,
60 'Url' : 'http://%s:%d/' % (server, restPort),
62 'Username' : username, 61 'Username' : username,
63 'Password' : password, 62 'Password' : password,
64 'DicomAet' : aet, 63 'DicomAet' : aet,
65 'DicomPort' : dicomPort 64 'DicomPort' : dicomPort
66 } 65 }
132 return _DoPutOrPost(orthanc, uri, 'PUT', data, contentType, {}) 131 return _DoPutOrPost(orthanc, uri, 'PUT', data, contentType, {})
133 132
134 def DoPost(orthanc, uri, data = {}, contentType = '', headers = {}): 133 def DoPost(orthanc, uri, data = {}, contentType = '', headers = {}):
135 return _DoPutOrPost(orthanc, uri, 'POST', data, contentType, headers) 134 return _DoPutOrPost(orthanc, uri, 'POST', data, contentType, headers)
136 135
136 def GetDatabasePath(filename):
137 return os.path.join(os.path.dirname(__file__), '..', 'Database', filename)
138
137 def UploadInstance(orthanc, filename): 139 def UploadInstance(orthanc, filename):
138 global HERE 140 f = open(GetDatabasePath(filename), 'rb')
139 p = os.path.join(HERE, '..', 'Database', filename)
140 f = open(p, 'rb')
141 d = f.read() 141 d = f.read()
142 f.close() 142 f.close()
143 return DoPost(orthanc, '/instances', d, 'application/dicom') 143 return DoPost(orthanc, '/instances', d, 'application/dicom')
144 144
145 def UploadFolder(orthanc, path): 145 def UploadFolder(orthanc, path):
146 global HERE 146 for i in os.listdir(GetDatabasePath(path)):
147 p = os.path.join(HERE, '..', 'Database', path)
148 for i in os.listdir(p):
149 try: 147 try:
150 UploadInstance(orthanc, os.path.join(path, i)) 148 UploadInstance(orthanc, os.path.join(path, i))
151 except: 149 except:
152 pass 150 pass
153 151