schema.py
changeset 2128 464edb198faa
parent 2126 a25859917ccc
child 2144 51c84d585456
equal deleted inserted replaced
2127:bc86aa68cc43 2128:464edb198faa
    60         msg = '%s has been renamed to %s, please update your code' % (
    60         msg = '%s has been renamed to %s, please update your code' % (
    61             etype, ETYPE_NAME_MAP[etype])
    61             etype, ETYPE_NAME_MAP[etype])
    62         warn(msg, DeprecationWarning, stacklevel=4)
    62         warn(msg, DeprecationWarning, stacklevel=4)
    63         etype = ETYPE_NAME_MAP[etype]
    63         etype = ETYPE_NAME_MAP[etype]
    64     return etype
    64     return etype
    65 
       
    66 # monkey path yams.builder.RelationDefinition to support a new wildcard type '@'
       
    67 # corresponding to system entity (ie meta but not schema)
       
    68 def _actual_types(self, schema, etype):
       
    69     # two bits of error checking & reporting :
       
    70     if type(etype) not in (str, list, tuple):
       
    71         raise RuntimeError, ('Entity types must not be instances but strings or'
       
    72                              ' list/tuples thereof. Ex. (bad, good) : '
       
    73                              'SubjectRelation(Foo), SubjectRelation("Foo"). '
       
    74                              'Hence, %r is not acceptable.' % etype)
       
    75     # real work :
       
    76     if etype == '**':
       
    77         return self._pow_etypes(schema)
       
    78     if isinstance(etype, (tuple, list)):
       
    79         return etype
       
    80     if '*' in etype or '@' in etype:
       
    81         assert len(etype) in (1, 2)
       
    82         etypes = ()
       
    83         if '*' in etype:
       
    84             etypes += tuple(self._wildcard_etypes(schema))
       
    85         # XXX deprecate, too clumsy
       
    86         if '@' in etype:
       
    87             etypes += tuple(system_etypes(schema))
       
    88         return etypes
       
    89     return (etype,)
       
    90 ybo.RelationDefinition._actual_types = _actual_types
       
    91 
    65 
    92 
    66 
    93 ## cubicweb provides a RichString class for convenience
    67 ## cubicweb provides a RichString class for convenience
    94 class RichString(ybo.String):
    68 class RichString(ybo.String):
    95     """Convenience RichString attribute type
    69     """Convenience RichString attribute type
   294         iter = super(CubicWebEntitySchema, self).attribute_definitions()
   268         iter = super(CubicWebEntitySchema, self).attribute_definitions()
   295         for rschema, attrschema in iter:
   269         for rschema, attrschema in iter:
   296             if rschema.type == 'has_text':
   270             if rschema.type == 'has_text':
   297                 continue
   271                 continue
   298             yield rschema, attrschema
   272             yield rschema, attrschema
       
   273 
       
   274     def main_attribute(self):
       
   275         """convenience method that returns the *main* (i.e. the first non meta)
       
   276         attribute defined in the entity schema
       
   277         """
       
   278         for rschema, _ in self.attribute_definitions():
       
   279             if not (rschema in META_RELATIONS_TYPES
       
   280                     or self.is_metadata(rschema)):
       
   281                 return rschema
   299 
   282 
   300     def add_subject_relation(self, rschema):
   283     def add_subject_relation(self, rschema):
   301         """register the relation schema as possible subject relation"""
   284         """register the relation schema as possible subject relation"""
   302         super(CubicWebEntitySchema, self).add_subject_relation(rschema)
   285         super(CubicWebEntitySchema, self).add_subject_relation(rschema)
   303         self._update_has_text()
   286         self._update_has_text()