schema.py
branch3.5
changeset 2931 17224e90a1c4
parent 2920 64322aa83a1d
parent 2926 4484387ed012
child 2957 2daabf4c646e
equal deleted inserted replaced
2924:b5aadbd3fc5b 2931:17224e90a1c4
    14 from warnings import warn
    14 from warnings import warn
    15 
    15 
    16 from logilab.common.decorators import cached, clear_cache, monkeypatch
    16 from logilab.common.decorators import cached, clear_cache, monkeypatch
    17 from logilab.common.logging_ext import set_log_methods
    17 from logilab.common.logging_ext import set_log_methods
    18 from logilab.common.deprecation import deprecated
    18 from logilab.common.deprecation import deprecated
       
    19 from logilab.common.graph import get_cycles
    19 from logilab.common.compat import any
    20 from logilab.common.compat import any
    20 
    21 
    21 from yams import BadSchemaDefinition, buildobjs as ybo
    22 from yams import BadSchemaDefinition, buildobjs as ybo
    22 from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema
    23 from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema
    23 from yams.constraints import (BaseConstraint, StaticVocabularyConstraint,
    24 from yams.constraints import (BaseConstraint, StaticVocabularyConstraint,
    56 
    57 
    57 # schema entities created from serialized schema have an eid rproperty
    58 # schema entities created from serialized schema have an eid rproperty
    58 ybo.ETYPE_PROPERTIES += ('eid',)
    59 ybo.ETYPE_PROPERTIES += ('eid',)
    59 ybo.RTYPE_PROPERTIES += ('eid',)
    60 ybo.RTYPE_PROPERTIES += ('eid',)
    60 ybo.RDEF_PROPERTIES += ('eid',)
    61 ybo.RDEF_PROPERTIES += ('eid',)
       
    62 
       
    63 
       
    64 # XXX same algorithm as in reorder_cubes and probably other place,
       
    65 # may probably extract a generic function
       
    66 def order_eschemas(eschemas):
       
    67     """return entity schemas ordered such that entity types which specializes an
       
    68     other one appears after that one
       
    69     """
       
    70     graph = {}
       
    71     for eschema in eschemas:
       
    72         if eschema.specializes():
       
    73             graph[eschema] = set((eschema.specializes(),))
       
    74         else:
       
    75             graph[eschema] = set()
       
    76     cycles = get_cycles(graph)
       
    77     if cycles:
       
    78         cycles = '\n'.join(' -> '.join(cycle) for cycle in cycles)
       
    79         raise Exception('cycles in entity schema specialization: %s'
       
    80                         % cycles)
       
    81     eschemas = []
       
    82     while graph:
       
    83         # sorted to get predictable results
       
    84         for eschema, deps in sorted(graph.items()):
       
    85             if not deps:
       
    86                 eschemas.append(eschema)
       
    87                 del graph[eschema]
       
    88                 for deps in graph.itervalues():
       
    89                     try:
       
    90                         deps.remove(eschema)
       
    91                     except KeyError:
       
    92                         continue
       
    93     return eschemas
    61 
    94 
    62 def bw_normalize_etype(etype):
    95 def bw_normalize_etype(etype):
    63     if etype in ETYPE_NAME_MAP:
    96     if etype in ETYPE_NAME_MAP:
    64         msg = '%s has been renamed to %s, please update your code' % (
    97         msg = '%s has been renamed to %s, please update your code' % (
    65             etype, ETYPE_NAME_MAP[etype])
    98             etype, ETYPE_NAME_MAP[etype])
   802         return self._check(session, **kwargs)
   835         return self._check(session, **kwargs)
   803 
   836 
   804 PyFileReader.context['RRQLExpression'] = yobsolete(RRQLExpression)
   837 PyFileReader.context['RRQLExpression'] = yobsolete(RRQLExpression)
   805 
   838 
   806 # workflow extensions #########################################################
   839 # workflow extensions #########################################################
       
   840 
   807 from yams.buildobjs import _add_relation as yams_add_relation
   841 from yams.buildobjs import _add_relation as yams_add_relation
   808 
   842 
   809 class workflowable_definition(ybo.metadefinition):
   843 class workflowable_definition(ybo.metadefinition):
   810     """extends default EntityType's metaclass to add workflow relations
   844     """extends default EntityType's metaclass to add workflow relations
   811     (i.e. in_state and wf_info_for).
   845     (i.e. in_state and wf_info_for).