Back to index

plone3  3.1.7
FSZSQLMethod.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Customizable ZSQL methods that come from the filesystem.
00014 
00015 $Id: FSZSQLMethod.py 76464 2007-06-07 15:38:16Z tseaver $
00016 """
00017 
00018 import logging
00019 
00020 import Globals
00021 from AccessControl import ClassSecurityInfo
00022 from Acquisition import ImplicitAcquisitionWrapper
00023 from Products.ZSQLMethods.SQL import SQL
00024 
00025 from DirectoryView import registerFileExtension
00026 from DirectoryView import registerMetaType
00027 from FSObject import FSObject
00028 from permissions import View
00029 from permissions import ViewManagementScreens
00030 from utils import _dtmldir
00031 
00032 logger = logging.getLogger('CMFCore.FSZSQLMethod')
00033 
00034 
00035 class FSZSQLMethod(SQL, FSObject):
00036 
00037     """FSZSQLMethods act like Z SQL Methods but are not directly
00038     modifiable from the management interface."""
00039 
00040     meta_type = 'Filesystem Z SQL Method'
00041 
00042     manage_options = ({'label':'Customize', 'action':'manage_customise'},
00043                       {'label':'Test', 'action':'manage_testForm',
00044                        'help':('ZSQLMethods','Z-SQL-Method_Test.stx')},
00045                      )
00046 
00047     security = ClassSecurityInfo()
00048     security.declareObjectProtected(View)
00049 
00050     # Make mutators private
00051     security.declarePrivate('manage_main')
00052     security.declarePrivate('manage_edit')
00053     security.declarePrivate('manage_advanced')
00054     security.declarePrivate('manage_advancedForm')
00055     manage=None
00056 
00057     security.declareProtected(ViewManagementScreens, 'manage_customise')
00058     manage_customise = Globals.DTMLFile('custzsql', _dtmldir)
00059 
00060     def __init__(self, id, filepath, fullname=None, properties=None):
00061         FSObject.__init__(self, id, filepath, fullname, properties)
00062 
00063     def _createZODBClone(self):
00064         """Create a ZODB (editable) equivalent of this object."""
00065         # I guess it's bad to 'reach inside' ourselves like this,
00066         # but Z SQL Methods don't have accessor methdods ;-)
00067         s = SQL(self.id,
00068                 self.title,
00069                 self.connection_id,
00070                 self.arguments_src,
00071                 self.src)
00072         s.manage_advanced(self.max_rows_,
00073                           self.max_cache_,
00074                           self.cache_time_,
00075                           self.class_name_,
00076                           self.class_file_,
00077                           connection_hook=self.connection_hook,
00078                           direct=self.allow_simple_one_argument_traversal)
00079         return s
00080 
00081     def _readFile(self, reparse):
00082         """Read the data from the filesystem.
00083         """
00084         file = open(self._filepath, 'r') # not 'rb', as this is a text file!
00085         try:
00086             data = file.read()
00087         finally:
00088             file.close()
00089 
00090         # parse parameters
00091         parameters={}
00092         start = data.find('<dtml-comment>')
00093         end   = data.find('</dtml-comment>')
00094         if start==-1 or end==-1 or start>end:
00095             raise ValueError,'Could not find parameter block'
00096         block = data[start+14:end]
00097 
00098         for line in block.split('\n'):
00099             pair = line.split(':',1)
00100             if len(pair)!=2:
00101                 continue
00102             parameters[pair[0].strip().lower()]=pair[1].strip()
00103 
00104         # check for required parameters
00105         try:
00106             connection_id =   ( parameters.get('connection id', '') or
00107                                 parameters['connection_id'] )
00108         except KeyError,e:
00109             raise ValueError("The '%s' parameter is required "
00110                              "but was not supplied" % e)
00111 
00112         # Optional parameters
00113         title =           parameters.get('title','')
00114         arguments =       parameters.get('arguments','')
00115         max_rows =        parameters.get('max_rows',1000)
00116         max_cache =       parameters.get('max_cache',100)
00117         cache_time =      parameters.get('cache_time',0)
00118         class_name =      parameters.get('class_name','')
00119         class_file =      parameters.get('class_file','')
00120         connection_hook = parameters.get('connection_hook',None)
00121         direct = parameters.get('allow_simple_one_argument_traversal', None)
00122 
00123         self.manage_edit(title, connection_id, arguments, template=data)
00124 
00125         self.manage_advanced(max_rows,
00126                              max_cache,
00127                              cache_time,
00128                              class_name,
00129                              class_file,
00130                              connection_hook=connection_hook,
00131                              direct=direct)
00132 
00133         # do we need to do anything on reparse?
00134 
00135 
00136     if Globals.DevelopmentMode:
00137         # Provide an opportunity to update the properties.
00138         def __of__(self, parent):
00139             try:
00140                 self = ImplicitAcquisitionWrapper(self, parent)
00141                 self._updateFromFS()
00142                 return self
00143             except:
00144                 logger.exception("Error during __of__")
00145                 raise
00146 
00147 Globals.InitializeClass(FSZSQLMethod)
00148 
00149 registerFileExtension('zsql', FSZSQLMethod)
00150 registerMetaType('Z SQL Method', FSZSQLMethod)