[Checkins] SVN: Sandbox/darrylcousins/mars.adapter/s Removed tests
directory and replaced with tests.py. Doctest runs now from
adapter.txt
Darryl Cousins
darryl at darrylcousins.net.nz
Sun Jul 15 23:49:16 EDT 2007
Log message for revision 78012:
Removed tests directory and replaced with tests.py. Doctest runs now from adapter.txt
Changed:
U Sandbox/darrylcousins/mars.adapter/setup.py
U Sandbox/darrylcousins/mars.adapter/src/mars/adapter/README.txt
A Sandbox/darrylcousins/mars.adapter/src/mars/adapter/adapter.txt
U Sandbox/darrylcousins/mars.adapter/src/mars/adapter/meta.py
D Sandbox/darrylcousins/mars.adapter/src/mars/adapter/tests/
A Sandbox/darrylcousins/mars.adapter/src/mars/adapter/tests.py
-=-
Modified: Sandbox/darrylcousins/mars.adapter/setup.py
===================================================================
--- Sandbox/darrylcousins/mars.adapter/setup.py 2007-07-15 21:45:32 UTC (rev 78011)
+++ Sandbox/darrylcousins/mars.adapter/setup.py 2007-07-16 03:49:14 UTC (rev 78012)
@@ -23,22 +23,9 @@
license='ZPL',
dependency_links = ['http://download.zope.org/distribution'],
extras_require = dict(
- test=['zope.app.testing',
- 'zope.testbrowser',
- 'zope.app.zcmlfiles',
- 'zope.app.securitypolicy',
- 'zope.app.authentication',
- 'z3c.layer',
- ]
- ),
+ test=['zope.app.testing']),
install_requires = [
'setuptools',
- 'zope.app.intid',
- 'zope.app.catalog',
- 'zope.component',
- 'zope.interface',
- 'zope.publisher',
'grok',
- 'martian',
],
)
Modified: Sandbox/darrylcousins/mars.adapter/src/mars/adapter/README.txt
===================================================================
--- Sandbox/darrylcousins/mars.adapter/src/mars/adapter/README.txt 2007-07-15 21:45:32 UTC (rev 78011)
+++ Sandbox/darrylcousins/mars.adapter/src/mars/adapter/README.txt 2007-07-16 03:49:14 UTC (rev 78012)
@@ -21,6 +21,17 @@
The mars.adapter package provides the means of creating and configuring simple
``adapters`` for an application using Zope3.
+It is to be used primarily when an existing adapter is required to be registered
+(as in the following example) and can be used in place of the zcml directive::
+
+ <adapter factory="MyAdapter" />
+
+The adapter should define what interface is provided and which interfaces shall
+be adapted.
+
+For more complex adapters and multi-adapters ``grok.Adapter`` and
+``grok.MultiAdapter`` could be used.
+
Example Code
------------
@@ -32,7 +43,6 @@
lambda adapter: datetime.date.today(),
field=IHelloWorld['when'], view=IAddForm))
-
Directives specific to this package
-----------------------------------
@@ -47,10 +57,7 @@
If defined the factory will be registered as a `named adapter`.
Default: empty string
+Doctest
+-------
-Tests
------
-
-See test directory.
-
-
+Please see `./adapter.txt`.
Added: Sandbox/darrylcousins/mars.adapter/src/mars/adapter/adapter.txt
===================================================================
--- Sandbox/darrylcousins/mars.adapter/src/mars/adapter/adapter.txt (rev 0)
+++ Sandbox/darrylcousins/mars.adapter/src/mars/adapter/adapter.txt 2007-07-16 03:49:14 UTC (rev 78012)
@@ -0,0 +1,96 @@
+============
+Mars Adapter
+============
+
+This package offers a simple factory grokker to register adapters. More complex
+adapters can be built using the grok.Adapter class but often we want to register
+an existing adapter factory in a simple way. Example of the use of this adapter
+factory can be found in `mars.formdemo` where it used to register button and
+labels for `z3c.form`.
+
+Set up
+------
+
+First we make a few imports and then define two interfaces. The first, `IValue`
+is the interface to which our `field` object will be adapted to and the second,
+`IField` defines the interface that the `field` object will directly implement.
+
+ >>> import zope.interface
+ >>> import zope.component
+ >>> import grok
+ >>> import mars.adapter
+ >>> class IValue(zope.interface.Interface):
+ ... pass
+ >>> class IField(zope.interface.Interface):
+ ... pass
+
+Now we define the `Field` object.
+
+ >>> class Field(object):
+ ... """A field"""
+ ... zope.interface.implements(IField)
+
+And create the object to be used later.
+
+ >>> field = Field()
+
+Un-named Adapter
+----------------
+
+First we define an adapter that we wish to look up for the `field` object. It is
+a simple adapter that will take only the adapted object in its __init__ method.
+
+ >>> class ValueAdapter(object):
+ ... """Static value adapter."""
+ ... zope.component.adapts(IField)
+ ... zope.interface.implements(IValue)
+ ...
+ ... def __init__(self, context):
+ ... self.context = context
+ ...
+ ... def __repr__(self):
+ ... return self.__class__.__name__
+
+Then we can define the adapter factory that subclasses AdapterFactory and
+contains a single directive (which is required) that defined the adapter to be
+used.
+
+ >>> class UnNamedValueAdapter(mars.adapter.AdapterFactory):
+ ... mars.adapter.factory(ValueAdapter)
+
+In the test we manually ``grok`` the factory, normally this happens when a
+module is ``grokked`` on start up.
+
+ >>> from mars.adapter.meta import AdapterFactoryGrokker
+ >>> AdapterFactoryGrokker().grok('unnamed', UnNamedValueAdapter, Field, None, None)
+ True
+
+It should now be possible to retrieve the adapter from the component
+architecture.
+
+ >>> print zope.component.queryAdapter(field, IValue)
+ ValueAdapter
+
+Named Adapter
+------------
+
+This example differs from the first only that we wish now to be able to look up
+the adapter using a name. To do this it is only required that we add the
+``grok.name`` directive.
+
+ >>> class NamedValueAdapter(mars.adapter.AdapterFactory):
+ ... grok.name('named')
+ ... mars.adapter.factory(ValueAdapter)
+
+Again we manually ``grok`` the factory.
+
+ >>> AdapterFactoryGrokker().grok('named', NamedValueAdapter, Field, None, None)
+ True
+
+It should now be possible to retrieve the adapter from the component
+architecture using the name defined.
+
+ >>> print zope.component.queryAdapter(field, IValue, 'named')
+ ValueAdapter
+
+
Property changes on: Sandbox/darrylcousins/mars.adapter/src/mars/adapter/adapter.txt
___________________________________________________________________
Name: svn:keywords
+ Date Author
Modified: Sandbox/darrylcousins/mars.adapter/src/mars/adapter/meta.py
===================================================================
--- Sandbox/darrylcousins/mars.adapter/src/mars/adapter/meta.py 2007-07-15 21:45:32 UTC (rev 78011)
+++ Sandbox/darrylcousins/mars.adapter/src/mars/adapter/meta.py 2007-07-16 03:49:14 UTC (rev 78012)
@@ -12,8 +12,6 @@
component_class = mars.adapter.AdapterFactory
def grok(self, name, factory, context, module_info, templates):
- #adapter_context = util.determine_class_context(factory, context)
- #provides = util.class_annotation(factory, 'grok.provides', None)
name = util.class_annotation(factory, 'grok.name', '')
factory = util.class_annotation(factory, 'mars.adapter.factory', None)
provided = zope.component.registry._getAdapterProvided(factory)
Added: Sandbox/darrylcousins/mars.adapter/src/mars/adapter/tests.py
===================================================================
--- Sandbox/darrylcousins/mars.adapter/src/mars/adapter/tests.py (rev 0)
+++ Sandbox/darrylcousins/mars.adapter/src/mars/adapter/tests.py 2007-07-16 03:49:14 UTC (rev 78012)
@@ -0,0 +1,17 @@
+import unittest
+from zope.testing import doctest
+
+optionflags = doctest.NORMALIZE_WHITESPACE + doctest.ELLIPSIS
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTests([doctest.DocFileSuite('adapter.txt',
+ optionflags=optionflags),
+ ])
+
+ return suite
+
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
+
Property changes on: Sandbox/darrylcousins/mars.adapter/src/mars/adapter/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
More information about the Checkins
mailing list