File: FSZSQLMethod.py

package info (click to toggle)
zope-cmf 1.3.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,540 kB
  • ctags: 2,579
  • sloc: python: 16,976; sh: 51; makefile: 41
file content (138 lines) | stat: -rw-r--r-- 4,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright (c) 2001 New Information Paradigms Ltd
#
# This Software is released under the MIT License:
# http://www.opensource.org/licenses/mit-license.html
# See license.txt for more details.
#
# $Id: FSZSQLMethod.py,v 1.2.36.4 2003/09/01 16:09:22 tseaver Exp $

""" (not yet)Customizable ZSQL methods that come from the filesystem.

$Id: FSZSQLMethod.py,v 1.2.36.4 2003/09/01 16:09:22 tseaver Exp $
"""

import Globals
from AccessControl import ClassSecurityInfo
from zLOG import LOG,ERROR

from Products.CMFCore.CMFCorePermissions import View, ViewManagementScreens
from Products.CMFCore.DirectoryView import registerFileExtension, registerMetaType, expandpath
from Products.CMFCore.FSObject import FSObject

from Products.ZSQLMethods.SQL import SQL

from utils import _dtmldir

import Acquisition

class FSZSQLMethod(SQL, FSObject):
    """FSZSQLMethods act like Z SQL Methods but are not directly
    modifiable from the management interface."""

    meta_type = 'Filesystem Z SQL Method'

    manage_options=(
        (
            {'label':'Customize', 'action':'manage_customise'},
            {'label':'Test', 'action':'manage_testForm',
             'help':('ZSQLMethods','Z-SQL-Method_Test.stx')},
            )
        )

    # Use declarative security
    security = ClassSecurityInfo()
    
    security.declareObjectProtected(View)
    
    # Make mutators private
    security.declarePrivate('manage_main','manage_edit','manage_advanced','manage_advancedForm')
    manage=None
    
    security.declareProtected(ViewManagementScreens, 'manage_customise')
    manage_customise = Globals.DTMLFile('custzsql', _dtmldir)

    def __init__(self, id, filepath, fullname=None, properties=None):
        FSObject.__init__(self, id, filepath, fullname, properties)

    def _createZODBClone(self):
        """Create a ZODB (editable) equivalent of this object."""
        # I guess it's bad to 'reach inside' ourselves like this,
        # but Z SQL Methods don't have accessor methdods ;-)
        s = SQL(self.id,
                self.title,
                self.connection_id,
                self.arguments_src,
                self.src)
        s.manage_advanced(self.max_rows_,
                          self.max_cache_,
                          self.cache_time_,
                          '',
                          '')
        return s

    def _readFile(self, reparse):
        fp = expandpath(self._filepath)
        file = open(fp, 'r')    # not 'rb', as this is a text file!
        try:
            data = file.read()
        finally: file.close()

        # parse parameters
        parameters={}
        start = data.find('<dtml-comment>')
        end   = data.find('</dtml-comment>')
        if start==-1 or end==-1 or start>end:
            raise ValueError,'Could not find parameter block'
        block = data[start+14:end]

        for line in block.split('\n'):
            pair = line.split(':',1)
            if len(pair)!=2:
                continue
            parameters[pair[0].strip().lower()]=pair[1].strip()

        # check for required an optional parameters
        try:            
            title =         parameters.get('title','')
            connection_id = parameters.get('connection id',parameters['connection_id'])
            arguments =     parameters.get('arguments','')
            max_rows =      parameters.get('max_rows',1000)
            max_cache =     parameters.get('max_cache',100)
            cache_time =    parameters.get('cache_time',0)            
        except KeyError,e:
            raise ValueError,"The '%s' parameter is required but was not supplied" % e
        
        self.manage_edit(title,
                         connection_id,
                         arguments,
                         template=data)

        self.manage_advanced(max_rows,
                             max_cache,
                             cache_time,
                             '', # don't really see any point in allowing
                             '') # brain specification...

        # do we need to do anything on reparse?


    if Globals.DevelopmentMode:
        # Provide an opportunity to update the properties.
        def __of__(self, parent):
            try:
                self = Acquisition.ImplicitAcquisitionWrapper(self, parent)
                self._updateFromFS()
                return self
            except:
                from zLOG import LOG, ERROR
                import sys
                LOG('FS Z SQL Method',
                    ERROR,
                    'Error during __of__',
                    error=sys.exc_info())
                raise

Globals.InitializeClass(FSZSQLMethod)

registerFileExtension('zsql', FSZSQLMethod)
registerMetaType('Z SQL Method', FSZSQLMethod)