removed Instance; added schema package

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1734 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2007-05-15 09:36:02 +00:00
parent 954db76abe
commit 993f523696
9 changed files with 177 additions and 53 deletions

View file

@ -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

View file

@ -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()

View file

@ -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')

View file

@ -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.
"""

38
composer/schema/client.py Normal file
View file

@ -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, '-')

37
composer/schema/field.py Normal file
View file

@ -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

View file

@ -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

33
composer/schema/schema.py Normal file
View file

@ -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

22
composer/schema/tests.py Executable file
View file

@ -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')