vendor/CMF/1.6.3/CMFCore

view FSZSQLMethod.py @ 2:4c712d7bd1d7

Added tag 1.6.3 for changeset 1babb9d61518
author Georges Racinet on purity.racinet.fr <georges@racinet.fr>
date Fri, 09 Sep 2011 12:44:00 +0200
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Customizable ZSQL methods that come from the filesystem.
15 $Id$
16 """
18 import logging
19 import Globals
20 from AccessControl import ClassSecurityInfo
21 from Acquisition import ImplicitAcquisitionWrapper
22 from Products.ZSQLMethods.SQL import SQL
24 from DirectoryView import registerFileExtension
25 from DirectoryView import registerMetaType
26 from FSObject import FSObject
27 from permissions import View
28 from permissions import ViewManagementScreens
29 from utils import _dtmldir
30 from utils import expandpath
33 logger = logging.getLogger('CMFCore.FSZSQLMethod')
36 class FSZSQLMethod(SQL, FSObject):
37 """FSZSQLMethods act like Z SQL Methods but are not directly
38 modifiable from the management interface."""
40 meta_type = 'Filesystem Z SQL Method'
42 manage_options=(
43 (
44 {'label':'Customize', 'action':'manage_customise'},
45 {'label':'Test', 'action':'manage_testForm',
46 'help':('ZSQLMethods','Z-SQL-Method_Test.stx')},
47 )
48 )
50 # Use declarative security
51 security = ClassSecurityInfo()
53 security.declareObjectProtected(View)
55 # Make mutators private
56 security.declarePrivate('manage_main','manage_edit','manage_advanced','manage_advancedForm')
57 manage=None
59 security.declareProtected(ViewManagementScreens, 'manage_customise')
60 manage_customise = Globals.DTMLFile('custzsql', _dtmldir)
62 def __init__(self, id, filepath, fullname=None, properties=None):
63 FSObject.__init__(self, id, filepath, fullname, properties)
65 def _createZODBClone(self):
66 """Create a ZODB (editable) equivalent of this object."""
67 # I guess it's bad to 'reach inside' ourselves like this,
68 # but Z SQL Methods don't have accessor methdods ;-)
69 s = SQL(self.id,
70 self.title,
71 self.connection_id,
72 self.arguments_src,
73 self.src)
74 s.manage_advanced(self.max_rows_,
75 self.max_cache_,
76 self.cache_time_,
77 self.class_name_,
78 self.class_file_,
79 connection_hook=self.connection_hook,
80 direct=self.allow_simple_one_argument_traversal)
81 return s
83 def _readFile(self, reparse):
84 fp = expandpath(self._filepath)
85 file = open(fp, 'r') # not 'rb', as this is a text file!
86 try:
87 data = file.read()
88 finally: file.close()
90 # parse parameters
91 parameters={}
92 start = data.find('<dtml-comment>')
93 end = data.find('</dtml-comment>')
94 if start==-1 or end==-1 or start>end:
95 raise ValueError,'Could not find parameter block'
96 block = data[start+14:end]
98 for line in block.split('\n'):
99 pair = line.split(':',1)
100 if len(pair)!=2:
101 continue
102 parameters[pair[0].strip().lower()]=pair[1].strip()
104 # check for required parameters
105 try:
106 connection_id = ( parameters.get('connection id', '') or
107 parameters['connection_id'] )
108 except KeyError,e:
109 raise ValueError,"The '%s' parameter is required but was not supplied" % e
111 # Optional parameters
112 title = parameters.get('title','')
113 arguments = parameters.get('arguments','')
114 max_rows = parameters.get('max_rows',1000)
115 max_cache = parameters.get('max_cache',100)
116 cache_time = parameters.get('cache_time',0)
117 class_name = parameters.get('class_name','')
118 class_file = parameters.get('class_file','')
119 connection_hook = parameters.get('connection_hook',None)
120 direct = parameters.get('allow_simple_one_argument_traversal', None)
122 self.manage_edit(title, connection_id, arguments, template=data)
124 self.manage_advanced(max_rows,
125 max_cache,
126 cache_time,
127 class_name,
128 class_file,
129 connection_hook=connection_hook,
130 direct=direct)
132 # do we need to do anything on reparse?
135 if Globals.DevelopmentMode:
136 # Provide an opportunity to update the properties.
137 def __of__(self, parent):
138 try:
139 self = ImplicitAcquisitionWrapper(self, parent)
140 self._updateFromFS()
141 return self
142 except:
143 logger.error("Error during __of__", exc_info=True)
144 raise
146 Globals.InitializeClass(FSZSQLMethod)
148 registerFileExtension('zsql', FSZSQLMethod)
149 registerMetaType('Z SQL Method', FSZSQLMethod)