Skip to main content

Infrastructure for maintaining a registry of available behaviors

Project description

plone.behavior

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.0b2.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plone.behavior-1.0b2-py2.4.egg (20.1 kB view details)

Uploaded Egg

File details

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

File metadata

File hashes

Hashes for plone.behavior-1.0b2.tar.gz
Algorithm Hash digest
SHA256 102ba3bce2e1741d0e7291f9c8dc0343eee331d1742f1b1125f54c1a310239eb
MD5 20d83528cd109eb99d570249c51c42e6
BLAKE2b-256 9c65cc3e7d8751ea2bcce31c2bc5ec31179b1fcd9e7bf29535ed5bf7197fa4ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for plone.behavior-1.0b2-py2.4.egg
Algorithm Hash digest
SHA256 da574a9851eef042948cb2e7648721cf5054964f0d4e3ac57ffc12770acb6d36
MD5 3663db5ba1ed70b4d4432913b5ee4dbf
BLAKE2b-256 0a15deec5ea5a314b6030b446d58e360759f1e79cd961c426ca13b43165564ca

See more details on using hashes here.

Supported by

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