You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

adapter.py 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2004 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """Adapter management
  15. """
  16. import weakref
  17. from zope.interface import implementer
  18. from zope.interface import providedBy
  19. from zope.interface import Interface
  20. from zope.interface import ro
  21. from zope.interface.interfaces import IAdapterRegistry
  22. from zope.interface._compat import _normalize_name
  23. from zope.interface._compat import STRING_TYPES
  24. _BLANK = u''
  25. class BaseAdapterRegistry(object):
  26. # List of methods copied from lookup sub-objects:
  27. _delegated = ('lookup', 'queryMultiAdapter', 'lookup1', 'queryAdapter',
  28. 'adapter_hook', 'lookupAll', 'names',
  29. 'subscriptions', 'subscribers')
  30. # All registries maintain a generation that can be used by verifying
  31. # registries
  32. _generation = 0
  33. def __init__(self, bases=()):
  34. # The comments here could be improved. Possibly this bit needs
  35. # explaining in a separate document, as the comments here can
  36. # be quite confusing. /regebro
  37. # {order -> {required -> {provided -> {name -> value}}}}
  38. # Here "order" is actually an index in a list, "required" and
  39. # "provided" are interfaces, and "required" is really a nested
  40. # key. So, for example:
  41. # for order == 0 (that is, self._adapters[0]), we have:
  42. # {provided -> {name -> value}}
  43. # but for order == 2 (that is, self._adapters[2]), we have:
  44. # {r1 -> {r2 -> {provided -> {name -> value}}}}
  45. #
  46. self._adapters = []
  47. # {order -> {required -> {provided -> {name -> [value]}}}}
  48. # where the remarks about adapters above apply
  49. self._subscribers = []
  50. # Set, with a reference count, keeping track of the interfaces
  51. # for which we have provided components:
  52. self._provided = {}
  53. # Create ``_v_lookup`` object to perform lookup. We make this a
  54. # separate object to to make it easier to implement just the
  55. # lookup functionality in C. This object keeps track of cache
  56. # invalidation data in two kinds of registries.
  57. # Invalidating registries have caches that are invalidated
  58. # when they or their base registies change. An invalidating
  59. # registry can only have invalidating registries as bases.
  60. # See LookupBaseFallback below for the pertinent logic.
  61. # Verifying registies can't rely on getting invalidation messages,
  62. # so have to check the generations of base registries to determine
  63. # if their cache data are current. See VerifyingBasePy below
  64. # for the pertinent object.
  65. self._createLookup()
  66. # Setting the bases causes the registries described above
  67. # to be initialized (self._setBases -> self.changed ->
  68. # self._v_lookup.changed).
  69. self.__bases__ = bases
  70. def _setBases(self, bases):
  71. self.__dict__['__bases__'] = bases
  72. self.ro = ro.ro(self)
  73. self.changed(self)
  74. __bases__ = property(lambda self: self.__dict__['__bases__'],
  75. lambda self, bases: self._setBases(bases),
  76. )
  77. def _createLookup(self):
  78. self._v_lookup = self.LookupClass(self)
  79. for name in self._delegated:
  80. self.__dict__[name] = getattr(self._v_lookup, name)
  81. def changed(self, originally_changed):
  82. self._generation += 1
  83. self._v_lookup.changed(originally_changed)
  84. def register(self, required, provided, name, value):
  85. if not isinstance(name, STRING_TYPES):
  86. raise ValueError('name is not a string')
  87. if value is None:
  88. self.unregister(required, provided, name, value)
  89. return
  90. required = tuple(map(_convert_None_to_Interface, required))
  91. name = _normalize_name(name)
  92. order = len(required)
  93. byorder = self._adapters
  94. while len(byorder) <= order:
  95. byorder.append({})
  96. components = byorder[order]
  97. key = required + (provided,)
  98. for k in key:
  99. d = components.get(k)
  100. if d is None:
  101. d = {}
  102. components[k] = d
  103. components = d
  104. if components.get(name) is value:
  105. return
  106. components[name] = value
  107. n = self._provided.get(provided, 0) + 1
  108. self._provided[provided] = n
  109. if n == 1:
  110. self._v_lookup.add_extendor(provided)
  111. self.changed(self)
  112. def registered(self, required, provided, name=_BLANK):
  113. required = tuple(map(_convert_None_to_Interface, required))
  114. name = _normalize_name(name)
  115. order = len(required)
  116. byorder = self._adapters
  117. if len(byorder) <= order:
  118. return None
  119. components = byorder[order]
  120. key = required + (provided,)
  121. for k in key:
  122. d = components.get(k)
  123. if d is None:
  124. return None
  125. components = d
  126. return components.get(name)
  127. def unregister(self, required, provided, name, value=None):
  128. required = tuple(map(_convert_None_to_Interface, required))
  129. order = len(required)
  130. byorder = self._adapters
  131. if order >= len(byorder):
  132. return False
  133. components = byorder[order]
  134. key = required + (provided,)
  135. # Keep track of how we got to `components`:
  136. lookups = []
  137. for k in key:
  138. d = components.get(k)
  139. if d is None:
  140. return
  141. lookups.append((components, k))
  142. components = d
  143. old = components.get(name)
  144. if old is None:
  145. return
  146. if (value is not None) and (old is not value):
  147. return
  148. del components[name]
  149. if not components:
  150. # Clean out empty containers, since we don't want our keys
  151. # to reference global objects (interfaces) unnecessarily.
  152. # This is often a problem when an interface is slated for
  153. # removal; a hold-over entry in the registry can make it
  154. # difficult to remove such interfaces.
  155. for comp, k in reversed(lookups):
  156. d = comp[k]
  157. if d:
  158. break
  159. else:
  160. del comp[k]
  161. while byorder and not byorder[-1]:
  162. del byorder[-1]
  163. n = self._provided[provided] - 1
  164. if n == 0:
  165. del self._provided[provided]
  166. self._v_lookup.remove_extendor(provided)
  167. else:
  168. self._provided[provided] = n
  169. self.changed(self)
  170. def subscribe(self, required, provided, value):
  171. required = tuple(map(_convert_None_to_Interface, required))
  172. name = _BLANK
  173. order = len(required)
  174. byorder = self._subscribers
  175. while len(byorder) <= order:
  176. byorder.append({})
  177. components = byorder[order]
  178. key = required + (provided,)
  179. for k in key:
  180. d = components.get(k)
  181. if d is None:
  182. d = {}
  183. components[k] = d
  184. components = d
  185. components[name] = components.get(name, ()) + (value, )
  186. if provided is not None:
  187. n = self._provided.get(provided, 0) + 1
  188. self._provided[provided] = n
  189. if n == 1:
  190. self._v_lookup.add_extendor(provided)
  191. self.changed(self)
  192. def unsubscribe(self, required, provided, value=None):
  193. required = tuple(map(_convert_None_to_Interface, required))
  194. order = len(required)
  195. byorder = self._subscribers
  196. if order >= len(byorder):
  197. return
  198. components = byorder[order]
  199. key = required + (provided,)
  200. # Keep track of how we got to `components`:
  201. lookups = []
  202. for k in key:
  203. d = components.get(k)
  204. if d is None:
  205. return
  206. lookups.append((components, k))
  207. components = d
  208. old = components.get(_BLANK)
  209. if not old:
  210. # this is belt-and-suspenders against the failure of cleanup below
  211. return # pragma: no cover
  212. if value is None:
  213. new = ()
  214. else:
  215. new = tuple([v for v in old if v != value])
  216. if new == old:
  217. return
  218. if new:
  219. components[_BLANK] = new
  220. else:
  221. # Instead of setting components[_BLANK] = new, we clean out
  222. # empty containers, since we don't want our keys to
  223. # reference global objects (interfaces) unnecessarily. This
  224. # is often a problem when an interface is slated for
  225. # removal; a hold-over entry in the registry can make it
  226. # difficult to remove such interfaces.
  227. del components[_BLANK]
  228. for comp, k in reversed(lookups):
  229. d = comp[k]
  230. if d:
  231. break
  232. else:
  233. del comp[k]
  234. while byorder and not byorder[-1]:
  235. del byorder[-1]
  236. if provided is not None:
  237. n = self._provided[provided] + len(new) - len(old)
  238. if n == 0:
  239. del self._provided[provided]
  240. self._v_lookup.remove_extendor(provided)
  241. self.changed(self)
  242. # XXX hack to fake out twisted's use of a private api. We need to get them
  243. # to use the new registed method.
  244. def get(self, _): # pragma: no cover
  245. class XXXTwistedFakeOut:
  246. selfImplied = {}
  247. return XXXTwistedFakeOut
  248. _not_in_mapping = object()
  249. class LookupBaseFallback(object):
  250. def __init__(self):
  251. self._cache = {}
  252. self._mcache = {}
  253. self._scache = {}
  254. def changed(self, ignored=None):
  255. self._cache.clear()
  256. self._mcache.clear()
  257. self._scache.clear()
  258. def _getcache(self, provided, name):
  259. cache = self._cache.get(provided)
  260. if cache is None:
  261. cache = {}
  262. self._cache[provided] = cache
  263. if name:
  264. c = cache.get(name)
  265. if c is None:
  266. c = {}
  267. cache[name] = c
  268. cache = c
  269. return cache
  270. def lookup(self, required, provided, name=_BLANK, default=None):
  271. if not isinstance(name, STRING_TYPES):
  272. raise ValueError('name is not a string')
  273. cache = self._getcache(provided, name)
  274. required = tuple(required)
  275. if len(required) == 1:
  276. result = cache.get(required[0], _not_in_mapping)
  277. else:
  278. result = cache.get(tuple(required), _not_in_mapping)
  279. if result is _not_in_mapping:
  280. result = self._uncached_lookup(required, provided, name)
  281. if len(required) == 1:
  282. cache[required[0]] = result
  283. else:
  284. cache[tuple(required)] = result
  285. if result is None:
  286. return default
  287. return result
  288. def lookup1(self, required, provided, name=_BLANK, default=None):
  289. if not isinstance(name, STRING_TYPES):
  290. raise ValueError('name is not a string')
  291. cache = self._getcache(provided, name)
  292. result = cache.get(required, _not_in_mapping)
  293. if result is _not_in_mapping:
  294. return self.lookup((required, ), provided, name, default)
  295. if result is None:
  296. return default
  297. return result
  298. def queryAdapter(self, object, provided, name=_BLANK, default=None):
  299. return self.adapter_hook(provided, object, name, default)
  300. def adapter_hook(self, provided, object, name=_BLANK, default=None):
  301. if not isinstance(name, STRING_TYPES):
  302. raise ValueError('name is not a string')
  303. required = providedBy(object)
  304. cache = self._getcache(provided, name)
  305. factory = cache.get(required, _not_in_mapping)
  306. if factory is _not_in_mapping:
  307. factory = self.lookup((required, ), provided, name)
  308. if factory is not None:
  309. result = factory(object)
  310. if result is not None:
  311. return result
  312. return default
  313. def lookupAll(self, required, provided):
  314. cache = self._mcache.get(provided)
  315. if cache is None:
  316. cache = {}
  317. self._mcache[provided] = cache
  318. required = tuple(required)
  319. result = cache.get(required, _not_in_mapping)
  320. if result is _not_in_mapping:
  321. result = self._uncached_lookupAll(required, provided)
  322. cache[required] = result
  323. return result
  324. def subscriptions(self, required, provided):
  325. cache = self._scache.get(provided)
  326. if cache is None:
  327. cache = {}
  328. self._scache[provided] = cache
  329. required = tuple(required)
  330. result = cache.get(required, _not_in_mapping)
  331. if result is _not_in_mapping:
  332. result = self._uncached_subscriptions(required, provided)
  333. cache[required] = result
  334. return result
  335. LookupBasePy = LookupBaseFallback # BBB
  336. try:
  337. from zope.interface._zope_interface_coptimizations import LookupBase
  338. except ImportError:
  339. LookupBase = LookupBaseFallback
  340. class VerifyingBaseFallback(LookupBaseFallback):
  341. # Mixin for lookups against registries which "chain" upwards, and
  342. # whose lookups invalidate their own caches whenever a parent registry
  343. # bumps its own '_generation' counter. E.g., used by
  344. # zope.component.persistentregistry
  345. def changed(self, originally_changed):
  346. LookupBaseFallback.changed(self, originally_changed)
  347. self._verify_ro = self._registry.ro[1:]
  348. self._verify_generations = [r._generation for r in self._verify_ro]
  349. def _verify(self):
  350. if ([r._generation for r in self._verify_ro]
  351. != self._verify_generations):
  352. self.changed(None)
  353. def _getcache(self, provided, name):
  354. self._verify()
  355. return LookupBaseFallback._getcache(self, provided, name)
  356. def lookupAll(self, required, provided):
  357. self._verify()
  358. return LookupBaseFallback.lookupAll(self, required, provided)
  359. def subscriptions(self, required, provided):
  360. self._verify()
  361. return LookupBaseFallback.subscriptions(self, required, provided)
  362. VerifyingBasePy = VerifyingBaseFallback #BBB
  363. try:
  364. from zope.interface._zope_interface_coptimizations import VerifyingBase
  365. except ImportError:
  366. VerifyingBase = VerifyingBaseFallback
  367. class AdapterLookupBase(object):
  368. def __init__(self, registry):
  369. self._registry = registry
  370. self._required = {}
  371. self.init_extendors()
  372. super(AdapterLookupBase, self).__init__()
  373. def changed(self, ignored=None):
  374. super(AdapterLookupBase, self).changed(None)
  375. for r in self._required.keys():
  376. r = r()
  377. if r is not None:
  378. r.unsubscribe(self)
  379. self._required.clear()
  380. # Extendors
  381. # ---------
  382. # When given an target interface for an adapter lookup, we need to consider
  383. # adapters for interfaces that extend the target interface. This is
  384. # what the extendors dictionary is about. It tells us all of the
  385. # interfaces that extend an interface for which there are adapters
  386. # registered.
  387. # We could separate this by order and name, thus reducing the
  388. # number of provided interfaces to search at run time. The tradeoff,
  389. # however, is that we have to store more information. For example,
  390. # if the same interface is provided for multiple names and if the
  391. # interface extends many interfaces, we'll have to keep track of
  392. # a fair bit of information for each name. It's better to
  393. # be space efficient here and be time efficient in the cache
  394. # implementation.
  395. # TODO: add invalidation when a provided interface changes, in case
  396. # the interface's __iro__ has changed. This is unlikely enough that
  397. # we'll take our chances for now.
  398. def init_extendors(self):
  399. self._extendors = {}
  400. for p in self._registry._provided:
  401. self.add_extendor(p)
  402. def add_extendor(self, provided):
  403. _extendors = self._extendors
  404. for i in provided.__iro__:
  405. extendors = _extendors.get(i, ())
  406. _extendors[i] = (
  407. [e for e in extendors if provided.isOrExtends(e)]
  408. +
  409. [provided]
  410. +
  411. [e for e in extendors if not provided.isOrExtends(e)]
  412. )
  413. def remove_extendor(self, provided):
  414. _extendors = self._extendors
  415. for i in provided.__iro__:
  416. _extendors[i] = [e for e in _extendors.get(i, ())
  417. if e != provided]
  418. def _subscribe(self, *required):
  419. _refs = self._required
  420. for r in required:
  421. ref = r.weakref()
  422. if ref not in _refs:
  423. r.subscribe(self)
  424. _refs[ref] = 1
  425. def _uncached_lookup(self, required, provided, name=_BLANK):
  426. required = tuple(required)
  427. result = None
  428. order = len(required)
  429. for registry in self._registry.ro:
  430. byorder = registry._adapters
  431. if order >= len(byorder):
  432. continue
  433. extendors = registry._v_lookup._extendors.get(provided)
  434. if not extendors:
  435. continue
  436. components = byorder[order]
  437. result = _lookup(components, required, extendors, name, 0,
  438. order)
  439. if result is not None:
  440. break
  441. self._subscribe(*required)
  442. return result
  443. def queryMultiAdapter(self, objects, provided, name=_BLANK, default=None):
  444. factory = self.lookup(map(providedBy, objects), provided, name)
  445. if factory is None:
  446. return default
  447. result = factory(*objects)
  448. if result is None:
  449. return default
  450. return result
  451. def _uncached_lookupAll(self, required, provided):
  452. required = tuple(required)
  453. order = len(required)
  454. result = {}
  455. for registry in reversed(self._registry.ro):
  456. byorder = registry._adapters
  457. if order >= len(byorder):
  458. continue
  459. extendors = registry._v_lookup._extendors.get(provided)
  460. if not extendors:
  461. continue
  462. components = byorder[order]
  463. _lookupAll(components, required, extendors, result, 0, order)
  464. self._subscribe(*required)
  465. return tuple(result.items())
  466. def names(self, required, provided):
  467. return [c[0] for c in self.lookupAll(required, provided)]
  468. def _uncached_subscriptions(self, required, provided):
  469. required = tuple(required)
  470. order = len(required)
  471. result = []
  472. for registry in reversed(self._registry.ro):
  473. byorder = registry._subscribers
  474. if order >= len(byorder):
  475. continue
  476. if provided is None:
  477. extendors = (provided, )
  478. else:
  479. extendors = registry._v_lookup._extendors.get(provided)
  480. if extendors is None:
  481. continue
  482. _subscriptions(byorder[order], required, extendors, _BLANK,
  483. result, 0, order)
  484. self._subscribe(*required)
  485. return result
  486. def subscribers(self, objects, provided):
  487. subscriptions = self.subscriptions(map(providedBy, objects), provided)
  488. if provided is None:
  489. result = ()
  490. for subscription in subscriptions:
  491. subscription(*objects)
  492. else:
  493. result = []
  494. for subscription in subscriptions:
  495. subscriber = subscription(*objects)
  496. if subscriber is not None:
  497. result.append(subscriber)
  498. return result
  499. class AdapterLookup(AdapterLookupBase, LookupBase):
  500. pass
  501. @implementer(IAdapterRegistry)
  502. class AdapterRegistry(BaseAdapterRegistry):
  503. LookupClass = AdapterLookup
  504. def __init__(self, bases=()):
  505. # AdapterRegisties are invalidating registries, so
  506. # we need to keep track of out invalidating subregistries.
  507. self._v_subregistries = weakref.WeakKeyDictionary()
  508. super(AdapterRegistry, self).__init__(bases)
  509. def _addSubregistry(self, r):
  510. self._v_subregistries[r] = 1
  511. def _removeSubregistry(self, r):
  512. if r in self._v_subregistries:
  513. del self._v_subregistries[r]
  514. def _setBases(self, bases):
  515. old = self.__dict__.get('__bases__', ())
  516. for r in old:
  517. if r not in bases:
  518. r._removeSubregistry(self)
  519. for r in bases:
  520. if r not in old:
  521. r._addSubregistry(self)
  522. super(AdapterRegistry, self)._setBases(bases)
  523. def changed(self, originally_changed):
  524. super(AdapterRegistry, self).changed(originally_changed)
  525. for sub in self._v_subregistries.keys():
  526. sub.changed(originally_changed)
  527. class VerifyingAdapterLookup(AdapterLookupBase, VerifyingBase):
  528. pass
  529. @implementer(IAdapterRegistry)
  530. class VerifyingAdapterRegistry(BaseAdapterRegistry):
  531. LookupClass = VerifyingAdapterLookup
  532. def _convert_None_to_Interface(x):
  533. if x is None:
  534. return Interface
  535. else:
  536. return x
  537. def _lookup(components, specs, provided, name, i, l):
  538. if i < l:
  539. for spec in specs[i].__sro__:
  540. comps = components.get(spec)
  541. if comps:
  542. r = _lookup(comps, specs, provided, name, i+1, l)
  543. if r is not None:
  544. return r
  545. else:
  546. for iface in provided:
  547. comps = components.get(iface)
  548. if comps:
  549. r = comps.get(name)
  550. if r is not None:
  551. return r
  552. return None
  553. def _lookupAll(components, specs, provided, result, i, l):
  554. if i < l:
  555. for spec in reversed(specs[i].__sro__):
  556. comps = components.get(spec)
  557. if comps:
  558. _lookupAll(comps, specs, provided, result, i+1, l)
  559. else:
  560. for iface in reversed(provided):
  561. comps = components.get(iface)
  562. if comps:
  563. result.update(comps)
  564. def _subscriptions(components, specs, provided, name, result, i, l):
  565. if i < l:
  566. for spec in reversed(specs[i].__sro__):
  567. comps = components.get(spec)
  568. if comps:
  569. _subscriptions(comps, specs, provided, name, result, i+1, l)
  570. else:
  571. for iface in reversed(provided):
  572. comps = components.get(iface)
  573. if comps:
  574. comps = comps.get(name)
  575. if comps:
  576. result.extend(comps)