def __get__(self, dummy, entity_class): mb_cls = get_member_class(entity_class) attr_map = OrderedDict() for rc_attr in itervalues_(mb_cls.__everest_attributes__): attr_map[rc_attr.entity_attr] = rc_attr # This replaces the injector. entity_class.__everest_attributes__ = attr_map return attr_map
def initialize_all(self): """ Convenience method to initialize all repositories that have not been initialized yet. """ for repo in itervalues_(self.__repositories): if not repo.is_initialized: repo.initialize()
def get_mappings(self): """ Returns an iterator over all registered mappings. :returns: iterator yielding tuples containing a mapped class as the first and a :class:`Mapping` instance as the second item. """ return itervalues_(self.__mappings)
def get_domain_class_relationship_attribute_iterator(ent): """ Returns an iterator over all terminal attributes in the given registered resource. """ for attr in itervalues_(ent.__everest_attributes__): if attr.kind != RESOURCE_ATTRIBUTE_KINDS.TERMINAL: yield attr
def get_domain_class_collection_attribute_iterator(ent): """ Returns an iterator over all terminal attributes in the given registered resource. """ for attr in itervalues_(ent.__everest_attributes__): if attr.kind == RESOURCE_ATTRIBUTE_KINDS.COLLECTION: yield attr
def __cache_attributes(self, mapped_class, key): attr_map = self.__collect_mapped_attributes(mapped_class, key) # For lookup by repr attribute name, we keep another map. repr_attr_map = dict([(attr.repr_name, attr) for attr in itervalues_(attr_map)]) self.__mapped_attr_cache[(mapped_class, key)] = (attr_map, repr_attr_map) return (attr_map, repr_attr_map)
def get_resource_class_member_attribute_iterator(rc): """ Returns an iterator over all terminal attributes in the given registered resource. """ for attr in itervalues_(rc.__everest_attributes__): if attr.kind == RESOURCE_ATTRIBUTE_KINDS.MEMBER: yield attr
def test_find_connected_with_deps(self): member = _make_test_entity_member() dep_grph = \ build_resource_dependency_graph(self._interfaces, include_backrefs=True) ent_map = find_connected_resources(member, dependency_graph=dep_grph) # Backrefs should not make a difference since we check for duplicates. for ents in itervalues_(ent_map): self.assert_equal(len(ents), 1)
def run(self): sess = ScopedSessionMaker() for bc in self.__barcodes: rack = self.__get_rack(bc) for src_cnt in itervalues_(rack.container_positions): if not src_cnt is None: if not src_cnt.sample is None: sess.delete(src_cnt.sample) rack.status = get_item_status_future()
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
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
def traverse_many(self, attribute, source_sequence, target_sequence, visitor): """ Traverses the given source and target sequences and makes appropriate calls to :method:`traverse_one`. Algorithm: 1) Build a map target item ID -> target data item from the target sequence; 2) For each source data item in the source sequence check if it has a not-None ID; if yes, remove the corresponding target from the map generated in step 1) and use as target data item for the source data item; if no, use `None` as target data item; 3) For the remaining items in the target map from 1), call :method:`traverse_one` passing `None` as source (REMOVE); 4) For all source/target data item pairs generated in 2, call :method:`traverse_one` (ADD or UPDATE depending on whether target item is `None`). :param source_sequence: iterable of source data proxies :type source_sequence: iterator yielding instances of `DataTraversalProxy` or None :param target_sequence: iterable of target data proxies :type target_sequence: iterator yielding instances of `DataTraversalProxy` or None """ target_map = {} if not target_sequence is None: for target in target_sequence: target_map[target.get_id()] = target src_tgt_pairs = [] if not source_sequence is None: for source in source_sequence: source_id = source.get_id() if not source_id is None: # Check if target exists for UPDATE. target = target_map.pop(source_id, None) else: # Source is new, there is no target, so ADD. target = None src_tgt_pairs.append((source, target)) # All targets that are now still in the map where not present in the # source and therefore need to be REMOVEd. for target in itervalues_(target_map): if not (None, target) in self.__trv_path: self.traverse_one(attribute, None, target, visitor) # for source, target in src_tgt_pairs: if not (source, target) in self.__trv_path: self.traverse_one(attribute, source, target, visitor)
def _attribute_iterator(self, mapped_class, key): """ Returns an iterator over the attributes in this mapping for the given mapped class and attribute key. If this is a pruning mapping, attributes that are ignored because of a custom configuration or because of the default ignore rules are skipped. """ for attr in itervalues_(self.__get_attribute_map(mapped_class, key)): if self.is_pruning: do_ignore = attr.should_ignore(key) else: do_ignore = False if not do_ignore: yield attr
def _attribute_iterator(self, mapped_class, key): """ Returns an iterator over the attributes in this mapping for the given mapped class and attribute key. If this is a pruning mapping, attributes that are ignored because of a custom configuration or because of the default ignore rules are skipped. """ for attr in \ itervalues_(self.__get_attribute_map(mapped_class, key, 0)): if self.is_pruning: do_ignore = attr.should_ignore(key) else: do_ignore = False if not do_ignore: yield attr
def __process_loader_options(self, entity_attr_names, mapper, query): if len(entity_attr_names) > 0: for entity_attr_name in sorted(entity_attr_names): prop = None ent = mapper.entity ent_cls_attrs = [] for entity_attr_name_token in entity_attr_name.split('.'): try: ent_attr = getattr(ent, entity_attr_name_token) except AttributeError, exc: # Try finding the attribute in the polymorphic map of # the parent attribute mapper. ent_attr = None if not prop is None: for mp in itervalues_( prop.mapper.polymorphic_map): ent_attr = getattr(mp.entity, entity_attr_name_token, None) if not ent_attr is None: break if ent_attr is None: raise exc try: prop = ent_attr.property except AttributeError: # The class attribute was not an instrumented # attribute - skip optimization. break ent = prop.mapper.entity ent_cls_attrs.append(ent_attr) if len(ent_cls_attrs) > 0: opt = reduce(lambda opt, ent_cls: opt.joinedload(ent_cls), ent_cls_attrs[1:], joinedload(ent_cls_attrs[0])) query = query.options(opt)
def test_itervalues(self): environ = {'zooma': 1} inst = self._makeOne(environ) self.assertEqual(list(inst.itervalues()), list(itervalues_(environ)))
def test_itervalues(self): environ = {'zooma':1} inst = self._makeOne(environ) self.assertEqual(list(inst.itervalues()), list(itervalues_(environ)))
def reset_all(self): for repo in itervalues_(self.__repositories): if repo.is_initialized: repo.reset()
def get_domain_class_attribute_iterator(ent): """ Returns an iterator over all attributes in the given registered resource. """ return itervalues_(ent.__everest_attributes__)
def itervalues(self): return itervalues_(self.environ)
def test_find_connected_with_collection(self): member = _make_test_entity_member() ent_map = find_connected_resources(member.__parent__) for ents in itervalues_(ent_map): self.assert_equal(len(ents), 1)