Package occi :: Package protocol :: Module occi_parser
[hide private]
[frames] | no frames]

Source Code for Module occi.protocol.occi_parser

  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  Set of functions to parse stuff. 
 21   
 22  Created on Jun 28, 2011 
 23   
 24  @author: tmetsch 
 25  ''' 
 26   
 27  # disabling 'Too many local variables' pylint check (text renderings :-() 
 28  # disabling 'Too many branches' pylint check (text renderings :-() 
 29  # pylint: disable=R0914,R0912 
 30   
 31  from occi.core_model import Category, Link, Mixin, Kind 
 32   
 33  #============================================================================== 
 34  # Following are text/occi and text/plain related parsing functions. 
 35  #============================================================================== 
 36   
 37   
38 -def get_category(category_string, registry, extras, is_mixin=False):
39 ''' 40 Create a Category from a string rendering. 41 42 If found it will return the object from the registry. 43 44 If is_mixin is set to true it will not match with the registry and just 45 return a Mixin. 46 47 category_string -- A string rendering of a category. 48 registry -- To generate a list of registered categories. 49 extras -- The passed on extras argument 50 is_mixin -- Mixin will be created and no matching will be done. 51 ''' 52 categories = registry.get_categories(extras) 53 # find term 54 term = category_string[:category_string.find(';')].strip() 55 56 # find scheme 57 scheme = find_in_string(category_string, 'scheme') 58 59 if is_mixin: 60 location = find_in_string(category_string, 'location') 61 if not location[-1] == '/': 62 raise AttributeError('Illegal location; must end with /') 63 if location[0] != '/' and location.find('http') != 0: 64 raise AttributeError('Illegal location; Either provide full URL' 65 ' or just a path starting with /.') 66 mixin = Mixin(scheme, term, location=location) 67 mixin.extras = registry.get_extras(extras) 68 69 try: 70 related = find_in_string(category_string, 'rel') 71 except AttributeError: 72 return mixin 73 else: 74 for item in categories: 75 if str(item) == related: 76 mixin.related = [item] 77 return mixin 78 raise AttributeError('Related category cannot be found.') 79 80 # return the category from registry... 81 tmp = Category(scheme, term, '', {}, '') 82 if extras is not None: 83 tmp.extras = registry.get_extras(extras) 84 for item in categories: 85 if item.extras is None: 86 tmp.extras = None 87 if tmp == item: 88 del tmp 89 return item 90 tmp.extras = registry.get_extras(extras) 91 elif item.extras is not None: 92 if tmp == item: 93 del tmp 94 return item 95 raise AttributeError('The following category is not registered within' 96 + ' this service (See Query interfaces): ' 97 + str(scheme) + str(term))
98 99
100 -def get_category_str(category, registry):
101 ''' 102 Create a string rendering for a Category. 103 104 category -- A category. 105 registry -- registry to retrieve hostname. 106 ''' 107 108 tmp = '' 109 tmp += category.term 110 tmp += '; scheme="' + category.scheme + '"' 111 tmp += '; class="' + repr(category) + '"' 112 if hasattr(category, 'title') and category.title is not '': 113 tmp += '; title="' + category.title + '"' 114 if hasattr(category, 'related') and len(category.related) > 0: 115 rel_list = [] 116 for item in category.related: 117 rel_list.append(str(item)) 118 tmp += '; rel="' + ' '.join(rel_list) + '"' 119 if hasattr(category, 'location') and category.location is not None: 120 tmp += '; location="' 121 if category.location.find('http') == -1: 122 tmp += registry.get_hostname() 123 tmp += category.location + '"' 124 if hasattr(category, 'attributes') and len(category.attributes) > 0: 125 attr_list = [] 126 for item in category.attributes: 127 if category.attributes[item] == 'required': 128 attr_list.append(item + '{required}') 129 elif category.attributes[item] == 'immutable': 130 attr_list.append(item + '{immutable}') 131 else: 132 attr_list.append(item) 133 tmp += '; attributes="' + ' '.join(attr_list) + '"' 134 if hasattr(category, 'actions') and len(category.actions) > 0: 135 action_list = [] 136 for item in category.actions: 137 action_list.append(str(item)) 138 tmp += '; actions="' + ' '.join(action_list) + '"' 139 return tmp
140 141 163 164 214 215 244 245
246 -def get_attributes(attribute_string):
247 ''' 248 Retrieve the attributes from the HTTP X-OCCI-Attribute rendering. 249 ''' 250 tmp = _strip_all(attribute_string) 251 if tmp.find('=') == -1: 252 raise AttributeError('Mailformed Attribute description!') 253 key = _strip_all(tmp[:tmp.find('=')]) 254 value = tmp[tmp.find('=') + 1:] 255 if value.find('"') is not -1: 256 value = _strip_all(value) 257 258 return key, value
259 260 #============================================================================== 261 # Helpers 262 #============================================================================== 263 264
265 -def _strip_all(string):
266 ''' 267 Removes beginning / ending quotes and whitespaces. 268 ''' 269 return string.lstrip().lstrip('"').rstrip().rstrip('"')
270 271
272 -def find_in_string(string, name):
273 ''' 274 Search for string which is surrounded by '<name>=' and ';'. Raises 275 AttributeError if value cannot be found. 276 277 string -- The string to look into. 278 name -- The name of the value to look for. 279 ''' 280 begin = string.find(name + '=') 281 end = string.find(';', begin) 282 result = string[begin + len(name) + 1:end].rstrip('"').lstrip('"').strip() 283 if begin == -1: 284 raise AttributeError('Could not determine the value for: ' + name) 285 return result
286