vregistry.py
changeset 2788 8d3dbe577d3a
parent 2771 8074dd88e21b
child 2820 66b31686d92b
equal deleted inserted replaced
2787:ece5bbed3cd2 2788:8d3dbe577d3a
   123             result += objs
   123             result += objs
   124         return result
   124         return result
   125 
   125 
   126     # dynamic selection methods ################################################
   126     # dynamic selection methods ################################################
   127 
   127 
   128     @deprecated('use select instead of object_by_id')
   128     @deprecated('[3.5] use select instead of object_by_id')
   129     def object_by_id(self, oid, *args, **kwargs):
   129     def object_by_id(self, oid, *args, **kwargs):
   130         """return object with the given oid. Only one object is expected to be
   130         """return object with the given oid. Only one object is expected to be
   131         found.
   131         found.
   132 
   132 
   133         raise `ObjectNotFound` if not object with id <oid> in <registry>
   133         raise `ObjectNotFound` if not object with id <oid> in <registry>
   152         """
   152         """
   153         try:
   153         try:
   154             return self.select(oid, *args, **kwargs)
   154             return self.select(oid, *args, **kwargs)
   155         except (NoSelectableObject, ObjectNotFound):
   155         except (NoSelectableObject, ObjectNotFound):
   156             return None
   156             return None
   157     select_object = deprecated('use select_or_none instead of select_object'
   157     select_object = deprecated('[3.5] use select_or_none instead of select_object'
   158                                )(select_or_none)
   158                                )(select_or_none)
   159 
   159 
   160     def possible_objects(self, *args, **kwargs):
   160     def possible_objects(self, *args, **kwargs):
   161         """return an iterator on possible objects in this registry for the given
   161         """return an iterator on possible objects in this registry for the given
   162         context
   162         context
   195                 raise Exception('select ambiguity, args: %s\nkwargs: %s %s'
   195                 raise Exception('select ambiguity, args: %s\nkwargs: %s %s'
   196                                 % (args, kwargs.keys(),
   196                                 % (args, kwargs.keys(),
   197                                    [repr(v) for v in winners]))
   197                                    [repr(v) for v in winners]))
   198         # return the result of calling the appobject
   198         # return the result of calling the appobject
   199         return winners[0](*args, **kwargs)
   199         return winners[0](*args, **kwargs)
   200     select_best = deprecated('select_best is now private')(_select_best)
   200     select_best = deprecated('[3.5] select_best is now private')(_select_best)
   201 
   201 
   202 class VRegistry(dict):
   202 class VRegistry(dict):
   203     """class responsible to register, propose and select the various
   203     """class responsible to register, propose and select the various
   204     elements used to build the web interface. Currently, we have templates,
   204     elements used to build the web interface. Currently, we have templates,
   205     views, actions and components.
   205     views, actions and components.
   222         except KeyError:
   222         except KeyError:
   223             raise RegistryNotFound(name), None, sys.exc_info()[-1]
   223             raise RegistryNotFound(name), None, sys.exc_info()[-1]
   224 
   224 
   225     # dynamic selection methods ################################################
   225     # dynamic selection methods ################################################
   226 
   226 
   227     @deprecated('use vreg[registry].object_by_id(oid, *args, **kwargs)')
   227     @deprecated('[3.4] use vreg[registry].object_by_id(oid, *args, **kwargs)')
   228     def object_by_id(self, registry, oid, *args, **kwargs):
   228     def object_by_id(self, registry, oid, *args, **kwargs):
   229         """return object in <registry>.<oid>
   229         """return object in <registry>.<oid>
   230 
   230 
   231         raise `ObjectNotFound` if not object with id <oid> in <registry>
   231         raise `ObjectNotFound` if not object with id <oid> in <registry>
   232         raise `AssertionError` if there is more than one object there
   232         raise `AssertionError` if there is more than one object there
   233         """
   233         """
   234         return self[registry].object_by_id(oid)
   234         return self[registry].object_by_id(oid)
   235 
   235 
   236     @deprecated('use vreg[registry].select(oid, *args, **kwargs)')
   236     @deprecated('[3.4] use vreg[registry].select(oid, *args, **kwargs)')
   237     def select(self, registry, oid, *args, **kwargs):
   237     def select(self, registry, oid, *args, **kwargs):
   238         """return the most specific object in <registry>.<oid> according to
   238         """return the most specific object in <registry>.<oid> according to
   239         the given context
   239         the given context
   240 
   240 
   241         raise `ObjectNotFound` if not object with id <oid> in <registry>
   241         raise `ObjectNotFound` if not object with id <oid> in <registry>
   242         raise `NoSelectableObject` if not object apply
   242         raise `NoSelectableObject` if not object apply
   243         """
   243         """
   244         return self[registry].select(oid, *args, **kwargs)
   244         return self[registry].select(oid, *args, **kwargs)
   245 
   245 
   246     @deprecated('use vreg[registry].select_or_none(oid, *args, **kwargs)')
   246     @deprecated('[3.4] use vreg[registry].select_or_none(oid, *args, **kwargs)')
   247     def select_object(self, registry, oid, *args, **kwargs):
   247     def select_object(self, registry, oid, *args, **kwargs):
   248         """return the most specific object in <registry>.<oid> according to
   248         """return the most specific object in <registry>.<oid> according to
   249         the given context, or None if no object apply
   249         the given context, or None if no object apply
   250         """
   250         """
   251         return self[registry].select_or_none(oid, *args, **kwargs)
   251         return self[registry].select_or_none(oid, *args, **kwargs)
   252 
   252 
   253     @deprecated('use vreg[registry].possible_objects(*args, **kwargs)')
   253     @deprecated('[3.4] use vreg[registry].possible_objects(*args, **kwargs)')
   254     def possible_objects(self, registry, *args, **kwargs):
   254     def possible_objects(self, registry, *args, **kwargs):
   255         """return an iterator on possible objects in <registry> for the given
   255         """return an iterator on possible objects in <registry> for the given
   256         context
   256         context
   257         """
   257         """
   258         return self[registry].possible_objects(*args, **kwargs)
   258         return self[registry].possible_objects(*args, **kwargs)
   432 
   432 
   433 # XXX bw compat functions #####################################################
   433 # XXX bw compat functions #####################################################
   434 
   434 
   435 from cubicweb.appobject import objectify_selector, AndSelector, OrSelector, Selector
   435 from cubicweb.appobject import objectify_selector, AndSelector, OrSelector, Selector
   436 
   436 
   437 objectify_selector = deprecated('objectify_selector has been moved to appobject module')(objectify_selector)
   437 objectify_selector = deprecated('[3.4] objectify_selector has been moved to appobject module')(objectify_selector)
   438 
   438 
   439 Selector = class_moved(Selector)
   439 Selector = class_moved(Selector)
   440 
   440 
   441 @deprecated('use & operator (binary and)')
   441 @deprecated('[3.4] use & operator (binary and)')
   442 def chainall(*selectors, **kwargs):
   442 def chainall(*selectors, **kwargs):
   443     """return a selector chaining given selectors. If one of
   443     """return a selector chaining given selectors. If one of
   444     the selectors fail, selection will fail, else the returned score
   444     the selectors fail, selection will fail, else the returned score
   445     will be the sum of each selector'score
   445     will be the sum of each selector'score
   446     """
   446     """
   449     selector = AndSelector(*selectors)
   449     selector = AndSelector(*selectors)
   450     if 'name' in kwargs:
   450     if 'name' in kwargs:
   451         selector.__name__ = kwargs['name']
   451         selector.__name__ = kwargs['name']
   452     return selector
   452     return selector
   453 
   453 
   454 @deprecated('use | operator (binary or)')
   454 @deprecated('[3.4] use | operator (binary or)')
   455 def chainfirst(*selectors, **kwargs):
   455 def chainfirst(*selectors, **kwargs):
   456     """return a selector chaining given selectors. If all
   456     """return a selector chaining given selectors. If all
   457     the selectors fail, selection will fail, else the returned score
   457     the selectors fail, selection will fail, else the returned score
   458     will be the first non-zero selector score
   458     will be the first non-zero selector score
   459     """
   459     """