def __update(self, opts, mp_opts): if not opts is None: for option_name, option_value in iteritems_(opts): if not option_value is None: self.set_option(option_name, option_value) if not mp_opts is None: for attr_name, attr_mp_options in iteritems_(mp_opts): for mp_opt_name, mp_opt_value in iteritems_(attr_mp_options): if not mp_opt_value is None: self.set_attribute_option(attr_name, mp_opt_name, mp_opt_value)
def __collect_attributes(mcs, dicts): # Loop over the namespace of the given resource class and its base # classes looking for descriptors inheriting from # :class:`everest.resources.descriptors.attribute_base`. descr_map = {} for base_cls_namespace in dicts: for descr_name, descr in iteritems_(base_cls_namespace): if isinstance(descr, attribute_base): descr_map[descr_name] = descr # Order by descriptor index (=sequence in which they were declared). ordered_descr_map = OrderedDict() cmp_fnc = lambda item1, item2: (item1[1].index > item2[1].index) - \ (item1[1].index < item2[1].index) for item in sorted(descr_map.items(), key=functools.cmp_to_key(cmp_fnc)): name, descr = item if not type(descr) in (terminal_resource_attribute, member_resource_attribute, collection_resource_attribute): raise TypeError('Unknown resource attribute type "%s".' % type(descr)) ordered_descr_map[name] = descr # It would be awkward to repeat the resource attribute name # in the parameters to the descriptor, so we set it manually # here. descr.resource_attr = name return ordered_descr_map
def visit_member(self, attribute_key, attribute, member_node, member_data, is_link_node, parent_data, index=None): if is_link_node: new_field_name = self.__get_field_name(attribute_key.names[:-1], attribute) mb_data = CsvData({new_field_name: member_node.get_url()}) else: rpr_mb_data = OrderedDict() for attr, value in iteritems_(member_data): new_field_name = self.__get_field_name(attribute_key.names, attr) rpr_mb_data[new_field_name] = self.__encode(value) mb_data = CsvData(rpr_mb_data) if not index is None: # Collection member. Store in parent data with index as key. parent_data[index] = mb_data elif len(attribute_key) == 0: # Top level - store as CSV data.. self.__csv_data = mb_data else: # Nested member. Store in parent data with attribute as key. parent_data[attribute] = mb_data
def visit_member(self, attribute_key, attribute, member_node, member_data, is_link_node, parent_data, index=None): if is_link_node: mb_data_el = self._create_link_data_element(attribute, member_node) else: mb_data_el = self._create_member_data_element( attribute, member_node) # Process attributes. for attr, value in iteritems_(member_data): if attr.kind == RESOURCE_ATTRIBUTE_KINDS.TERMINAL: self._set_terminal_attribute(mb_data_el, attr, value) else: mb_data_el.set_nested(attr, value) if not index is None: # Collection member. Store in parent data with index as key. parent_data[index] = mb_data_el elif len(attribute_key) == 0: # Top level - store root data element. self.__data_el = mb_data_el else: # Nested member. Store in parent data with attribute as key. parent_data[attribute] = mb_data_el
def visit_member(self, attribute_key, attribute, member_node, member_data, is_link_node, parent_data, index=None): if is_link_node: mb_data = member_node.get_url() else: # Using an ordered dict gives us reproducible representations. mb_data = OrderedDict() for attr, value in iteritems_(member_data): # if attr.kind == RESOURCE_ATTRIBUTE_KINDS.TERMINAL: mb_data[attr.repr_name] = value # Use the relation for class hinting. mb_cls = member_node.mapping.mapped_class mb_data['__jsonclass__'] = mb_cls.relation if not index is None: parent_data[index] = mb_data elif len(attribute_key) == 0: self.__json_data = mb_data else: parent_data[attribute] = mb_data
def add_representer(self, content_type=None, representer_class=None, options=None, _info=u''): if content_type is None and representer_class is None: raise ValueError('Either content type or representer class must ' 'be provided.') if not content_type is None and not representer_class is None: raise ValueError('Either content type or representer class may ' 'be provided, but not both.') if options is None: options = {} rpr_reg = self.get_registered_utility(IRepresenterRegistry) if not representer_class is None: rpr_reg.register_representer_class(representer_class) if issubclass(representer_class, MappingResourceRepresenter): mp_reg = rpr_reg.get_mapping_registry( representer_class.content_type) else: # pragma: no cover # FIXME: This is for representers that bypass the mapping # machinery by using custom parsers/generators. Needs # a test case. mp_reg = None else: mp_reg = rpr_reg.get_mapping_registry(content_type) if not mp_reg is None: for name, value in iteritems_(options): mp_reg.set_default_config_option(name, value)
def after(self): # Register resources eagerly so the various adapters and utilities are # available for other directives. discriminator = ('resource', self.interface) reg = get_current_registry() config = Configurator(reg, package=self.context.package) config.add_resource(self.interface, self.member, self.entity, collection=self.collection, collection_root_name=self.collection_root_name, collection_title=self.collection_title, repository=self.repository, expose=self.expose, _info=self.context.info) for key, value in iteritems_(self.representers): cnt_type, rc_kind = key opts, mp_opts = value if rc_kind == RESOURCE_KINDS.MEMBER: rc = get_member_class(self.interface) elif rc_kind == RESOURCE_KINDS.COLLECTION: rc = get_collection_class(self.interface) else: # None rc = self.interface discriminator = ('resource_representer', rc, cnt_type, rc_kind) self.action( discriminator=discriminator, # pylint: disable=E1101 callable=config.add_resource_representer, args=(rc, cnt_type), kw=dict(options=opts, attribute_options=mp_opts, _info=self.context.info), )
def _set_request_extensions(event): request = event.request exts = request.registry.queryUtility(IRequestExtensions) for name, fn in iteritems_(exts.methods): method = fn.__get__(request, request.__class__) setattr(request, name, method) request._set_properties(exts.descriptors)
def after(self): # Register resources eagerly so the various adapters and utilities are # available for other directives. discriminator = ('resource', self.interface) reg = get_current_registry() config = Configurator(reg, package=self.context.package) config.add_resource(self.interface, self.member, self.entity, collection=self.collection, collection_root_name=self.collection_root_name, collection_title=self.collection_title, repository=self.repository, expose=self.expose, _info=self.context.info) for key, value in iteritems_(self.representers): cnt_type, rc_kind = key opts, mp_opts = value if rc_kind == RESOURCE_KINDS.MEMBER: rc = get_member_class(self.interface) elif rc_kind == RESOURCE_KINDS.COLLECTION: rc = get_collection_class(self.interface) else: # None rc = self.interface discriminator = ('resource_representer', rc, cnt_type, rc_kind) self.action(discriminator=discriminator, # pylint: disable=E1101 callable=config.add_resource_representer, args=(rc, cnt_type), kw=dict(options=opts, attribute_options=mp_opts, _info=self.context.info), )
def with_updated_configuration(self, options=None, attribute_options=None): """ Returns a context in which this mapping is updated with the given options and attribute options. """ new_cfg = self.__configurations[-1].copy() if not options is None: for o_name, o_value in iteritems_(options): new_cfg.set_option(o_name, o_value) if not attribute_options is None: for attr_name, ao_opts in iteritems_(attribute_options): for ao_name, ao_value in iteritems_(ao_opts): new_cfg.set_attribute_option(attr_name, ao_name, ao_value) # upd_cfg = type(new_cfg)(options=options, # attribute_options=attribute_options) # new_cfg.update(upd_cfg) return MappingConfigurationContext(self, new_cfg)
def _decode_dict(self, value): # Attribute dictionaries should be case-insensitive. python-ldap # defines this, although for some reason, it doesn't appear to use it # for search results. decoded = self.ldap.cidict.cidict() for k, v in iteritems_(value): decoded[self.decode(k)] = self.decode(v) return decoded
def to_zipfile(self, resource, zipfile): """ Dumps the given resource and all resources linked to it into the given ZIP file. """ rpr_map = self.to_strings(resource) with ZipFile(zipfile, 'w') as zipf: for (mb_cls, rpr_string) in iteritems_(rpr_map): fn = get_collection_filename(mb_cls, self.__content_type) zipf.writestr(fn, rpr_string, compress_type=ZIP_DEFLATED)
def __init__(self, init_map=None, map_type=dict): """ :param init_map: map-like object to initialize this instance with :param map_type: type to use for the left and right item maps (dictionary like) """ self.__left = map_type() self.__right = map_type() if not init_map is None: for left, right in iteritems_(init_map): self.__setitem__(left, right)
def to_files(self, resource, directory): """ Dumps the given resource and all resources linked to it into a set of representation files in the given directory. """ collections = self.__collect(resource) for (mb_cls, coll) in iteritems_(collections): fn = get_write_collection_path(mb_cls, self.__content_type, directory=directory) with open_text(os.path.join(directory, fn)) as strm: dump_resource(coll, strm, content_type=self.__content_type)
def run(self, visitor): """ Traverses this representer configuration traverser with the given visitor. :param visitor: :class:`RepresenterConfigVisitorBase` instance. """ attr_option_map = self.__config.get_attribute_options() # Sorting the keys results in a depth-first traversal, which is just # what we want. for (key, key_attr_option_map) in sorted(iteritems_(attr_option_map)): if not self.__max_depth is None and len(key) > self.__max_depth: continue visitor.visit(key, key_attr_option_map)
def check_attributes(test_object, attribute_map): """ Utility function to test whether the test object attributes match the expected ones (given the dictionary). :param test_object: a test object :param attribute_map: a dictionary with key = attribute name and value = expected value for this attribute """ for attr_name, exp_val in iteritems_(attribute_map): obj_val = getattr(test_object, attr_name) if obj_val != exp_val: raise AssertionError('Values for attribute %s differ!' % attr_name)
def run(self): src_rack = self.__get_rack(self.__source_barcode) for tgt_bc in self.__target_barcodes: tgt_rack = self.__get_rack(tgt_bc) for pos, src_cnt_loc in iteritems_(src_rack.container_positions): if not src_cnt_loc.container is None: src_cnt = src_cnt_loc.container if not src_cnt.sample is None: src_smpl = src_cnt.sample tgt_cnt = tgt_rack.container_positions[pos] tgt_smpl = tgt_cnt.make_sample(self.__transfer_volume) for sm in src_smpl.sample_molecules: tgt_smpl.make_sample_molecule(sm.molecule, sm.concentration) tgt_rack.status = get_item_status_managed()
def run(self): src_rack = self.__get_rack(self.__source_barcode) for tgt_bc in self.__target_barcodes: tgt_rack = self.__get_rack(tgt_bc) for pos, src_cnt_loc in iteritems_(src_rack.container_positions): if not src_cnt_loc.container is None: src_cnt = src_cnt_loc.container if not src_cnt.sample is None: src_smpl = src_cnt.sample tgt_cnt = tgt_rack.container_positions[pos] tgt_smpl = tgt_cnt.make_sample(self.__transfer_volume) for sm in src_smpl.sample_molecules: tgt_smpl.make_sample_molecule( sm.molecule, sm.concentration) tgt_rack.status = get_item_status_managed()
def __init__(self, data=None): if data is None: data = {} self.fields = [] self.data = [] for attr_name, value in iteritems_(data): if not isinstance(value, CsvData): self.fields.append(attr_name) if len(self.data) == 0: self.data.append([value]) else: for row in self.data: row.append(value) else: self.expand(value)
def check_attributes(test_object, attribute_map): """ Utility function to test whether the test object attributes match the expected ones (given the dictionary). :param test_object: a test object :param attribute_map: a dictionary with key = attribute name and value = expected value for this attribute """ for attr_name, exp_val in iteritems_(attribute_map): obj_val = getattr(test_object, attr_name) if obj_val != exp_val: raise AssertionError('Values for attribute %s differ! (expected: ' '%s, found: %s)' % (attr_name, exp_val, obj_val))
def __clone(self, entity, cache): clone = object.__new__(entity.__class__) # We add the clone with its ID set to the cache *before* we load it # so that circular references will work. clone.id = entity.id cache.add(clone) state = EntityState.get_state_data(entity) id_attr = None for attr, value in iteritems_(state): if attr.entity_attr == 'id': id_attr = attr continue attr_type = attr.attr_type if attr.kind != RESOURCE_ATTRIBUTE_KINDS.TERMINAL \ and not self.__repository.is_registered_resource(attr_type): # Prevent loading of entities from other repositories. # FIXME: Doing this here is inconsistent, since e.g. the RDB # session does not perform this kind of check. continue elif attr.kind == RESOURCE_ATTRIBUTE_KINDS.MEMBER \ and not value is None: ent_cls = get_entity_class(attr_type) new_value = self.load(ent_cls, value) state[attr] = new_value elif attr.kind == RESOURCE_ATTRIBUTE_KINDS.COLLECTION \ and len(value) > 0: value_type = type(value) new_value = value_type.__new__(value_type) if issubclass(value_type, MutableSequence): add_op = new_value.append elif issubclass(value_type, MutableSet): add_op = new_value.add else: raise ValueError('Do not know how to clone value of type ' '%s for resource attribute %s.' % (type(new_value), attr)) ent_cls = get_entity_class(attr_type) for child in value: child_clone = self.load(ent_cls, child) add_op(child_clone) state[attr] = new_value # We set the ID already above. if not id_attr is None: del state[id_attr] EntityState.set_state_data(clone, state) return clone
def to_strings(self, resource): """ Dumps the all resources reachable from the given resource to a map of string representations using the specified content_type (defaults to CSV). :returns: dictionary mapping resource member classes to string representations """ collections = self.__collect(resource) # Build a map of representations. rpr_map = OrderedDict() for (mb_cls, coll) in iteritems_(collections): strm = NativeIO('w') dump_resource(coll, strm, content_type=self.__content_type) rpr_map[mb_cls] = strm.getvalue() return rpr_map
def apply_request_extensions(request, extensions=None): """Apply request extensions (methods and properties) to an instance of :class:`pyramid.interfaces.IRequest`. This method is dependent on the ``request`` containing a properly initialized registry. After invoking this method, the ``request`` should have the methods and properties that were defined using :meth:`pyramid.config.Configurator.add_request_method`. """ if extensions is None: extensions = request.registry.queryUtility(IRequestExtensions) if extensions is not None: for name, fn in iteritems_(extensions.methods): method = fn.__get__(request, request.__class__) setattr(request, name, method) InstancePropertyHelper.apply_properties( request, extensions.descriptors)
def apply_request_extensions(request, extensions=None): """Apply request extensions (methods and properties) to an instance of :class:`pyramid.interfaces.IRequest`. This method is dependent on the ``request`` containing a properly initialized registry. After invoking this method, the ``request`` should have the methods and properties that were defined using :meth:`pyramid.config.Configurator.add_request_method`. """ if extensions is None: extensions = request.registry.queryUtility(IRequestExtensions) if extensions is not None: for name, fn in iteritems_(extensions.methods): method = fn.__get__(request, request.__class__) setattr(request, name, method) InstancePropertyHelper.apply_properties(request, extensions.descriptors)
def test_member_data_element(self): data_el = SimpleMemberDataElement.create() # Test nesteds. assert list(data_el.nesteds.keys()) == [] parent_data_el = SimpleMemberDataElement.create() rc_nested_attr = \ get_resource_class_attribute(MyEntityMember, 'parent') mp_nested_attr = MappedAttribute(rc_nested_attr) data_el.set_nested(mp_nested_attr, parent_data_el) assert data_el.get_nested(mp_nested_attr) is parent_data_el assert list(data_el.nesteds.keys()) == ['parent'] # Test terminals. assert list(data_el.terminals.keys()) == [] utc = timezone('UTC') ldt = datetime.datetime(2012, 8, 29, 16, 20, 0, tzinfo=utc) term_attr_data = OrderedDict(text='foo', number=0, date_time=ldt) for term_attr_name, term_attr_value in term_attr_data.items(): rc_attr = get_resource_class_attribute(MyEntityMember, term_attr_name) mp_attr = MappedAttribute(rc_attr) # Check setting to None value. data_el.set_terminal(mp_attr, None) assert data_el.get_terminal(mp_attr) is None data_el.set_terminal_converted(mp_attr, None) assert data_el.get_terminal(mp_attr) is None # Check setting to value. data_el.set_terminal(mp_attr, term_attr_value) assert data_el.get_terminal(mp_attr) == term_attr_value rpr_val = data_el.get_terminal_converted(mp_attr) data_el.set_terminal_converted(mp_attr, rpr_val) assert data_el.get_terminal(mp_attr) == term_attr_value assert list(data_el.terminals.keys()) == list(term_attr_data.keys()) # Printing. prt_str = str(data_el) assert prt_str.startswith(data_el.__class__.__name__) assert prt_str.endswith(')') # Attribute iteration. for attr_name, attr_value in iteritems_(term_attr_data): assert data_el.get_attribute(attr_name) == attr_value for de_attr_name, de_attr_value in data_el.iterator(): if de_attr_name in term_attr_data: assert de_attr_value == term_attr_data[de_attr_name]
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 from_settings(cls, settings={}, prefix="ipauth.", **kwds): """Construct an IPAuthenticationPolicy from deployment settings.""" # Grab out all the settings keys that start with our prefix. ipauth_settings = {} for name, value in iteritems_(settings): if not name.startswith(prefix): continue ipauth_settings[name[len(prefix):]] = value # Update with any additional keyword arguments. ipauth_settings.update(kwds) # Now look for specific keys of interest. ipaddrs = ipauth_settings.get("ipaddrs", "") userid = ipauth_settings.get("userid", None) principals = aslist(ipauth_settings.get("principals", "")) proxies = ipauth_settings.get("proxies", None) get_userid = ipauth_settings.get("get_userid", None) get_principals = ipauth_settings.get("get_principals", None) # The constructor uses make_ip_set to parse out strings, # so we're free to just pass them on in. return cls(ipaddrs, userid, principals, proxies, get_userid, get_principals)
def get_attribute_options(self, attribute=None): """ Returns a copy of the mapping options for the given attribute name or a copy of all mapping options, if no attribute name is provided. All options that were not explicitly configured are given a default value of `None`. :param tuple attribute_key: attribute name or tuple specifying an attribute path. :returns: mapping options dictionary (including default `None` values) """ attribute_key = self.__make_key(attribute) if attribute_key is None: opts = defaultdict(self._default_attributes_options.copy) for attr, mp_options in iteritems_(self.__attribute_options): opts[attr].update(mp_options) else: opts = self._default_attributes_options.copy() attr_opts = self.__attribute_options[attribute_key] opts.update(attr_opts) return opts
def _convert_to_entity(self): init_map = {} nested_map = {} mapped_class = self._data.mapping.mapped_class for attr in \ self.__mapping.attribute_iterator(mapped_class=mapped_class, key=self.__attribute_key): try: val = self._get_proxied_attribute_value(attr) except AttributeError: continue else: attr_name = attr.entity_attr if not '.' in attr_name: init_map[attr_name] = val else: nested_map[attr_name] = val ent_cls = get_entity_class(mapped_class) entity = ent_cls.create_from_data(init_map) for nested_name, nested_value in iteritems_(nested_map): set_nested_attribute(entity, nested_name, nested_value) return entity
def __dump(data_el, stream, offset): name = data_el.__class__.__name__ stream.write("%s%s" % (' ' * offset, name)) offset += 2 ifcs = provided_by(data_el) if ICollectionDataElement in ifcs: stream.write("[") first_member = True for member_data_el in data_el.get_members(): if first_member: stream.write('%s' % os.linesep + ' ' * offset) first_member = False else: stream.write(',%s' % os.linesep + ' ' * offset) __dump(member_data_el, stream, offset) stream.write("]") else: stream.write("(") if ILinkedDataElement in ifcs: stream.write("url=%s, kind=%s, relation=%s" % (data_el.get_url(), data_el.get_kind(), data_el.get_relation())) else: first_attr = True for attr_name, attr_value in iteritems_(data_el.data): if first_attr: first_attr = False else: stream.write(',%s' % os.linesep + ' ' * (offset + len(name) + 1)) if attr_value is None: continue if not IResourceDataElement in provided_by(attr_value): stream.write("%s=%s" % (attr_name, attr_value)) else: stream.write("%s=" % attr_name) __dump(attr_value, stream, offset) stream.write(')')
def set_state_data(cls, entity, data): """ Sets the state data for the given entity to the given data. This also works for unmanaged entities. """ attr_names = get_domain_class_attribute_names(type(entity)) nested_items = [] for attr, new_attr_value in iteritems_(data): if not attr.entity_attr in attr_names: raise ValueError('Can not set attribute "%s" for entity ' '"%s".' % (attr.entity_attr, entity)) if '.' in attr.entity_attr: nested_items.append((attr, new_attr_value)) continue else: setattr(entity, attr.entity_attr, new_attr_value) for attr, new_attr_value in nested_items: try: set_nested_attribute(entity, attr.entity_attr, new_attr_value) except AttributeError as exc: if not new_attr_value is None: raise exc
def visit_member(self, attribute_key, attribute, member_node, member_data, is_link_node, parent_data, index=None): if is_link_node: mb_data_el = self._create_link_data_element(attribute, member_node) else: mb_data_el = self._create_member_data_element(attribute, member_node) # Process attributes. for attr, value in iteritems_(member_data): if attr.kind == RESOURCE_ATTRIBUTE_KINDS.TERMINAL: self._set_terminal_attribute(mb_data_el, attr, value) else: mb_data_el.set_nested(attr, value) if not index is None: # Collection member. Store in parent data with index as key. parent_data[index] = mb_data_el elif len(attribute_key) == 0: # Top level - store root data element. self.__data_el = mb_data_el else: # Nested member. Store in parent data with attribute as key. parent_data[attribute] = mb_data_el
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)
def from_rack(cls, tube_rack): """ Creates a :class:`RackScanningLayout` using the data of a the passed rack. :param tube_rack: The tube rack to create the layout for. :type tube_rack: :class:`thelma.entities.rack.TubeRack` :raises TypeError: If the tube rack is the wrong type (incl. other rack types). :return: :class:`RackScanningLayout` """ if not isinstance(tube_rack, TubeRack): msg = 'The tube rack must be a %s type (obtained: %s).' \ % (TubeRack.__class__.__name__, tube_rack.__class__.__name__) raise TypeError(msg) rsl = RackScanningLayout(rack_barcode=tube_rack.barcode, timestamp=get_utc_time()) # for tube in tube_rack.containers: # rack_pos = tube.position # rsl.add_position(rack_pos, tube.barcode) for pos, tube in iteritems_(tube_rack.container_positions): rsl.add_position(pos, tube.barcode) return rsl
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)
def test_iteritems(self): environ = {'zooma': 1} inst = self._makeOne(environ) self.assertEqual(list(inst.iteritems()), list(iteritems_(environ)))
def _set_extensions(self, extensions): for name, fn in iteritems_(extensions.methods): method = fn.__get__(self, self.__class__) setattr(self, name, method) self._set_properties(extensions.descriptors)
def test_iteritems(self): environ = {'zooma':1} inst = self._makeOne(environ) self.assertEqual(list(inst.iteritems()), list(iteritems_(environ)))
def nesteds(self): if self.__data is None: self.__data = OrderedDict() return OrderedDict((k, v) for (k, v) in iteritems_(self.__data) if isinstance(v, DataElement))