1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 '''
20 Module which holds a abstract registry definition class and one simple
21 implementation.
22
23 Created on Aug 22, 2011
24
25 @author: tmetsch
26 '''
27
28
29
30
31
32
33 from occi.backend import KindBackend, ActionBackend, MixinBackend
34 from occi.exceptions import HTTPError
35 from occi.protocol.occi_rendering import Rendering
36
37
39 '''
40 Abstract class so users can implement registries themselves.
41 '''
42
43 hostname = ''
44
45 default_mime_type = 'text/plain'
46
48 '''
49 Returns the hostname of the service.
50 '''
51 return self.hostname
52
58
64
66 '''
67 Retrieve a rendering for a given mime type.
68
69 mime_type -- The mime type you a looking for.
70 '''
71 raise NotImplementedError('Registry implementation seems to be'
72 ' incomplete.')
73
75 '''
76 Retrieve a rendering for a given mime type.
77
78 mime_type -- The mime type you want to add a rendering for.
79 renderer -- Instance of an Rendering class.
80 '''
81 raise NotImplementedError('Registry implementation seems to be'
82 ' incomplete.')
83
85 """
86 Retrieve a backend which is able to deal with the given category.
87
88 category -- The category a backend is needed for.
89 extras -- Extras object - same as the one passed on to the backends.
90 """
91 raise NotImplementedError('Registry implementation seems to be'
92 ' incomplete.')
93
95 """
96 Retrieve all backends associated with a resource instance
97
98 entity -- The resource instance.
99 extras -- Extras object - same as the one passed on to the backends.
100 """
101 raise NotImplementedError('Registry implementation seems to be'
102 ' incomplete.')
103
105 """
106 Set a backend which is able to deal with the given category.
107
108 category -- The category a backend is needed for.
109 backend -- The backend which should handle this category.
110 extras -- Extras object - same as the one passed on to the backends.
111 """
112 raise NotImplementedError('Registry implementation seems to be'
113 ' incomplete.')
114
116 '''
117 Remove a mixin from the service.
118
119 mixin -- The mixin
120 extras -- Extras object - same as the one passed on to the backends.
121 '''
122 raise NotImplementedError('Registry implementation seems to be'
123 ' incomplete.')
124
126 '''
127 Return the category which is associated with an Location.
128
129 path -- The location which the category should define.
130 extras -- Extras object - same as the one passed on to the backends.
131 '''
132 raise NotImplementedError('Registry implementation seems to be'
133 ' incomplete.')
134
136 '''
137 Return all registered categories.
138
139 extras -- Extras object - same as the one passed on to the backends.
140 '''
141 raise NotImplementedError('Registry implementation seems to be'
142 ' incomplete.')
143
145 '''
146 Return a certain resource.
147
148 key -- Unique identifier of the resource.
149 extras -- Extras object - same as the one passed on to the backends.
150 '''
151 raise NotImplementedError('Registry implementation seems to be'
152 ' incomplete.')
153
155 '''
156 Add a resource.
157
158 key -- the unique identifier.
159 entity -- the OCCI representation.
160 extras -- Extras object - same as the one passed on to the backends.
161 '''
162 raise NotImplementedError('Registry implementation seems to be'
163 ' incomplete.')
164
166 '''
167 Delete a resource.
168
169 key -- Unique identifier of the resource.
170 extras -- Extras object - same as the one passed on to the backends.
171 '''
172 raise NotImplementedError('Registry implementation seems to be'
173 ' incomplete.')
174
176 '''
177 Return all keys of all resources.
178
179 extras -- Extras object - same as the one passed on to the backends.
180 '''
181 raise NotImplementedError('Registry implementation seems to be'
182 ' incomplete.')
183
185 '''
186 Return all resources.
187
188 extras -- Extras object - same as the one passed on to the backends.
189 '''
190 raise NotImplementedError('Registry implementation seems to be'
191 ' incomplete.')
192
194 '''
195 Will return what goes into the extras attribute of the entity and
196 category.
197 '''
198 return None
199
200
202 '''
203 None optimized/persistent registry for the OCCI service.
204
205 It is encouraged to write an own registry with e.g. DB lookups.
206
207 Created on Jun 28, 2011
208
209 @author: tmetsch
210 '''
211
218
220 parser = None
221
222 for tmp in mime_type.split(','):
223 type_str = tmp.strip()
224 if type_str.find(';q=') > -1:
225 type_str = type_str[:type_str.find(';q=')]
226
227 if type_str in self.renderings:
228 parser = self.renderings[type_str]
229 break
230 elif type_str == '*/*':
231 parser = self.renderings[self.get_default_type()]
232 break
233
234 if parser is None:
235 raise HTTPError(406, 'This service is unable to understand the ' +
236 ' mime type: ' + repr(mime_type))
237 else:
238 return parser
239
241 if not isinstance(renderer, Rendering):
242 raise AttributeError('renderer needs to derive from Rendering.')
243 self.renderings[mime_type] = renderer
244
246
247
248 try:
249 back = self.backends[category]
250 if repr(category) == 'kind' and isinstance(back, KindBackend):
251 return back
252 if repr(category) == 'action' and isinstance(back,
253 ActionBackend):
254 return back
255 if repr(category) == 'mixin' and isinstance(back, MixinBackend):
256 return back
257 except KeyError:
258 raise AttributeError('Cannot find corresponding Backend.')
259
261 res = [self.get_backend(entity.kind, extras)]
262 for mixin in entity.mixins:
263 res.append(self.get_backend(mixin, extras))
264
265 return list(set(res))
266
268 if extras is not None:
269
270 category.extras = self.get_extras(extras)
271 self.backends[category] = backend
272
274
275
276
277 self.backends.pop(mixin)
278
280
281 for category in self.backends.keys():
282 if category.location == path:
283 return category
284 return None
285
287 result = []
288 for item in self.backends.keys():
289 if item.extras is None:
290
291 result.append(item)
292 elif extras is not None and self.get_extras(extras) == item.extras:
293
294 result.append(item)
295 return result
296
298 if self.resources[key].extras is not None and self.resources[key]\
299 .extras != self.get_extras(extras):
300 raise KeyError
301 return self.resources[key]
302
304 if extras is not None:
305 resource.extras = self.get_extras(extras)
306 self.resources[key] = resource
307
309
310
311 self.resources.pop(key)
312
314 result = []
315 for key in self.resources.keys():
316 if self.resources[key].extras is None:
317 result.append(key)
318 elif self.resources[key].extras is not None and \
319 self.resources[key].extras == self.get_extras(extras):
320 result.append(key)
321 return result
322
324 result = []
325 for item in self.resources.values():
326 if item.extras is None:
327 result.append(item)
328 elif item.extras is not None and \
329 item.extras == self.get_extras(extras):
330 result.append(item)
331 return result
332