Skip to main content

Infrastructure for maintaining a registry of available behaviors

Project description

By Martin Aspeli

This package provides optional support for “behaviors”. A behavior is essentially a conditionally available adapter.

For example, let’s say that your application needs to support object-level locking, and that this can be modelled via an adapter, but you want to leave it until runtime to determine whether locking is enabled for a particular object. You could then register locking as a behavior.

Requirements

This package comes with support for registering behaviors and factories. It does not, however, implement the policy for determining what behaviors are enabled on a particular object at a particular time. That decision is deferred to an IBehaviorAssignable adapter, which you must implement.

The intention is that behavior assignment is generic across an application, used for multiple, optional behaviors. It probably doesn’t make much sense to use plone.behavior for a single type of behavior. The means to keep track of which behaviors are enabled for what types of objects will be application specific.

Usage

A behavior is written much like an adapter, except that you don’t specify the type of context being adapted directly. For example:

from zope.interface import Interface, implements

class ILockingSupport(Interface):
   """Support locking
   """

   def lock():
       """Lock an object
       """

   def unlock():
       """Unlock an object
       """

class LockingSupport(object):
    implements(ILockingSupport)

    def __init__(self, context):
        self.context = context

    def lock(self):
        # do something

    def unlock(self):
        # do something

This interface (which describes the type of behavior) and class (which describes the implementation of the behavior) then need to be registered.

The simplest way to do that is to load the meta.zcml file from this package and use ZCML:

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:plone="http://namespaces.plone.org/plone"
    i18n_domain="my.package">

    <include package="plone.behavior" file="meta.zcml" />

    <plone:behavior
        name="my.package.Locking"
        title="Locking support"
        description="Optional object-level locking"
        interface=".interfaces.ILockingSupport"
        factory=".locking.LockingSupport"
        />

</configure>

After this is done - and presuming an appropriate IBehaviorAssignable adapter exists for the context - you can adapt a context to ILockingSupport as normal:

locking = ILockingSupport(context, None)

if locking is not None:
    locking.lock()

You’ll get an instance of LockingSupport if context can be adapted to IBehaviorAssignable (which, recall, is application specific), and if the implementation of IBehaviorAssignable says that this context supports this particular behavior.

Please see behavior.txt and directives.txt for more details. Changelog =========

1.0b1 - Released April 27th 2008

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plone.behavior-1.0b1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

plone.behavior-1.0b1-py2.4.egg (14.8 kB view details)

Uploaded Source

File details

Details for the file plone.behavior-1.0b1.tar.gz.

File metadata

File hashes

Hashes for plone.behavior-1.0b1.tar.gz
Algorithm Hash digest
SHA256 1325d8ca5a78c34756066edbbef1ed095d9df3fc9c0a0f95d523f1f337d5b3dc
MD5 ea2f515b159c2f9fa5164ac899fa6f9c
BLAKE2b-256 4d11a39626a5973802f1fd6de5314949df37ae0443e609228579b14817d6f746

See more details on using hashes here.

File details

Details for the file plone.behavior-1.0b1-py2.4.egg.

File metadata

File hashes

Hashes for plone.behavior-1.0b1-py2.4.egg
Algorithm Hash digest
SHA256 c3881a6ad1577095b7f9f729366a114ddfdf5e6442df2d9866d7af512be5e321
MD5 6f83f3420b6721228d6228bea0e4f30b
BLAKE2b-256 0f1d3e56a6040d297af5e36eeec7add1336886a07f7de4cc9b30eff6cb90776d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page