vendor/CMF/1.6.3/CMFCore

view FSPropertiesObject.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) 2001 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 properties that come from the filesystem.
15 $Id$
16 """
17 import Globals
18 from AccessControl import ClassSecurityInfo
19 from Acquisition import ImplicitAcquisitionWrapper
20 from OFS.Folder import Folder
21 from OFS.PropertyManager import PropertyManager
22 from ZPublisher.Converters import get_converter
24 from DirectoryView import registerFileExtension
25 from DirectoryView import registerMetaType
26 from FSObject import FSObject
27 from permissions import ViewManagementScreens
28 from utils import _dtmldir
29 from utils import expandpath
32 class FSPropertiesObject (FSObject, PropertyManager):
33 """FSPropertiesObjects simply hold properties."""
35 meta_type = 'Filesystem Properties Object'
37 manage_options = ({'label':'Customize', 'action':'manage_main'},)
39 security = ClassSecurityInfo()
41 security.declareProtected(ViewManagementScreens, 'manage_main')
42 manage_main = Globals.DTMLFile('custprops', _dtmldir)
44 # Declare all (inherited) mutating methods private.
45 security.declarePrivate('manage_addProperty')
46 security.declarePrivate('manage_editProperties')
47 security.declarePrivate('manage_delProperties')
48 security.declarePrivate('manage_changeProperties')
49 security.declarePrivate('manage_propertiesForm')
50 security.declarePrivate('manage_propertyTypeForm')
51 security.declarePrivate('manage_changePropertyTypes')
53 security.declareProtected(ViewManagementScreens, 'manage_doCustomize')
54 def manage_doCustomize(self, folder_path, RESPONSE=None):
55 """Makes a ZODB Based clone with the same data.
57 Calls _createZODBClone for the actual work.
58 """
59 # Overridden here to provide a different redirect target.
61 FSObject.manage_doCustomize(self, folder_path, RESPONSE)
63 if RESPONSE is not None:
64 fpath = tuple(folder_path.split('/'))
65 folder = self.restrictedTraverse(fpath)
66 RESPONSE.redirect('%s/%s/manage_propertiesForm' % (
67 folder.absolute_url(), self.getId()))
69 def _createZODBClone(self):
70 """Create a ZODB (editable) equivalent of this object."""
71 # Create a Folder to hold the properties.
72 obj = Folder()
73 obj.id = self.getId()
74 map = []
75 for p in self._properties:
76 # This should be secure since the properties come
77 # from the filesystem.
78 setattr(obj, p['id'], getattr(self, p['id']))
79 map.append({'id': p['id'],
80 'type': p['type'],
81 'mode': 'wd',})
82 obj._properties = tuple(map)
84 return obj
86 def _readFile(self, reparse):
87 """Read the data from the filesystem.
89 Read the file (indicated by exandpath(self._filepath), and parse the
90 data if necessary.
91 """
92 fp = expandpath(self._filepath)
94 file = open(fp, 'r') # not 'rb', as this is a text file!
95 try:
96 lines = file.readlines()
97 finally:
98 file.close()
100 map = []
101 lino=0
103 for line in lines:
105 lino = lino + 1
106 line = line.strip()
108 if not line or line[0] == '#':
109 continue
111 try:
112 propname, proptv = line.split(':',1)
113 #XXX multi-line properties?
114 proptype, propvstr = proptv.split( '=', 1 )
115 propname = propname.strip()
116 proptype = proptype.strip()
117 propvstr = propvstr.strip()
118 converter = get_converter( proptype, lambda x: x )
119 propvalue = converter( propvstr )
120 # Should be safe since we're loading from
121 # the filesystem.
122 setattr(self, propname, propvalue)
123 map.append({'id':propname,
124 'type':proptype,
125 'mode':'',
126 'default_value':propvalue,
127 })
128 except:
129 raise ValueError, ( 'Error processing line %s of %s:\n%s'
130 % (lino,fp,line) )
131 self._properties = tuple(map)
133 if Globals.DevelopmentMode:
134 # Provide an opportunity to update the properties.
135 def __of__(self, parent):
136 self = ImplicitAcquisitionWrapper(self, parent)
137 self._updateFromFS()
138 return self
140 Globals.InitializeClass(FSPropertiesObject)
142 registerFileExtension('props', FSPropertiesObject)
143 registerMetaType('Properties Object', FSPropertiesObject)