Back to index

plone3  3.1.7
FSSTXMethod.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001, 2006 Zope Corporation and Contributors.
00004 # All Rights Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00008 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00009 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00010 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00011 # FOR A PARTICULAR PURPOSE.
00012 #
00013 ##############################################################################
00014 """ FSSTXMethod: Filesystem methodish Structured Text document.
00015 
00016 $Id: FSSTXMethod.py 77186 2007-06-28 19:06:19Z yuppie $
00017 """
00018 
00019 from AccessControl import ClassSecurityInfo
00020 from DocumentTemplate.DT_HTML import HTML as DTML_HTML
00021 from Globals import DTMLFile
00022 from Globals import InitializeClass
00023 from OFS.DTMLDocument import DTMLDocument
00024 from StructuredText.StructuredText import HTML as STX_HTML
00025 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
00026 
00027 from Products.CMFCore.DirectoryView import registerFileExtension
00028 from Products.CMFCore.DirectoryView import registerMetaType
00029 from Products.CMFCore.FSObject import FSObject
00030 from Products.CMFCore.permissions import FTPAccess
00031 from Products.CMFCore.permissions import View
00032 from Products.CMFCore.permissions import ViewManagementScreens
00033 from Products.CMFCore.utils import _dtmldir
00034 from Products.CMFCore.utils import _checkConditionalGET
00035 from Products.CMFCore.utils import _setCacheHeaders
00036 from Products.CMFCore.utils import _ViewEmulator
00037 
00038 _STX_TEMPLATE = 'ZPT'  # or 'DTML'
00039 
00040 _DEFAULT_TEMPLATE_DTML = """\
00041 <dtml-var standard_html_header>
00042 <dtml-var cooked>
00043 <dtml-var standard_html_footer>"""
00044 
00045 _CUSTOMIZED_TEMPLATE_DTML = """\
00046 <dtml-var standard_html_header>
00047 <dtml-var stx fmt="structured-text">
00048 <dtml-var standard_html_footer>"""
00049 
00050 _DEFAULT_TEMPLATE_ZPT = """\
00051 <html metal:use-macro="context/main_template/macros/master">
00052 <body>
00053 
00054 <metal:block metal:fill-slot="body"
00055 ><div tal:replace="structure options/cooked">
00056 COOKED TEXT HERE
00057 </div>
00058 </metal:block>
00059 
00060 </body>
00061 </html>
00062 """
00063 
00064 _CUSTOMIZED_TEMPLATE_ZPT = """\
00065 <html metal:use-macro="context/main_template/macros/master">
00066 <body>
00067 
00068 <metal:block metal:fill-slot="body"
00069 ><div tal:define="std modules/Products/PythonScripts/standard;
00070                   stx nocall:std/structured_text;"
00071       tal:replace="structure python:stx(template.stx)">
00072 COOKED TEXT HERE
00073 </div>
00074 </metal:block>
00075 
00076 </body>
00077 </html>
00078 """
00079 
00080 class FSSTXMethod(FSObject):
00081     """ A chunk of StructuredText, rendered as a skin method of a CMF site.
00082     """
00083     meta_type = 'Filesystem STX Method'
00084     _owner = None # unowned
00085 
00086     manage_options=({'label' : 'Customize','action' : 'manage_main'},
00087                     {'label' : 'View','action' : '',
00088                      'help' : ('OFSP' ,'DTML-DocumentOrMethod_View.stx')},
00089                    )
00090 
00091     security = ClassSecurityInfo()
00092     security.declareObjectProtected(View)
00093 
00094     security.declareProtected(ViewManagementScreens, 'manage_main')
00095     manage_main = DTMLFile('custstx', _dtmldir)
00096 
00097     #
00098     #   FSObject interface
00099     #
00100     def _createZODBClone(self):
00101         """
00102             Create a ZODB (editable) equivalent of this object.
00103         """
00104         if _STX_TEMPLATE == 'DTML':
00105             target = DTMLDocument(_CUSTOMIZED_TEMPLATE_DTML,
00106                                   __name__=self.getId())
00107         elif _STX_TEMPLATE == 'ZPT':
00108             target = ZopePageTemplate(self.getId(), _CUSTOMIZED_TEMPLATE_ZPT)
00109 
00110         target._setProperty('stx', self.raw, 'text')
00111         return target
00112 
00113     def _readFile(self, reparse):
00114         """Read the data from the filesystem.
00115         """
00116         file = open(self._filepath, 'r') # not 'rb', as this is a text file!
00117         try:
00118             data = file.read()
00119         finally:
00120             file.close()
00121         self.raw = data
00122 
00123         if reparse:
00124             self.cook()
00125 
00126     #
00127     #   "Wesleyan" interface (we need to be "methodish").
00128     #
00129     class func_code:
00130         pass
00131 
00132     func_code = func_code()
00133     func_code.co_varnames = ()
00134     func_code.co_argcount = 0
00135     func_code.__roles__ = ()
00136 
00137     func_defaults__roles__ = ()
00138     func_defaults = ()
00139 
00140     index_html = None   # No accidental acquisition
00141 
00142     default_content_type = 'text/html'
00143 
00144     def cook(self):
00145         if not hasattr(self, '_v_cooked'):
00146             self._v_cooked = STX_HTML(self.raw, level=1, header=0)
00147         return self._v_cooked
00148 
00149     _default_DTML_template = DTML_HTML(_DEFAULT_TEMPLATE_DTML)
00150     _default_ZPT_template = ZopePageTemplate('stxmethod_view',
00151                                              _DEFAULT_TEMPLATE_ZPT, 'text/html')
00152 
00153     def __call__( self, REQUEST={}, RESPONSE=None, **kw ):
00154         """ Return our rendered StructuredText.
00155         """
00156         self._updateFromFS()
00157 
00158         if RESPONSE is not None:
00159             RESPONSE.setHeader( 'Content-Type', 'text/html' )
00160 
00161         view = _ViewEmulator(self.getId()).__of__(self)
00162         if _checkConditionalGET(view, extra_context={}):
00163             return ''
00164 
00165         _setCacheHeaders(view, extra_context={})
00166 
00167         return self._render(REQUEST, RESPONSE, **kw)
00168 
00169     security.declarePrivate('modified')
00170     def modified(self):
00171         return self.getModTime()
00172 
00173     security.declarePrivate('_render')
00174     def _render(self, REQUEST={}, RESPONSE=None, **kw):
00175         """ Find the appropriate rendering template and use it to render us.
00176         """
00177         if _STX_TEMPLATE == 'DTML':
00178             default_template = self._default_DTML_template
00179         elif _STX_TEMPLATE == 'ZPT':
00180             default_template = self._default_ZPT_template
00181         else:
00182             raise TypeError('Invalid STX template: %s' % _STX_TEMPLATE)
00183 
00184         template = getattr(self, 'stxmethod_view', default_template)
00185 
00186         if getattr(template, 'isDocTemp', 0):
00187             #posargs = (self, REQUEST, RESPONSE)
00188             posargs = (self, REQUEST)
00189         else:
00190             posargs = ()
00191 
00192         kwargs = {'cooked': self.cook()}
00193         return template(*posargs, **kwargs)
00194 
00195     security.declareProtected(FTPAccess, 'manage_FTPget')
00196     def manage_FTPget(self):
00197         """ Fetch our source for delivery via FTP.
00198         """
00199         return self.raw
00200 
00201     security.declareProtected(ViewManagementScreens, 'PrincipiaSearchSource')
00202     def PrincipiaSearchSource(self):
00203         """ Fetch our source for indexing in a catalog.
00204         """
00205         return self.raw
00206 
00207     security.declareProtected(ViewManagementScreens, 'document_src')
00208     def document_src( self ):
00209         """ Fetch our source for rendering in the ZMI.
00210         """
00211         return self.raw
00212 
00213 InitializeClass(FSSTXMethod)
00214 
00215 registerFileExtension('stx', FSSTXMethod)
00216 registerMetaType('STX Method', FSSTXMethod)