[Checkins] SVN: z3c.extfile/trunk/ new zcml directive for hashdir
utility
Bernd Dorn
bernd.dorn at lovelysystems.com
Tue Feb 5 09:00:11 EST 2008
Log message for revision 83530:
new zcml directive for hashdir utility
Changed:
U z3c.extfile/trunk/CHANGES.txt
A z3c.extfile/trunk/src/z3c/extfile/meta.zcml
U z3c.extfile/trunk/src/z3c/extfile/tests.py
U z3c.extfile/trunk/src/z3c/extfile/utility.py
A z3c.extfile/trunk/src/z3c/extfile/zcml.py
A z3c.extfile/trunk/src/z3c/extfile/zcml.txt
-=-
Modified: z3c.extfile/trunk/CHANGES.txt
===================================================================
--- z3c.extfile/trunk/CHANGES.txt 2008-02-05 13:26:22 UTC (rev 83529)
+++ z3c.extfile/trunk/CHANGES.txt 2008-02-05 14:00:10 UTC (rev 83530)
@@ -5,6 +5,8 @@
After
=====
+- added the hashDir zcml directive
+
- renamed environ variables to make it work with a server that sets
the HTTP_ prefix
Added: z3c.extfile/trunk/src/z3c/extfile/meta.zcml
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/meta.zcml (rev 0)
+++ z3c.extfile/trunk/src/z3c/extfile/meta.zcml 2008-02-05 14:00:10 UTC (rev 83530)
@@ -0,0 +1,16 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ xmlns:meta="http://namespaces.zope.org/meta">
+
+ <meta:directives namespace="http://namespaces.zope.org/zope">
+
+ <meta:directive
+ name="hashDir"
+ schema=".zcml.IHashDirDirective"
+ handler=".zcml.hashDirDirective"
+ />
+
+ </meta:directives>
+
+</configure>
+
Property changes on: z3c.extfile/trunk/src/z3c/extfile/meta.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: z3c.extfile/trunk/src/z3c/extfile/tests.py
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/tests.py 2008-02-05 13:26:22 UTC (rev 83529)
+++ z3c.extfile/trunk/src/z3c/extfile/tests.py 2008-02-05 14:00:10 UTC (rev 83530)
@@ -24,6 +24,8 @@
from paste.fixture import setup_module
from paste.fixture import TestApp
import os
+from zope.app.appsetup import product
+
here = os.path.dirname(__file__)
def setUpWSGI(test):
@@ -31,8 +33,9 @@
relative_to=here)
def setUpNoEnv(test):
- if os.environ['EXTFILE_STORAGEDIR']:
+ if os.environ.has_key('EXTFILE_STORAGEDIR'):
del os.environ['EXTFILE_STORAGEDIR']
+ product._configs.clear()
def test_suite():
@@ -67,6 +70,9 @@
DocFileSuite('testing.txt', setUp=setUpNoEnv,
optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
),
+ DocFileSuite('zcml.txt', setUp=setUpNoEnv,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+ ),
))
if __name__ == '__main__':
Modified: z3c.extfile/trunk/src/z3c/extfile/utility.py
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/utility.py 2008-02-05 13:26:22 UTC (rev 83529)
+++ z3c.extfile/trunk/src/z3c/extfile/utility.py 2008-02-05 14:00:10 UTC (rev 83530)
@@ -61,6 +61,13 @@
"""
path = getPath()
if path is not None:
+ # look first if we have utility already
+ hd = component.queryUtility(interfaces.IHashDir)
+ if hd is not None:
+ log.warn('Ignoring hashdir path %r using already'
+ ' registered IHashDir Utility at %r' % (
+ path,hd.path))
+ return
hd = hashdir.HashDir(path)
component.provideUtility(hd, provides=interfaces.IHashDir)
log.info('Registered IHashDir utility with storagedir: %r' % path)
Added: z3c.extfile/trunk/src/z3c/extfile/zcml.py
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/zcml.py (rev 0)
+++ z3c.extfile/trunk/src/z3c/extfile/zcml.py 2008-02-05 14:00:10 UTC (rev 83530)
@@ -0,0 +1,44 @@
+##############################################################################
+#
+# Copyright (c) 2006-2007 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.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import os
+
+from zope import interface
+from zope import schema
+from zope.component import zcml
+from zope.configuration.fields import Path
+from interfaces import IHashDir
+from z3c.extfile import hashdir
+
+class IHashDirDirective(interface.Interface):
+ """Parameters for the cache settings directive."""
+
+ path = Path(title = u'Path',
+ required=True)
+
+def hashDirDirective(_context, path, permission=None):
+ """Function to create hashdir utility"""
+
+ util = hashdir.HashDir(path)
+ zcml.utility(_context,
+ provides=IHashDir,
+ component=util,
+ permission=permission,
+ name='')
+
+
Property changes on: z3c.extfile/trunk/src/z3c/extfile/zcml.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.extfile/trunk/src/z3c/extfile/zcml.txt
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/zcml.txt (rev 0)
+++ z3c.extfile/trunk/src/z3c/extfile/zcml.txt 2008-02-05 14:00:10 UTC (rev 83530)
@@ -0,0 +1,77 @@
+==================
+ZCML Configuration
+==================
+
+In order to register a global hashdir utility a zcml directive is
+provided.
+
+ >>> baseTemplate = """<configure
+ ... xmlns='http://namespaces.zope.org/zope'
+ ... xmlns:test='http://www.zope.org/NS/Zope3/test'
+ ... i18n_domain='zope'>
+ ... <hashDir path="%s"/>
+ ... </configure>"""
+
+ >>> from zope.configuration.xmlconfig import xmlconfig, XMLConfig
+
+Do the meta stuff.
+
+ >>> import z3c.extfile
+ >>> XMLConfig('meta.zcml', z3c.extfile)()
+
+Let us register a hashdir util.
+
+ >>> from StringIO import StringIO
+ >>> path = "./its/not/there"
+ >>> xmlconfig(StringIO(baseTemplate % path))
+ Traceback (most recent call last):
+ ...
+ ZopeXMLConfigurationError: ...
+ OSError: ... No such file or directory: '.../its/not/there'
+
+We need an existing path.
+
+ >>> from zope import component
+ >>> from z3c.extfile.interfaces import IHashDir
+ >>> import tempfile, os, shutil
+ >>> tmp1 = tempfile.mkdtemp()
+ >>> xmlconfig(StringIO(baseTemplate % tmp1))
+ >>> sorted(os.listdir(tmp1))
+ ['tmp', 'var']
+ >>> util = component.getUtility(IHashDir)
+ >>> util
+ <z3c.extfile.hashdir.HashDir object at ...>
+
+ >>> util.path == tmp1
+ True
+
+
+
+Also the bootstrapsubscriber looks if the utility is already
+registered and issues a warning if so and returns without any action.
+Let us enable the logger.
+
+ >>> import logging, sys
+ >>> log = logging.getLogger('z3c.extfile')
+ >>> logStream = sys.stdout
+ >>> log.setLevel(logging.WARN)
+ >>> logHandler = logging.StreamHandler(sys.stdout)
+ >>> log.addHandler(logHandler)
+
+Now let us call the bootstrap subscriber.
+
+ >>> tmp2 = tempfile.mkdtemp()
+ >>> os.environ['EXTFILE_STORAGEDIR'] = tmp2
+
+Now we should get a log output.
+
+ >>> from z3c.extfile.utility import bootStrapSubscriber
+ >>> bootStrapSubscriber(None)
+ Ignoring hashdir path '...' using already registered IHashDir Utility at u'...'
+
+Some cleanup.
+
+ >>> log.removeHandler(logHandler)
+ >>> del os.environ['EXTFILE_STORAGEDIR']
+ >>> shutil.rmtree(tmp1)
+ >>> shutil.rmtree(tmp2)
Property changes on: z3c.extfile/trunk/src/z3c/extfile/zcml.txt
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the Checkins
mailing list