def test_single_dependency(self): graph = {'a':['b'], 'b':[]} ordered = ordered_nodes(graph) self.assertEqual(ordered, ('a','b')) graph = {'a':[], 'b':['a']} ordered = ordered_nodes(graph) self.assertEqual(ordered, ('b','a'))
def sort_checkers(self, checkers=None): if checkers is None: checkers = [checker for checkers in self._checkers.values() for checker in checkers] graph = {} cls_instance = {} for checker in checkers: graph[checker.__class__] = set(checker.needs_checkers) cls_instance[checker.__class__] = checker checkers = [cls_instance.get(cls) for cls in ordered_nodes(graph)] checkers.remove(self) checkers.insert(0, self) return checkers
def sort_checkers(self, checkers=None): if checkers is None: checkers = [ checker for checkers in self._checkers.values() for checker in checkers ] graph = {} cls_instance = {} for checker in checkers: graph[checker.__class__] = set(checker.needs_checkers) cls_instance[checker.__class__] = checker checkers = [cls_instance.get(cls) for cls in ordered_nodes(graph)] checkers.remove(self) checkers.insert(0, self) return checkers
def _ordered_formparams(self): """ Return form parameters dictionaries for each edited entity. We ensure that entities can be created in this order accounting for mandatory inlined relations. """ req = self._cw graph = {} get_rschema = self._cw.vreg.schema.rschema # minparams = 2, because at least __type and eid are needed values_by_eid = dict((eid, req.extract_entity_params(eid, minparams=2)) for eid in req.edited_eids()) # iterate over all the edited entities for eid, values in values_by_eid.items(): # add eid to the dependency graph graph.setdefault(eid, set()) # search entity's edited fields for mandatory inlined relation for param in values['_cw_entity_fields'].split(','): try: rtype, role = param.split('-') except ValueError: # e.g. param='__type' continue rschema = get_rschema(rtype) if rschema.inlined: for target in rschema.targets(values['__type'], role): rdef = rschema.role_rdef(values['__type'], target, role) # if cardinality is 1 and if the target entity is being # simultaneously edited, the current entity must be # created before the target one if rdef.cardinality[0 if role == 'subject' else 1] == '1': # use .get since param may be unspecified (though it will usually lead # to a validation error later) target_eid = values.get(param) if target_eid in values_by_eid: # add dependency from the target entity to the # current one if role == 'object': graph.setdefault(target_eid, set()).add(eid) else: graph.setdefault(eid, set()).add(target_eid) break for eid in reversed(ordered_nodes(graph)): yield values_by_eid[eid]
def reorder_cubes(cls, cubes): """reorder cubes from the top level cubes to inner dependencies cubes """ from logilab.common.graph import ordered_nodes, UnorderableGraph graph = {} for cube in cubes: cube = CW_MIGRATION_MAP.get(cube, cube) graph[cube] = set(dep for dep in cls.cube_dependencies(cube) if dep in cubes) graph[cube] |= set(dep for dep in cls.cube_recommends(cube) if dep in cubes) try: return ordered_nodes(graph) except UnorderableGraph as ex: raise ConfigurationError(ex)
def serialize_to_python(s): out = StringIO() w = out.write w(u'from yams.buildobjs import *\n\n') graph = {} for entity in s.entities(): l = graph.setdefault(entity, []) if entity._specialized_type is not None: l.append(entity._specialized_type) for e in reversed(ordered_nodes(graph)): if not e.final: if e._specialized_type: base = e._specialized_type else: base = 'EntityType' w(u'class %s(%s):\n' % (e.type, base)) attr_defs = list(e.attribute_definitions()) if attr_defs: for attr, obj in attr_defs: w(u' %s = %s()\n' % (attr.type, obj.type)) else: w(u' pass\n') w(u'\n') for r in s.relations(): if not r.final: if r.subjects() and r.objects(): w(u'class %s(RelationDefinition):\n' % r.type) w(u' subject = (%s,)\n' % ', '.join("'%s'" % x for x in r.subjects())) w(u' object = (%s,)\n' % ', '.join("'%s'" % x for x in r.objects())) w(u'\n') else: logging.warning( 'relation definition %s missing subject/object' % r.type) return out.getvalue()
def test_three_items_two_dependencies(self): graph = {'a': ['b'], 'b': ['c'], 'c':[]} ordered = ordered_nodes(graph) self.assertEqual(ordered, ('a', 'b', 'c'))
def test_two_items_no_dependency(self): graph = {'a':[], 'b':[]} ordered = ordered_nodes(graph) self.assertEqual(ordered, ('a','b'))
def test_one_item(self): graph = {'a':[]} ordered = ordered_nodes(graph) self.assertEqual(ordered, ('a',))