Package occi :: Module registry
[hide private]
[frames] | no frames]

Source Code for Module occi.registry

  1  # coding=utf-8 
  2  # 
  3  # Copyright (C) 2010-2012 Platform Computing 
  4  # 
  5  # This library is free software; you can redistribute it and/or 
  6  # modify it under the terms of the GNU Lesser General Public 
  7  # License as published by the Free Software Foundation; either 
  8  # version 2.1 of the License, or (at your option) any later version. 
  9  # 
 10  # This library is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 13  # Lesser General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU Lesser General Public 
 16  # License along with this library; if not, write to the Free Software 
 17  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA 
 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  # disabling 'Abstract class only ref. once' pylint check (designed for ext.) 
 29  # disabling 'Unsued argument' pylint check (is there to be overwritten) 
 30  # disabling 'Method could be function' pylint check (see above) 
 31  # pylint: disable=R0922,W0613,R0201 
 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   
38 -class Registry(object):
39 ''' 40 Abstract class so users can implement registries themselves. 41 ''' 42 43 hostname = '' 44 45 default_mime_type = 'text/plain' 46
47 - def get_hostname(self):
48 ''' 49 Returns the hostname of the service. 50 ''' 51 return self.hostname
52
53 - def set_hostname(self, hostname):
54 ''' 55 Set the hostname of the service. 56 ''' 57 self.hostname = hostname
58
59 - def get_default_type(self):
60 ''' 61 Returns the default mime type. 62 ''' 63 return self.default_mime_type
64
65 - def get_renderer(self, mime_type):
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
74 - def set_renderer(self, mime_type, renderer):
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
84 - def get_backend(self, category, extras):
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
94 - def get_all_backends(self, entity, extras):
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
104 - def set_backend(self, category, backend, extras):
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
115 - def delete_mixin(self, mixin, extras):
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
125 - def get_category(self, path, extras):
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
135 - def get_categories(self, extras):
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
144 - def get_resource(self, key, extras):
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
154 - def add_resource(self, key, entity, extras):
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
165 - def delete_resource(self, key, extras):
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
175 - def get_resource_keys(self, extras):
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
184 - def get_resources(self, extras):
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
193 - def get_extras(self, extras):
194 ''' 195 Will return what goes into the extras attribute of the entity and 196 category. 197 ''' 198 return None
199 200
201 -class NonePersistentRegistry(Registry):
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
212 - def __init__(self):
213 self.backends = {} 214 self.renderings = {} 215 self.resources = {} 216 self.host = '' 217 super(NonePersistentRegistry, self).__init__()
218
219 - def get_renderer(self, mime_type):
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
240 - def set_renderer(self, mime_type, renderer):
241 if not isinstance(renderer, Rendering): 242 raise AttributeError('renderer needs to derive from Rendering.') 243 self.renderings[mime_type] = renderer
244
245 - def get_backend(self, category, extras):
246 # no need to check - a get_categories or get_categroy will be called 247 # first. 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
260 - def get_all_backends(self, entity, extras):
261 res = [self.get_backend(entity.kind, extras)] 262 for mixin in entity.mixins: 263 res.append(self.get_backend(mixin, extras)) 264 # remove duplicates - only need to call backs once - right? 265 return list(set(res))
266
267 - def set_backend(self, category, backend, extras):
268 if extras is not None: 269 # category belongs to single user... 270 category.extras = self.get_extras(extras) 271 self.backends[category] = backend
272
273 - def delete_mixin(self, mixin, extras):
274 # no need to check because in get_category in renderer it is assured 275 # that the user only sees own. Will get not found if he tries to delete 276 # mixin from other user. 277 self.backends.pop(mixin)
278
279 - def get_category(self, path, extras):
280 # no need for ownership check - paths cannot overlap! 281 for category in self.backends.keys(): 282 if category.location == path: 283 return category 284 return None
285
286 - def get_categories(self, extras):
287 result = [] 288 for item in self.backends.keys(): 289 if item.extras is None: 290 # categories visible to all! 291 result.append(item) 292 elif extras is not None and self.get_extras(extras) == item.extras: 293 # categories visible to this user! 294 result.append(item) 295 return result
296
297 - def get_resource(self, key, extras):
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
303 - def add_resource(self, key, resource, extras):
304 if extras is not None: 305 resource.extras = self.get_extras(extras) 306 self.resources[key] = resource
307
308 - def delete_resource(self, key, extras):
309 # get_resources and get_resource is called before this - no need for 310 # ownership checking. 311 self.resources.pop(key)
312
313 - def get_resource_keys(self, extras):
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
323 - def get_resources(self, extras):
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