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

Source Code for Module occi.protocol.json_rendering

  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  '''
 
 21  JSON based rendering.
 
 22  
 
 23  Created on 01.02.2012
 
 24  
 
 25  @author: tmetsch
 
 26  ''' 
 27  
 
 28  # L8R: check if this can be move partly to occi_rendering (once standardized)
 
 29  # and rename to a parser class.
 
 30  
 
 31  # disabling 'Method is abstract' pylint check (currently only support GETs!)
 
 32  # pylint: disable=W0223
 
 33  
 
 34  from occi.core_model import Resource 
 35  from occi.handlers import CONTENT_TYPE 
 36  from occi.protocol.rendering import Rendering 
 37  import json 
 38  
 
 39  
 
40 -def _from_category(category):
41 ''' 42 Create a JSON struct for a category. 43 ''' 44 data = {'term': category.term, 'scheme': category.scheme} 45 if hasattr(category, 'title') and category.title is not '': 46 data['title'] = category.title 47 if hasattr(category, 'related') and len(category.related) > 0: 48 rel_list = [] 49 for item in category.related: 50 rel_list.append(str(item)) 51 data['related'] = rel_list 52 if hasattr(category, 'location') and category.location is not None: 53 data['location'] = category.location 54 if hasattr(category, 'attributes') and len(category.attributes) >= 1: 55 attr_list = {} 56 for item in category.attributes: 57 if category.attributes[item] == 'required': 58 attr_list[item] = 'required' 59 elif category.attributes[item] == 'immutable': 60 attr_list[item] = 'immutable' 61 else: 62 attr_list[item] = 'muttable' 63 data['attributes'] = attr_list 64 if hasattr(category, 'actions') and len(category.actions) > 0: 65 action_list = [] 66 for item in category.actions: 67 action_list.append(str(item)) 68 data['actions'] = action_list 69 return data
70 71
72 -def _from_entity(entity):
73 ''' 74 Create a JSON struct for an entity. 75 ''' 76 data = {'kind': _from_category(entity.kind)} 77 # kind 78 79 # mixins 80 mixins = [] 81 for mixin in entity.mixins: 82 tmp = _from_category(mixin) 83 mixins.append(tmp) 84 data['mixins'] = mixins 85 86 # actions 87 actions = [] 88 for action in entity.actions: 89 tmp = {'kind': _from_category(action), 'link': entity.identifier + 90 '?action=' + action.term} 91 actions.append(tmp) 92 data['actions'] = actions 93 94 # links 95 if isinstance(entity, Resource): 96 links = [] 97 for link in entity.links: 98 tmp = _from_entity(link) 99 tmp['source'] = link.source.identifier 100 tmp['target'] = link.target.identifier 101 links.append(tmp) 102 data['links'] = links 103 104 # attributes 105 attr = {} 106 for attribute in entity.attributes: 107 attr[attribute] = entity.attributes[attribute] 108 data['attributes'] = attr 109 110 return data
111 112
113 -class JsonRendering(Rendering):
114 ''' 115 This is a rendering which will use the HTTP header to place the information 116 in an syntax and semantics as defined in the OCCI specification. 117 ''' 118 119 mime_type = 'application/occi+json' 120
121 - def from_entity(self, entity):
122 data = _from_entity(entity) 123 124 body = json.dumps(data, sort_keys=True, indent=2) 125 return {CONTENT_TYPE: self.mime_type}, body
126
127 - def from_entities(self, entities, key):
128 data = [] 129 for item in entities: 130 data.append(_from_entity(item)) 131 132 body = json.dumps(data, sort_keys=True, indent=2) 133 return {CONTENT_TYPE: self.mime_type}, body
134
135 - def from_categories(self, categories):
136 data = [] 137 for item in categories: 138 data.append(_from_category(item)) 139 140 body = json.dumps(data, sort_keys=True, indent=2) 141 return {CONTENT_TYPE: self.mime_type}, body
142