diff --git a/composer/interfaces.py b/composer/interfaces.py index 028cbda..b3297eb 100644 --- a/composer/interfaces.py +++ b/composer/interfaces.py @@ -58,7 +58,7 @@ class ITemplate(Interface): # instances class IInstance(Interface): - """ Represents (adapts) an object that uses a template. + """ Represents (adapts) an object (a client) that uses a template. """ context = Attribute('Object this instance adapter has been created for') diff --git a/composer/schema/client.py b/composer/schema/client.py new file mode 100644 index 0000000..b769fd9 --- /dev/null +++ b/composer/schema/client.py @@ -0,0 +1,51 @@ +# +# Copyright (c) 2007 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Client implementations. + +$Id$ +""" + +from zope.component import adapts +from zope.interface import implements + +from cybertools.composer.schema.interfaces import IClient +from cybertools.composer.schema.interfaces import IClientManager, IClientFactory + + +class Client(object): + + implements(IClient) + + def __init__(self, manager): + self.manager = manager + + +class ClientFactory(object): + + implements(IClientFactory) + adapts(IClientManager) + + def __init__(self, context): + self.context = context + + def __call__(self): + return Client(self.context) + + diff --git a/composer/schema/instance.py b/composer/schema/instance.py index be3b909..5d25db0 100644 --- a/composer/schema/instance.py +++ b/composer/schema/instance.py @@ -17,14 +17,18 @@ # """ -Client classes for schemas. +Instance adapter classes for schemas. $Id$ """ +from BTrees.OOBTree import OOBTree +from zope.component import adapts from zope.interface import implements from cybertools.composer.instance import Instance +from cybertools.composer.interfaces import IInstance +from cybertools.composer.schema.interfaces import IClient class Editor(Instance): @@ -39,3 +43,34 @@ class Editor(Instance): # or directly use request... print c.name, getattr(self.context, c.name, '-') + +class ClientInstanceAdapter(object): + + implements(IInstance) + adapts(IClient) + + baseAspect = 'schema.client.' + schema = 'default' + + @property + def aspect(self): + return self.baseAspect + self.schema + + @property + def template(self): + return self.context.manager.clientSchemas.get(self.schema, None) + + def __init__(self, context): + self.context = context + + def applyTemplate(self, data={}, schema='default', **kw): + if getattr(self.context, 'attributes', None) is None: + self.context.attributes = OOBTree() + self.schema = schema + template = self.template + attributes = self.context.attributes.setdefault(self.aspect, OOBTree()) + if template is not None: + for c in template.components: + name = c.name + attributes[name] = data.get(name, u'') + diff --git a/composer/schema/interfaces.py b/composer/schema/interfaces.py index 4826ef9..467b0e3 100644 --- a/composer/schema/interfaces.py +++ b/composer/schema/interfaces.py @@ -73,3 +73,32 @@ class IField(IComponent): description=_(u'Must a value been entered into this field?'), required=False,) + +# clients + +class IClient(Interface): + """ An fairly abstract interface for objects to be used as clients + for other objects (e.g. services). + """ + + manager = Attribute('The object that cares for this client.') + + +class IClientFactory(Interface): + """ Creates client objects. + """ + + def __call__(): + """ Creates and returns a client object. + """ + + +class IClientManager(Interface): + """ Cares for a client typically providing schemas. + """ + + clientSchemas = Attribute('A collection of schema objects ' + 'that describe the data fields of the client ' + 'objects.') + +