Example #1
0
    def __init__(self):
        self._categories = OrderedDict()
        self._locations = {}

        # Always register OCCI Core types
        self.register(EntityKind)
        self.register(ResourceKind)
        self.register(LinkKind)
Example #2
0
    def __init__(self,
                 term,
                 scheme,
                 title=None,
                 related=None,
                 attributes=None,
                 defaults=None):
        self.term = term
        self.scheme = scheme
        self.title = title
        self.related = related
        self.attributes = OrderedDict()
        self.unique_attributes = OrderedDict()
        self.defaults = defaults or OrderedDict()

        # Attribute definitions
        if related:
            self.attributes = related.attributes.copy()
        for attr in attributes or ():
            self.unique_attributes[attr.name] = attr
        self.attributes.update(self.unique_attributes)
Example #3
0
    def _json_obj(self, obj):
        """Render a `DataObject` into a JSON-friendly dictionary structure.
        """
        json_obj = OrderedDict()
        json_obj['kind'] = None
        json_obj['kinds'] = []
        json_obj['mixins'] = []
        json_obj['categories'] = []
        json_obj['actions'] = []
        json_obj['links'] = []
        json_obj['attributes'] = OrderedDict()

        # Categories
        for category in obj.categories:
            d = OrderedDict()
            d['term'] = category.term
            d['scheme'] = category.scheme

            d['title'] = category.title
            if category.related:
                d['related'] = str(category.related)
            if category.attributes:
                attr_defs = OrderedDict()
                for attr in category.unique_attributes.itervalues():
                    attr_props = OrderedDict()
                    attr_props['mutable'] = attr.mutable
                    attr_props['required'] = attr.required
                    attr_props['type'] = attr.type_name
                    attr_defs[attr.name] = attr_props
                d['attributes'] = attr_defs
            if category.defaults:
                d['defaults'] = category.defaults
            if hasattr(category, 'actions') and category.actions:
                d['actions'] = [str(cat) for cat in category.actions]
            if hasattr(category, 'location') and category.location:
                d['location'] = obj.translator.url_build(category.location,
                                                         path_only=True)

            cat_class = category.__class__.__name__.lower()
            if cat_class == 'kind':
                json_obj['kinds'].append(d)
            elif cat_class == 'mixin':
                json_obj['mixins'].append(d)
            else:
                json_obj['categories'].append(d)

        # Links
        for link in obj.links:
            d = OrderedDict()
            if link.target_title:
                d['title'] = link.target_title
            d['target_uri'] = link.target_location
            d['target_type'] = [str(cat) for cat in link.target_categories]
            if link.link_location:
                d['link_uri'] = link.link_location
            if link.link_categories:
                d['link_type'] = [str(cat) for cat in link.link_categories]
            if link.link_attributes:
                attrs = OrderedDict()
                for name, value in link.link_attributes:
                    attrs[name] = value
                d['attributes'] = attrs
            json_obj['links'].append(d)

        # Actions
        for action in obj.actions:
            d = OrderedDict()
            if action.target_title:
                d['title'] = action.target_title
            d['uri'] = action.target_location
            assert (len(action.target_categories) == 1)
            d['type'] = str(action.target_categories[0])
            json_obj['actions'].append(d)

        # Attributes
        for name, value in obj.attributes:
            json_obj['attributes'][name] = value

        # If this is a resource instance ...
        if 'resource_instance' in obj.render_flags:
            try:
                json_obj['kind'] = json_obj['kinds'][0]
                del json_obj['kinds']
            except KeyError, IndexError:
                raise RendererError(
                    'Resource instance MUST be of one and only one Kind')
Example #4
0
 def __init__(self):
     super(DummyBackend, self).__init__()
     self._db = OrderedDict()
     self._user_mixins = {}