vregistry.py
changeset 7990 a673d1d9a738
parent 7272 771f594c12a2
child 8190 2a3c1b787688
equal deleted inserted replaced
7989:db76e8aaec29 7990:a673d1d9a738
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
   195         """
   195         """
   196         try:
   196         try:
   197             return self.select(__oid, *args, **kwargs)
   197             return self.select(__oid, *args, **kwargs)
   198         except (NoSelectableObject, ObjectNotFound):
   198         except (NoSelectableObject, ObjectNotFound):
   199             return None
   199             return None
   200     select_object = deprecated('[3.6] use select_or_none instead of select_object'
       
   201                                )(select_or_none)
       
   202 
   200 
   203     def possible_objects(self, *args, **kwargs):
   201     def possible_objects(self, *args, **kwargs):
   204         """return an iterator on possible objects in this registry for the given
   202         """return an iterator on possible objects in this registry for the given
   205         context
   203         context
   206         """
   204         """
   216 
   214 
   217         return None if not object apply (don't raise `NoSelectableObject` since
   215         return None if not object apply (don't raise `NoSelectableObject` since
   218         it's costly when searching appobjects using `possible_objects`
   216         it's costly when searching appobjects using `possible_objects`
   219         (e.g. searching for hooks).
   217         (e.g. searching for hooks).
   220         """
   218         """
   221         if len(args) > 1:
       
   222             warn('[3.5] only the request param can not be named when calling select*',
       
   223                  DeprecationWarning, stacklevel=3)
       
   224         score, winners = 0, None
   219         score, winners = 0, None
   225         for appobject in appobjects:
   220         for appobject in appobjects:
   226             appobjectscore = appobject.__select__(appobject, *args, **kwargs)
   221             appobjectscore = appobject.__select__(appobject, *args, **kwargs)
   227             if appobjectscore > score:
   222             if appobjectscore > score:
   228                 score, winners = appobjectscore, [appobject]
   223                 score, winners = appobjectscore, [appobject]
   238                 raise Exception(msg % (winners, args, kwargs.keys()))
   233                 raise Exception(msg % (winners, args, kwargs.keys()))
   239             self.error(msg, winners, args, kwargs.keys())
   234             self.error(msg, winners, args, kwargs.keys())
   240         # return the result of calling the appobject
   235         # return the result of calling the appobject
   241         return winners[0](*args, **kwargs)
   236         return winners[0](*args, **kwargs)
   242 
   237 
   243     select_best = deprecated('[3.6] select_best is now private')(_select_best)
       
   244 
       
   245     # these are overridden by set_log_methods below
   238     # these are overridden by set_log_methods below
   246     # only defining here to prevent pylint from complaining
   239     # only defining here to prevent pylint from complaining
   247     info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
   240     info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
   248 
   241 
   249 
   242 
   279         """
   272         """
   280         try:
   273         try:
   281             return super(VRegistry, self).__getitem__(name)
   274             return super(VRegistry, self).__getitem__(name)
   282         except KeyError:
   275         except KeyError:
   283             raise RegistryNotFound(name), None, sys.exc_info()[-1]
   276             raise RegistryNotFound(name), None, sys.exc_info()[-1]
   284 
       
   285     # dynamic selection methods ################################################
       
   286 
       
   287     @deprecated('[3.4] use vreg[registry].object_by_id(oid, *args, **kwargs)')
       
   288     def object_by_id(self, registry, oid, *args, **kwargs):
       
   289         """return object in <registry>.<oid>
       
   290 
       
   291         raise `ObjectNotFound` if not object with id <oid> in <registry>
       
   292         raise `AssertionError` if there is more than one object there
       
   293         """
       
   294         return self[registry].object_by_id(oid)
       
   295 
       
   296     @deprecated('[3.4] use vreg[registry].select(oid, *args, **kwargs)')
       
   297     def select(self, registry, oid, *args, **kwargs):
       
   298         """return the most specific object in <registry>.<oid> according to
       
   299         the given context
       
   300 
       
   301         raise `ObjectNotFound` if not object with id <oid> in <registry>
       
   302         raise `NoSelectableObject` if not object apply
       
   303         """
       
   304         return self[registry].select(oid, *args, **kwargs)
       
   305 
       
   306     @deprecated('[3.4] use vreg[registry].select_or_none(oid, *args, **kwargs)')
       
   307     def select_object(self, registry, oid, *args, **kwargs):
       
   308         """return the most specific object in <registry>.<oid> according to
       
   309         the given context, or None if no object apply
       
   310         """
       
   311         return self[registry].select_or_none(oid, *args, **kwargs)
       
   312 
       
   313     @deprecated('[3.4] use vreg[registry].possible_objects(*args, **kwargs)')
       
   314     def possible_objects(self, registry, *args, **kwargs):
       
   315         """return an iterator on possible objects in <registry> for the given
       
   316         context
       
   317         """
       
   318         return self[registry].possible_objects(*args, **kwargs)
       
   319 
   277 
   320     # methods for explicit (un)registration ###################################
   278     # methods for explicit (un)registration ###################################
   321 
   279 
   322     # default class, when no specific class set
   280     # default class, when no specific class set
   323     REGISTRY_FACTORY = {None: Registry}
   281     REGISTRY_FACTORY = {None: Registry}
   538 
   496 
   539 # XXX bw compat functions #####################################################
   497 # XXX bw compat functions #####################################################
   540 
   498 
   541 from cubicweb.appobject import objectify_selector, AndSelector, OrSelector, Selector
   499 from cubicweb.appobject import objectify_selector, AndSelector, OrSelector, Selector
   542 
   500 
   543 objectify_selector = deprecated('[3.4] objectify_selector has been moved to appobject module')(objectify_selector)
       
   544 
       
   545 Selector = class_moved(Selector)
   501 Selector = class_moved(Selector)
   546 
       
   547 @deprecated('[3.4] use & operator (binary and)')
       
   548 def chainall(*selectors, **kwargs):
       
   549     """return a selector chaining given selectors. If one of
       
   550     the selectors fail, selection will fail, else the returned score
       
   551     will be the sum of each selector'score
       
   552     """
       
   553     assert selectors
       
   554     # XXX do we need to create the AndSelector here, a tuple might be enough
       
   555     selector = AndSelector(*selectors)
       
   556     if 'name' in kwargs:
       
   557         selector.__name__ = kwargs['name']
       
   558     return selector
       
   559 
       
   560 @deprecated('[3.4] use | operator (binary or)')
       
   561 def chainfirst(*selectors, **kwargs):
       
   562     """return a selector chaining given selectors. If all
       
   563     the selectors fail, selection will fail, else the returned score
       
   564     will be the first non-zero selector score
       
   565     """
       
   566     assert selectors
       
   567     selector = OrSelector(*selectors)
       
   568     if 'name' in kwargs:
       
   569         selector.__name__ = kwargs['name']
       
   570     return selector