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

Jim Fulton jim@zope.com
Wed, 25 Dec 2002 09:14:07 -0500


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

Added Files:
	__init__.py engine.py simpleviewclass.py 
	viewpagetemplatefile.py 
Log Message:
Grand renaming:

- Renamed most files (especially python modules) to lower case.

- Moved views and interfaces into separate hierarchies within each
  project, where each top-level directory under the zope package
  is a separate project.

- Moved everything to src from lib/python.

  lib/python will eventually go away. I need access to the cvs
  repository to make this happen, however.

There are probably some bits that are broken. All tests pass
and zope runs, but I haven't tried everything. There are a number
of cleanups I'll work on tomorrow.



=== Zope3/src/zope/app/pagetemplate/__init__.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:07 2002
+++ Zope3/src/zope/app/pagetemplate/__init__.py	Wed Dec 25 09:13:06 2002
@@ -0,0 +1,16 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+
+from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+from zope.app.pagetemplate.simpleviewclass import SimpleViewClass


=== Zope3/src/zope/app/pagetemplate/engine.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:07 2002
+++ Zope3/src/zope/app/pagetemplate/engine.py	Wed Dec 25 09:13:06 2002
@@ -0,0 +1,106 @@
+##############################################################################
+#
+# 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$
+"""
+__metaclass__ = type # All classes are new style when run with Python 2.2+
+
+import sys
+
+from zope.pagetemplate.expressions \
+     import PathExpr, StringExpr, NotExpr, DeferExpr
+from zope.pagetemplate.pythonexpr import PythonExpr
+from zope.pagetemplate.tales \
+     import ExpressionEngine, RegistrationError, Context
+
+from zope.app.interfaces.traversing.traverser import ITraverser
+from zope.app.traversing.traverser import Traverser
+from zope.i18n.globaltranslationservice import translationService
+from zope.proxy.introspection import removeAllProxies
+from zope.security.proxy import ProxyFactory
+from zope.security.restrictedbuiltins import RestrictedBuiltins
+
+
+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


=== Zope3/src/zope/app/pagetemplate/simpleviewclass.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:07 2002
+++ Zope3/src/zope/app/pagetemplate/simpleviewclass.py	Wed Dec 25 09:13:06 2002
@@ -0,0 +1,57 @@
+##############################################################################
+#
+# 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 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_


=== Zope3/src/zope/app/pagetemplate/viewpagetemplatefile.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:07 2002
+++ Zope3/src/zope/app/pagetemplate/viewpagetemplatefile.py	Wed Dec 25 09:13:06 2002
@@ -0,0 +1,76 @@
+##############################################################################
+#
+# 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