registry.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This file is part of Logilab-common.
  5. #
  6. # Logilab-common is free software: you can redistribute it and/or modify it
  7. # under the terms of the GNU Lesser General Public License as published by the
  8. # Free Software Foundation, either version 2.1 of the License, or (at your
  9. # option) any later version.
  10. #
  11. # Logilab-common is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License along
  17. # with Logilab-common. If not, see <http://www.gnu.org/licenses/>.
  18. """This module provides bases for predicates dispatching (the pattern in use
  19. here is similar to what's refered as multi-dispatch or predicate-dispatch in the
  20. literature, though a bit different since the idea is to select across different
  21. implementation 'e.g. classes), not to dispatch a message to a function or
  22. method. It contains the following classes:
  23. * :class:`RegistryStore`, the top level object which loads implementation
  24. objects and stores them into registries. You'll usually use it to access
  25. registries and their contained objects;
  26. * :class:`Registry`, the base class which contains objects semantically grouped
  27. (for instance, sharing a same API, hence the 'implementation' name). You'll
  28. use it to select the proper implementation according to a context. Notice you
  29. may use registries on their own without using the store.
  30. .. Note::
  31. implementation objects are usually designed to be accessed through the
  32. registry and not by direct instantiation, besides to use it as base classe.
  33. The selection procedure is delegated to a selector, which is responsible for
  34. scoring the object according to some context. At the end of the selection, if an
  35. implementation has been found, an instance of this class is returned. A selector
  36. is built from one or more predicates combined together using AND, OR, NOT
  37. operators (actually `&`, `|` and `~`). You'll thus find some base classes to
  38. build predicates:
  39. * :class:`Predicate`, the abstract base predicate class
  40. * :class:`AndPredicate`, :class:`OrPredicate`, :class:`NotPredicate`, which you
  41. shouldn't have to use directly. You'll use `&`, `|` and '~' operators between
  42. predicates directly
  43. * :func:`objectify_predicate`
  44. You'll eventually find one concrete predicate: :class:`yes`
  45. .. autoclass:: RegistryStore
  46. .. autoclass:: Registry
  47. Predicates
  48. ----------
  49. .. autoclass:: Predicate
  50. .. autofunc:: objectify_predicate
  51. .. autoclass:: yes
  52. Debugging
  53. ---------
  54. .. autoclass:: traced_selection
  55. Exceptions
  56. ----------
  57. .. autoclass:: RegistryException
  58. .. autoclass:: RegistryNotFound
  59. .. autoclass:: ObjectNotFound
  60. .. autoclass:: NoSelectableObject
  61. """
  62. __docformat__ = "restructuredtext en"
  63. import sys
  64. import types
  65. import weakref
  66. from os import listdir, stat
  67. from os.path import join, isdir, exists
  68. from logging import getLogger
  69. from logilab.common.logging_ext import set_log_methods
  70. class RegistryException(Exception):
  71. """Base class for registry exception."""
  72. class RegistryNotFound(RegistryException):
  73. """Raised when an unknown registry is requested.
  74. This is usually a programming/typo error.
  75. """
  76. class ObjectNotFound(RegistryException):
  77. """Raised when an unregistered object is requested.
  78. This may be a programming/typo or a misconfiguration error.
  79. """
  80. class NoSelectableObject(RegistryException):
  81. """Raised when no object is selectable for a given context."""
  82. def __init__(self, args, kwargs, objects):
  83. self.args = args
  84. self.kwargs = kwargs
  85. self.objects = objects
  86. def __str__(self):
  87. return ('args: %s, kwargs: %s\ncandidates: %s'
  88. % (self.args, self.kwargs.keys(), self.objects))
  89. def _toload_info(path, extrapath, _toload=None):
  90. """Return a dictionary of <modname>: <modpath> and an ordered list of
  91. (file, module name) to load
  92. """
  93. from logilab.common.modutils import modpath_from_file
  94. if _toload is None:
  95. assert isinstance(path, list)
  96. _toload = {}, []
  97. for fileordir in path:
  98. if isdir(fileordir) and exists(join(fileordir, '__init__.py')):
  99. subfiles = [join(fileordir, fname) for fname in listdir(fileordir)]
  100. _toload_info(subfiles, extrapath, _toload)
  101. elif fileordir[-3:] == '.py':
  102. modpath = modpath_from_file(fileordir, extrapath)
  103. # omit '__init__' from package's name to avoid loading that module
  104. # once for each name when it is imported by some other object
  105. # module. This supposes import in modules are done as::
  106. #
  107. # from package import something
  108. #
  109. # not::
  110. #
  111. # from package.__init__ import something
  112. #
  113. # which seems quite correct.
  114. if modpath[-1] == '__init__':
  115. modpath.pop()
  116. modname = '.'.join(modpath)
  117. _toload[0][modname] = fileordir
  118. _toload[1].append((fileordir, modname))
  119. return _toload
  120. def classid(cls):
  121. """returns a unique identifier for an object class"""
  122. return '%s.%s' % (cls.__module__, cls.__name__)
  123. def class_registries(cls, registryname):
  124. """return a tuple of registry names (see __registries__)"""
  125. if registryname:
  126. return (registryname,)
  127. return cls.__registries__
  128. class Registry(dict):
  129. """The registry store a set of implementations associated to identifier:
  130. * to each identifier are associated a list of implementations
  131. * to select an implementation of a given identifier, you should use one of the
  132. :meth:`select` or :meth:`select_or_none` method
  133. * to select a list of implementations for a context, you should use the
  134. :meth:`possible_objects` method
  135. * dictionary like access to an identifier will return the bare list of
  136. implementations for this identifier.
  137. To be usable in a registry, the only requirement is to have a `__select__`
  138. attribute.
  139. At the end of the registration process, the :meth:`__registered__`
  140. method is called on each registered object which have them, given the
  141. registry in which it's registered as argument.
  142. Registration methods:
  143. .. automethod: register
  144. .. automethod: unregister
  145. Selection methods:
  146. .. automethod: select
  147. .. automethod: select_or_none
  148. .. automethod: possible_objects
  149. .. automethod: object_by_id
  150. """
  151. def __init__(self, debugmode):
  152. super(Registry, self).__init__()
  153. self.debugmode = debugmode
  154. def __getitem__(self, name):
  155. """return the registry (list of implementation objects) associated to
  156. this name
  157. """
  158. try:
  159. return super(Registry, self).__getitem__(name)
  160. except KeyError:
  161. raise ObjectNotFound(name), None, sys.exc_info()[-1]
  162. def initialization_completed(self):
  163. """call method __registered__() on registered objects when the callback
  164. is defined"""
  165. for objects in self.itervalues():
  166. for objectcls in objects:
  167. registered = getattr(objectcls, '__registered__', None)
  168. if registered:
  169. registered(self)
  170. if self.debugmode:
  171. wrap_predicates(_lltrace)
  172. def register(self, obj, oid=None, clear=False):
  173. """base method to add an object in the registry"""
  174. assert not '__abstract__' in obj.__dict__
  175. assert obj.__select__
  176. oid = oid or obj.__regid__
  177. assert oid
  178. if clear:
  179. objects = self[oid] = []
  180. else:
  181. objects = self.setdefault(oid, [])
  182. assert not obj in objects, \
  183. 'object %s is already registered' % obj
  184. objects.append(obj)
  185. def register_and_replace(self, obj, replaced):
  186. """remove <replaced> and register <obj>"""
  187. # XXXFIXME this is a duplication of unregister()
  188. # remove register_and_replace in favor of unregister + register
  189. # or simplify by calling unregister then register here
  190. if not isinstance(replaced, basestring):
  191. replaced = classid(replaced)
  192. # prevent from misspelling
  193. assert obj is not replaced, 'replacing an object by itself: %s' % obj
  194. registered_objs = self.get(obj.__regid__, ())
  195. for index, registered in enumerate(registered_objs):
  196. if classid(registered) == replaced:
  197. del registered_objs[index]
  198. break
  199. else:
  200. self.warning('trying to replace %s that is not registered with %s',
  201. replaced, obj)
  202. self.register(obj)
  203. def unregister(self, obj):
  204. """remove object <obj> from this registry"""
  205. clsid = classid(obj)
  206. oid = obj.__regid__
  207. for registered in self.get(oid, ()):
  208. # use classid() to compare classes because vreg will probably
  209. # have its own version of the class, loaded through execfile
  210. if classid(registered) == clsid:
  211. self[oid].remove(registered)
  212. break
  213. else:
  214. self.warning('can\'t remove %s, no id %s in the registry',
  215. clsid, oid)
  216. def all_objects(self):
  217. """return a list containing all objects in this registry.
  218. """
  219. result = []
  220. for objs in self.values():
  221. result += objs
  222. return result
  223. # dynamic selection methods ################################################
  224. def object_by_id(self, oid, *args, **kwargs):
  225. """return object with the `oid` identifier. Only one object is expected
  226. to be found.
  227. raise :exc:`ObjectNotFound` if not object with id <oid> in <registry>
  228. raise :exc:`AssertionError` if there is more than one object there
  229. """
  230. objects = self[oid]
  231. assert len(objects) == 1, objects
  232. return objects[0](*args, **kwargs)
  233. def select(self, __oid, *args, **kwargs):
  234. """return the most specific object among those with the given oid
  235. according to the given context.
  236. raise :exc:`ObjectNotFound` if not object with id <oid> in <registry>
  237. raise :exc:`NoSelectableObject` if not object apply
  238. """
  239. obj = self._select_best(self[__oid], *args, **kwargs)
  240. if obj is None:
  241. raise NoSelectableObject(args, kwargs, self[__oid] )
  242. return obj
  243. def select_or_none(self, __oid, *args, **kwargs):
  244. """return the most specific object among those with the given oid
  245. according to the given context, or None if no object applies.
  246. """
  247. try:
  248. return self.select(__oid, *args, **kwargs)
  249. except (NoSelectableObject, ObjectNotFound):
  250. return None
  251. def possible_objects(self, *args, **kwargs):
  252. """return an iterator on possible objects in this registry for the given
  253. context
  254. """
  255. for objects in self.itervalues():
  256. obj = self._select_best(objects, *args, **kwargs)
  257. if obj is None:
  258. continue
  259. yield obj
  260. def _select_best(self, objects, *args, **kwargs):
  261. """return an instance of the most specific object according
  262. to parameters
  263. return None if not object apply (don't raise `NoSelectableObject` since
  264. it's costly when searching objects using `possible_objects`
  265. (e.g. searching for hooks).
  266. """
  267. score, winners = 0, None
  268. for obj in objects:
  269. objectscore = obj.__select__(obj, *args, **kwargs)
  270. if objectscore > score:
  271. score, winners = objectscore, [obj]
  272. elif objectscore > 0 and objectscore == score:
  273. winners.append(obj)
  274. if winners is None:
  275. return None
  276. if len(winners) > 1:
  277. # log in production environement / test, error while debugging
  278. msg = 'select ambiguity: %s\n(args: %s, kwargs: %s)'
  279. if self.debugmode:
  280. # raise bare exception in debug mode
  281. raise Exception(msg % (winners, args, kwargs.keys()))
  282. self.error(msg, winners, args, kwargs.keys())
  283. # return the result of calling the object
  284. return winners[0](*args, **kwargs)
  285. # these are overridden by set_log_methods below
  286. # only defining here to prevent pylint from complaining
  287. info = warning = error = critical = exception = debug = lambda msg, *a, **kw: None
  288. class RegistryStore(dict):
  289. """This class is responsible for loading implementations and storing them
  290. in their registry which are created on the fly as needed.
  291. It handles dynamic registration of objects and provides a convenient api to
  292. access them. To be recognized as an object that should be stored into one of
  293. the store's registry (:class:`Registry`), an object (usually a class) has
  294. the following attributes, used control how they interact with the registry:
  295. :attr:`__registry__` or `__registries__`
  296. name of the registry for this object (string like 'views', 'templates'...)
  297. or list of registry names if you want your object to be added to multiple
  298. registries
  299. :attr:`__regid__`
  300. implementation's identifier in the registry (string like 'main',
  301. 'primary', 'folder_box')
  302. :attr:`__select__`
  303. the implementation's selector
  304. Moreover, the :attr:`__abstract__` attribute may be set to `True` to
  305. indicate that a class is abstract and should not be registered (inherited
  306. attributes not considered).
  307. .. Note::
  308. When using the store to load objects dynamically, you *always* have
  309. to use **super()** to get the methods and attributes of the
  310. superclasses, and not use the class identifier. Else, you'll get into
  311. trouble when reloading comes into the place.
  312. For example, instead of writing::
  313. class Thing(Parent):
  314. __regid__ = 'athing'
  315. __select__ = yes()
  316. def f(self, arg1):
  317. Parent.f(self, arg1)
  318. You must write::
  319. class Thing(Parent):
  320. __regid__ = 'athing'
  321. __select__ = yes()
  322. def f(self, arg1):
  323. super(Parent, self).f(arg1)
  324. Controlling objects registration
  325. --------------------------------
  326. Dynamic loading is triggered by calling the :meth:`register_objects` method,
  327. given a list of directory to inspect for python modules.
  328. .. automethod: register_objects
  329. For each module, by default, all compatible objects are registered
  330. automatically, though if some objects have to replace other objects, or have
  331. to be included only if some condition is met, you'll have to define a
  332. `registration_callback(vreg)` function in your module and explicitly
  333. register **all objects** in this module, using the api defined below.
  334. .. automethod:: RegistryStore.register_all
  335. .. automethod:: RegistryStore.register_and_replace
  336. .. automethod:: RegistryStore.register
  337. .. automethod:: RegistryStore.unregister
  338. .. Note::
  339. Once the function `registration_callback(vreg)` is implemented in a
  340. module, all the objects from this module have to be explicitly
  341. registered as it disables the automatic objects registration.
  342. Examples:
  343. .. sourcecode:: python
  344. # cubicweb/web/views/basecomponents.py
  345. def registration_callback(store):
  346. # register everything in the module except SeeAlsoComponent
  347. store.register_all(globals().values(), __name__, (SeeAlsoVComponent,))
  348. # conditionally register SeeAlsoVComponent
  349. if 'see_also' in store.schema:
  350. store.register(SeeAlsoVComponent)
  351. In this example, we register all application object classes defined in the module
  352. except `SeeAlsoVComponent`. This class is then registered only if the 'see_also'
  353. relation type is defined in the instance'schema.
  354. .. sourcecode:: python
  355. # goa/appobjects/sessions.py
  356. def registration_callback(store):
  357. store.register(SessionsCleaner)
  358. # replace AuthenticationManager by GAEAuthenticationManager
  359. store.register_and_replace(GAEAuthenticationManager, AuthenticationManager)
  360. # replace PersistentSessionManager by GAEPersistentSessionManager
  361. store.register_and_replace(GAEPersistentSessionManager, PersistentSessionManager)
  362. In this example, we explicitly register classes one by one:
  363. * the `SessionCleaner` class
  364. * the `GAEAuthenticationManager` to replace the `AuthenticationManager`
  365. * the `GAEPersistentSessionManager` to replace the `PersistentSessionManager`
  366. If at some point we register a new appobject class in this module, it won't be
  367. registered at all without modification to the `registration_callback`
  368. implementation. The previous example will register it though, thanks to the call
  369. to the `register_all` method.
  370. Controlling registry instantation
  371. ---------------------------------
  372. The `REGISTRY_FACTORY` class dictionary allows to specify which class should
  373. be instantiated for a given registry name. The class associated to `None` in
  374. it will be the class used when there is no specific class for a name.
  375. """
  376. def __init__(self, debugmode=False):
  377. super(RegistryStore, self).__init__()
  378. self.debugmode = debugmode
  379. def reset(self):
  380. """clear all registries managed by this store"""
  381. # don't use self.clear, we want to keep existing subdictionaries
  382. for subdict in self.itervalues():
  383. subdict.clear()
  384. self._lastmodifs = {}
  385. def __getitem__(self, name):
  386. """return the registry (dictionary of class objects) associated to
  387. this name
  388. """
  389. try:
  390. return super(RegistryStore, self).__getitem__(name)
  391. except KeyError:
  392. raise RegistryNotFound(name), None, sys.exc_info()[-1]
  393. # methods for explicit (un)registration ###################################
  394. # default class, when no specific class set
  395. REGISTRY_FACTORY = {None: Registry}
  396. def registry_class(self, regid):
  397. """return existing registry named regid or use factory to create one and
  398. return it"""
  399. try:
  400. return self.REGISTRY_FACTORY[regid]
  401. except KeyError:
  402. return self.REGISTRY_FACTORY[None]
  403. def setdefault(self, regid):
  404. try:
  405. return self[regid]
  406. except KeyError:
  407. self[regid] = self.registry_class(regid)(self.debugmode)
  408. return self[regid]
  409. def register_all(self, objects, modname, butclasses=()):
  410. """register all `objects` given. Objects which are not from the module
  411. `modname` or which are in `butclasses` won't be registered.
  412. Typical usage is:
  413. .. sourcecode:: python
  414. store.register_all(globals().values(), __name__, (ClassIWantToRegisterExplicitly,))
  415. So you get partially automatic registration, keeping manual registration
  416. for some object (to use
  417. :meth:`~logilab.common.registry.RegistryStore.register_and_replace`
  418. for instance)
  419. """
  420. for obj in objects:
  421. try:
  422. if obj.__module__ != modname or obj in butclasses:
  423. continue
  424. oid = obj.__regid__
  425. except AttributeError:
  426. continue
  427. if oid and not obj.__dict__.get('__abstract__'):
  428. self.register(obj, oid=oid)
  429. def register(self, obj, registryname=None, oid=None, clear=False):
  430. """register `obj` implementation into `registryname` or
  431. `obj.__registry__` if not specified, with identifier `oid` or
  432. `obj.__regid__` if not specified.
  433. If `clear` is true, all objects with the same identifier will be
  434. previously unregistered.
  435. """
  436. assert not obj.__dict__.get('__abstract__')
  437. try:
  438. vname = obj.__name__
  439. except AttributeError:
  440. # XXX may occurs?
  441. vname = obj.__class__.__name__
  442. for registryname in class_registries(obj, registryname):
  443. registry = self.setdefault(registryname)
  444. registry.register(obj, oid=oid, clear=clear)
  445. self.debug('register %s in %s[\'%s\']',
  446. vname, registryname, oid or obj.__regid__)
  447. self._loadedmods.setdefault(obj.__module__, {})[classid(obj)] = obj
  448. def unregister(self, obj, registryname=None):
  449. """unregister `obj` implementation object from the registry
  450. `registryname` or `obj.__registry__` if not specified.
  451. """
  452. for registryname in class_registries(obj, registryname):
  453. self[registryname].unregister(obj)
  454. def register_and_replace(self, obj, replaced, registryname=None):
  455. """register `obj` implementation object into `registryname` or
  456. `obj.__registry__` if not specified. If found, the `replaced` object
  457. will be unregistered first (else a warning will be issued as it's
  458. generally unexpected).
  459. """
  460. for registryname in class_registries(obj, registryname):
  461. self[registryname].register_and_replace(obj, replaced)
  462. # initialization methods ###################################################
  463. def init_registration(self, path, extrapath=None):
  464. """reset registry and walk down path to return list of (path, name)
  465. file modules to be loaded"""
  466. # XXX make this private by renaming it to _init_registration ?
  467. self.reset()
  468. # compute list of all modules that have to be loaded
  469. self._toloadmods, filemods = _toload_info(path, extrapath)
  470. # XXX is _loadedmods still necessary ? It seems like it's useful
  471. # to avoid loading same module twice, especially with the
  472. # _load_ancestors_then_object logic but this needs to be checked
  473. self._loadedmods = {}
  474. return filemods
  475. def register_objects(self, path, extrapath=None):
  476. """register all objects found walking down <path>"""
  477. # load views from each directory in the instance's path
  478. # XXX inline init_registration ?
  479. filemods = self.init_registration(path, extrapath)
  480. for filepath, modname in filemods:
  481. self.load_file(filepath, modname)
  482. self.initialization_completed()
  483. def initialization_completed(self):
  484. """call initialization_completed() on all known registries"""
  485. for reg in self.itervalues():
  486. reg.initialization_completed()
  487. def _mdate(self, filepath):
  488. try:
  489. return stat(filepath)[-2]
  490. except OSError:
  491. # this typically happens on emacs backup files (.#foo.py)
  492. self.warning('Unable to load %s. It is likely to be a backup file',
  493. filepath)
  494. return None
  495. def is_reload_needed(self, path):
  496. """return True if something module changed and the registry should be
  497. reloaded
  498. """
  499. lastmodifs = self._lastmodifs
  500. for fileordir in path:
  501. if isdir(fileordir) and exists(join(fileordir, '__init__.py')):
  502. if self.is_reload_needed([join(fileordir, fname)
  503. for fname in listdir(fileordir)]):
  504. return True
  505. elif fileordir[-3:] == '.py':
  506. mdate = self._mdate(fileordir)
  507. if mdate is None:
  508. continue # backup file, see _mdate implementation
  509. elif "flymake" in fileordir:
  510. # flymake + pylint in use, don't consider these they will corrupt the registry
  511. continue
  512. if fileordir not in lastmodifs or lastmodifs[fileordir] < mdate:
  513. self.info('File %s changed since last visit', fileordir)
  514. return True
  515. return False
  516. def load_file(self, filepath, modname):
  517. """load app objects from a python file"""
  518. from logilab.common.modutils import load_module_from_name
  519. if modname in self._loadedmods:
  520. return
  521. self._loadedmods[modname] = {}
  522. mdate = self._mdate(filepath)
  523. if mdate is None:
  524. return # backup file, see _mdate implementation
  525. elif "flymake" in filepath:
  526. # flymake + pylint in use, don't consider these they will corrupt the registry
  527. return
  528. # set update time before module loading, else we get some reloading
  529. # weirdness in case of syntax error or other error while importing the
  530. # module
  531. self._lastmodifs[filepath] = mdate
  532. # load the module
  533. module = load_module_from_name(modname)
  534. self.load_module(module)
  535. def load_module(self, module):
  536. """load objects from a module using registration_callback() when it exists
  537. """
  538. self.info('loading %s from %s', module.__name__, module.__file__)
  539. if hasattr(module, 'registration_callback'):
  540. module.registration_callback(self)
  541. else:
  542. for objname, obj in vars(module).items():
  543. if objname.startswith('_'):
  544. continue
  545. self._load_ancestors_then_object(module.__name__, obj)
  546. def _load_ancestors_then_object(self, modname, objectcls):
  547. """handle automatic object class registration:
  548. - first ensure parent classes are already registered
  549. - class with __abstract__ == True in their local dictionary or
  550. with a name starting with an underscore are not registered
  551. - object class needs to have __registry__ and __regid__ attributes
  552. set to a non empty string to be registered.
  553. """
  554. # imported classes
  555. objmodname = getattr(objectcls, '__module__', None)
  556. if objmodname != modname:
  557. if objmodname in self._toloadmods:
  558. self.load_file(self._toloadmods[objmodname], objmodname)
  559. return
  560. # skip non registerable object
  561. try:
  562. if not (getattr(objectcls, '__regid__', None)
  563. and getattr(objectcls, '__select__', None)):
  564. return
  565. except TypeError:
  566. return
  567. clsid = classid(objectcls)
  568. if clsid in self._loadedmods[modname]:
  569. return
  570. self._loadedmods[modname][clsid] = objectcls
  571. for parent in objectcls.__bases__:
  572. self._load_ancestors_then_object(modname, parent)
  573. if (objectcls.__dict__.get('__abstract__')
  574. or objectcls.__name__[0] == '_'
  575. or not objectcls.__registries__
  576. or not objectcls.__regid__):
  577. return
  578. try:
  579. self.register(objectcls)
  580. except Exception, ex:
  581. if self.debugmode:
  582. raise
  583. self.exception('object %s registration failed: %s',
  584. objectcls, ex)
  585. # these are overridden by set_log_methods below
  586. # only defining here to prevent pylint from complaining
  587. info = warning = error = critical = exception = debug = lambda msg, *a, **kw: None
  588. # init logging
  589. set_log_methods(RegistryStore, getLogger('registry.store'))
  590. set_log_methods(Registry, getLogger('registry'))
  591. # helpers for debugging selectors
  592. TRACED_OIDS = None
  593. def _trace_selector(cls, selector, args, ret):
  594. vobj = args[0]
  595. if TRACED_OIDS == 'all' or vobj.__regid__ in TRACED_OIDS:
  596. print '%s -> %s for %s(%s)' % (cls, ret, vobj, vobj.__regid__)
  597. def _lltrace(selector):
  598. """use this decorator on your predicates so they become traceable with
  599. :class:`traced_selection`
  600. """
  601. def traced(cls, *args, **kwargs):
  602. ret = selector(cls, *args, **kwargs)
  603. if TRACED_OIDS is not None:
  604. _trace_selector(cls, selector, args, ret)
  605. return ret
  606. traced.__name__ = selector.__name__
  607. traced.__doc__ = selector.__doc__
  608. return traced
  609. class traced_selection(object): # pylint: disable=C0103
  610. """
  611. Typical usage is :
  612. .. sourcecode:: python
  613. >>> from logilab.common.registry import traced_selection
  614. >>> with traced_selection():
  615. ... # some code in which you want to debug selectors
  616. ... # for all objects
  617. Don't forget the 'from __future__ import with_statement' at the module top-level
  618. if you're using python prior to 2.6.
  619. This will yield lines like this in the logs::
  620. selector one_line_rset returned 0 for <class 'cubicweb.web.views.basecomponents.WFHistoryVComponent'>
  621. You can also give to :class:`traced_selection` the identifiers of objects on
  622. which you want to debug selection ('oid1' and 'oid2' in the example above).
  623. .. sourcecode:: python
  624. >>> with traced_selection( ('regid1', 'regid2') ):
  625. ... # some code in which you want to debug selectors
  626. ... # for objects with __regid__ 'regid1' and 'regid2'
  627. A potentially useful point to set up such a tracing function is
  628. the `logilab.common.registry.Registry.select` method body.
  629. """
  630. def __init__(self, traced='all'):
  631. self.traced = traced
  632. def __enter__(self):
  633. global TRACED_OIDS
  634. TRACED_OIDS = self.traced
  635. def __exit__(self, exctype, exc, traceback):
  636. global TRACED_OIDS
  637. TRACED_OIDS = None
  638. return traceback is None
  639. # selector base classes and operations ########################################
  640. def objectify_predicate(selector_func):
  641. """Most of the time, a simple score function is enough to build a selector.
  642. The :func:`objectify_predicate` decorator turn it into a proper selector
  643. class::
  644. @objectify_predicate
  645. def one(cls, req, rset=None, **kwargs):
  646. return 1
  647. class MyView(View):
  648. __select__ = View.__select__ & one()
  649. """
  650. return type(selector_func.__name__, (Predicate,),
  651. {'__doc__': selector_func.__doc__,
  652. '__call__': lambda self, *a, **kw: selector_func(*a, **kw)})
  653. _PREDICATES = {}
  654. def wrap_predicates(decorator):
  655. for predicate in _PREDICATES.itervalues():
  656. if not '_decorators' in predicate.__dict__:
  657. predicate._decorators = set()
  658. if decorator in predicate._decorators:
  659. continue
  660. predicate._decorators.add(decorator)
  661. predicate.__call__ = decorator(predicate.__call__)
  662. class PredicateMetaClass(type):
  663. def __new__(cls, *args, **kwargs):
  664. # use __new__ so subclasses doesn't have to call Predicate.__init__
  665. inst = type.__new__(cls, *args, **kwargs)
  666. proxy = weakref.proxy(inst, lambda p: _PREDICATES.pop(id(p)))
  667. _PREDICATES[id(proxy)] = proxy
  668. return inst
  669. class Predicate(object):
  670. """base class for selector classes providing implementation
  671. for operators ``&``, ``|`` and ``~``
  672. This class is only here to give access to binary operators, the selector
  673. logic itself should be implemented in the :meth:`__call__` method. Notice it
  674. should usually accept any arbitrary arguments (the context), though that may
  675. vary depending on your usage of the registry.
  676. a selector is called to help choosing the correct object for a
  677. particular context by returning a score (`int`) telling how well
  678. the implementation given as first argument fit to the given context.
  679. 0 score means that the class doesn't apply.
  680. """
  681. __metaclass__ = PredicateMetaClass
  682. @property
  683. def func_name(self):
  684. # backward compatibility
  685. return self.__class__.__name__
  686. def search_selector(self, selector):
  687. """search for the given selector, selector instance or tuple of
  688. selectors in the selectors tree. Return None if not found.
  689. """
  690. if self is selector:
  691. return self
  692. if (isinstance(selector, type) or isinstance(selector, tuple)) and \
  693. isinstance(self, selector):
  694. return self
  695. return None
  696. def __str__(self):
  697. return self.__class__.__name__
  698. def __and__(self, other):
  699. return AndPredicate(self, other)
  700. def __rand__(self, other):
  701. return AndPredicate(other, self)
  702. def __iand__(self, other):
  703. return AndPredicate(self, other)
  704. def __or__(self, other):
  705. return OrPredicate(self, other)
  706. def __ror__(self, other):
  707. return OrPredicate(other, self)
  708. def __ior__(self, other):
  709. return OrPredicate(self, other)
  710. def __invert__(self):
  711. return NotPredicate(self)
  712. # XXX (function | function) or (function & function) not managed yet
  713. def __call__(self, cls, *args, **kwargs):
  714. return NotImplementedError("selector %s must implement its logic "
  715. "in its __call__ method" % self.__class__)
  716. def __repr__(self):
  717. return u'<Predicate %s at %x>' % (self.__class__.__name__, id(self))
  718. class MultiPredicate(Predicate):
  719. """base class for compound selector classes"""
  720. def __init__(self, *selectors):
  721. self.selectors = self.merge_selectors(selectors)
  722. def __str__(self):
  723. return '%s(%s)' % (self.__class__.__name__,
  724. ','.join(str(s) for s in self.selectors))
  725. @classmethod
  726. def merge_selectors(cls, selectors):
  727. """deal with selector instanciation when necessary and merge
  728. multi-selectors if possible:
  729. AndPredicate(AndPredicate(sel1, sel2), AndPredicate(sel3, sel4))
  730. ==> AndPredicate(sel1, sel2, sel3, sel4)
  731. """
  732. merged_selectors = []
  733. for selector in selectors:
  734. # XXX do we really want magic-transformations below?
  735. # if so, wanna warn about them?
  736. if isinstance(selector, types.FunctionType):
  737. selector = objectify_predicate(selector)()
  738. if isinstance(selector, type) and issubclass(selector, Predicate):
  739. selector = selector()
  740. assert isinstance(selector, Predicate), selector
  741. if isinstance(selector, cls):
  742. merged_selectors += selector.selectors
  743. else:
  744. merged_selectors.append(selector)
  745. return merged_selectors
  746. def search_selector(self, selector):
  747. """search for the given selector or selector instance (or tuple of
  748. selectors) in the selectors tree. Return None if not found
  749. """
  750. for childselector in self.selectors:
  751. if childselector is selector:
  752. return childselector
  753. found = childselector.search_selector(selector)
  754. if found is not None:
  755. return found
  756. # if not found in children, maybe we are looking for self?
  757. return super(MultiPredicate, self).search_selector(selector)
  758. class AndPredicate(MultiPredicate):
  759. """and-chained selectors"""
  760. def __call__(self, cls, *args, **kwargs):
  761. score = 0
  762. for selector in self.selectors:
  763. partscore = selector(cls, *args, **kwargs)
  764. if not partscore:
  765. return 0
  766. score += partscore
  767. return score
  768. class OrPredicate(MultiPredicate):
  769. """or-chained selectors"""
  770. def __call__(self, cls, *args, **kwargs):
  771. for selector in self.selectors:
  772. partscore = selector(cls, *args, **kwargs)
  773. if partscore:
  774. return partscore
  775. return 0
  776. class NotPredicate(Predicate):
  777. """negation selector"""
  778. def __init__(self, selector):
  779. self.selector = selector
  780. def __call__(self, cls, *args, **kwargs):
  781. score = self.selector(cls, *args, **kwargs)
  782. return int(not score)
  783. def __str__(self):
  784. return 'NOT(%s)' % self.selector
  785. class yes(Predicate): # pylint: disable=C0103
  786. """Return the score given as parameter, with a default score of 0.5 so any
  787. other selector take precedence.
  788. Usually used for objects which can be selected whatever the context, or
  789. also sometimes to add arbitrary points to a score.
  790. Take care, `yes(0)` could be named 'no'...
  791. """
  792. def __init__(self, score=0.5):
  793. self.score = score
  794. def __call__(self, *args, **kwargs):
  795. return self.score