Back to index

plone3  3.1.7
ecmaview.py
Go to the documentation of this file.
00001 # Copyright (c) 2005-2007
00002 # Authors: KSS Project Contributors (see docs/CREDITS.txt)
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License version 2 as published
00006 # by the Free Software Foundation.
00007 #
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00016 # 02111-1307, USA.
00017 
00018 try:
00019     from Products.Five import BrowserView
00020 except ImportError:
00021     from zope.app.publisher.browser import BrowserView
00022 
00023 from zope.interface import Interface, implements
00024 from zope.app import zapi
00025 from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
00026 import os.path
00027 from zope.publisher.interfaces.browser import IBrowserPublisher
00028 from zope.publisher.interfaces import NotFound
00029 
00030 # Oh well... file resource has different import locations in zope and five,
00031 # the factories take different parameters order,
00032 # plus these are not "entirely clean" as far as caching is concerned...
00033 # Instead, use the resources implemented from concatresource:
00034 # this is version free and has properly implemented cache control.
00035 from kss.core.pluginregistry._concatresource.resource import ConcatResourceFactory
00036 
00037 _marker = object()
00038 
00039 # z3 only
00040 from zope.security.checker import CheckerPublic, NamesChecker
00041 allowed_names = ('GET', 'HEAD', 'publishTraverse', 'browserDefault',
00042                  'request', '__call__')
00043 permission = CheckerPublic
00044 checker = NamesChecker(allowed_names, permission)
00045 
00046 class ViewFile(object):
00047     '''A wrapper for file resources that can be used in a view
00048     
00049     Similar to ViewPageTemplate in usage.
00050     (We only use the FileResource here, no distinction on content types
00051     like in the resourceDirectory code.)
00052     '''
00053 
00054     def __init__(self, name, path):
00055         # Create the resource with cache control most proper for debugging.
00056         self.resource_factory = ConcatResourceFactory([path], name, 
00057                 compress_level='none', caching='memory', lmt_check_period=0.0,
00058                 checker=checker)
00059         self.name = name
00060 
00061     def __get__(self, obj, cls=None):
00062         'The view wants a method only.'
00063         request = obj.request
00064         resource = self.resource_factory(request)
00065         resource.__parent__ = obj
00066         resource.__name__ = self.name
00067         return resource
00068 
00069 def absolute_dir(path):
00070     here = os.path.split(globals()['__file__'])[0]
00071     return os.path.abspath(os.path.join(here, path))
00072 
00073 class EcmaView(BrowserView):
00074     '''Kukit test view
00075     
00076     This allows the runner.html to be used on this view.
00077 
00078     This provides the tests run with the compiled kukit.js
00079     resource, in the same way as they would be run 
00080     in production with kss.
00081     '''
00082 
00083     implements(IBrowserPublisher)
00084 
00085     _testdir = absolute_dir('../kukit/tests')
00086 
00087     _runner = ViewPageTemplateFile('../kukit/tests/runner.html')
00088     
00089     # The next is only necessary on Zope (<=) 2.9,
00090     # provides a docstring to the method
00091     def _runner_proxy(self, *arg, **kw):
00092         'Publishable method'
00093         return self._runner(*arg, **kw)
00094 
00095     def publishTraverse(self, request, name):
00096         '''See interface IBrowserPublisher'''
00097         return self.get(name)
00098 
00099     def browserDefault(self, request):
00100         '''See interface IBrowserPublisher'''
00101         return self, ()
00102 
00103     def __getitem__(self, name):
00104         res = self.get(name, None)
00105         if res is None:
00106             raise KeyError(name)
00107         return res
00108 
00109     def get(self, name, default=_marker):
00110         # runner.html is compiled as a pagetemplate
00111         if name == 'runner.html':
00112             # XXX For Zope2.9 we need this.
00113             if not hasattr(self.request, 'debug'):
00114                 self.request.debug = None
00115             # proxy is used to make it publishable, on Zope <= 2.9
00116             return self._runner_proxy
00117 
00118         # We store them on the view on demand.
00119         # Is it there yet?
00120         if name[0] != '_':
00121             try:
00122                 return getattr(self, name)
00123             except AttributeError:
00124                 pass
00125 
00126         # See the file we need
00127         path = os.path.join(self._testdir, name)
00128         if os.path.isfile(path):
00129             # Ok, this is a file. Cook it.
00130             resource = ViewFile(name, path)
00131             setattr(self.__class__, name, resource)
00132             # important: return accessed *from* the view.
00133             return getattr(self, name)
00134 
00135         # Not found.
00136         if default is _marker:
00137             raise NotFound(None, name)
00138         return default
00139 
00140     def __call__(self):
00141         'By default we redirect to runner.html.'
00142         # on Zope3, the url is of zope.publisher.http.URLGetter class, so we must stringify it
00143         return self.request.response.redirect(str(self.request.URL) + '/runner.html')