schema.py
changeset 2183 9dfa88ac9f49
parent 2181 94ca417b9b07
child 2185 09d0445641e2
equal deleted inserted replaced
2181:94ca417b9b07 2183:9dfa88ac9f49
    17 from logilab.common.compat import any
    17 from logilab.common.compat import any
    18 
    18 
    19 from yams import BadSchemaDefinition, buildobjs as ybo
    19 from yams import BadSchemaDefinition, buildobjs as ybo
    20 from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema
    20 from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema
    21 from yams.constraints import BaseConstraint, StaticVocabularyConstraint
    21 from yams.constraints import BaseConstraint, StaticVocabularyConstraint
    22 from yams.reader import (CONSTRAINTS, RelationFileReader, PyFileReader,
    22 from yams.reader import CONSTRAINTS, PyFileReader, SchemaLoader
    23                          SchemaLoader)
       
    24 
    23 
    25 from rql import parse, nodes, RQLSyntaxError, TypeResolverException
    24 from rql import parse, nodes, RQLSyntaxError, TypeResolverException
    26 
    25 
    27 from cubicweb import ETYPE_NAME_MAP, ValidationError, Unauthorized
    26 from cubicweb import ETYPE_NAME_MAP, ValidationError, Unauthorized
    28 from cubicweb import set_log_methods
    27 from cubicweb import set_log_methods
   855 
   854 
   856 PyFileReader.context['WorkflowableEntityType'] = WorkflowableEntityType
   855 PyFileReader.context['WorkflowableEntityType'] = WorkflowableEntityType
   857 
   856 
   858 # schema loading ##############################################################
   857 # schema loading ##############################################################
   859 
   858 
   860 class CubicWebRelationFileReader(RelationFileReader):
       
   861     """cubicweb specific relation file reader, handling additional RQL
       
   862     constraints on a relation definition
       
   863     """
       
   864 
       
   865     def handle_constraint(self, rdef, constraint_text):
       
   866         """arbitrary constraint is an rql expression for cubicweb"""
       
   867         if not rdef.constraints:
       
   868             rdef.constraints = []
       
   869         rdef.constraints.append(RQLVocabularyConstraint(constraint_text))
       
   870 
       
   871     def process_properties(self, rdef, relation_def):
       
   872         if 'inline' in relation_def:
       
   873             rdef.inlined = True
       
   874         RelationFileReader.process_properties(self, rdef, relation_def)
       
   875 
       
   876 
       
   877 CONSTRAINTS['RQLConstraint'] = RQLConstraint
   859 CONSTRAINTS['RQLConstraint'] = RQLConstraint
   878 CONSTRAINTS['RQLUniqueConstraint'] = RQLUniqueConstraint
   860 CONSTRAINTS['RQLUniqueConstraint'] = RQLUniqueConstraint
   879 CONSTRAINTS['RQLVocabularyConstraint'] = RQLVocabularyConstraint
   861 CONSTRAINTS['RQLVocabularyConstraint'] = RQLVocabularyConstraint
   880 PyFileReader.context.update(CONSTRAINTS)
   862 PyFileReader.context.update(CONSTRAINTS)
   881 
   863 
   883 class BootstrapSchemaLoader(SchemaLoader):
   865 class BootstrapSchemaLoader(SchemaLoader):
   884     """cubicweb specific schema loader, loading only schema necessary to read
   866     """cubicweb specific schema loader, loading only schema necessary to read
   885     the persistent schema
   867     the persistent schema
   886     """
   868     """
   887     schemacls = CubicWebSchema
   869     schemacls = CubicWebSchema
   888     SchemaLoader.file_handlers.update({'.rel' : CubicWebRelationFileReader,
       
   889                                        })
       
   890 
   870 
   891     def load(self, config, path=(), **kwargs):
   871     def load(self, config, path=(), **kwargs):
   892         """return a Schema instance from the schema definition read
   872         """return a Schema instance from the schema definition read
   893         from <directory>
   873         from <directory>
   894         """
   874         """