entity.py
branchtls-sprint
changeset 1177 7074698c6522
parent 1154 9b23a6836c32
child 1264 fe2934a7df7f
equal deleted inserted replaced
1176:0ff3d29e91c9 1177:7074698c6522
    52         for tag in tags:
    52         for tag in tags:
    53             if tag in MODE_TAGS:
    53             if tag in MODE_TAGS:
    54                 EditBox.rmode.set_rtag(tag, rtype, role, stype, otype)
    54                 EditBox.rmode.set_rtag(tag, rtype, role, stype, otype)
    55             elif tag in CATEGORY_TAGS:
    55             elif tag in CATEGORY_TAGS:
    56                 AutomaticEntityForm.rcategories.set_rtag(tag, rtype, role, stype, otype)
    56                 AutomaticEntityForm.rcategories.set_rtag(tag, rtype, role, stype, otype)
    57             elif tag == 'inlined':
    57             elif tag == 'inlineview':
    58                 AutomaticEntityForm.rinline.set_rtag(True, rtype, role, stype, otype)
    58                 AutomaticEntityForm.rinlined.set_rtag(True, rtype, role, stype, otype)
    59             else:
    59             else:
    60                 raise ValueError(tag)
    60                 raise ValueError(tag)
    61             
    61             
    62 except ImportError:
    62 except ImportError:
    63     AutomaticEntityForm = None
    63     AutomaticEntityForm = None
    64     
    64     
    65     def dispatch_rtags(*args):
    65     def dispatch_rtags(*args):
    66         pass
    66         pass
    67     
    67 
       
    68 def _get_etype(bases, classdict):
       
    69     try:
       
    70         return classdict['id']
       
    71     except KeyError:
       
    72         for base in bases:
       
    73             etype = getattr(base, 'id', None)
       
    74             if etype and etype != 'Any':
       
    75                 return etype
       
    76             
       
    77 def _get_defs(attr, name, bases, classdict):
       
    78     try:
       
    79         yield name, classdict.pop(attr)
       
    80     except KeyError:
       
    81         for base in bases:
       
    82             try:
       
    83                 value = getattr(base, attr)
       
    84                 delattr(base, attr)
       
    85                 yield base.__name__, value
       
    86             except AttributeError:
       
    87                 continue
       
    88             
    68 class metaentity(type):
    89 class metaentity(type):
    69     """this metaclass sets the relation tags on the entity class
    90     """this metaclass sets the relation tags on the entity class
    70     and deals with the `widgets` attribute
    91     and deals with the `widgets` attribute
    71     """
    92     """
    72     def __new__(mcs, name, bases, classdict):
    93     def __new__(mcs, name, bases, classdict):
    73         # collect baseclass' rtags
    94         # collect baseclass' rtags
    74         if '__rtags__' in classdict:
    95         etype = _get_etype(bases, classdict)
    75             etype = classdict['id']
    96         if etype and AutomaticEntityForm is not None:
    76             warn('%s: __rtags__ is deprecated' % name, DeprecationWarning)
    97             for name, rtags in _get_defs('__rtags__', name, bases, classdict):
    77             for relation, tags in classdict.pop('__rtags__').iteritems():
    98                 warn('%s: __rtags__ is deprecated' % name, DeprecationWarning)
    78                 # tags must become an iterable
    99                 for relation, tags in rtags.iteritems():
    79                 if isinstance(tags, basestring):
   100                     # tags must become an iterable
    80                     tags = (tags,)
   101                     if isinstance(tags, basestring):
    81                 # relation must become a 3-uple (rtype, targettype, role)
   102                         tags = (tags,)
    82                 if isinstance(relation, basestring):
   103                     # relation must become a 3-uple (rtype, targettype, role)
    83                     dispatch_rtags(tags, relation, 'subject', etype, '*')
   104                     if isinstance(relation, basestring):
    84                     dispatch_rtags(tags, relation, 'object', '*', etype)
   105                         dispatch_rtags(tags, relation, 'subject', etype, '*')
    85                 elif len(relation) == 1: # useful ?
   106                         dispatch_rtags(tags, relation, 'object', '*', etype)
    86                     dispatch_rtags(tags, relation[0], 'subject', etype, '*')
   107                     elif len(relation) == 1: # useful ?
    87                     dispatch_rtags(tags, relation[0], 'object', '*', etype)
   108                         dispatch_rtags(tags, relation[0], 'subject', etype, '*')
    88                 elif len(relation) == 2:
   109                         dispatch_rtags(tags, relation[0], 'object', '*', etype)
    89                     rtype, ttype = relation
   110                     elif len(relation) == 2:
    90                     ttype = bw_normalize_etype(ttype) # XXX bw compat
   111                         rtype, ttype = relation
    91                     dispatch_rtags(tags, rtype, 'subject', etype, ttype)
   112                         ttype = bw_normalize_etype(ttype) # XXX bw compat
    92                     dispatch_rtags(tags, rtype, 'object', ttype, etype)
       
    93                 elif len(relation) == 3:
       
    94                     rtype, ttype, role = relation
       
    95                     ttype = bw_normalize_etype(ttype)
       
    96                     if role == 'subject':
       
    97                         dispatch_rtags(tags, rtype, 'subject', etype, ttype)
   113                         dispatch_rtags(tags, rtype, 'subject', etype, ttype)
       
   114                         dispatch_rtags(tags, rtype, 'object', ttype, etype)
       
   115                     elif len(relation) == 3:
       
   116                         rtype, ttype, role = relation
       
   117                         ttype = bw_normalize_etype(ttype)
       
   118                         if role == 'subject':
       
   119                             dispatch_rtags(tags, rtype, 'subject', etype, ttype)
       
   120                         else:
       
   121                             dispatch_rtags(tags, rtype, 'object', ttype, etype)
    98                     else:
   122                     else:
    99                         dispatch_rtags(tags, rtype, 'object', ttype, etype)
   123                         raise ValueError('bad rtag definition (%r)' % (relation,))
   100                 else:
   124             for name, widgets in _get_defs('widgets', name, bases, classdict):
   101                     raise ValueError('bad rtag definition (%r)' % (relation,))
   125                 warn('%s: widgets is deprecated' % name, DeprecationWarning)
   102         if 'widgets' in classdict and AutomaticEntityForm is not None:
   126                 for rtype, wdgname in widgets.iteritems():
   103             etype = classdict['id']
   127                     AutomaticEntityForm.rwidgets.set_rtag(wdgname, rtype, 'subject', etype)
   104             warn('%s: widgets is deprecated' % name, DeprecationWarning)
       
   105             for relation, wdgname in classdict.pop('widgets').iteritems():
       
   106                 AutomaticEntityForm.rwidgets.set_rtag(wdgname, rtype, 'subject', etype)
       
   107         return super(metaentity, mcs).__new__(mcs, name, bases, classdict)
   128         return super(metaentity, mcs).__new__(mcs, name, bases, classdict)
   108 
   129 
   109 
   130 
   110 class Entity(AppRsetObject, dict):
   131 class Entity(AppRsetObject, dict):
   111     """an entity instance has e_schema automagically set on
   132     """an entity instance has e_schema automagically set on