From 993f5236962c89e644f5521b3eeecfd85aad8734 Mon Sep 17 00:00:00 2001 From: helmutm Date: Tue, 15 May 2007 09:36:02 +0000 Subject: [PATCH] removed Instance; added schema package git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1734 fd906abe-77d9-0310-91a1-e0d9ade77398 --- composer/README.txt | 21 ++++++++----------- composer/base.py | 5 +++-- composer/client.py | 25 +++++------------------ composer/interfaces.py | 22 ++++---------------- composer/schema/client.py | 38 +++++++++++++++++++++++++++++++++++ composer/schema/field.py | 37 ++++++++++++++++++++++++++++++++++ composer/schema/interfaces.py | 27 +++++++++++++++++++++++++ composer/schema/schema.py | 33 ++++++++++++++++++++++++++++++ composer/schema/tests.py | 22 ++++++++++++++++++++ 9 files changed, 177 insertions(+), 53 deletions(-) create mode 100644 composer/schema/client.py create mode 100644 composer/schema/field.py create mode 100644 composer/schema/interfaces.py create mode 100644 composer/schema/schema.py create mode 100755 composer/schema/tests.py diff --git a/composer/README.txt b/composer/README.txt index 541f5a6..475ee63 100644 --- a/composer/README.txt +++ b/composer/README.txt @@ -5,7 +5,7 @@ Composer - Building Complex Structures with Templates or Schemas ($Id$) >>> from cybertools.composer.base import Element, Compound, Template - >>> from cybertools.composer.client import Instance, Client + >>> from cybertools.composer.client import Client We set up a very simple demonstration system using a PC configurator. We start with two classes denoting a configuration and a simple @@ -40,21 +40,16 @@ We need another class denoting the product that will be created. >>> c001 = Product('c001') -The real stuff will be done by an instance that connects the product -(via the client) with the template. +The real stuff will be done by a client adpater that connects the product +with the template. - >>> class ConfigurationInstance(Instance): + >>> class ConfigurationAdapter(Client): ... def applyTemplate(self): ... for c in self.template.components: - ... print c, self.parent.context.parts.get(c.name, '-') + ... print c, self.context.parts.get(c.name, '-') -In this case we can directly use the basic client adapter for setting up the -connection. As we have only one template we also associate only one -instance with the client. - - >>> client = Client(c001) - >>> client.instances.append(ConfigurationInstance(client, desktop)) - >>> client.applyTemplates() + >>> client = ConfigurationAdapter(c001, desktop) + >>> client.applyTemplate() case - mainboard - cpu - @@ -63,7 +58,7 @@ instance with the client. If we have configured a CPU for our configuration this will be listed. >>> c001.parts['cpu'] = Product('z80') - >>> client.applyTemplates() + >>> client.applyTemplate() case - mainboard - cpu z80 diff --git a/composer/base.py b/composer/base.py index 0a0af1b..21a8ae8 100644 --- a/composer/base.py +++ b/composer/base.py @@ -26,6 +26,7 @@ from zope.interface import implements from cybertools.composer.interfaces import IComponent, IElement, ICompound from cybertools.composer.interfaces import ITemplate +from cybertools.util.jeep import Jeep class Component(object): @@ -43,7 +44,7 @@ class Compound(Component): implements(ICompound) def __init__(self): - self.parts = [] + self.parts = Jeep() class Template(object): @@ -51,5 +52,5 @@ class Template(object): implements(ITemplate) def __init__(self): - self.components = [] + self.components = Jeep() diff --git a/composer/client.py b/composer/client.py index c4a6041..d6273c3 100644 --- a/composer/client.py +++ b/composer/client.py @@ -24,33 +24,18 @@ $Id$ from zope.interface import implements -from cybertools.composer.interfaces import IInstance, IClient - - -class Instance(object): - - implements(IInstance) - - parent = None - template = None - - def __init__(self, parent, template): - self.parent = parent - self.template = template - - def applyTemplate(self, *args, **kw): - raise ValueError('To be implemented by subclass') +from cybertools.composer.interfaces import IClient class Client(object): implements(IClient) - def __init__(self, context): + def __init__(self, context, template): self.context = context + self.template = template self.instances = [] - def applyTemplates(self, *args, **kw): - for inst in self.instances: - inst.applyTemplate(*args, **kw) + def applyTemplate(self, *args, **kw): + raise ValueError('To be implemented by subclass') diff --git a/composer/interfaces.py b/composer/interfaces.py index f80233a..7c26020 100644 --- a/composer/interfaces.py +++ b/composer/interfaces.py @@ -55,31 +55,17 @@ class ITemplate(Interface): 'object is built upon') -# client side interfaces - -class IInstance(Interface): - """ Represents an object that uses a template. - """ - - parent = Attribute('The client this instance belongs to') - template = Attribute('Template this instance is associated with') - - def applyTemplate(*args, **kw): - """ Apply the template (in the parent's context). Note that this - method is just an example - instance classes may define - other methods that provide more specific actions. - """ - +# client side class IClient(Interface): """ Represents an object that uses a set of templates via its instances. """ context = Attribute('Object this client adapter has been created for') - instances = Attribute('An ordered or unordered sequence of instance objects') + template = Attribute('The template to be used for this client') - def applyTemplates(*args, **kw): - """ Apply the templates of all instances. Note that this + def applyTemplate(*args, **kw): + """ Apply the template using the client's context. Note that this method is just an example - client classes may define other methods that provide more specific actions. """ diff --git a/composer/schema/client.py b/composer/schema/client.py new file mode 100644 index 0000000..1a910a6 --- /dev/null +++ b/composer/schema/client.py @@ -0,0 +1,38 @@ +# +# 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 classes for schemas. + +$Id$ +""" + +from zope.interface import implements + +from cybertools.composer.client import Client + + +class Editor(Client): + + def applyTemplate(self, data={}, *args, **kw): + for c in self.template.components: + # save data (if available) in context + # build sequence of fields with data from context + # or directly use request... + print c.name, getattr(self.context, c.name, '-') + diff --git a/composer/schema/field.py b/composer/schema/field.py new file mode 100644 index 0000000..ec36264 --- /dev/null +++ b/composer/schema/field.py @@ -0,0 +1,37 @@ +# +# 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 +# + +""" +Schema fields + +$Id$ +""" + +from zope.interface import implements + +from cybertools.composer.base import Component, Element, Compound +from cybertools.composer.base import Template + + +class Field(Component): + + def __init__(self, name, title=None, **kw): + assert name + self.name = self.__name__ = name + self.title = title is None and name or title + diff --git a/composer/schema/interfaces.py b/composer/schema/interfaces.py new file mode 100644 index 0000000..1046ac9 --- /dev/null +++ b/composer/schema/interfaces.py @@ -0,0 +1,27 @@ +# +# 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 +# + +""" +Schemas and Fields. + +$Id$ +""" + +from zope.interface import Interface, Attribute + + diff --git a/composer/schema/schema.py b/composer/schema/schema.py new file mode 100644 index 0000000..82e70b1 --- /dev/null +++ b/composer/schema/schema.py @@ -0,0 +1,33 @@ +# +# 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 +# + +""" +Basic classes for a complex template structures. + +$Id$ +""" + +from zope.interface import implements + +from cybertools.composer.base import Component, Element, Compound +from cybertools.composer.base import Template + + +class Schema(Template): + + pass diff --git a/composer/schema/tests.py b/composer/schema/tests.py new file mode 100755 index 0000000..2e7a216 --- /dev/null +++ b/composer/schema/tests.py @@ -0,0 +1,22 @@ +# $Id$ + +import unittest, doctest +from zope.testing.doctestunit import DocFileSuite + + +class Test(unittest.TestCase): + "Basic tests for the cybertools.composer.schema package." + + def testBasics(self): + pass + + +def test_suite(): + flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS + return unittest.TestSuite(( + unittest.makeSuite(Test), + DocFileSuite('README.txt', optionflags=flags), + )) + +if __name__ == '__main__': + unittest.main(defaultTest='test_suite')