diff --git a/entities.py b/entities.py index b6a9b1abe346b8a2a6ce5a86eb47aa872b479aa2_ZW50aXRpZXMucHk=..6402bd623407c747d8f624e682e29f5bdc123396_ZW50aXRpZXMucHk= 100644 --- a/entities.py +++ b/entities.py @@ -29,6 +29,5 @@ """ from functools import wraps -from warnings import warn from cubicweb import neg_role @@ -33,6 +32,5 @@ from cubicweb import neg_role -from cubicweb.rset import ResultSet from cubicweb.schema import META_RTYPES, WORKFLOW_RTYPES, SYSTEM_RTYPES from cubicweb.view import EntityAdapter from cubicweb.predicates import is_instance, partial_relation_possible @@ -36,7 +34,6 @@ from cubicweb.schema import META_RTYPES, WORKFLOW_RTYPES, SYSTEM_RTYPES from cubicweb.view import EntityAdapter from cubicweb.predicates import is_instance, partial_relation_possible -from cubicweb.entities.adapters import ITreeAdapter def skip_meta(func): @@ -358,132 +355,6 @@ clones[child] = copy_entity(child, **kwargs) -@skip_meta -def tree_def(schema, etype, skiprtypes=(), skipetypes=()): - """Return a dictionary {etype: [(relation type, role)]} which are - reachable through composite relations from the root <etype>. Each key - gives the name of an entity type, associated to a list of relation/role - allowing to access to its children (which is expected to be a contained). - """ - contained = {} - if etype not in schema: - # Would occur, e.g., during migration. - warn('%s not found in schema, cannot build a container' % etype, - RuntimeWarning) - return {} - candidates = [schema[etype]] - while candidates: - eschema = candidates.pop() - assert eschema not in contained, (eschema.type, contained) - contained[eschema.type] = set() - for rschema, _, role in eschema.relation_definitions(): - if rschema.meta or rschema in skiprtypes: - continue - target_role = neg_role(role) - for rdef in rschema.rdefs.itervalues(): - if rdef.composite != role: - continue - target = getattr(rdef, target_role) - if target in skipetypes: - continue - if target not in contained: - candidates.append(target) - contained[eschema.type].add((rschema.type, role)) - candidates = list(set(candidates)) - return contained - - -class IContainedToITree(ITreeAdapter): - """Map IContained adapters to ITree, additionaly configured with a list of relations leading to - contained's children - """ - - children_relations = None # list of (relation type, role) to get entity's children - - @classmethod - def build_class(cls, etype, children_relations): - selector = is_instance(etype) - return type(etype + 'ITree', (cls,), - {'__select__': selector, - 'children_relations': children_relations}) - - @property - def tree_relation(self): - parent_relation = self.entity.cw_adapt_to('IContained').parent_relation() - return parent_relation[0] - - @property - def child_role(self): - parent_relation = self.entity.cw_adapt_to('IContained').parent_relation() - return parent_relation[1] - - def children_rql(self): - # XXX may have different shapes - return ' UNION '.join('(%s)' % self.entity.cw_related_rql(rel, role) - for rel, role in self.children_relations) - - def _children(self, entities=True): - if entities: - res = [] - else: - res = ResultSet([], '') - res.req = self._cw - for rel, role in self.children_relations: - res += self.entity.related(rel, role, entities=entities) - return res - - def different_type_children(self, entities=True): - """Return children entities of different type as this entity. - - According to the `entities` parameter, return entity objects or the - equivalent result set. - """ - res = self._children(entities) - etype = self.entity.cw_etype - if entities: - return [e for e in res if e.cw_etype != etype] - return res.filtered_rset(lambda x: x.cw_etype != etype, self.entity.cw_col) - - def same_type_children(self, entities=True): - """Return children entities of the same type as this entity. - - According to the `entities` parameter, return entity objects or the - equivalent result set. - """ - res = self._children(entities) - etype = self.entity.cw_etype - if entities: - return [e for e in res if e.cw_etype == etype] - return res.filtered_rset(lambda x: x.cw_etype == etype, self.entity.cw_col) - - def children(self, entities=True, sametype=False): - """Return children entities. - - According to the `entities` parameter, return entity objects or the - equivalent result set. - """ - if sametype: - return self.same_type_children(entities) - else: - return self._children(entities) - - -class IContainerToITree(IContainedToITree): - """Map IContainer adapters to ITree, similarly to :class:`IContainedToITree` but considering - parent as None - """ - - @classmethod - def build_class(cls, etype, children_relations): - selector = is_instance(etype) - return type(etype + 'ITree', (cls,), - {'__select__': selector, - 'children_relations': children_relations}) - - def parent(self): - return None - - def registration_callback(vreg): vreg.register_all(globals().values(), __name__) # Necessary during db-init or test mode. diff --git a/test/test_compound.py b/test/test_compound.py index b6a9b1abe346b8a2a6ce5a86eb47aa872b479aa2_dGVzdC90ZXN0X2NvbXBvdW5kLnB5..6402bd623407c747d8f624e682e29f5bdc123396_dGVzdC90ZXN0X2NvbXBvdW5kLnB5 100644 --- a/test/test_compound.py +++ b/test/test_compound.py @@ -17,7 +17,6 @@ from cubicweb.devtools.testlib import CubicWebTC -from cubes.compound.entities import (CompositeGraph, - copy_entity, - tree_def) +from cubes.compound.entities import (CompositeGraph, IClonableAdapter, + copy_entity) from cubes.compound.views import CloneAction @@ -23,5 +22,4 @@ from cubes.compound.views import CloneAction -from cubes.compound.entities import IClonableAdapter def sort_keys(dic): @@ -205,23 +203,6 @@ yield self.assertCountEqual, entities_graph, expected -class TreeTC(CubicWebTC): - - def test_tree(self): - defn = sort_keys(tree_def(self.schema, 'Agent')) - expected = { - 'OnlineAccount': [], - 'Agent': [('account', 'subject'), - ('biography', 'subject'), - ('narrated_by', 'object')], - 'Event': [], - 'Biography': [('event', 'subject')], - 'Anecdote': [('comments', 'object'), ('relates', 'subject')], - 'Comment': [('comments', 'object')], - } - self.assertEqual(defn, expected) - - class ContainerStructureTC(CubicWebTC): def test_adapters(self):