[Zope3-checkins] CVS: Zope3/src/zope/app/pagetemplate - __init__.py:1.1.2.1 engine.py:1.1.2.1 simpleviewclass.py:1.1.2.1 viewpagetemplatefile.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 23 Dec 2002 14:31:57 -0500


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

Added Files:
      Tag: NameGeddon-branch
	__init__.py engine.py simpleviewclass.py 
	viewpagetemplatefile.py 
Log Message:
Initial renaming before debugging

=== Added File Zope3/src/zope/app/pagetemplate/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/app/pagetemplate/engine.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
# 
##############################################################################
"""Expression engine configuration and registration.

Each expression engine can have its own expression types and base names.

$Id: engine.py,v 1.1.2.1 2002/12/23 19:31:56 jim Exp $
"""
__metaclass__ = type # All classes are new style when run with Python 2.2+

from zope.pagetemplate.tales \
     import ExpressionEngine, RegistrationError, Context
from zope.pagetemplate.expressions \
     import PathExpr, StringExpr, NotExpr, DeferExpr
from zope.pagetemplate.pythonexpr import PythonExpr
from zope.security.proxy import ProxyFactory
from zope.app.interfaces.traversing.traverser import ITraverser
from zope.security.restrictedbuiltins import RestrictedBuiltins
from zope.proxy.introspection import removeAllProxies
from zope.i18n.globaltranslationservice import translationService
from zope.app.traversing.traverser import Traverser
import sys

def zopeTraverser(object, path_items, econtext):
    """Traverses a sequence of names, first trying attributes then items.
    """
    traverser = Traverser(object)
    return traverser.traverse(path_items,
                              request=getattr(econtext, 'request', None))


class ZopePathExpr(PathExpr):

    def __init__(self, name, expr, engine):
        super(ZopePathExpr, self).__init__(name, expr, engine, zopeTraverser)

class ZopePythonExpr(PythonExpr):

    def __call__(self, econtext):
        __traceback_info__ = self.text
        vars = self._bind_used_names(econtext, RestrictedBuiltins)
        return eval(self._code, vars)

class ZopeContext(Context):

    def evaluateMacro(self, expr):
        macro = Context.evaluateMacro(self, expr)
        macro = removeAllProxies(macro)
        return macro

    def translate(self, domain, msgid, mapping):
        return translationService.translate(domain, msgid, mapping,
                                            self.request)
    

class ZopeEngine(ExpressionEngine):

    def getContext(self, __namespace=None, **namespace):
        if __namespace:
            if namespace:
                namespace.update(__namespace)
            else:
                namespace = __namespace
                
        context = ZopeContext(self, namespace)

        # Put request into context so path traversal can find it
        if 'request' in namespace:
            context.request = namespace['request']

        return context

def Engine():
    e = ZopeEngine()
    for pt in ZopePathExpr._default_type_names:
        e.registerType(pt, ZopePathExpr)
    e.registerType('string', StringExpr)
    e.registerType('python', ZopePythonExpr)
    e.registerType('not', NotExpr)
    e.registerType('defer', DeferExpr)
    e.registerBaseName('modules', ProxyFactory(sys.modules))
    return e

Engine = Engine()

class AppPT:
    
    # Use our special engine
    pt_getEngineContext = Engine.getContext
    
    def pt_getEngine(self):
        return Engine



=== Added File Zope3/src/zope/app/pagetemplate/simpleviewclass.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: simpleviewclass.py,v 1.1.2.1 2002/12/23 19:31:56 jim Exp $
"""

import sys
from zope.publisher.browser import BrowserView
from zope.publisher.interfaces.browser import IBrowserPublisher
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
from zope.security.checker import defineChecker, NamesChecker

class simple(BrowserView):

    __implements__ = IBrowserPublisher, BrowserView.__implements__

    def browserDefault(self, request):
        return self, ()

    def publishTraverse(self, request, name):
        if name == 'index.html':
            return self.index
        
        raise NotFound(self, name, request)

    # XXX: we need some unittests for this !!!
    def __getitem__(self, name):
        return self.index.macros[name]

    def __call__(self, *args, **kw):
        return self.index(self.request, *args, **kw)

def SimpleViewClass(src, offering=None, used_for=None, bases=()):
    if offering is None:
        offering = sys._getframe(1).f_globals

    bases += (simple, )

    class_ = type("SimpleViewClass from %s" % src, bases,
                  {'index': ViewPageTemplateFile(src, offering)})

    if used_for is not None:
        class_.__used_for__ = used_for

    return class_


=== Added File Zope3/src/zope/app/pagetemplate/viewpagetemplatefile.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.
# 
##############################################################################
"""
See ViewPageTemplateFile

$Id%
"""
__metaclass__ = type # All classes are new style when run with Python 2.2+

from zope.pagetemplate.pagetemplatefile import PageTemplateFile
from zope.component import getView
from zope.app.pagetemplate.engine import AppPT

class ViewPageTemplateFile(AppPT, PageTemplateFile):
    """Page Templates used as methods of views defined as Python classes.
    """

    def pt_getContext(self, instance, request, **_kw):
        # instance is a View component
        namespace = super(ViewPageTemplateFile, self).pt_getContext(**_kw)
        namespace['request'] = request
        namespace['view'] = instance
        namespace['context'] = context = instance.context
        namespace['views'] = ViewMapper(context, request)
        return namespace

    def __call__(self, instance, *args, **keywords):
        namespace = self.pt_getContext(
            request=instance.request,
            instance=instance,
            args=args, options=keywords)
        return self.pt_render(namespace)

    def __get__(self, instance, type=None):
        return BoundPageTemplate(self, instance)

    # Instances of this class are pretending to be methods.
    # In general, they need to be ContextMethods.
    __Zope_ContextWrapper_contextful_get__ = True

class ViewMapper:
    def __init__(self, ob, request):
        self.ob = ob
        self.request = request 

    def __getitem__(self, name):
        return getView(self.ob, name, self.request)


class BoundPageTemplate:
    def __init__(self, pt, ob):
        object.__setattr__(self, 'im_func', pt)
        object.__setattr__(self, 'im_self', ob)

    def __call__(self, REQUEST=None, **kw):
        return self.im_func(self.im_self, REQUEST, **kw)

    def __getattr__(self, name):
        return getattr(self.im_func, name)

    def __setattr__(self, name, v):
        raise AttributeError("Can't set attribute", name)

    def __repr__(self):
        return "<BoundPageTemplateFile of %r>" % self.im_self