[Zope3-checkins] CVS: Zope3/src/zope/app/publisher/browser - directoryresource.py:1.1 pagetemplateresource.py:1.1 configure.zcml:1.6 meta.zcml:1.13 metaconfigure.py:1.12 metadirectives.py:1.5 resourcemeta.py:1.11

Philipp von Weitershausen philikon at philikon.de
Mon Aug 11 11:58:46 EDT 2003


Update of /cvs-repository/Zope3/src/zope/app/publisher/browser
In directory cvs.zope.org:/tmp/cvs-serv28048/browser

Modified Files:
	configure.zcml meta.zcml metaconfigure.py metadirectives.py 
	resourcemeta.py 
Added Files:
	directoryresource.py pagetemplateresource.py 
Log Message:
Added implementations and tests for

a) PageTemplateResource

  This allows resources to be page templates. They will not have any context,
  container nor view variable in their namespace. My current and only use
  case for them right now is to provide i18n'd, markup-based resources.
  You may add one using::

  <browser:resource name="foobar.html" template="foobar.pt" />

b) DirectoryResource

  This is a browser resource that represents a file system directory. It
  uses a simple mapping based on file extensions to delegate to either
  FileResourceFactory, ImageResourceFactory or PageTemplateResourceFactory.
  You may add one using::

  <browser:resourceDirectory name="myresdir" directory="myresdir" />


=== Added File Zope3/src/zope/app/publisher/browser/directoryresource.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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: directoryresource.py,v 1.1 2003/08/11 14:58:07 philikon Exp $
"""

import os

from zope.interface import implements
from zope.exceptions import NotFoundError
from zope.security.proxy import Proxy
from zope.publisher.browser import BrowserView
from zope.publisher.interfaces.browser import IBrowserResource, \
     IBrowserPublisher

from zope.app.publisher.browser.resource import Resource

from fileresource import FileResourceFactory, ImageResourceFactory
from pagetemplateresource import PageTemplateResourceFactory
from resources import empty

_marker = []

# we only need this class a context for DirectoryResource
class Directory:

    def __init__(self, path, checker):
        self.path = path
        self.checker = checker

class DirectoryResource(BrowserView, Resource):

    implements(IBrowserResource, IBrowserPublisher)

    resource_factories = {
        'gif':  ImageResourceFactory,
        'png':  ImageResourceFactory,
        'jpg':  ImageResourceFactory,
        'pt':   PageTemplateResourceFactory,
        'zpt':  PageTemplateResourceFactory,
        'html': PageTemplateResourceFactory,
        }

    default_factory = FileResourceFactory

    def publishTraverse(self, request, name):
        '''See interface IBrowserPublisher'''
        return self.get(name)

    def browserDefault(self, request):
        '''See interface IBrowserPublisher'''
        return empty, ()

    def __getitem__(self, name):
        res = self.get(name, None)
        if res is None:
            raise KeyError, name
        return res

    def get(self, name, default=_marker):
        path = self.context.path
        filename = os.path.join(path, name)
        if not os.path.isfile(filename):
            if default is _marker:
                raise NotFoundError(name)
            return default
        ext = name.split('.')[-1]
        factory = self.resource_factories.get(ext, self.default_factory)
        return factory(filename, self.context.checker)(self.request)

class DirectoryResourceFactory:

    def __init__(self, path, checker):
        self.__dir = Directory(path, checker)
        self.__checker = checker

    def __call__(self, request):
        return Proxy(DirectoryResource(self.__dir, request),
                     self.__checker)


=== Added File Zope3/src/zope/app/publisher/browser/pagetemplateresource.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
"""Page Template Resource

$Id: pagetemplateresource.py,v 1.1 2003/08/11 14:58:07 philikon Exp $
"""

from zope.interface import implements
from zope.exceptions import NotFoundError
from zope.security.proxy import Proxy
from zope.publisher.browser import BrowserView
from zope.publisher.interfaces.browser import IBrowserResource, \
     IBrowserPublisher

from zope.app.publisher.pagetemplateresource import PageTemplate
from zope.app.publisher.browser.resource import Resource

class PageTemplateResource(BrowserView, Resource):

    implements(IBrowserResource, IBrowserPublisher)

    def publishTraverse(self, request, name):
        '''See interface IBrowserPublisher'''
        raise NotFoundError(name)

    def browserDefault(self, request):
        '''See interface IBrowserPublisher'''
        return self, ()

    def __call__(self):
        pt = self.context
        return pt(self.request)

class PageTemplateResourceFactory:

    def __init__(self, path, checker):
        self.__pt = PageTemplate(path)
        self.__checker = checker

    def __call__(self, request):
        return Proxy(PageTemplateResource(self.__pt, request), self.__checker)


=== Zope3/src/zope/app/publisher/browser/configure.zcml 1.5 => 1.6 ===
--- Zope3/src/zope/app/publisher/browser/configure.zcml:1.5	Tue May 27 08:55:35 2003
+++ Zope3/src/zope/app/publisher/browser/configure.zcml	Mon Aug 11 10:58:07 2003
@@ -50,6 +50,10 @@
   <allow attributes="GET HEAD __call__" />
 </content>
 
+<content class=".pagetemplateresource.PageTemplateResource">
+  <allow interface="zope.publisher.interfaces.browser.IBrowserPublisher" />
+  <allow attributes="__call__" />
+</content>
 
 <browser:page 
     name=""


=== Zope3/src/zope/app/publisher/browser/meta.zcml 1.12 => 1.13 ===
--- Zope3/src/zope/app/publisher/browser/meta.zcml:1.12	Sat Aug  2 22:13:17 2003
+++ Zope3/src/zope/app/publisher/browser/meta.zcml	Mon Aug 11 10:58:07 2003
@@ -79,6 +79,12 @@
         handler=".metaconfigure.resource"
         />
 
+    <meta:directive
+        name="resourceDirectory"
+        schema=".metadirectives.IResourceDirectoryDirective"
+        handler=".metaconfigure.resourceDirectory"
+        />
+
     <meta:complexDirective
         name="i18n-resource"
         schema=".metadirectives.II18nResourceDirective"


=== Zope3/src/zope/app/publisher/browser/metaconfigure.py 1.11 => 1.12 ===
--- Zope3/src/zope/app/publisher/browser/metaconfigure.py:1.11	Sat Aug  2 22:13:17 2003
+++ Zope3/src/zope/app/publisher/browser/metaconfigure.py	Mon Aug 11 10:58:07 2003
@@ -23,7 +23,8 @@
 from zope.app.component.metaconfigure import handler
 
 # referred to through ZCML
-from zope.app.publisher.browser.resourcemeta import resource
+from zope.app.publisher.browser.resourcemeta import resource, \
+     resourceDirectory
 from zope.app.publisher.browser.i18nresourcemeta import I18nResource
 
 from zope.app.publisher.browser.viewmeta import view


=== Zope3/src/zope/app/publisher/browser/metadirectives.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/publisher/browser/metadirectives.py:1.4	Sun Aug  3 13:50:20 2003
+++ Zope3/src/zope/app/publisher/browser/metadirectives.py	Mon Aug 11 10:58:07 2003
@@ -279,6 +279,14 @@
         required=False
         )
 
+    template = Path(
+        title=u"Template",
+        description=u"""
+        If the image attribute is used, then a page template resource,
+        rather than a file resource will be created.""",
+        required=False
+        )
+
 class II18nResourceDirective(IBasicResourceInformation):
     """
     Defines an i18n'd resource.
@@ -325,6 +333,29 @@
         If the image attribute is used, then an image resource, rather
         than a file resource will be created.""",
         required=False
+        )
+
+class IResourceDirectoryDirective(IBasicResourceInformation):
+    """
+    Defines a directory containing browser resource
+    """
+
+    name = TextLine(
+        title=u"The name of the resource",
+        description=u"""
+        This is the name used in resource urls. Resource urls are of
+        the form site/@@/resourcename, where site is the url of
+        "site", a folder with a service manager.
+
+        We make resource urls site-relative (as opposed to
+        content-relative) so as not to defeat caches.""",
+        required=True
+        )
+
+    directory = Path(
+        title=u"Directory",
+        description=u"The directory containing the resource data.",
+        required=True
         )
 
 #


=== Zope3/src/zope/app/publisher/browser/resourcemeta.py 1.10 => 1.11 ===
--- Zope3/src/zope/app/publisher/browser/resourcemeta.py:1.10	Sat Aug  2 22:13:17 2003
+++ Zope3/src/zope/app/publisher/browser/resourcemeta.py	Mon Aug 11 10:58:07 2003
@@ -16,6 +16,8 @@
 $Id$
 """
 
+import os
+
 from zope.security.checker import CheckerPublic, NamesChecker
 from zope.configuration.exceptions import ConfigurationError
 from zope.app.services.servicenames import Resources
@@ -23,29 +25,54 @@
 from zope.app.component.metaconfigure import handler
 
 from fileresource import FileResourceFactory, ImageResourceFactory
+from pagetemplateresource import PageTemplateResourceFactory
+from directoryresource import DirectoryResourceFactory
 
 allowed_names = ('GET', 'HEAD', 'publishTraverse', 'browserDefault',
                  'request', '__call__')
 
 def resource(_context, name, layer='default', permission='zope.Public',
-             file=None, image=None):
+             file=None, image=None, template=None):
 
     if permission == 'zope.Public':
         permission = CheckerPublic
 
     checker = NamesChecker(allowed_names, permission)
 
-    if file and image or not (file or image):
+    if ((file and image) or (file and template) or
+        (image and template) or not (file or image or template)):
         raise ConfigurationError(
-            "Must use exactly one of file or image "
+            "Must use exactly one of file or image or template"
             "attributes for resource directives"
             )
 
     if file:
-        factory = FileResourceFactory(_context.path(file), checker)
+        factory = FileResourceFactory(file, checker)
+    elif image:
+        factory = ImageResourceFactory(image, checker)
     else:
-        factory = ImageResourceFactory(_context.path(image), checker)
+        factory = PageTemplateResourceFactory(template, checker)
+
+    _context.action(
+        discriminator = ('resource', name, IBrowserPresentation, layer),
+        callable = handler,
+        args = (Resources, 'provideResource',
+                name, IBrowserPresentation, factory, layer),
+        )
+
+def resourceDirectory(_context, name, directory, layer='default',
+                      permission='zope.Public'):
+    if permission == 'zope.Public':
+        permission = CheckerPublic
+
+    checker = NamesChecker(allowed_names, permission)
+
+    if not os.path.isdir(directory):
+        raise ConfigurationError(
+            "Directory %s does not exist" % directory
+            )
 
+    factory = DirectoryResourceFactory(directory, checker)
     _context.action(
         discriminator = ('resource', name, IBrowserPresentation, layer),
         callable = handler,




More information about the Zope3-Checkins mailing list