Beispiel #1
0
 def __get_patch_context(self, rc, attributes):
     # Use this to create am XML representation of the given resource
     # which *only* contains the given attributes (useful for PATCHing).
     attr_map = get_resource_class_attributes(rc)
     cfg_opts = dict([((attr_name, ), {
         IGNORE_OPTION: True
     }) for attr_name in attr_map])
     for attr_key in attributes:
         attr_cfg_opts = {IGNORE_OPTION: False}
         if isinstance(attr_key, string_types):
             key = (attr_key, )
             rc_attr = attr_map[attr_key]
             if not rc_attr.kind == RESOURCE_ATTRIBUTE_KINDS.TERMINAL:
                 attr_cfg_opts[WRITE_AS_LINK_OPTION] = False
                 # Disable nested attributes.
                 # FIXME: This should be recursive.
                 for attr_attr_name in \
                         get_resource_class_attributes(rc_attr.attr_type):
                     cfg_opts[(attr_key, attr_attr_name)] = \
                                                 {IGNORE_OPTION:True}
         else:
             key = attr_key
         cfg_opts[key] = attr_cfg_opts
     return UpdatingRepresenterConfigurationContext(
         type(rc), XmlMime, attribute_options=cfg_opts)
Beispiel #2
0
 def __get_patch_context(self, rc, attributes):
     # Use this to create am XML representation of the given resource
     # which *only* contains the given attributes (useful for PATCHing).
     attr_map = get_resource_class_attributes(rc)
     cfg_opts = dict([((attr_name,), {IGNORE_OPTION:True})
                      for attr_name in attr_map])
     for attr_key in attributes:
         attr_cfg_opts = {IGNORE_OPTION:False}
         if isinstance(attr_key, string_types):
             key = (attr_key,)
             rc_attr = attr_map[attr_key]
             if not rc_attr.kind == RESOURCE_ATTRIBUTE_KINDS.TERMINAL:
                 attr_cfg_opts[WRITE_AS_LINK_OPTION] = False
                 # Disable nested attributes.
                 # FIXME: This should be recursive.
                 for attr_attr_name in \
                         get_resource_class_attributes(rc_attr.attr_type):
                     cfg_opts[(attr_key, attr_attr_name)] = \
                                                 {IGNORE_OPTION:True}
         else:
             key = attr_key
         cfg_opts[key] = attr_cfg_opts
     return UpdatingRepresenterConfigurationContext(type(rc),
                                                   XmlMime,
                                                   attribute_options=
                                                                 cfg_opts)
Beispiel #3
0
 def __convert_to_entity_attr(self, rc_attr_name):
     entity_attr_tokens = []
     rc_class = self.__rc_class
     for rc_attr_token in rc_attr_name.split('.'):
         rc_attr = get_resource_class_attributes(rc_class)[rc_attr_token]
         ent_attr_name = rc_attr.entity_name
         if ent_attr_name is None:
             raise ValueError('Resource attribute "%s" does not have a '
                              'corresponding entity attribute.'
                              % rc_attr.name)
         if rc_attr.kind != ResourceAttributeKinds.TERMINAL:
             # Look up the member class for the specified member or
             # collection resource interface.
             rc_class = get_member_class(rc_attr.value_type)
         entity_attr_tokens.append(ent_attr_name)
     return '.'.join(entity_attr_tokens)
Beispiel #4
0
 def __collect_mapped_attributes(self, mapped_class, key):
     if isinstance(key, MappedAttributeKey):
         names = key.names
     else:
         names = key
     collected_mp_attrs = OrderedDict()
     is_mapped_cls = mapped_class is self.__mapped_cls
     if len(names) == 0 and is_mapped_cls:
         # Bootstrapping: fetch resource attributes and create new
         # mapped attributes.
         rc_attrs = get_resource_class_attributes(self.__mapped_cls)
         for rc_attr in itervalues_(rc_attrs):
             attr_key = names + (rc_attr.resource_attr,)
             attr_mp_opts = \
                 self.__configurations[-1].get_attribute_options(attr_key)
             new_mp_attr = MappedAttribute(rc_attr, options=attr_mp_opts)
             collected_mp_attrs[new_mp_attr.resource_attr] = new_mp_attr
     else:
         # Indirect access - fetch mapped attributes from some other
         # class' mapping and clone.
         if is_mapped_cls:
             mp = self
         elif len(names) == 0 and self.__is_collection_mapping:
             if provides_member_resource(mapped_class):
                 # Mapping a polymorphic member class.
                 mapped_coll_cls = get_collection_class(mapped_class)
             else:
                 # Mapping a derived collection class.
                 mapped_coll_cls = mapped_class
             mp = self.__mp_reg.find_or_create_mapping(mapped_coll_cls)
         else:
             mp = self.__mp_reg.find_or_create_mapping(mapped_class)
         mp_attrs = mp.get_attribute_map()
         for mp_attr in itervalues_(mp_attrs):
             attr_key = names + (mp_attr.name,)
             attr_mp_opts = \
                 dict(((k, v)
                       for (k, v) in
                         iteritems_(self.__configurations[-1]
                                    .get_attribute_options(attr_key))
                       if not v is None))
             clnd_mp_attr = mp_attr.clone(options=attr_mp_opts)
             collected_mp_attrs[mp_attr.name] = clnd_mp_attr
     return collected_mp_attrs
Beispiel #5
0
 def __collect_mapped_attributes(self, mapped_class, key):
     if isinstance(key, MappedAttributeKey):
         names = key.names
     else:
         names = key
     collected_mp_attrs = OrderedDict()
     is_mapped_cls = mapped_class is self.__mapped_cls
     if len(names) == 0 and is_mapped_cls:
         # Bootstrapping: fetch resource attributes and create new
         # mapped attributes.
         rc_attrs = get_resource_class_attributes(self.__mapped_cls)
         for rc_attr in itervalues_(rc_attrs):
             attr_key = names + (rc_attr.resource_attr,)
             attr_mp_opts = \
                 self.__configurations[-1].get_attribute_options(attr_key)
             new_mp_attr = MappedAttribute(rc_attr, options=attr_mp_opts)
             collected_mp_attrs[new_mp_attr.resource_attr] = new_mp_attr
     else:
         # Indirect access - fetch mapped attributes from some other
         # class' mapping and clone.
         if is_mapped_cls:
             mp = self
         elif len(names) == 0 and self.__is_collection_mapping:
             if provides_member_resource(mapped_class):
                 # Mapping a polymorphic member class.
                 mapped_coll_cls = get_collection_class(mapped_class)
             else:
                 # Mapping a derived collection class.
                 mapped_coll_cls = mapped_class
             mp = self.__mp_reg.find_or_create_mapping(mapped_coll_cls)
         else:
             mp = self.__mp_reg.find_or_create_mapping(mapped_class)
         mp_attrs = mp.get_attribute_map()
         for mp_attr in itervalues_(mp_attrs):
             attr_key = names + (mp_attr.name,)
             attr_mp_opts = \
                 dict(((k, v)
                       for (k, v) in
                         iteritems_(self.__configurations[-1]
                                    .get_attribute_options(attr_key))
                       if not v is None))
             clnd_mp_attr = mp_attr.clone(options=attr_mp_opts)
             collected_mp_attrs[mp_attr.resource_attr] = clnd_mp_attr
     return collected_mp_attrs
Beispiel #6
0
 def test_types(self):
     attrs = iter(get_resource_class_attributes(MyEntityMember).values())
     attr0 = next(attrs)
     self.assert_equal(attr0.resource_attr, ATTRIBUTE_NAMES[0])
     self.assert_equal(attr0.kind, RESOURCE_ATTRIBUTE_KINDS.TERMINAL)
     self.assert_equal(attr0.entity_attr, 'id')
     self.assert_equal(attr0.attr_type, int)
     attr1 = next(attrs)
     self.assert_equal(attr1.resource_attr, ATTRIBUTE_NAMES[1])
     self.assert_equal(attr1.kind, RESOURCE_ATTRIBUTE_KINDS.MEMBER)
     self.assert_equal(attr1.entity_attr, 'parent')
     self.assert_equal(attr1.attr_type, IMyEntityParent)
     attr2 = next(attrs)
     self.assert_equal(attr2.resource_attr, ATTRIBUTE_NAMES[2])
     self.assert_equal(attr2.kind,
                       RESOURCE_ATTRIBUTE_KINDS.COLLECTION)
     self.assert_equal(attr2.entity_attr, 'children')
     self.assert_equal(attr2.attr_type, IMyEntityChild)
     attr3 = next(attrs)
     self.assert_equal(attr3.resource_attr, ATTRIBUTE_NAMES[3])
     self.assert_equal(attr3.kind, RESOURCE_ATTRIBUTE_KINDS.TERMINAL)
     self.assert_equal(attr3.entity_attr, 'text')
     self.assert_equal(attr3.attr_type, str)
     attr4 = next(attrs)
     self.assert_equal(attr4.resource_attr, ATTRIBUTE_NAMES[4])
     attr5 = next(attrs)
     self.assert_equal(attr5.resource_attr, ATTRIBUTE_NAMES[5])
     self.assert_equal(attr5.kind, RESOURCE_ATTRIBUTE_KINDS.TERMINAL)
     self.assert_equal(attr5.entity_attr, 'number')
     self.assert_equal(attr5.attr_type, int)
     self.assert_true(is_resource_class_member_attribute(MyEntityMember,
                                                         'parent'))
     self.assert_true(is_resource_class_collection_attribute(MyEntityMember,
                                                             'children'))
     self.assert_true(is_resource_class_resource_attribute(MyEntityMember,
                                                           'parent'))
     self.assert_true(is_resource_class_resource_attribute(MyEntityMember,
                                                           'children'))
     self.assert_true(isinstance(getattr(MyEntityMember, 'id'),
                                 terminal_attribute))
Beispiel #7
0
 def test_types(self):
     attrs = iter(get_resource_class_attributes(MyEntityMember).values())
     attr0 = next(attrs)
     self.assert_equal(attr0.resource_attr, ATTRIBUTE_NAMES[0])
     self.assert_equal(attr0.kind, RESOURCE_ATTRIBUTE_KINDS.TERMINAL)
     self.assert_equal(attr0.entity_attr, 'id')
     self.assert_equal(attr0.attr_type, int)
     attr1 = next(attrs)
     self.assert_equal(attr1.resource_attr, ATTRIBUTE_NAMES[1])
     self.assert_equal(attr1.kind, RESOURCE_ATTRIBUTE_KINDS.MEMBER)
     self.assert_equal(attr1.entity_attr, 'parent')
     self.assert_equal(attr1.attr_type, IMyEntityParent)
     attr2 = next(attrs)
     self.assert_equal(attr2.resource_attr, ATTRIBUTE_NAMES[2])
     self.assert_equal(attr2.kind, RESOURCE_ATTRIBUTE_KINDS.COLLECTION)
     self.assert_equal(attr2.entity_attr, 'children')
     self.assert_equal(attr2.attr_type, IMyEntityChild)
     attr3 = next(attrs)
     self.assert_equal(attr3.resource_attr, ATTRIBUTE_NAMES[3])
     self.assert_equal(attr3.kind, RESOURCE_ATTRIBUTE_KINDS.TERMINAL)
     self.assert_equal(attr3.entity_attr, 'text')
     self.assert_equal(attr3.attr_type, str)
     attr4 = next(attrs)
     self.assert_equal(attr4.resource_attr, ATTRIBUTE_NAMES[4])
     attr5 = next(attrs)
     self.assert_equal(attr5.resource_attr, ATTRIBUTE_NAMES[5])
     self.assert_equal(attr5.kind, RESOURCE_ATTRIBUTE_KINDS.TERMINAL)
     self.assert_equal(attr5.entity_attr, 'number')
     self.assert_equal(attr5.attr_type, int)
     self.assert_true(
         is_resource_class_member_attribute(MyEntityMember, 'parent'))
     self.assert_true(
         is_resource_class_collection_attribute(MyEntityMember, 'children'))
     self.assert_true(
         is_resource_class_resource_attribute(MyEntityMember, 'parent'))
     self.assert_true(
         is_resource_class_resource_attribute(MyEntityMember, 'children'))
     self.assert_true(
         isinstance(getattr(MyEntityMember, 'id'), terminal_attribute))
Beispiel #8
0
 def visit(rc, grph, dep_grph):
     mb_cls = type(rc)
     attr_map = get_resource_class_attributes(mb_cls)
     for attr_name, attr in iteritems_(attr_map):
         if is_resource_class_terminal_attribute(mb_cls, attr_name):
             continue
         # Only follow the resource attribute if the dependency graph
         # has an edge here.
         child_mb_cls = get_member_class(attr.attr_type)
         if not dep_grph.has_edge((mb_cls, child_mb_cls)):
             continue
         child_rc = getattr(rc, attr_name)
         if is_resource_class_collection_attribute(mb_cls, attr_name):
             for child_mb in child_rc:
                 if not grph.has_node(child_mb): # Ignore cyclic references.
                     grph.add_node(child_mb)
                     grph.add_edge((rc, child_mb))
                     visit(child_mb, grph, dep_grph)
         else: # Member.
             if not grph.has_node(child_rc): # Ignore cyclic references.
                 grph.add_node(child_rc)
                 grph.add_edge((rc, child_rc))
                 visit(child_rc, grph, dep_grph)
Beispiel #9
0
 def visit(rc, grph, dep_grph):
     mb_cls = type(rc)
     attr_map = get_resource_class_attributes(mb_cls)
     for attr_name, attr in iteritems_(attr_map):
         if is_resource_class_terminal_attribute(mb_cls, attr_name):
             continue
         # Only follow the resource attribute if the dependency graph
         # has an edge here.
         child_mb_cls = get_member_class(attr.attr_type)
         if not dep_grph.has_edge((mb_cls, child_mb_cls)):
             continue
         child_rc = getattr(rc, attr_name)
         if is_resource_class_collection_attribute(mb_cls, attr_name):
             for child_mb in child_rc:
                 if not grph.has_node(
                         child_mb):  # Ignore cyclic references.
                     grph.add_node(child_mb)
                     grph.add_edge((rc, child_mb))
                     visit(child_mb, grph, dep_grph)
         else:  # Member.
             if not grph.has_node(child_rc):  # Ignore cyclic references.
                 grph.add_node(child_rc)
                 grph.add_edge((rc, child_rc))
                 visit(child_rc, grph, dep_grph)
Beispiel #10
0
 def __getitem__(self, item):
     ident = identifier_from_slug(item)
     attr = get_resource_class_attributes(self.__class__).get(ident)
     if attr is None or not attr.is_nested:
         raise KeyError('%s' % ident)
     return getattr(self, ident)