[Zope-Checkins] SVN: Products.Five/trunk/ Enabled ability to
declare a regular python package as an official Zope2 product
via zcml five:registerPackage.
Rocky Burt
rocky at serverzen.com
Tue May 2 19:22:03 EDT 2006
Log message for revision 67874:
Enabled ability to declare a regular python package as an official Zope2 product via zcml five:registerPackage.
Changed:
U Products.Five/trunk/fiveconfigure.py
U Products.Five/trunk/fivedirectives.py
U Products.Five/trunk/meta.zcml
A Products.Five/trunk/tests/test_registerpackage.py
A Products.Five/trunk/tests/testing/pythonproduct1.py
A Products.Five/trunk/tests/testing/pythonproduct2/
A Products.Five/trunk/tests/testing/pythonproduct2/Extensions/
A Products.Five/trunk/tests/testing/pythonproduct2/Extensions/__init__.py
A Products.Five/trunk/tests/testing/pythonproduct2/Extensions/somemodule.py
A Products.Five/trunk/tests/testing/pythonproduct2/__init__.py
-=-
Modified: Products.Five/trunk/fiveconfigure.py
===================================================================
--- Products.Five/trunk/fiveconfigure.py 2006-05-02 22:48:25 UTC (rev 67873)
+++ Products.Five/trunk/fiveconfigure.py 2006-05-02 23:22:01 UTC (rev 67874)
@@ -24,7 +24,10 @@
import logging
import App.config
+from App.Product import initializeProduct
+from App.ProductContext import ProductContext
import Products
+import Zope2
from zope.interface import classImplements, classImplementsOnly, implementedBy
from zope.interface.interface import InterfaceClass
@@ -199,6 +202,37 @@
args = (class_, meta_type, permission, addview, icon, global_)
)
+def _registerPackage(module_, init_func=None):
+ """Registers the given python package as a Zope 2 style product
+ """
+
+ if not hasattr(module_, '__path__'):
+ raise ValueError("Must be a package and the " \
+ "package must be filesystem based")
+
+ app = Zope2.app()
+ product = initializeProduct(module_,
+ module_.__name__,
+ module_.__path__[0],
+ app)
+
+ product.package_name = module_.__name__
+
+ if init_func is not None:
+ newContext = ProductContext(product, app, module_)
+ init_func(newContext)
+
+
+def registerPackage(_context, package, initialize=None):
+ """ZCML directive function for registering a python package product
+ """
+
+ _context.action(
+ discriminator = ('registerPackage', package),
+ callable = _registerPackage,
+ args = (package,initialize)
+ )
+
# clean up code
def killMonkey(class_, name, fallback, attr=None):
Modified: Products.Five/trunk/fivedirectives.py
===================================================================
--- Products.Five/trunk/fivedirectives.py 2006-05-02 22:48:25 UTC (rev 67873)
+++ Products.Five/trunk/fivedirectives.py 2006-05-02 23:22:01 UTC (rev 67874)
@@ -170,6 +170,8 @@
required=False
)
+
+
class IInclude(Interface):
file = BytesLine(
@@ -178,3 +180,20 @@
u'installed Product. If the file does not exist, for a '
u'particular product, no error is raised.',
required=False)
+
+class IRegisterPackageDirective(Interface):
+ """Registers the given python package which at a minimum fools zope2 into
+ thinking of it as a zope2 product.
+ """
+
+ package = GlobalObject(
+ title=u'Target package',
+ required=True
+ )
+
+ initialize = GlobalObject(
+ title=u'Initialization function to invoke',
+ description=u'The dotted name of a function that will get invoked '
+ u'with a ProductContext instance',
+ required=False
+ )
Modified: Products.Five/trunk/meta.zcml
===================================================================
--- Products.Five/trunk/meta.zcml 2006-05-02 22:48:25 UTC (rev 67873)
+++ Products.Five/trunk/meta.zcml 2006-05-02 23:22:01 UTC (rev 67874)
@@ -160,6 +160,12 @@
handler=".fiveconfigure.registerClass"
/>
+ <meta:directive
+ name="registerPackage"
+ schema=".fivedirectives.IRegisterPackageDirective"
+ handler=".fiveconfigure.registerPackage"
+ />
+
</meta:directives>
</configure>
Added: Products.Five/trunk/tests/test_registerpackage.py
===================================================================
--- Products.Five/trunk/tests/test_registerpackage.py 2006-05-02 22:48:25 UTC (rev 67873)
+++ Products.Five/trunk/tests/test_registerpackage.py 2006-05-02 23:22:01 UTC (rev 67874)
@@ -0,0 +1,91 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation 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.
+#
+##############################################################################
+"""Unit tests for the registerPackage directive.
+
+$Id$
+"""
+import os, sys
+if __name__ == '__main__':
+ execfile(os.path.join(sys.path[0], 'framework.py'))
+
+# need to add the testing package to the pythonpath in order to
+# test python-packages-as-products
+from Products.Five.tests import testing
+sys.path.append(testing.__path__[0])
+
+def test_registerPackage():
+ """
+ Testing registerPackage
+
+ >>> from zope.app.testing.placelesssetup import setUp, tearDown
+ >>> setUp()
+ >>> import Products
+ >>> import Products.Five
+ >>> from Products.Five import zcml
+ >>> zcml.load_config('meta.zcml', Products.Five)
+
+ Make sure a python package with no initialize (even though one
+ is specified) will fail::
+
+ >>> configure_zcml = '''
+ ... <configure
+ ... xmlns="http://namespaces.zope.org/zope"
+ ... xmlns:five="http://namespaces.zope.org/five"
+ ... i18n_domain="foo">
+ ... <five:registerPackage
+ ... package="pythonproduct1"
+ ... initialize="pythonproduct1.initialize"
+ ... />
+ ... </configure>'''
+ >>> zcml.load_string(configure_zcml)
+ Traceback (most recent call last):
+ ...
+ ZopeXMLConfigurationError: ...
+ ConfigurationError: ('...pythonproduct1 has no global initialize')
+
+ Make sure a python package with a valid initialize gets its
+ initialize function called::
+
+ >>> configure_zcml = '''
+ ... <configure
+ ... xmlns="http://namespaces.zope.org/zope"
+ ... xmlns:five="http://namespaces.zope.org/five"
+ ... i18n_domain="foo">
+ ... <five:registerPackage
+ ... package="pythonproduct2"
+ ... initialize="pythonproduct2.initialize"
+ ... />
+ ... </configure>'''
+ >>> zcml.load_string(configure_zcml)
+ pythonproduct2 initialized
+
+ Test to see if the pythonproduct2 python package actually gets setup
+ as a zope2 product in the Control Panel.
+
+ >>> product_listing = app.Control_Panel.Products.objectIds()
+ >>> 'pythonproduct2' in product_listing
+ True
+
+ Clean up:
+
+ >>> tearDown()
+ """
+
+
+def test_suite():
+ from Testing.ZopeTestCase import ZopeDocTestSuite
+ return ZopeDocTestSuite()
+
+if __name__ == '__main__':
+ framework()
Added: Products.Five/trunk/tests/testing/pythonproduct1.py
===================================================================
--- Products.Five/trunk/tests/testing/pythonproduct1.py 2006-05-02 22:48:25 UTC (rev 67873)
+++ Products.Five/trunk/tests/testing/pythonproduct1.py 2006-05-02 23:22:01 UTC (rev 67874)
@@ -0,0 +1,18 @@
+##############################################################################
+#
+# Copyright (c) 2004, 2005 Zope Corporation 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.
+#
+##############################################################################
+"""Python
+
+$Id$
+"""
+
Added: Products.Five/trunk/tests/testing/pythonproduct2/Extensions/__init__.py
===================================================================
Added: Products.Five/trunk/tests/testing/pythonproduct2/Extensions/somemodule.py
===================================================================
--- Products.Five/trunk/tests/testing/pythonproduct2/Extensions/somemodule.py 2006-05-02 22:48:25 UTC (rev 67873)
+++ Products.Five/trunk/tests/testing/pythonproduct2/Extensions/somemodule.py 2006-05-02 23:22:01 UTC (rev 67874)
@@ -0,0 +1,4 @@
+
+
+def somemethod(self):
+ print "Executed somemethod"
Added: Products.Five/trunk/tests/testing/pythonproduct2/__init__.py
===================================================================
--- Products.Five/trunk/tests/testing/pythonproduct2/__init__.py 2006-05-02 22:48:25 UTC (rev 67873)
+++ Products.Five/trunk/tests/testing/pythonproduct2/__init__.py 2006-05-02 23:22:01 UTC (rev 67874)
@@ -0,0 +1,4 @@
+
+
+def initialize(context):
+ print "pythonproduct2 initialized"
More information about the Zope-Checkins
mailing list