Beispiel #1
0
 def run(self):
     csv_rdr = CsvDictReader(self._stream,
                             dialect=self.get_option('dialect'))
     is_member_rpr = provides_member_resource(self._resource_class)
     if is_member_rpr:
         coll_data_el = None
     else:
         coll_data_el = self._mapping.create_data_element()
     for row_data in csv_rdr:
         if self.__is_first_row:
             self.__first_row_field_names = set(csv_rdr.fieldnames)
             self.__first_row_data = row_data.copy()
         if not self.__coll_data is None:
             # We need to generate the row data key now because we
             # get attribute values destructively from the row_data.
             self.__row_data_key = self.__coll_data.make_key(row_data)
         mb_data_el = self.__process_row(row_data, self._resource_class,
                                         MappedAttributeKey(()))
         if self.__is_first_row:
             self.__is_first_row = False
             if len(self.__first_row_field_names) > 0:
                 raise ValueError('Invalid field name(s): %s' %
                                  ','.join(self.__first_row_field_names))
         if None in row_data.keys():
             raise ValueError('Invalid row length.')
         if not coll_data_el is None:
             # The member data element will be None for all but the first
             # member of nested collection resources.
             if not mb_data_el is None:
                 coll_data_el.add_member(mb_data_el)
     if is_member_rpr:
         result_data_el = mb_data_el
     else:
         result_data_el = coll_data_el
     return result_data_el
Beispiel #2
0
 def __get_attribute_map(self, mapped_class, key, index):
     if mapped_class is None:
         mapped_class = self.__mapped_cls
     if key is None:
         key = MappedAttributeKey(()) # Top level access.
     attr_maps = self.__mapped_attr_cache.get((mapped_class, key))
     if attr_maps is None:
         attr_maps = self.__cache_attributes(mapped_class, key)
     return attr_maps[index]
Beispiel #3
0
    def get_attribute_map(self, mapped_class=None, key=None):
        """
        Returns an ordered map of the mapped attributes for the given mapped
        class and attribute key.

        :param key: Tuple of attribute names specifying a path to a nested
          attribute in a resource tree. If this is not given, all attributes
          in this mapping will be returned.
        """
        if mapped_class is None:
            mapped_class = self.__mapped_cls
        if key is None:
            key = MappedAttributeKey(())
        return OrderedDict([(attr.resource_attr, attr)
                            for attr in self._attribute_iterator(mapped_class,
                                                                 key)])
Beispiel #4
0
 def __init__(self,
              data,
              accessor,
              relationship_direction,
              relation_operation,
              attribute_key=None,
              mapping=None):
     """
     :param mapping: resource attribute mapping. This needs to be passed
       along to all other, nested proxies generated by this one in the
       traversal process.
     :type mapping: :class:`everest.representers.mapping.Mapping`
     """
     DataTraversalProxy.__init__(self, data, accessor,
                                 relationship_direction, relation_operation)
     if attribute_key is None:
         attribute_key = MappedAttributeKey(())
     self.__relationships = {}
     self.__attribute_key = attribute_key
     if mapping is None:
         mapping = data.mapping
     self.__mapping = mapping
Beispiel #5
0
 def run(self, visitor):
     """
     Runs this traverser.
     """
     self._dispatch(MappedAttributeKey(()), None, self.__root, None,
                    visitor)
 def test_mapping_access(self):
     key = MappedAttributeKey(())
     self.assert_true(str(key).startswith(key.__class__.__name__))
     attr_map = self.mapping.get_attribute_map(key=key)
     self.assert_true(attr_map['children'].options[IGNORE_OPTION] is False)
 def test_add(self):
     key = MappedAttributeKey((MyEntityMember.children,)) \
           + (MyEntityChildMember.children,)
     self.assert_true(self.data[key] is True)
 def test_attribute_key_append(self):
     key = MappedAttributeKey((MyEntityMember.children,))
     key.append(MyEntityChildMember.children)
     self.assert_true(self.data[key] is True)
 def test_attribute_key_pop(self):
     key = MappedAttributeKey((MyEntityMember.children,))
     attr = key.pop()
     self.assert_equal(attr.resource_attr, 'children')
     self.assert_raises(KeyError, self.data.__getitem__, key)
 def test_iteration(self):
     key = MappedAttributeKey((MyEntityMember.children,
                                 MyEntityChildMember.children))
     self.assert_true(next(iter(key)) is MyEntityMember.children)
     self.assert_true(key[1] is MyEntityChildMember.children)
     self.assert_equal(len(key), 2)
 def test_invalid_kind(self):
     attr = terminal_attribute(str, 'foo')
     attr.kind = 'INVALID'
     mp_attr = MappedAttribute(attr, options={IGNORE_OPTION:None})
     key = MappedAttributeKey(())
     self.assert_raises(ValueError, mp_attr.should_ignore, key)