interfaces.py 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2002 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. """Interface Package Interfaces
  15. """
  16. __docformat__ = 'restructuredtext'
  17. from zope.interface.interface import Attribute
  18. from zope.interface.interface import Interface
  19. from zope.interface.declarations import implementer
  20. _BLANK = u''
  21. class IElement(Interface):
  22. """Objects that have basic documentation and tagged values.
  23. """
  24. __name__ = Attribute('__name__', 'The object name')
  25. __doc__ = Attribute('__doc__', 'The object doc string')
  26. def getTaggedValue(tag):
  27. """Returns the value associated with `tag`.
  28. Raise a `KeyError` of the tag isn't set.
  29. """
  30. def queryTaggedValue(tag, default=None):
  31. """Returns the value associated with `tag`.
  32. Return the default value of the tag isn't set.
  33. """
  34. def getTaggedValueTags():
  35. """Returns a list of all tags."""
  36. def setTaggedValue(tag, value):
  37. """Associates `value` with `key`."""
  38. class IAttribute(IElement):
  39. """Attribute descriptors"""
  40. interface = Attribute('interface',
  41. 'Stores the interface instance in which the '
  42. 'attribute is located.')
  43. class IMethod(IAttribute):
  44. """Method attributes"""
  45. def getSignatureInfo():
  46. """Returns the signature information.
  47. This method returns a dictionary with the following keys:
  48. o `positional` - All positional arguments.
  49. o `required` - A list of all required arguments.
  50. o `optional` - A list of all optional arguments.
  51. o `varargs` - The name of the varargs argument.
  52. o `kwargs` - The name of the kwargs argument.
  53. """
  54. def getSignatureString():
  55. """Return a signature string suitable for inclusion in documentation.
  56. This method returns the function signature string. For example, if you
  57. have `func(a, b, c=1, d='f')`, then the signature string is `(a, b,
  58. c=1, d='f')`.
  59. """
  60. class ISpecification(Interface):
  61. """Object Behavioral specifications"""
  62. def providedBy(object):
  63. """Test whether the interface is implemented by the object
  64. Return true of the object asserts that it implements the
  65. interface, including asserting that it implements an extended
  66. interface.
  67. """
  68. def implementedBy(class_):
  69. """Test whether the interface is implemented by instances of the class
  70. Return true of the class asserts that its instances implement the
  71. interface, including asserting that they implement an extended
  72. interface.
  73. """
  74. def isOrExtends(other):
  75. """Test whether the specification is or extends another
  76. """
  77. def extends(other, strict=True):
  78. """Test whether a specification extends another
  79. The specification extends other if it has other as a base
  80. interface or if one of it's bases extends other.
  81. If strict is false, then the specification extends itself.
  82. """
  83. def weakref(callback=None):
  84. """Return a weakref to the specification
  85. This method is, regrettably, needed to allow weakrefs to be
  86. computed to security-proxied specifications. While the
  87. zope.interface package does not require zope.security or
  88. zope.proxy, it has to be able to coexist with it.
  89. """
  90. __bases__ = Attribute("""Base specifications
  91. A tuple if specifications from which this specification is
  92. directly derived.
  93. """)
  94. __sro__ = Attribute("""Specification-resolution order
  95. A tuple of the specification and all of it's ancestor
  96. specifications from most specific to least specific.
  97. (This is similar to the method-resolution order for new-style classes.)
  98. """)
  99. __iro__ = Attribute("""Interface-resolution order
  100. A tuple of the of the specification's ancestor interfaces from
  101. most specific to least specific. The specification itself is
  102. included if it is an interface.
  103. (This is similar to the method-resolution order for new-style classes.)
  104. """)
  105. def get(name, default=None):
  106. """Look up the description for a name
  107. If the named attribute is not defined, the default is
  108. returned.
  109. """
  110. class IInterface(ISpecification, IElement):
  111. """Interface objects
  112. Interface objects describe the behavior of an object by containing
  113. useful information about the object. This information includes:
  114. o Prose documentation about the object. In Python terms, this
  115. is called the "doc string" of the interface. In this element,
  116. you describe how the object works in prose language and any
  117. other useful information about the object.
  118. o Descriptions of attributes. Attribute descriptions include
  119. the name of the attribute and prose documentation describing
  120. the attributes usage.
  121. o Descriptions of methods. Method descriptions can include:
  122. - Prose "doc string" documentation about the method and its
  123. usage.
  124. - A description of the methods arguments; how many arguments
  125. are expected, optional arguments and their default values,
  126. the position or arguments in the signature, whether the
  127. method accepts arbitrary arguments and whether the method
  128. accepts arbitrary keyword arguments.
  129. o Optional tagged data. Interface objects (and their attributes and
  130. methods) can have optional, application specific tagged data
  131. associated with them. Examples uses for this are examples,
  132. security assertions, pre/post conditions, and other possible
  133. information you may want to associate with an Interface or its
  134. attributes.
  135. Not all of this information is mandatory. For example, you may
  136. only want the methods of your interface to have prose
  137. documentation and not describe the arguments of the method in
  138. exact detail. Interface objects are flexible and let you give or
  139. take any of these components.
  140. Interfaces are created with the Python class statement using
  141. either Interface.Interface or another interface, as in::
  142. from zope.interface import Interface
  143. class IMyInterface(Interface):
  144. '''Interface documentation'''
  145. def meth(arg1, arg2):
  146. '''Documentation for meth'''
  147. # Note that there is no self argument
  148. class IMySubInterface(IMyInterface):
  149. '''Interface documentation'''
  150. def meth2():
  151. '''Documentation for meth2'''
  152. You use interfaces in two ways:
  153. o You assert that your object implement the interfaces.
  154. There are several ways that you can assert that an object
  155. implements an interface:
  156. 1. Call zope.interface.implements in your class definition.
  157. 2. Call zope.interfaces.directlyProvides on your object.
  158. 3. Call 'zope.interface.classImplements' to assert that instances
  159. of a class implement an interface.
  160. For example::
  161. from zope.interface import classImplements
  162. classImplements(some_class, some_interface)
  163. This approach is useful when it is not an option to modify
  164. the class source. Note that this doesn't affect what the
  165. class itself implements, but only what its instances
  166. implement.
  167. o You query interface meta-data. See the IInterface methods and
  168. attributes for details.
  169. """
  170. def names(all=False):
  171. """Get the interface attribute names
  172. Return a sequence of the names of the attributes, including
  173. methods, included in the interface definition.
  174. Normally, only directly defined attributes are included. If
  175. a true positional or keyword argument is given, then
  176. attributes defined by base classes will be included.
  177. """
  178. def namesAndDescriptions(all=False):
  179. """Get the interface attribute names and descriptions
  180. Return a sequence of the names and descriptions of the
  181. attributes, including methods, as name-value pairs, included
  182. in the interface definition.
  183. Normally, only directly defined attributes are included. If
  184. a true positional or keyword argument is given, then
  185. attributes defined by base classes will be included.
  186. """
  187. def __getitem__(name):
  188. """Get the description for a name
  189. If the named attribute is not defined, a KeyError is raised.
  190. """
  191. def direct(name):
  192. """Get the description for the name if it was defined by the interface
  193. If the interface doesn't define the name, returns None.
  194. """
  195. def validateInvariants(obj, errors=None):
  196. """Validate invariants
  197. Validate object to defined invariants. If errors is None,
  198. raises first Invalid error; if errors is a list, appends all errors
  199. to list, then raises Invalid with the errors as the first element
  200. of the "args" tuple."""
  201. def __contains__(name):
  202. """Test whether the name is defined by the interface"""
  203. def __iter__():
  204. """Return an iterator over the names defined by the interface
  205. The names iterated include all of the names defined by the
  206. interface directly and indirectly by base interfaces.
  207. """
  208. __module__ = Attribute("""The name of the module defining the interface""")
  209. class IDeclaration(ISpecification):
  210. """Interface declaration
  211. Declarations are used to express the interfaces implemented by
  212. classes or provided by objects.
  213. """
  214. def __contains__(interface):
  215. """Test whether an interface is in the specification
  216. Return true if the given interface is one of the interfaces in
  217. the specification and false otherwise.
  218. """
  219. def __iter__():
  220. """Return an iterator for the interfaces in the specification
  221. """
  222. def flattened():
  223. """Return an iterator of all included and extended interfaces
  224. An iterator is returned for all interfaces either included in
  225. or extended by interfaces included in the specifications
  226. without duplicates. The interfaces are in "interface
  227. resolution order". The interface resolution order is such that
  228. base interfaces are listed after interfaces that extend them
  229. and, otherwise, interfaces are included in the order that they
  230. were defined in the specification.
  231. """
  232. def __sub__(interfaces):
  233. """Create an interface specification with some interfaces excluded
  234. The argument can be an interface or an interface
  235. specifications. The interface or interfaces given in a
  236. specification are subtracted from the interface specification.
  237. Removing an interface that is not in the specification does
  238. not raise an error. Doing so has no effect.
  239. Removing an interface also removes sub-interfaces of the interface.
  240. """
  241. def __add__(interfaces):
  242. """Create an interface specification with some interfaces added
  243. The argument can be an interface or an interface
  244. specifications. The interface or interfaces given in a
  245. specification are added to the interface specification.
  246. Adding an interface that is already in the specification does
  247. not raise an error. Doing so has no effect.
  248. """
  249. def __nonzero__():
  250. """Return a true value of the interface specification is non-empty
  251. """
  252. class IInterfaceDeclaration(Interface):
  253. """Declare and check the interfaces of objects
  254. The functions defined in this interface are used to declare the
  255. interfaces that objects provide and to query the interfaces that have
  256. been declared.
  257. Interfaces can be declared for objects in two ways:
  258. - Interfaces are declared for instances of the object's class
  259. - Interfaces are declared for the object directly.
  260. The interfaces declared for an object are, therefore, the union of
  261. interfaces declared for the object directly and the interfaces
  262. declared for instances of the object's class.
  263. Note that we say that a class implements the interfaces provided
  264. by it's instances. An instance can also provide interfaces
  265. directly. The interfaces provided by an object are the union of
  266. the interfaces provided directly and the interfaces implemented by
  267. the class.
  268. """
  269. def providedBy(ob):
  270. """Return the interfaces provided by an object
  271. This is the union of the interfaces directly provided by an
  272. object and interfaces implemented by it's class.
  273. The value returned is an IDeclaration.
  274. """
  275. def implementedBy(class_):
  276. """Return the interfaces implemented for a class' instances
  277. The value returned is an IDeclaration.
  278. """
  279. def classImplements(class_, *interfaces):
  280. """Declare additional interfaces implemented for instances of a class
  281. The arguments after the class are one or more interfaces or
  282. interface specifications (IDeclaration objects).
  283. The interfaces given (including the interfaces in the
  284. specifications) are added to any interfaces previously
  285. declared.
  286. Consider the following example::
  287. class C(A, B):
  288. ...
  289. classImplements(C, I1, I2)
  290. Instances of ``C`` provide ``I1``, ``I2``, and whatever interfaces
  291. instances of ``A`` and ``B`` provide.
  292. """
  293. def implementer(*interfaces):
  294. """Create a decorator for declaring interfaces implemented by a facory
  295. A callable is returned that makes an implements declaration on
  296. objects passed to it.
  297. """
  298. def classImplementsOnly(class_, *interfaces):
  299. """Declare the only interfaces implemented by instances of a class
  300. The arguments after the class are one or more interfaces or
  301. interface specifications (IDeclaration objects).
  302. The interfaces given (including the interfaces in the
  303. specifications) replace any previous declarations.
  304. Consider the following example::
  305. class C(A, B):
  306. ...
  307. classImplements(C, IA, IB. IC)
  308. classImplementsOnly(C. I1, I2)
  309. Instances of ``C`` provide only ``I1``, ``I2``, and regardless of
  310. whatever interfaces instances of ``A`` and ``B`` implement.
  311. """
  312. def implementer_only(*interfaces):
  313. """Create a decorator for declaring the only interfaces implemented
  314. A callable is returned that makes an implements declaration on
  315. objects passed to it.
  316. """
  317. def directlyProvidedBy(object):
  318. """Return the interfaces directly provided by the given object
  319. The value returned is an IDeclaration.
  320. """
  321. def directlyProvides(object, *interfaces):
  322. """Declare interfaces declared directly for an object
  323. The arguments after the object are one or more interfaces or
  324. interface specifications (IDeclaration objects).
  325. The interfaces given (including the interfaces in the
  326. specifications) replace interfaces previously
  327. declared for the object.
  328. Consider the following example::
  329. class C(A, B):
  330. ...
  331. ob = C()
  332. directlyProvides(ob, I1, I2)
  333. The object, ``ob`` provides ``I1``, ``I2``, and whatever interfaces
  334. instances have been declared for instances of ``C``.
  335. To remove directly provided interfaces, use ``directlyProvidedBy`` and
  336. subtract the unwanted interfaces. For example::
  337. directlyProvides(ob, directlyProvidedBy(ob)-I2)
  338. removes I2 from the interfaces directly provided by
  339. ``ob``. The object, ``ob`` no longer directly provides ``I2``,
  340. although it might still provide ``I2`` if it's class
  341. implements ``I2``.
  342. To add directly provided interfaces, use ``directlyProvidedBy`` and
  343. include additional interfaces. For example::
  344. directlyProvides(ob, directlyProvidedBy(ob), I2)
  345. adds I2 to the interfaces directly provided by ob.
  346. """
  347. def alsoProvides(object, *interfaces):
  348. """Declare additional interfaces directly for an object::
  349. alsoProvides(ob, I1)
  350. is equivalent to::
  351. directlyProvides(ob, directlyProvidedBy(ob), I1)
  352. """
  353. def noLongerProvides(object, interface):
  354. """Remove an interface from the list of an object's directly
  355. provided interfaces::
  356. noLongerProvides(ob, I1)
  357. is equivalent to::
  358. directlyProvides(ob, directlyProvidedBy(ob)-I1)
  359. with the exception that if ``I1`` is an interface that is
  360. provided by ``ob`` through the class's implementation,
  361. ValueError is raised.
  362. """
  363. def implements(*interfaces):
  364. """Declare interfaces implemented by instances of a class
  365. This function is called in a class definition (Python 2.x only).
  366. The arguments are one or more interfaces or interface
  367. specifications (IDeclaration objects).
  368. The interfaces given (including the interfaces in the
  369. specifications) are added to any interfaces previously
  370. declared.
  371. Previous declarations include declarations for base classes
  372. unless implementsOnly was used.
  373. This function is provided for convenience. It provides a more
  374. convenient way to call classImplements. For example::
  375. implements(I1)
  376. is equivalent to calling::
  377. classImplements(C, I1)
  378. after the class has been created.
  379. Consider the following example (Python 2.x only)::
  380. class C(A, B):
  381. implements(I1, I2)
  382. Instances of ``C`` implement ``I1``, ``I2``, and whatever interfaces
  383. instances of ``A`` and ``B`` implement.
  384. """
  385. def implementsOnly(*interfaces):
  386. """Declare the only interfaces implemented by instances of a class
  387. This function is called in a class definition (Python 2.x only).
  388. The arguments are one or more interfaces or interface
  389. specifications (IDeclaration objects).
  390. Previous declarations including declarations for base classes
  391. are overridden.
  392. This function is provided for convenience. It provides a more
  393. convenient way to call classImplementsOnly. For example::
  394. implementsOnly(I1)
  395. is equivalent to calling::
  396. classImplementsOnly(I1)
  397. after the class has been created.
  398. Consider the following example (Python 2.x only)::
  399. class C(A, B):
  400. implementsOnly(I1, I2)
  401. Instances of ``C`` implement ``I1``, ``I2``, regardless of what
  402. instances of ``A`` and ``B`` implement.
  403. """
  404. def classProvides(*interfaces):
  405. """Declare interfaces provided directly by a class
  406. This function is called in a class definition.
  407. The arguments are one or more interfaces or interface
  408. specifications (IDeclaration objects).
  409. The given interfaces (including the interfaces in the
  410. specifications) are used to create the class's direct-object
  411. interface specification. An error will be raised if the module
  412. class has an direct interface specification. In other words, it is
  413. an error to call this function more than once in a class
  414. definition.
  415. Note that the given interfaces have nothing to do with the
  416. interfaces implemented by instances of the class.
  417. This function is provided for convenience. It provides a more
  418. convenient way to call directlyProvides for a class. For example::
  419. classProvides(I1)
  420. is equivalent to calling::
  421. directlyProvides(theclass, I1)
  422. after the class has been created.
  423. """
  424. def provider(*interfaces):
  425. """A class decorator version of classProvides"""
  426. def moduleProvides(*interfaces):
  427. """Declare interfaces provided by a module
  428. This function is used in a module definition.
  429. The arguments are one or more interfaces or interface
  430. specifications (IDeclaration objects).
  431. The given interfaces (including the interfaces in the
  432. specifications) are used to create the module's direct-object
  433. interface specification. An error will be raised if the module
  434. already has an interface specification. In other words, it is
  435. an error to call this function more than once in a module
  436. definition.
  437. This function is provided for convenience. It provides a more
  438. convenient way to call directlyProvides for a module. For example::
  439. moduleImplements(I1)
  440. is equivalent to::
  441. directlyProvides(sys.modules[__name__], I1)
  442. """
  443. def Declaration(*interfaces):
  444. """Create an interface specification
  445. The arguments are one or more interfaces or interface
  446. specifications (IDeclaration objects).
  447. A new interface specification (IDeclaration) with
  448. the given interfaces is returned.
  449. """
  450. class IAdapterRegistry(Interface):
  451. """Provide an interface-based registry for adapters
  452. This registry registers objects that are in some sense "from" a
  453. sequence of specification to an interface and a name.
  454. No specific semantics are assumed for the registered objects,
  455. however, the most common application will be to register factories
  456. that adapt objects providing required specifications to a provided
  457. interface.
  458. """
  459. def register(required, provided, name, value):
  460. """Register a value
  461. A value is registered for a *sequence* of required specifications, a
  462. provided interface, and a name, which must be text.
  463. """
  464. def registered(required, provided, name=_BLANK):
  465. """Return the component registered for the given interfaces and name
  466. name must be text.
  467. Unlike the lookup method, this methods won't retrieve
  468. components registered for more specific required interfaces or
  469. less specific provided interfaces.
  470. If no component was registered exactly for the given
  471. interfaces and name, then None is returned.
  472. """
  473. def lookup(required, provided, name='', default=None):
  474. """Lookup a value
  475. A value is looked up based on a *sequence* of required
  476. specifications, a provided interface, and a name, which must be
  477. text.
  478. """
  479. def queryMultiAdapter(objects, provided, name=_BLANK, default=None):
  480. """Adapt a sequence of objects to a named, provided, interface
  481. """
  482. def lookup1(required, provided, name=_BLANK, default=None):
  483. """Lookup a value using a single required interface
  484. A value is looked up based on a single required
  485. specifications, a provided interface, and a name, which must be
  486. text.
  487. """
  488. def queryAdapter(object, provided, name=_BLANK, default=None):
  489. """Adapt an object using a registered adapter factory.
  490. """
  491. def adapter_hook(provided, object, name=_BLANK, default=None):
  492. """Adapt an object using a registered adapter factory.
  493. name must be text.
  494. """
  495. def lookupAll(required, provided):
  496. """Find all adapters from the required to the provided interfaces
  497. An iterable object is returned that provides name-value two-tuples.
  498. """
  499. def names(required, provided):
  500. """Return the names for which there are registered objects
  501. """
  502. def subscribe(required, provided, subscriber, name=_BLANK):
  503. """Register a subscriber
  504. A subscriber is registered for a *sequence* of required
  505. specifications, a provided interface, and a name.
  506. Multiple subscribers may be registered for the same (or
  507. equivalent) interfaces.
  508. """
  509. def subscriptions(required, provided, name=_BLANK):
  510. """Get a sequence of subscribers
  511. Subscribers for a *sequence* of required interfaces, and a provided
  512. interface are returned.
  513. """
  514. def subscribers(objects, provided, name=_BLANK):
  515. """Get a sequence of subscription adapters
  516. """
  517. # begin formerly in zope.component
  518. class ComponentLookupError(LookupError):
  519. """A component could not be found."""
  520. class Invalid(Exception):
  521. """A component doesn't satisfy a promise."""
  522. class IObjectEvent(Interface):
  523. """An event related to an object.
  524. The object that generated this event is not necessarily the object
  525. refered to by location.
  526. """
  527. object = Attribute("The subject of the event.")
  528. @implementer(IObjectEvent)
  529. class ObjectEvent(object):
  530. def __init__(self, object):
  531. self.object = object
  532. class IComponentLookup(Interface):
  533. """Component Manager for a Site
  534. This object manages the components registered at a particular site. The
  535. definition of a site is intentionally vague.
  536. """
  537. adapters = Attribute(
  538. "Adapter Registry to manage all registered adapters.")
  539. utilities = Attribute(
  540. "Adapter Registry to manage all registered utilities.")
  541. def queryAdapter(object, interface, name=_BLANK, default=None):
  542. """Look for a named adapter to an interface for an object
  543. If a matching adapter cannot be found, returns the default.
  544. """
  545. def getAdapter(object, interface, name=_BLANK):
  546. """Look for a named adapter to an interface for an object
  547. If a matching adapter cannot be found, a ComponentLookupError
  548. is raised.
  549. """
  550. def queryMultiAdapter(objects, interface, name=_BLANK, default=None):
  551. """Look for a multi-adapter to an interface for multiple objects
  552. If a matching adapter cannot be found, returns the default.
  553. """
  554. def getMultiAdapter(objects, interface, name=_BLANK):
  555. """Look for a multi-adapter to an interface for multiple objects
  556. If a matching adapter cannot be found, a ComponentLookupError
  557. is raised.
  558. """
  559. def getAdapters(objects, provided):
  560. """Look for all matching adapters to a provided interface for objects
  561. Return an iterable of name-adapter pairs for adapters that
  562. provide the given interface.
  563. """
  564. def subscribers(objects, provided):
  565. """Get subscribers
  566. Subscribers are returned that provide the provided interface
  567. and that depend on and are comuted from the sequence of
  568. required objects.
  569. """
  570. def handle(*objects):
  571. """Call handlers for the given objects
  572. Handlers registered for the given objects are called.
  573. """
  574. def queryUtility(interface, name='', default=None):
  575. """Look up a utility that provides an interface.
  576. If one is not found, returns default.
  577. """
  578. def getUtilitiesFor(interface):
  579. """Look up the registered utilities that provide an interface.
  580. Returns an iterable of name-utility pairs.
  581. """
  582. def getAllUtilitiesRegisteredFor(interface):
  583. """Return all registered utilities for an interface
  584. This includes overridden utilities.
  585. An iterable of utility instances is returned. No names are
  586. returned.
  587. """
  588. class IRegistration(Interface):
  589. """A registration-information object
  590. """
  591. registry = Attribute("The registry having the registration")
  592. name = Attribute("The registration name")
  593. info = Attribute("""Information about the registration
  594. This is information deemed useful to people browsing the
  595. configuration of a system. It could, for example, include
  596. commentary or information about the source of the configuration.
  597. """)
  598. class IUtilityRegistration(IRegistration):
  599. """Information about the registration of a utility
  600. """
  601. factory = Attribute("The factory used to create the utility. Optional.")
  602. component = Attribute("The object registered")
  603. provided = Attribute("The interface provided by the component")
  604. class _IBaseAdapterRegistration(IRegistration):
  605. """Information about the registration of an adapter
  606. """
  607. factory = Attribute("The factory used to create adapters")
  608. required = Attribute("""The adapted interfaces
  609. This is a sequence of interfaces adapters by the registered
  610. factory. The factory will be caled with a sequence of objects, as
  611. positional arguments, that provide these interfaces.
  612. """)
  613. provided = Attribute("""The interface provided by the adapters.
  614. This interface is implemented by the factory
  615. """)
  616. class IAdapterRegistration(_IBaseAdapterRegistration):
  617. """Information about the registration of an adapter
  618. """
  619. class ISubscriptionAdapterRegistration(_IBaseAdapterRegistration):
  620. """Information about the registration of a subscription adapter
  621. """
  622. class IHandlerRegistration(IRegistration):
  623. handler = Attribute("An object called used to handle an event")
  624. required = Attribute("""The handled interfaces
  625. This is a sequence of interfaces handled by the registered
  626. handler. The handler will be caled with a sequence of objects, as
  627. positional arguments, that provide these interfaces.
  628. """)
  629. class IRegistrationEvent(IObjectEvent):
  630. """An event that involves a registration"""
  631. @implementer(IRegistrationEvent)
  632. class RegistrationEvent(ObjectEvent):
  633. """There has been a change in a registration
  634. """
  635. def __repr__(self):
  636. return "%s event:\n%r" % (self.__class__.__name__, self.object)
  637. class IRegistered(IRegistrationEvent):
  638. """A component or factory was registered
  639. """
  640. @implementer(IRegistered)
  641. class Registered(RegistrationEvent):
  642. pass
  643. class IUnregistered(IRegistrationEvent):
  644. """A component or factory was unregistered
  645. """
  646. @implementer(IUnregistered)
  647. class Unregistered(RegistrationEvent):
  648. """A component or factory was unregistered
  649. """
  650. pass
  651. class IComponentRegistry(Interface):
  652. """Register components
  653. """
  654. def registerUtility(component=None, provided=None, name=_BLANK,
  655. info=_BLANK, factory=None):
  656. """Register a utility
  657. factory
  658. Factory for the component to be registerd.
  659. component
  660. The registered component
  661. provided
  662. This is the interface provided by the utility. If the
  663. component provides a single interface, then this
  664. argument is optional and the component-implemented
  665. interface will be used.
  666. name
  667. The utility name.
  668. info
  669. An object that can be converted to a string to provide
  670. information about the registration.
  671. Only one of component and factory can be used.
  672. A Registered event is generated with an IUtilityRegistration.
  673. """
  674. def unregisterUtility(component=None, provided=None, name=_BLANK,
  675. factory=None):
  676. """Unregister a utility
  677. A boolean is returned indicating whether the registry was
  678. changed. If the given component is None and there is no
  679. component registered, or if the given component is not
  680. None and is not registered, then the function returns
  681. False, otherwise it returns True.
  682. factory
  683. Factory for the component to be unregisterd.
  684. component
  685. The registered component The given component can be
  686. None, in which case any component registered to provide
  687. the given provided interface with the given name is
  688. unregistered.
  689. provided
  690. This is the interface provided by the utility. If the
  691. component is not None and provides a single interface,
  692. then this argument is optional and the
  693. component-implemented interface will be used.
  694. name
  695. The utility name.
  696. Only one of component and factory can be used.
  697. An UnRegistered event is generated with an IUtilityRegistration.
  698. """
  699. def registeredUtilities():
  700. """Return an iterable of IUtilityRegistration instances.
  701. These registrations describe the current utility registrations
  702. in the object.
  703. """
  704. def registerAdapter(factory, required=None, provided=None, name=_BLANK,
  705. info=_BLANK):
  706. """Register an adapter factory
  707. Parameters:
  708. factory
  709. The object used to compute the adapter
  710. required
  711. This is a sequence of specifications for objects to be
  712. adapted. If omitted, then the value of the factory's
  713. __component_adapts__ attribute will be used. The
  714. __component_adapts__ attribute is usually attribute is
  715. normally set in class definitions using adapts
  716. function, or for callables using the adapter
  717. decorator. If the factory doesn't have a
  718. __component_adapts__ adapts attribute, then this
  719. argument is required.
  720. provided
  721. This is the interface provided by the adapter and
  722. implemented by the factory. If the factory
  723. implements a single interface, then this argument is
  724. optional and the factory-implemented interface will be
  725. used.
  726. name
  727. The adapter name.
  728. info
  729. An object that can be converted to a string to provide
  730. information about the registration.
  731. A Registered event is generated with an IAdapterRegistration.
  732. """
  733. def unregisterAdapter(factory=None, required=None,
  734. provided=None, name=_BLANK):
  735. """Unregister an adapter factory
  736. A boolean is returned indicating whether the registry was
  737. changed. If the given component is None and there is no
  738. component registered, or if the given component is not
  739. None and is not registered, then the function returns
  740. False, otherwise it returns True.
  741. Parameters:
  742. factory
  743. This is the object used to compute the adapter. The
  744. factory can be None, in which case any factory
  745. registered to implement the given provided interface
  746. for the given required specifications with the given
  747. name is unregistered.
  748. required
  749. This is a sequence of specifications for objects to be
  750. adapted. If the factory is not None and the required
  751. arguments is omitted, then the value of the factory's
  752. __component_adapts__ attribute will be used. The
  753. __component_adapts__ attribute attribute is normally
  754. set in class definitions using adapts function, or for
  755. callables using the adapter decorator. If the factory
  756. is None or doesn't have a __component_adapts__ adapts
  757. attribute, then this argument is required.
  758. provided
  759. This is the interface provided by the adapter and
  760. implemented by the factory. If the factory is not
  761. None and implements a single interface, then this
  762. argument is optional and the factory-implemented
  763. interface will be used.
  764. name
  765. The adapter name.
  766. An Unregistered event is generated with an IAdapterRegistration.
  767. """
  768. def registeredAdapters():
  769. """Return an iterable of IAdapterRegistration instances.
  770. These registrations describe the current adapter registrations
  771. in the object.
  772. """
  773. def registerSubscriptionAdapter(factory, required=None, provides=None,
  774. name=_BLANK, info=''):
  775. """Register a subscriber factory
  776. Parameters:
  777. factory
  778. The object used to compute the adapter
  779. required
  780. This is a sequence of specifications for objects to be
  781. adapted. If omitted, then the value of the factory's
  782. __component_adapts__ attribute will be used. The
  783. __component_adapts__ attribute is usually attribute is
  784. normally set in class definitions using adapts
  785. function, or for callables using the adapter
  786. decorator. If the factory doesn't have a
  787. __component_adapts__ adapts attribute, then this
  788. argument is required.
  789. provided
  790. This is the interface provided by the adapter and
  791. implemented by the factory. If the factory implements
  792. a single interface, then this argument is optional and
  793. the factory-implemented interface will be used.
  794. name
  795. The adapter name.
  796. Currently, only the empty string is accepted. Other
  797. strings will be accepted in the future when support for
  798. named subscribers is added.
  799. info
  800. An object that can be converted to a string to provide
  801. information about the registration.
  802. A Registered event is generated with an
  803. ISubscriptionAdapterRegistration.
  804. """
  805. def unregisterSubscriptionAdapter(factory=None, required=None,
  806. provides=None, name=_BLANK):
  807. """Unregister a subscriber factory.
  808. A boolean is returned indicating whether the registry was
  809. changed. If the given component is None and there is no
  810. component registered, or if the given component is not
  811. None and is not registered, then the function returns
  812. False, otherwise it returns True.
  813. Parameters:
  814. factory
  815. This is the object used to compute the adapter. The
  816. factory can be None, in which case any factories
  817. registered to implement the given provided interface
  818. for the given required specifications with the given
  819. name are unregistered.
  820. required
  821. This is a sequence of specifications for objects to be
  822. adapted. If the factory is not None and the required
  823. arguments is omitted, then the value of the factory's
  824. __component_adapts__ attribute will be used. The
  825. __component_adapts__ attribute attribute is normally
  826. set in class definitions using adapts function, or for
  827. callables using the adapter decorator. If the factory
  828. is None or doesn't have a __component_adapts__ adapts
  829. attribute, then this argument is required.
  830. provided
  831. This is the interface provided by the adapter and
  832. implemented by the factory. If the factory is not
  833. None implements a single interface, then this argument
  834. is optional and the factory-implemented interface will
  835. be used.
  836. name
  837. The adapter name.
  838. Currently, only the empty string is accepted. Other
  839. strings will be accepted in the future when support for
  840. named subscribers is added.
  841. An Unregistered event is generated with an
  842. ISubscriptionAdapterRegistration.
  843. """
  844. def registeredSubscriptionAdapters():
  845. """Return an iterable of ISubscriptionAdapterRegistration instances.
  846. These registrations describe the current subscription adapter
  847. registrations in the object.
  848. """
  849. def registerHandler(handler, required=None, name=_BLANK, info=''):
  850. """Register a handler.
  851. A handler is a subscriber that doesn't compute an adapter
  852. but performs some function when called.
  853. Parameters:
  854. handler
  855. The object used to handle some event represented by
  856. the objects passed to it.
  857. required
  858. This is a sequence of specifications for objects to be
  859. adapted. If omitted, then the value of the factory's
  860. __component_adapts__ attribute will be used. The
  861. __component_adapts__ attribute is usually attribute is
  862. normally set in class definitions using adapts
  863. function, or for callables using the adapter
  864. decorator. If the factory doesn't have a
  865. __component_adapts__ adapts attribute, then this
  866. argument is required.
  867. name
  868. The handler name.
  869. Currently, only the empty string is accepted. Other
  870. strings will be accepted in the future when support for
  871. named handlers is added.
  872. info
  873. An object that can be converted to a string to provide
  874. information about the registration.
  875. A Registered event is generated with an IHandlerRegistration.
  876. """
  877. def unregisterHandler(handler=None, required=None, name=_BLANK):
  878. """Unregister a handler.
  879. A handler is a subscriber that doesn't compute an adapter
  880. but performs some function when called.
  881. A boolean is returned indicating whether the registry was
  882. changed.
  883. Parameters:
  884. handler
  885. This is the object used to handle some event
  886. represented by the objects passed to it. The handler
  887. can be None, in which case any handlers registered for
  888. the given required specifications with the given are
  889. unregistered.
  890. required
  891. This is a sequence of specifications for objects to be
  892. adapted. If omitted, then the value of the factory's
  893. __component_adapts__ attribute will be used. The
  894. __component_adapts__ attribute is usually attribute is
  895. normally set in class definitions using adapts
  896. function, or for callables using the adapter
  897. decorator. If the factory doesn't have a
  898. __component_adapts__ adapts attribute, then this
  899. argument is required.
  900. name
  901. The handler name.
  902. Currently, only the empty string is accepted. Other
  903. strings will be accepted in the future when support for
  904. named handlers is added.
  905. An Unregistered event is generated with an IHandlerRegistration.
  906. """
  907. def registeredHandlers():
  908. """Return an iterable of IHandlerRegistration instances.
  909. These registrations describe the current handler registrations
  910. in the object.
  911. """
  912. class IComponents(IComponentLookup, IComponentRegistry):
  913. """Component registration and access
  914. """
  915. # end formerly in zope.component