[Zope3-checkins] CVS: Zope3/src/zope/app/zptpage - __init__.py:1.2 configure.zcml:1.2 fssync.py:1.2 interfaces.py:1.2 zptpage.py:1.2

Philipp von Weitershausen philikon at philikon.de
Tue Feb 24 11:51:18 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/zptpage
In directory cvs.zope.org:/tmp/cvs-serv27280/src/zope/app/zptpage

Added Files:
	__init__.py configure.zcml fssync.py interfaces.py zptpage.py 
Log Message:


Moved the Template Page content type to its own package below
zope.app, including its interfaces and browser views.




=== Zope3/src/zope/app/zptpage/__init__.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:51:18 2004
+++ Zope3/src/zope/app/zptpage/__init__.py	Tue Feb 24 11:50:47 2004
@@ -0,0 +1,18 @@
+##############################################################################
+#
+# Copyright (c) 2004 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$
+"""
+
+from zptpage import ZPTPage


=== Zope3/src/zope/app/zptpage/configure.zcml 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:51:18 2004
+++ Zope3/src/zope/app/zptpage/configure.zcml	Tue Feb 24 11:50:47 2004
@@ -0,0 +1,100 @@
+<configure
+    xmlns='http://namespaces.zope.org/zope'
+    xmlns:fssync='http://namespaces.zope.org/fssync'
+    i18n_domain='zope'
+    >
+
+  <!-- Module alias for backward compat
+       Will go away once we have a conversion script -->
+
+  <modulealias
+      module=".zptpage"
+      alias="zope.app.content.zpt"
+      />
+
+  <modulealias
+      module=".interfaces"
+      alias="zope.app.interfaces.content.zpt"
+      />
+
+
+
+  <interface 
+      interface=".interfaces.IZPTPage" 
+      type="zope.app.interfaces.content.IContentType"
+      /> 
+
+  <content class=".zptpage.ZPTPage">
+    <factory
+        id="ZPTPage"
+        permission="zope.ManageContent"
+        title="ZPT Page"
+        description="A simple, content-based Page Template"
+        />
+
+    <require
+        permission="zope.View"
+        attributes="__call__ macros"
+        />
+
+    <require
+        permission="zope.ManageContent"
+        interface=".interfaces.IZPTPage"
+        set_attributes="source expand evaluateInlineCode"
+        />
+
+    <require
+        permission="zope.View"
+        interface=".interfaces.IRenderZPTPage"
+        />
+
+    <implements
+        interface="zope.app.interfaces.annotation.IAttributeAnnotatable"
+        />
+  </content>
+
+  <adapter 
+      for="zope.app.folder.interfaces.IFolder"
+      provides="zope.app.interfaces.file.IFileFactory"
+      name=".pt"
+      factory=".zptpage.ZPTFactory"
+      permission="zope.ManageContent"
+      />
+
+  <adapter 
+      for=".interfaces.IZPTPage"
+      provides="zope.app.interfaces.file.IReadFile"
+      factory=".zptpage.ZPTReadFile"
+      permission="zope.ManageContent"
+      />
+
+  <adapter 
+      for=".interfaces.IZPTPage"
+      provides="zope.app.interfaces.file.IWriteFile"
+      factory=".zptpage.ZPTWriteFile"
+      permission="zope.ManageContent"
+      />
+
+  <adapter
+      for=".interfaces.IZPTPage"
+      provides="zope.app.interfaces.index.text.ISearchableText"
+      factory=".zptpage.SearchableText"
+      />
+
+  <adapter
+      for=".interfaces.IZPTPage"
+      provides="zope.app.interfaces.size.ISized"
+      factory=".zptpage.Sized"
+      />
+
+  <fssync:adapter
+      class=".zptpage.ZPTPage"
+      factory=".fssync.ZPTPageAdapter"
+      />
+
+
+  <!-- include browser package -->
+
+  <include package=".browser" />
+
+</configure>


=== Zope3/src/zope/app/zptpage/fssync.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:51:18 2004
+++ Zope3/src/zope/app/zptpage/fssync.py	Tue Feb 24 11:50:47 2004
@@ -0,0 +1,35 @@
+##############################################################################
+#
+# Copyright (c) 2004 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.
+# 
+##############################################################################
+"""Filesystem synchronization support.
+
+$Id$
+"""
+
+from zope.interface import implements
+from zope.fssync.server.entryadapter import ObjectEntryAdapter
+from zope.fssync.server.interfaces import IObjectFile
+
+class ZPTPageAdapter(ObjectEntryAdapter):
+    """ObjectFile adapter for ZPT page objects.
+    """
+    implements(IObjectFile)
+
+    def getBody(self):
+        return self.context.getSource()
+
+    def setBody(self, data):
+        # Convert the data to Unicode, since that's what ZPTPage wants;
+        # it's normally read from a file so it'll be bytes.
+        # XXX This will die if it's not ASCII.  Guess encoding???
+        self.context.setSource(unicode(data))


=== Zope3/src/zope/app/zptpage/interfaces.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:51:18 2004
+++ Zope3/src/zope/app/zptpage/interfaces.py	Tue Feb 24 11:50:47 2004
@@ -0,0 +1,67 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Templated Page Content Component Interfaces
+
+$Id$
+"""
+from zope.schema import SourceText, Bool
+from zope.interface import Interface, Attribute
+from zope.app.i18n import ZopeMessageIDFactory as _
+
+class IZPTPage(Interface):
+    """ZPT Pages are a persistent implementation of Page Templates."""
+
+
+    def setSource(text, content_type='text/html'):
+        """Save the source of the page template.
+
+        'text' must be Unicode.
+        """
+
+    def getSource():
+        """Get the source of the page template."""
+
+    source = SourceText(
+        title=_("Source"),
+        description=_("The source of the page template."),
+        required=True)
+
+    expand = Bool(
+        title=_("Expand macros"),
+        description=_("Expand Macros so that they all are shown in the "
+                      "code."),
+        default=False,
+        required=True)
+
+    evaluateInlineCode = Bool(
+        title=_("Evaluate Inline Code"),
+        description=_("Evaluate code snippets in TAL. We usually discourage "
+                      "people from using this feature."),
+        default=False,
+        required=True)
+
+
+class IRenderZPTPage(Interface):
+
+    content_type = Attribute('Content type of generated output')
+
+    def render(request, *args, **kw):
+        """Render the page template.
+
+        The first argument is bound to the top-level 'request'
+        variable. The positional arguments are bound to the 'args'
+        variable and the keyword arguments are bound to the 'options'
+        variable.
+        """
+


=== Zope3/src/zope/app/zptpage/zptpage.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:51:18 2004
+++ Zope3/src/zope/app/zptpage/zptpage.py	Tue Feb 24 11:50:47 2004
@@ -0,0 +1,183 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""
+$Id$
+"""
+import re
+
+from persistent import Persistent
+
+from zope.proxy import removeAllProxies
+from zope.security.proxy import ProxyFactory
+from zope.interface import implements
+from zope.pagetemplate.pagetemplate import PageTemplate
+
+from zope.app.pagetemplate.engine import AppPT
+from zope.app.i18n import ZopeMessageIDFactory as _
+from zope.app.interfaces.index.text import ISearchableText
+from zope.app.interfaces.size import ISized
+from zope.app.interfaces.file import IReadFile, IWriteFile, IFileFactory
+from zope.app.container.contained import Contained
+
+from interfaces import IZPTPage, IRenderZPTPage
+
+__metaclass__ = type
+
+class ZPTPage(AppPT, PageTemplate, Persistent, Contained):
+
+    implements(IZPTPage, IRenderZPTPage)
+
+    # See zope.app.interfaces.content.zpt.IZPTPage
+    expand = False
+
+    # See zope.app.interfaces.content.zpt.IZPTPage
+    evaluateInlineCode = False
+
+    def getSource(self):
+        '''See zope.app.interfaces.content.zpt.IZPTPage'''
+        return self.read()
+
+    def setSource(self, text, content_type='text/html'):
+        '''See zope.app.interfaces.content.zpt.IZPTPage'''
+        if not isinstance(text, unicode):
+            raise TypeError("source text must be Unicode" , text)
+        self.pt_edit(text.encode('utf-8'), content_type)
+
+    # See zope.app.interfaces.content.zpt.IZPTPage
+    source = property(getSource, setSource, None,
+                      """Source of the Page Template.""")
+
+    def pt_getEngineContext(self, namespace):
+        context = self.pt_getEngine().getContext(namespace)
+        context.evaluateInlineCode = self.evaluateInlineCode
+        return context
+
+    def pt_getContext(self, instance, request, **_kw):
+        # instance is a View component
+        self = removeAllProxies(self)
+        namespace = super(ZPTPage, self).pt_getContext(**_kw)
+        namespace['template'] = self
+        namespace['request'] = request
+        namespace['container'] = namespace['context'] = instance
+        return namespace
+
+    def render(self, request, *args, **keywords):
+        instance = self.__parent__
+
+        request = ProxyFactory(request)
+        instance = ProxyFactory(instance)
+        if args: args = ProxyFactory(args)
+        kw = ProxyFactory(keywords)
+
+        namespace = self.pt_getContext(instance, request,
+                                       args=args, options=kw)
+
+        return self.pt_render(namespace)
+
+    source = property(getSource, setSource, None,
+                      """Source of the Page Template.""")
+
+
+class SearchableText:
+
+    __used_for__ = IZPTPage
+    implements(ISearchableText)
+
+    def __init__(self, page):
+        self.page = page
+
+    def getSearchableText(self):
+        text = self.page.getSource()
+        if isinstance(text, str):
+            text = unicode(self.page.source, 'utf-8')
+        # else:
+        #   text was already Unicode, which happens, but unclear how it
+        #   gets converted to Unicode since the ZPTPage stores UTF-8 as
+        #   an 8-bit string.
+
+        if self.page.content_type.startswith('text/html'):
+            tag = re.compile(r"<[^>]+>")
+            text = tag.sub('', text)
+
+        return [text]
+
+class Sized:
+
+    implements(ISized)
+
+    def __init__(self, page):
+        self.num_lines = len(page.getSource().splitlines())
+
+    def sizeForSorting(self):
+        'See ISized'
+        return ('line', self.num_lines)
+
+    def sizeForDisplay(self):
+        'See ISized'
+        if self.num_lines == 1:
+            return _('1 line')
+        lines  = _('${lines} lines')
+        lines.mapping = {'lines': str(self.num_lines)}
+        return lines
+
+# File-system access adapters
+
+class ZPTReadFile:
+
+    implements(IReadFile)
+
+    def __init__(self, context):
+        self.context = context
+
+    def read(self):
+        return self.context.getSource()
+
+    def size(self):
+        return len(self.read())
+
+class ZPTWriteFile:
+
+    implements(IWriteFile)
+
+    def __init__(self, context):
+        self.context = context
+
+    def write(self, data):
+        # XXX Hm, how does one figure out an ftp encoding. Waaa.
+        self.context.setSource(unicode(data), None)
+
+class ZPTFactory:
+
+    implements(IFileFactory)
+
+
+    def __init__(self, context):
+        self.context = context
+
+    def __call__(self, name, content_type, data):
+        r = ZPTPage()
+        # XXX Hm, how does one figure out an ftp encoding. Waaa.
+        r.setSource(unicode(data), content_type or 'text/html')
+        return r
+
+class ZPTSourceView:
+
+    def __init__(self, context, request):
+        self.context = context
+        self.request = request
+
+    def __str__(self):
+        return self.context.getSource()
+
+    __call__ = __str__




More information about the Zope3-Checkins mailing list