[Zope3-checkins] CVS: Zope3/src/zope/app/zptpage -
__init__.py:1.1.2.1 configure.zcml:1.1.2.1 fssync.py:1.1.2.1
interfaces.py:1.1.2.1 zptpage.py:1.1.2.1
Philipp von Weitershausen
philikon at philikon.de
Fri Feb 20 14:43:47 EST 2004
Update of /cvs-repository/Zope3/src/zope/app/zptpage
In directory cvs.zope.org:/tmp/cvs-serv22527
Added Files:
Tag: philikon-movecontent-branch
__init__.py configure.zcml fssync.py interfaces.py zptpage.py
Log Message:
ZPTPage moved to zope.app.
=== Added File Zope3/src/zope/app/zptpage/__init__.py ===
##############################################################################
#
# 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: __init__.py,v 1.1.2.1 2004/02/20 19:43:47 philikon Exp $
"""
from zptpage import ZPTPage
=== Added File Zope3/src/zope/app/zptpage/configure.zcml ===
<configure
xmlns='http://namespaces.zope.org/zope'
xmlns:fssync='http://namespaces.zope.org/fssync'
i18n_domain='zope'
>
<!-- Module alias for backward compat -->
<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>
=== Added File Zope3/src/zope/app/zptpage/fssync.py ===
##############################################################################
#
# 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: fssync.py,v 1.1.2.1 2004/02/20 19:43:47 philikon Exp $
"""
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))
=== Added File Zope3/src/zope/app/zptpage/interfaces.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.
#
##############################################################################
"""Templated Page Content Component Interfaces
$Id: interfaces.py,v 1.1.2.1 2004/02/20 19:43:47 philikon Exp $
"""
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.
"""
=== Added File Zope3/src/zope/app/zptpage/zptpage.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.
#
##############################################################################
"""
$Id: zptpage.py,v 1.1.2.1 2004/02/20 19:43:47 philikon Exp $
"""
import re
from persistence 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