[Checkins] SVN: z3c.sampledata/ Initial import: Sampledata
generator with plugin functionality.
Jürgen Kartnaller
juergen at kartnaller.at
Wed Aug 16 08:14:18 EDT 2006
Log message for revision 69559:
Initial import: Sampledata generator with plugin functionality.
Thanks to Albert (POV) for the initial algorithm to execute dependent plugins.
Changed:
A z3c.sampledata/
A z3c.sampledata/trunk/
A z3c.sampledata/trunk/src/
A z3c.sampledata/trunk/src/z3c/
A z3c.sampledata/trunk/src/z3c/sampledata/
A z3c.sampledata/trunk/src/z3c/sampledata/README.txt
A z3c.sampledata/trunk/src/z3c/sampledata/SETUP.cfg
A z3c.sampledata/trunk/src/z3c/sampledata/__init__.py
A z3c.sampledata/trunk/src/z3c/sampledata/browser/
A z3c.sampledata/trunk/src/z3c/sampledata/browser/README.txt
A z3c.sampledata/trunk/src/z3c/sampledata/browser/__init__.py
A z3c.sampledata/trunk/src/z3c/sampledata/browser/configure.zcml
A z3c.sampledata/trunk/src/z3c/sampledata/browser/generate.pt
A z3c.sampledata/trunk/src/z3c/sampledata/browser/managers.pt
A z3c.sampledata/trunk/src/z3c/sampledata/browser/views.py
A z3c.sampledata/trunk/src/z3c/sampledata/configure.zcml
A z3c.sampledata/trunk/src/z3c/sampledata/data.py
A z3c.sampledata/trunk/src/z3c/sampledata/ftesting.zcml
A z3c.sampledata/trunk/src/z3c/sampledata/ftests.py
A z3c.sampledata/trunk/src/z3c/sampledata/interfaces.py
A z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-configure.zcml
A z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-meta.zcml
A z3c.sampledata/trunk/src/z3c/sampledata/manager.py
A z3c.sampledata/trunk/src/z3c/sampledata/meta.zcml
A z3c.sampledata/trunk/src/z3c/sampledata/metaconfigure.py
A z3c.sampledata/trunk/src/z3c/sampledata/metadirectives.py
A z3c.sampledata/trunk/src/z3c/sampledata/principal.py
A z3c.sampledata/trunk/src/z3c/sampledata/principal.txt
A z3c.sampledata/trunk/src/z3c/sampledata/site.py
A z3c.sampledata/trunk/src/z3c/sampledata/site.zcml
A z3c.sampledata/trunk/src/z3c/sampledata/testlines.csv
A z3c.sampledata/trunk/src/z3c/sampledata/testlines.txt
A z3c.sampledata/trunk/src/z3c/sampledata/testprincipals.txt
A z3c.sampledata/trunk/src/z3c/sampledata/tests.py
-=-
Added: z3c.sampledata/trunk/src/z3c/sampledata/README.txt
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/README.txt 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/README.txt 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,245 @@
+==========================================
+Pluggable sample data framework for Lovely
+==========================================
+
+There are several goals to this framework:
+
+- provide the developers with an automatic setup that is close to
+ real-world use.
+
+- provide the users with an easy setup for evaluation with plausible data
+
+- provide the user a management interface for different sample generators
+
+The framework is pluggable and allows the creators of generator
+extensions to provide their own plugins that generate sample data for
+those extensions.
+
+
+Generators
+----------
+
+A generator generates a setup.
+
+ >>> from zope import interface
+ >>> from zope import component
+ >>> from lovely.sampledata.interfaces import ISampleDataPlugin
+
+ >>> class GeneratePrincipals(object):
+ ... interface.implements(ISampleDataPlugin)
+ ... name = 'lovely.principals'
+ ... dependencies = []
+ ... schema = None
+ ... def generate(self, context, param={}, seed=None):
+ ... if context != 'This is a site':
+ ... print 'Expected string "This is a site" !'
+ >>> principalPlugin = GeneratePrincipals()
+
+For the sample manager the plugin must be registered as a utility.
+
+ >>> component.provideUtility(principalPlugin, ISampleDataPlugin,'lovely.principals')
+
+For our tests we provide another generator :
+
+ >>> class GenerateSite(object):
+ ... interface.implements(ISampleDataPlugin)
+ ... name = 'lovely.site'
+ ... dependencies = []
+ ... schema = None
+ ... def generate(self, context, param={}, seed=None):
+ ... return 'This is a site'
+ >>> sitePlugin = GenerateSite()
+ >>> component.provideUtility(sitePlugin, ISampleDataPlugin,'lovely.site')
+
+
+Generator Manager
+-----------------
+
+A generator manager groups a collection of generators and allows to define
+dependencies between generator.
+
+ >>> from lovely.sampledata import Manager
+ >>> manager = Manager('manager', '')
+
+Now we can add generators to the manager.
+In addition to the "hardwired" dependencies in each generator it is possible
+to add dependencies in the generator manager.
+
+ >>> manager.add('lovely.principals',
+ ... dependsOn=['lovely.site',],
+ ... contextFrom='lovely.site')
+
+A manager provides it's generators.
+
+ >>> manager.generators.keys()
+ ['lovely.principals']
+
+We can tell the manager to generate all samples.
+There is no need to add the sample generator 'lovely.site', it is added
+automatically because of the dependency of 'lovely.principals'.
+
+ >>> infos = manager.generate(context=None, param={}, seed='something')
+ >>> [info.name for info in infos]
+ ['lovely.site', 'lovely.principals']
+
+Cycles are detected.
+
+ >>> manager = Manager('manager', '')
+ >>> manager.add('lovely.principals',
+ ... dependsOn=['lovely.site',],
+ ... contextFrom='lovely.site')
+ >>> manager.add('lovely.site',
+ ... dependsOn=['lovely.principals',])
+
+ >>> infos = manager.generate(context=None, param={}, seed='something')
+ Traceback (most recent call last):
+ ...
+ CyclicDependencyError: cyclic dependency at 'lovely.site'
+
+A test for a complex dependency.
+
+ >>> class Generator(object):
+ ... interface.implements(ISampleDataPlugin)
+ ... name = 'generator'
+ ... dependencies = []
+ ... schema = None
+ ... def generate(self, context, param={}, seed=None):
+ ... return 'I am a generator'
+ >>> component.provideUtility(Generator(), ISampleDataPlugin,'g.1')
+ >>> component.provideUtility(Generator(), ISampleDataPlugin,'g.2')
+ >>> component.provideUtility(Generator(), ISampleDataPlugin,'g.3')
+ >>> manager = Manager('manager', '')
+ >>> manager.add('g.1')
+ >>> manager.add('g.2', contextFrom='g.1')
+ >>> manager.add('g.3', dependsOn=['g.2', 'g.1'], contextFrom='g.1')
+ >>> infos = manager.generate(context=None, param={}, seed=None)
+ >>> [info.name for info in infos]
+ ['g.1', 'g.2', 'g.3']
+
+
+BuiltIn sample generators
+-------------------------
+
+To support functional tests some basic sample generators are already
+implemented.
+
+Site
+~~~~
+
+Creates a simple folder and makes it a site.
+
+ >>> from lovely.sampledata.site import SampleSite
+ >>> component.provideUtility(SampleSite(),
+ ... ISampleDataPlugin,
+ ... 'lovely.sampledata.site')
+ >>> manager = Manager('manager', '')
+ >>> manager.add('lovely.sampledata.site')
+ >>> from zope.app.folder.folder import Folder
+ >>> baseContext = Folder()
+ >>> infos = manager.generate(context=baseContext, param={'sitename':'test'}, seed=None)
+ >>> [info.name for info in infos]
+ ['lovely.sampledata.site']
+ >>> 'test' in baseContext
+ True
+
+
+IntIds
+~~~~~~
+
+Creates an IntIds utility inside the site given as context.
+This generator depends on the creation of a site.
+
+ >>> from lovely.sampledata.site import SampleIntIds
+ >>> component.provideUtility(SampleIntIds(),
+ ... ISampleDataPlugin,
+ ... 'lovely.sampledata.intids')
+ >>> manager.add('lovely.sampledata.intids',
+ ... contextFrom='lovely.sampledata.site',
+ ... )
+ >>> baseContext = Folder()
+ >>> infos = manager.generate(context=baseContext, param={'sitename':'intids'}, seed=None)
+ >>> [info.name for info in infos]
+ ['lovely.sampledata.site', 'lovely.sampledata.intids']
+ >>> site = baseContext['intids']
+ >>> site.getSiteManager()['default']['intid']
+ <zope.app.intid.IntIds object at ...>
+
+
+How do I create a sample data plugin?
+-------------------------------------
+
+In order to create a sample data plugin, you only have to register a
+named utility that implements the interface
+`lovely.sampledata.interfaces.ISampleDataPlugin`.
+
+A plugin must provide :
+
+ - name
+ - dependencies on other plugins (list of names of ISampleDataPlugin utilities)
+ - schema for the parameters
+
+
+How to setup configuration for the generator manager
+----------------------------------------------------
+
+Configuration can be done using ZCML.
+
+ <configure xmlns="http://namespaces.zope.org/zope">
+
+ <configure
+ xmlns:zcml="http://namespaces.zope.org/zcml"
+ zcml:condition="have devmode">
+
+ <utility
+ factory=".SampleSite"
+ provides="lovely.sampledata.interfaces.ISampleDataPlugin"
+ name="lovely.site"
+ />
+
+ <utility
+ factory=".SamplePrincipals"
+ provides="lovely.sampledata.interfaces.ISampleDataPlugin"
+ name="lovely.principals"
+ />
+
+ <SampleManager
+ name="Site with principals"
+ >
+ <generator name="lovely.site" />
+ <generator
+ name="lovely.principal"
+ dependsOn="lovely.site"
+ contextFrom="lovely.site" />
+ </SampleManager>
+
+ </configure>
+
+ </configure>
+
+
+Data Generator
+==============
+
+This package implements the base functionality for data generators.
+A data generator is used to provide the raw data for a sample generator.
+Raw data can be read from text files in different ways.
+
+ >>> from lovely.sampledata.data import DataGenerator
+ >>> generator = DataGenerator(55)
+
+The generator can read data lines from files.
+
+ >>> generator.readLines('testlines.txt')
+ [u'Line 1', u'Another line']
+
+The generator can read data from CSV files.
+
+ >>> generator.readCSV('testlines.csv')
+ [['Line 1', 'Col 2'], ['Another line', 'Another Col']]
+
+The generator can read a list of files from a path :
+
+ >>> import os
+ >>> generator.files(os.path.dirname(__file__))
+ ['...README.txt', ...]
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/README.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/SETUP.cfg
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/SETUP.cfg 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/SETUP.cfg 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,3 @@
+<data-files zopeskel/etc/package-includes>
+ lovely.sampledata-*.zcml
+</data-files>
Added: z3c.sampledata/trunk/src/z3c/sampledata/__init__.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/__init__.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/__init__.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,22 @@
+###############################################################################
+#
+# Copyright 2006 by Zope Foundation and Contributors
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+###############################################################################
+"""
+$Id$
+"""
+import zope.i18nmessageid
+
+_ = zope.i18nmessageid.MessageFactory('sampledata')
+
+from manager import Manager
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/__init__.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/browser/README.txt
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/browser/README.txt 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/browser/README.txt 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,23 @@
+=============================
+Sample Data Generator Manager
+=============================
+
+The sample data generator manager allowes the user to configurate the
+parameters for the registered generators.
+
+ >>> from zope.testbrowser.testing import Browser
+ >>> browser = Browser()
+ >>> browser.addHeader('Authorization','Basic mgr:mgrpw')
+ >>> browser.handleErrors = False
+
+ >>> browser.open('http://localhost/@@managesamples.html')
+ >>> link = browser.getLink(text='samplemanager')
+ >>> link.click()
+ >>> browser.url
+ 'http://localhost/@@generatesample.html?manager=samplemanager'
+
+ >>> browser.getControl(name='generator.seed').value = 'lovely'
+ >>> browser.getControl(name='lovely.principals.minPrincipals').value = '2'
+ >>> browser.getControl(name='lovely.principals.maxPrincipals').value = '34'
+ >>> browser.getControl('Generate').click()
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/browser/README.txt
___________________________________________________________________
Name: svn:executable
+
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/browser/__init__.py
===================================================================
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/browser/__init__.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/browser/configure.zcml
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/browser/configure.zcml 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/browser/configure.zcml 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,22 @@
+<configure
+ xmlns:zope="http://namespaces.zope.org/zope"
+ xmlns="http://namespaces.zope.org/browser"
+ i18n_domain="lovely.sampledata">
+
+ <page
+ name="managesamples.html"
+ menu="zmi_views" title="Manage Samples"
+ for="zope.app.folder.interfaces.IFolder"
+ class=".views.Managers"
+ permission="zope.ManageSite"
+ />
+
+ <page
+ name="generatesample.html"
+ for="zope.app.folder.interfaces.IFolder"
+ class=".views.Generate"
+ permission="zope.ManageSite"
+ />
+
+</configure>
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/browser/configure.zcml
___________________________________________________________________
Name: svn:executable
+
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/browser/generate.pt
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/browser/generate.pt 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/browser/generate.pt 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,39 @@
+<div metal:use-macro="view/base_template/macros/main" >
+ <div metal:fill-slot="extra_info">
+ <input type="hidden" name="manager" value="" tal:attributes="value view/manager" />
+ </div>
+ <div metal:fill-slot="main_form">
+ <tal:block condition="not: view/workDone">
+ <p i18n:translate="" class="error">Warning: proceeding will populate
+ the database with randomly generated data. Do not do this if you have
+ any valuable data in your instance.</p>
+
+ <p i18n:translate="">Setting a random seed allows you to reproduce
+ the same set of sample data by generating it again with the same value
+ of the random seed.</p>
+ <hr/>
+ <tal:block tal:repeat="subform view/subforms">
+ <p tal:content="subform/prefix"></p>
+ <div tal:replace="structure subform" /><hr/>
+ </tal:block>
+ </tal:block>
+
+ <tal:block condition="view/workDone">
+
+ <p>Sample data generated. Below is the list of plugins in the order they where
+ executed along with the CPU time it took to run them</p>
+
+ <table class="listing">
+ <tr>
+ <th>Plugin Name</th>
+ <th>CPU time used (seconds)</th>
+ </tr>
+ <tr tal:repeat="plugin view/workedOn">
+ <td tal:content="plugin/name"></td>
+ <td tal:content="plugin/time"></td>
+ </tr>
+ </table>
+ </tal:block>
+ </div>
+</div>
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/browser/generate.pt
___________________________________________________________________
Name: svn:executable
+
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/browser/managers.pt
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/browser/managers.pt 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/browser/managers.pt 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,24 @@
+<html metal:use-macro="view/@@standard_macros/page"
+ i18n_domain="lovely.sampledata">
+<head>
+ <title metal:fill-slot="title" i18n:translate="">
+ Media Portal Sample Data Generation
+ </title>
+</head>
+<body>
+
+<h1 metal:fill-slot="content-header"
+ i18n:translate="">Media Portal Sample Data Generation</h1>
+
+<metal:block metal:fill-slot="body">
+
+ <p>Select the sample manager</p>
+
+ <p tal:repeat="manager view/managers">
+ - <a href="" tal:attributes="href string:@@generatesample.html?manager=${manager}"
+ tal:content="manager" />
+ </p>
+
+</metal:block>
+</body>
+</html>
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/browser/managers.pt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/browser/views.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/browser/views.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/browser/views.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,107 @@
+from zope import component
+from zope import interface
+from zope import schema
+
+from zope import formlib
+from zope.formlib import form
+from zope.app.pagetemplate import ViewPageTemplateFile
+
+from lovely.sampledata.interfaces import ISampleDataPlugin, ISampleManager
+
+from lovely.sampledata import _
+
+
+class Managers(object):
+
+ template = ViewPageTemplateFile("managers.pt")
+
+ def managers(self):
+ m = [name for name, util in component.getUtilitiesFor(ISampleManager)]
+ return m
+
+ def __call__(self):
+ self.update()
+ return self.template()
+
+ def update(self):
+ if 'manager' in self.request:
+ managerName = self.request['manager']
+ self.request.response.redirect(
+ zapi.absoluteURL(self.context, self.request)+
+ '/@@generatesample.html?manager="%s"'%(managerName))
+
+
+class IGenerateSchema(interface.Interface):
+ """Schema for the minimal generator parameters"""
+
+ seed = schema.TextLine(
+ title = _(u'Seed'),
+ description = _(u'A seed for the random generator'),
+ default = u'lovely',
+ required=False,
+ )
+
+
+class Generate(form.EditForm):
+ """Edit all generator parameters for a given manager"""
+
+ base_template = form.EditForm.template
+ template = ViewPageTemplateFile('generate.pt')
+
+ workDone = False
+
+ def setUpWidgets(self, ignore_request=False):
+ managerName = self.request['manager']
+ manager = component.getUtility(ISampleManager, name=managerName)
+ plugins = manager.orderedPlugins()
+ self.form_fields = form.Fields()
+ self.subforms = []
+ subform = Generator(context=self.context,
+ request=self.request,
+ schema=IGenerateSchema,
+ prefix='generator')
+ subform.form_fields = form.Fields(IGenerateSchema)
+ self.subforms.append(subform)
+ for plugin in plugins:
+ if plugin.generator.schema is None:
+ continue
+ subform = Generator(context=self.context,
+ request=self.request,
+ plugin=plugin.generator,
+ prefix=plugin.name)
+ subform.form_fields = form.Fields(plugin.generator.schema)
+ self.subforms.append(subform)
+ super(Generate, self).setUpWidgets(ignore_request=ignore_request)
+
+ @form.action(_("Generate"))
+ def handle_generate_action(self, action, data):
+ managerName = self.request['manager']
+ manager = component.getUtility(ISampleManager, name=managerName)
+ for subform in self.subforms:
+ subform.update()
+ errors = form.getWidgetsData(subform.widgets, subform.prefix, data)
+ seed = None
+ if 'seed' in data:
+ seed = data['seed']
+ self.workedOn = manager.generate(self.context, data, seed)
+ self.workDone = True
+ self.actions = []
+
+ def manager(self):
+ return self.request['manager']
+
+
+class Generator(form.AddForm):
+ """An editor for a single generator"""
+ interface.implements(formlib.interfaces.ISubPageForm)
+
+ template = formlib.namedtemplate.NamedTemplate('default')
+
+ actions = []
+
+ def __init__(self, context, request, plugin=None, schema=None, prefix=''):
+ self.plugin = plugin
+ self.schema = schema
+ self.prefix = prefix
+ super(Generator, self).__init__(context, request)
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/browser/views.py
___________________________________________________________________
Name: svn:executable
+
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/configure.zcml
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/configure.zcml 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/configure.zcml 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,18 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+
+ <utility
+ name="lovely.site"
+ factory="lovely.sampledata.site.SampleSite"
+ provides="lovely.sampledata.interfaces.ISampleDataPlugin"
+ />
+
+ <utility
+ name="lovely.principals"
+ factory="lovely.sampledata.principal.SamplePrincipals"
+ provides="lovely.sampledata.interfaces.ISampleDataPlugin"
+ />
+
+ <include package=".browser" />
+
+</configure>
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/configure.zcml
___________________________________________________________________
Name: svn:executable
+
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/data.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/data.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/data.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,69 @@
+##############################################################################
+#
+# Copyright (c) 2006 Lovely Systems and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Sample data for the media site
+
+
+$Id: $
+"""
+__docformat__ = "reStructuredText"
+
+import os
+import random
+import csv
+
+
+class DataGenerator(object):
+ """Base functionality for data generators."""
+
+ path = os.path.dirname(__file__)
+
+ def __init__(self, seed, path=None):
+ self.random = random.Random()
+ self.random.seed(seed)
+ if path is not None:
+ self.path = path
+
+ def readLines(self, filename):
+ """Read in lines from file. Filename is relative to the module.
+
+ Returned lines are stripped.
+ """
+ fullpath = os.path.join(self.path, filename)
+ lines = file(fullpath).readlines()
+ return [unicode(line.strip()) for line in lines]
+
+ def readCSV(self, filename, delimiter=','):
+ """Read in lines from file. Filename is relative to the module.
+
+ Returned lines are stripped.
+ """
+ fullpath = os.path.join(self.path, filename)
+ f = file(fullpath)
+ reader = csv.reader(f, delimiter=delimiter)
+ return list(reader)
+
+ def files(self, path):
+ """Get a list of files from a path.
+
+ Subdirectories are ignored.
+ """
+ files = []
+ append = files.append
+ if os.path.exists(path):
+ for f in os.listdir(path):
+ fp = os.path.join(path, f)
+ if os.path.isfile(fp):
+ append(fp)
+ return files
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/data.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/ftesting.zcml
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/ftesting.zcml 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/ftesting.zcml 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,69 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+ xmlns:browser="http://namespaces.zope.org/browser"
+ i18n_domain="zope">
+
+ <include package="zope.app" />
+
+ <include package="zope.viewlet" file="meta.zcml"/>
+ <include package="zope.app.securitypolicy" file="meta.zcml" />
+ <include package="lovely.sampledata" file="meta.zcml" />
+
+ <include package="zope.app.server" />
+ <include package="zope.app.authentication" />
+ <securityPolicy
+ component="zope.app.securitypolicy.zopepolicy.ZopeSecurityPolicy" />
+ <include package="zope.app.securitypolicy" />
+ <include package="zope.app.session" />
+
+ <include package="zope.contentprovider"/>
+ <include package="zope.viewlet"/>
+ <include package="zope.formlib"/>
+ <include package="lovely.sampledata"/>
+
+ <role id="zope.Anonymous" title="Everybody"
+ description="All users have this role implicitly" />
+ <role id="zope.Manager" title="Site Manager" />
+ <grantAll role="zope.Manager" />
+ <principal
+ id="zope.manager"
+ title="Administrator"
+ login="mgr"
+ password="mgrpw" />
+ <grant
+ role="zope.Manager"
+ principal="zope.manager"
+ />
+
+ <unauthenticatedPrincipal
+ id="zope.anybody"
+ title="Unauthenticated User" />
+
+ <unauthenticatedGroup
+ id="zope.Anybody"
+ title="Unauthenticated Users"
+ />
+
+ <authenticatedGroup
+ id="zope.Authenticated"
+ title="Authenticated Users"
+ />
+
+ <everybodyGroup
+ id="zope.Everybody"
+ title="All Users"
+ />
+
+ <grant permission="zope.View"
+ role="zope.Anonymous" />
+ <grant permission="zope.app.dublincore.view"
+ role="zope.Anonymous" />
+
+ <SampleManager
+ name="samplemanager"
+ >
+ <generator
+ name="lovely.principals"
+ />
+ </SampleManager>
+
+</configure>
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/ftesting.zcml
___________________________________________________________________
Name: svn:executable
+
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/ftests.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/ftests.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/ftests.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,53 @@
+import unittest
+
+import zope.event
+from zope.lifecycleevent import ObjectCreatedEvent
+
+from zope.app.testing import functional
+from zope.app.authentication.authentication import PluggableAuthentication
+from zope.app.authentication.principalfolder import PrincipalFolder
+from zope.app.authentication.interfaces import IAuthenticatorPlugin
+from zope.app.security.interfaces import IAuthentication
+
+
+functional.defineLayer('TestLayer', 'ftesting.zcml')
+
+
+def setUp(test):
+ site = functional.getRootFolder()
+ sm = site.getSiteManager()
+ default = sm['default']
+ if not 'pau' in default:
+ pau = PluggableAuthentication()
+ zope.event.notify(ObjectCreatedEvent(pau))
+ default['pau'] = pau
+ sm.registerUtility(pau, IAuthentication)
+ else:
+ pau=default['pau']
+ # we can do this always
+ pau.credentialsPlugins = ('Session Credentials', )
+ pfNames = (u'members',)
+ pau.authenticatorPlugins = pfNames
+ for name in pfNames:
+ if name in pau:
+ continue
+ pf = PrincipalFolder(name +'.')
+ zope.event.notify(ObjectCreatedEvent(pf))
+ default['pau'][name] = pf
+ sm.registerUtility(pf, IAuthenticatorPlugin, name)
+
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suites = (
+ functional.FunctionalDocFileSuite('browser/README.txt',
+ setUp = setUp,
+ ),
+ )
+ for s in suites:
+ s.layer=TestLayer
+ suite.addTest(s)
+ return suite
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/ftests.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/interfaces.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/interfaces.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/interfaces.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,98 @@
+###############################################################################
+#
+# Copyright 2006 by Zope Foundation and Contributors
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+###############################################################################
+"""
+$Id$
+"""
+
+import zope.interface
+import zope.schema
+
+from lovely.sampledata import _
+
+
+class ISampleDataPlugin(zope.interface.Interface):
+ """A plugin that generates some sample data.
+
+ These plugins may depend on other sample data plugins. Say,
+ calendar event generators depend on person generators. All
+ plugins have unique names and other plugins reference their
+ dependencies by these names.
+ """
+
+ name = zope.schema.Id(title=u"Sample data generator name")
+
+ dependencies = zope.schema.List(
+ title=u"A list of dependenies",
+ value_type=zope.schema.Id(title=u"Sample data generator name"),
+ description=u"""
+ A list of names of sample data generators this one depends on.
+ """)
+
+ schema = zope.schema.InterfaceField(
+ title=u"Parameters",
+ description=u"The schema which provides the parameters.",
+ )
+
+ def generate(app, param={}, seed=None):
+ """Generate sample data of this plugin.
+
+ This method assumes the sample data this plugin depends on has
+ been created.
+ """
+
+
+class ISampleManager(zope.interface.Interface):
+ """A sample manager manages the generation of sample data.
+
+ The manager uses a list of sample generators.
+ """
+
+ name = zope.schema.TextLine(
+ title = _(u'Name'),
+ description = _(u'The unique name of the sample manager'),
+ )
+
+ def add(generator, dependsOn=[], contextFrom=None):
+ """Add a generator to the manager.
+
+ generator:
+ The name of a utility providing 'ISampleDataPlugin'.
+ dependsOn:
+ The names of generators this generator depends on.
+ This is used in addition to the dependencies defined in the
+ generator.
+ contextFrom:
+ The context for the plugin is the output of this plugin.
+ This plugin is automatically used as a dependent plugin.
+ """
+
+ def generate(context=None, param={}, seed=None):
+ """Generate the sample data.
+
+ Runs the generate functions of all plugins in an order such that
+ the dependencies are all generated before the dependents.
+
+ In essence, this function performs a topological sort in a
+ directed acyclic graph.
+
+ Raises a CyclicDependencyError if the dependency graph has a cycle.
+
+ Returns a dict with names of plugins run as keys and CPU times as
+ values.
+ """
+
+
+class CyclicDependencyError(ValueError):
+ """Cyclic dependency of sample data plugins"""
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/interfaces.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-configure.zcml
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-configure.zcml 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-configure.zcml 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,7 @@
+<configure
+ xmlns:zcml="http://namespaces.zope.org/zcml"
+ zcml:condition="have devmode">
+
+ <include package="lovely.sampledata" />
+
+</configure>
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-configure.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-meta.zcml
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-meta.zcml 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-meta.zcml 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,7 @@
+<configure
+ xmlns:zcml="http://namespaces.zope.org/zcml"
+ zcml:condition="have devmode">
+
+ <include package="lovely.sampledata" file="meta.zcml" />
+
+</configure>
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/lovely.sampledata-meta.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/manager.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/manager.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/manager.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,123 @@
+##############################################################################
+#
+# Copyright (c) 2006 Lovely Systems and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""A manager for sample generation.
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import time
+
+from zope import interface
+from zope import component
+
+from interfaces import ISampleDataPlugin, ISampleManager
+from interfaces import CyclicDependencyError
+
+
+class Manager(object):
+ interface.implements(ISampleManager)
+
+ def __init__(self, name, seed):
+ self.name = name
+ self.seed = seed
+ self.generators = {}
+
+ def add(self, generator, dependsOn=[], contextFrom=None):
+ info = self.generators.setdefault(generator, {})
+ info['dependsOn'] = dependsOn
+ info['contextFrom'] = contextFrom
+
+ def orderedPlugins(self):
+
+ # status is a dict plugin names as keys and statuses as values.
+ # Statuses can be as follows:
+ #
+ # new -- not yet visited
+ # closed -- already processed
+ # open -- being processed
+ #
+ # Stumbling over an 'open' node means there is a cyclic dependency
+ new = 'new'
+ open = 'open'
+ closed = 'closed'
+ status = {}
+
+ # plugins contains all plugins to be used including dependent plugins.
+ plugins = []
+ callstack = []
+
+ def insertPlugin(name):
+ """Insert the plugin into plugins including all dependent plugins.
+
+ Builds a calling list of all plugins in the correct order.
+ """
+ if name in callstack:
+ raise CyclicDependencyError("cyclic dependency at '%s'" % name)
+ callstack.append(name)
+ if name not in status:
+ status[name] = new
+ if status[name] == new:
+ status[name] = open
+ info = PluginInfo(name)
+ if name in self.generators:
+ pluginInfo = self.generators[name]
+ info.addDependents(pluginInfo['dependsOn'])
+ info.contextFrom = pluginInfo['contextFrom']
+ if info.contextFrom is not None:
+ info.addDependents([info.contextFrom])
+ generator = component.getUtility(ISampleDataPlugin, name)
+ info.generator = generator
+ info.addDependents(generator.dependencies)
+ for depName in info.dependencies:
+ if depName not in status or status[depName] is not closed:
+ insertPlugin(depName)
+ plugins.append(info)
+ status[name] = closed
+ callstack.pop(-1)
+
+ for name in self.generators.keys():
+ insertPlugin(name)
+ return plugins
+
+ def generate(self, context=None, param={}, seed=None):
+ plugins = self.orderedPlugins()
+
+ # contextFrom contains the return values of the plugins
+ contextFrom = {}
+
+ for info in plugins:
+ genContext = context
+ if info.contextFrom is not None:
+ genContext = contextFrom[info.contextFrom]
+ start = time.clock()
+ contextFrom[info.name] = info.generator.generate(genContext, param, seed)
+ info.time = time.clock() - start
+
+ return plugins
+
+
+class PluginInfo(object):
+ def __init__(self, name):
+ self.name = name
+ self.dependencies = []
+ self.contextFrom = None
+ self.generator = None
+ self.time = 0.0
+
+ def addDependents(self, dependsOn):
+ for dependent in dependsOn:
+ if dependent not in self.dependencies:
+ self.dependencies.append(dependent)
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/manager.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/meta.zcml
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/meta.zcml 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/meta.zcml 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,23 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ xmlns:meta="http://namespaces.zope.org/meta">
+
+ <meta:directives namespace="http://namespaces.zope.org/zope">
+
+ <meta:complexDirective
+ name="SampleManager"
+ schema=".metadirectives.ISampleManagerDirective"
+ handler=".metaconfigure.sampleManager"
+ >
+
+ <meta:subdirective
+ name="generator"
+ schema=".metadirectives.IGeneratorSubDirective"
+ />
+
+ </meta:complexDirective>
+
+ </meta:directives>
+
+</configure>
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/meta.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/metaconfigure.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/metaconfigure.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/metaconfigure.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,22 @@
+from zope import component
+from zope.component.zcml import utility
+
+from manager import Manager
+from interfaces import ISampleManager
+
+
+class sampleManager(object):
+
+ def __init__(self,_context, name, seed=''):
+ self.manager = Manager(name, seed)
+ utility(_context, ISampleManager, component=self.manager, name=name)
+
+ def generator(self, _context, name, dependsOn=None, contextFrom=None):
+ dependencies = []
+ if dependsOn is not None:
+ dependencies = dependsOn.split(' ')
+ self.manager.add(name, dependencies, contextFrom)
+
+ def __call__(self):
+ return
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/metaconfigure.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/metadirectives.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/metadirectives.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/metadirectives.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,46 @@
+from zope import interface
+from zope import schema
+
+from lovely.sampledata import _
+
+
+class ISampleManagerDirective(interface.Interface):
+ """Parameters for the sample manager."""
+
+ name = schema.TextLine(
+ title = _(u"Name"),
+ description = _(u'The unique name of the sample manager'),
+ )
+
+ seed = schema.TextLine(
+ title = _(u'Seed'),
+ description = _(u'The seed for the random generator'),
+ required = False,
+ default = u''
+ )
+
+
+class IGeneratorSubDirective(interface.Interface):
+ """Parameters for the 'generator' subdirective."""
+
+ name = schema.TextLine(
+ title = _(u"Name"),
+ description = _(u'The unique name of the sample manager'),
+ )
+
+ dependsOn = schema.TextLine(
+ title = _(u"Dependencies"),
+ description = _(u'The generators this generator depends on.\n'
+ u'As space separated list.'),
+ default = u'',
+ required = False,
+ )
+
+ contextFrom = schema.TextLine(
+ title = _(u"Context from"),
+ description = _(u'Context for the generator is taken from this'
+ u' generator.'),
+ default = u'',
+ required = False,
+ )
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/metadirectives.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/principal.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/principal.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/principal.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,129 @@
+##############################################################################
+#
+# Copyright (c) 2006 Lovely Systems and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Sample generator for principals
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import zope.interface
+import zope.component
+import zope.event
+import zope.schema
+import zope.lifecycleevent
+from zope.interface import implements
+from zope.security.proxy import removeSecurityProxy
+from zope.app.security.interfaces import IAuthentication
+from zope.app.authentication import principalfolder
+from zope.app.component import hooks
+
+from lovely.sampledata.interfaces import ISampleDataPlugin
+
+from lovely.sampledata.data import DataGenerator
+
+from lovely.sampledata import _
+
+
+class ISamplePrincipalParameters(zope.interface.Interface):
+ """The parameters for the sample principal generator."""
+
+ minPrincipals = zope.schema.Int(
+ title = _(u'Min principals'),
+ description = _(u'Create at least this number of pricipals from'
+ u' the principals file.\n'
+ u'This has higher priority than maxPricipals.\n'
+ u'-1 : create all principals from the file.'
+ ),
+ required = False,
+ default = -1,
+ )
+
+ maxPrincipals = zope.schema.Int(
+ title = _(u'Max principals'),
+ description = _(u'The maximum number of principals to create.'),
+ required = False,
+ default = -1,
+ )
+
+ pauLocation = zope.schema.TextLine(
+ title = _(u'PAU location'),
+ description = _(u'Path to the PAU inside the site manager.'),
+ required = False,
+ default = u'default/pau',
+ )
+
+
+class SamplePrincipals(object):
+
+ implements(ISampleDataPlugin)
+
+ name = 'lovely.principals'
+ dependencies = []
+ schema = ISamplePrincipalParameters
+
+ maxPrincipals = None
+ minPrincipals = None
+
+ def generate(self, context, param={}, seed=None):
+ """Generate sample pricipals"""
+ self.minPrincipals = int(param['minPrincipals'])
+ if self.minPrincipals<0:
+ self.minPrincipals = None
+ self.maxPrincipals = int(param['maxPrincipals'])
+ if self.maxPrincipals<0:
+ self.maxPrincipals = None
+ originalSite = hooks.getSite()
+ hooks.setSite(context)
+ sm = zope.component.getSiteManager(context)
+ self.pau = sm
+ for loc in param['pauLocation'].split('/'):
+ self.pau = self.pau[loc]
+
+ numCreated = 0
+ self.logins = []
+ generator = DataGenerator(str(seed) + self.name)
+ principals = generator.readCSV('testprincipals.txt')
+ for info in principals:
+ if ( self.maxPrincipals is not None
+ and numCreated>=self.maxPrincipals):
+ break
+ login = unicode(info[1])
+ if login in self.logins:
+ # ignore duplicate principals
+ continue
+ self._createPrincipal(info)
+ numCreated+=1
+
+ if ( self.minPrincipals is not None
+ and numCreated<self.minPrincipals):
+ for i in range(self.minPrincipals-numCreated):
+ info = ['group','login%i'%i,'name%i'%i,'%i'%i]
+ self._createPrincipal(info)
+
+ hooks.setSite(originalSite)
+
+ def _createPrincipal(self, info):
+ login = unicode(info[1])
+ self.logins.append(login)
+ if login in self.pau['members']:
+ return
+ groupId = unicode(info[0])
+ name = unicode(info[2])
+ password = unicode(info[3])
+ principal = principalfolder.InternalPrincipal(login,
+ password, name, passwordManagerName="SHA1")
+ zope.event.notify(
+ zope.lifecycleevent.ObjectCreatedEvent(principal))
+ self.pau['members'][login] = principal
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/principal.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/principal.txt
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/principal.txt 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/principal.txt 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,64 @@
+================================
+Principal Sample Data Generation
+================================
+
+This gerator creates principals from a CSV file.
+
+ >>> from lovely.sampledata.principal import SamplePrincipals
+ >>> generator = SamplePrincipals()
+
+Before we can use the generator we need to setup principal authentication.
+
+ >>> import zope.event
+ >>> from zope.lifecycleevent import ObjectCreatedEvent
+ >>> from zope.app.authentication.interfaces import IAuthenticatorPlugin
+ >>> from zope.app.security.interfaces import IAuthentication
+ >>> from zope.app.authentication.authentication import PluggableAuthentication
+ >>> from zope.app.authentication.principalfolder import PrincipalFolder
+
+ >>> sm = root.getSiteManager()
+ >>> default = sm['default']
+ >>> pau = PluggableAuthentication()
+ >>> zope.event.notify(ObjectCreatedEvent(pau))
+ >>> default['pau'] = pau
+ >>> sm.registerUtility(pau, IAuthentication)
+ >>> # we can do this always
+ >>> pau.credentialsPlugins = ('Session Credentials', )
+ >>> pfNames = (u'members',)
+ >>> pau.authenticatorPlugins = pfNames
+ >>> for name in pfNames:
+ ... if name in pau:
+ ... continue
+ ... pf = PrincipalFolder(name +'.')
+ ... default['pau'][name] = pf
+ ... sm.registerUtility(pf, IAuthenticatorPlugin, name)
+
+Now that everything is set up we can generate the principals for the root
+object.
+
+We can define a maximum number of users.
+
+ >>> generator.generate(root, param={'minPrincipals':-1,
+ ... 'maxPrincipals': 2,
+ ... 'pauLocation':'default/pau'})
+ >>> len(pau['members'])
+ 2
+
+With both parameter set to -1 all members from "testprincipals.txt" are generated.
+
+ >>> generator.generate(root, param={'minPrincipals':-1,
+ ... 'maxPrincipals':-1,
+ ... 'pauLocation':'default/pau'})
+ >>> principal = pau['members']['batlogg']
+ >>> principal.title
+ u'Jodok Batlogg'
+
+We can define a minimum number of users.
+
+ >>> numMembers = len(pau['members'])
+ >>> generator.generate(root, param={'minPrincipals': numMembers+10,
+ ... 'maxPrincipals':-1,
+ ... 'pauLocation':'default/pau'})
+ >>> len(pau['members']) == numMembers+10
+ True
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/principal.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/site.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/site.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/site.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,94 @@
+##############################################################################
+#
+# Copyright (c) 2006 Lovely Systems and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Sample generator for site generation.
+
+Contains some general purpose generators related to site specific data.
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import zope.interface
+import zope.component
+import zope.event
+import zope.schema
+import zope.lifecycleevent
+from zope.interface import implements
+from zope.app.component import hooks
+
+from zope.app.component.site import LocalSiteManager
+from zope.app.folder.folder import Folder
+from zope.app.intid import IntIds
+from zope.app.intid.interfaces import IIntIds
+
+from lovely.sampledata.interfaces import ISampleDataPlugin
+
+from lovely.sampledata import _
+
+
+class ISampleSiteParameters(zope.interface.Interface):
+ """The parameters for the site creation."""
+
+ sitename = zope.schema.TextLine(
+ title = _(u'Sitename'),
+ required = True,
+ default = u'',
+ )
+
+
+class SampleSite(object):
+
+ implements(ISampleDataPlugin)
+
+ name = 'lovely.sampledata.site'
+ dependencies = []
+ schema = ISampleSiteParameters
+
+ def generate(self, context, param={}, seed=None):
+ """Generate the site inside context"""
+ name = param['sitename']
+ folder = Folder()
+ zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(folder))
+ context[name] = folder
+ sm = LocalSiteManager(folder)
+ folder.setSiteManager(sm)
+ hooks.setSite(folder)
+ return folder
+
+
+class SampleIntIds(object):
+
+ implements(ISampleDataPlugin)
+
+ name = 'lovely.sampledata.intids'
+ dependencies = []
+ schema = None
+
+ def generate(self, context, param={}, seed=None):
+ """Generate an IntId utility inside context.
+
+ 'context' must be a site manager.
+ """
+ sm = context.getSiteManager()
+ default = sm['default']
+
+ if 'intid' not in default:
+ intid = IntIds()
+ zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(intid))
+ default['intid'] = intid
+ sm.registerUtility(intid, IIntIds)
+ return intid
+ else:
+ return default['intid']
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/site.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/site.zcml
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/site.zcml 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/site.zcml 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,40 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+
+ <configure
+ xmlns:zcml="http://namespaces.zope.org/zcml"
+ zcml:condition="have devmode">
+
+ <!-- This is a configuration for the testing environment -->
+
+ <utility
+ factory=".site.SampleSite"
+ provides="lovely.sampledata.interfaces.ISampleDataPlugin"
+ name="lovely.sampledata.site"
+ />
+
+ <utility
+ factory=".site.SampleIntIds"
+ provides="lovely.sampledata.interfaces.ISampleDataPlugin"
+ name="lovely.sampledata.intids"
+ />
+
+ <utility
+ factory=".principal.SamplePrincipals"
+ provides="lovely.sampledata.interfaces.ISampleDataPlugin"
+ name="lovely.sampledata.principals"
+ />
+
+ <SampleManager
+ name="Site with intid utility"
+ >
+ <generator name="lovely.sampledata.site" />
+ <generator
+ name="lovely.sampledata.intids"
+ dependsOn="lovely.sampledata.site"
+ contextFrom="lovely.sampledata.site" />
+ </SampleManager>
+
+ </configure>
+
+</configure>
+
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/site.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/testlines.csv
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/testlines.csv 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/testlines.csv 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,2 @@
+Line 1,Col 2
+Another line,Another Col
Added: z3c.sampledata/trunk/src/z3c/sampledata/testlines.txt
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/testlines.txt 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/testlines.txt 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,2 @@
+Line 1
+Another line
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/testlines.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/testprincipals.txt
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/testprincipals.txt 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/testprincipals.txt 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,6 @@
+group.Administrator,batlogg,Jodok Batlogg,bJB
+group.Administrator,jukart,Juergen Kartnaller,jJK
+group.Administrator,dobee,Bernd Dorn,dBD
+group.Administrator,srichter,Stephan Richter,sSR
+group.Administrator,byzo,Michael Breidenbruecker,bMB
+group.Administrator,oli,Oliver Ruhm,oOR
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/testprincipals.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.sampledata/trunk/src/z3c/sampledata/tests.py
===================================================================
--- z3c.sampledata/trunk/src/z3c/sampledata/tests.py 2006-08-16 11:55:18 UTC (rev 69558)
+++ z3c.sampledata/trunk/src/z3c/sampledata/tests.py 2006-08-16 12:14:17 UTC (rev 69559)
@@ -0,0 +1,49 @@
+##############################################################################
+#
+# Copyright 2006 by Zope Foundation and Contributors
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Tests for the package
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import doctest
+import unittest
+from zope.testing.doctestunit import DocFileSuite
+from zope.app.testing.setup import placefulSetUp, placefulTearDown
+
+
+def setUp(test):
+ root = placefulSetUp(site=True)
+ test.globs['root'] = root
+
+
+def tearDown(test):
+ placefulTearDown()
+
+
+def test_suite():
+
+ return unittest.TestSuite(
+ (
+ DocFileSuite('README.txt',
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+ ),
+ DocFileSuite('principal.txt',
+ setUp=setUp, tearDown=tearDown,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+ ),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Property changes on: z3c.sampledata/trunk/src/z3c/sampledata/tests.py
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the Checkins
mailing list