Back to index

plone3  3.1.7
FSPropertiesObject.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 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 properties that come from the filesystem.
00014 
00015 $Id: FSPropertiesObject.py 77019 2007-06-24 19:01:14Z hannosch $
00016 """
00017 
00018 import Globals
00019 from AccessControl import ClassSecurityInfo
00020 from Acquisition import ImplicitAcquisitionWrapper
00021 from OFS.Folder import Folder
00022 from OFS.PropertyManager import PropertyManager
00023 from ZPublisher.Converters import get_converter
00024 
00025 from DirectoryView import registerFileExtension
00026 from DirectoryView import registerMetaType
00027 from FSObject import FSObject
00028 from permissions import ViewManagementScreens
00029 from utils import _dtmldir
00030 
00031 
00032 class FSPropertiesObject(FSObject, PropertyManager):
00033 
00034     """FSPropertiesObjects simply hold properties."""
00035 
00036     meta_type = 'Filesystem Properties Object'
00037 
00038     manage_options = ({'label':'Customize', 'action':'manage_main'},)
00039 
00040     security = ClassSecurityInfo()
00041 
00042     security.declareProtected(ViewManagementScreens, 'manage_main')
00043     manage_main = Globals.DTMLFile('custprops', _dtmldir)
00044 
00045     # Declare all (inherited) mutating methods private.
00046     security.declarePrivate('manage_addProperty')
00047     security.declarePrivate('manage_editProperties')
00048     security.declarePrivate('manage_delProperties')
00049     security.declarePrivate('manage_changeProperties')
00050     security.declarePrivate('manage_propertiesForm')
00051     security.declarePrivate('manage_propertyTypeForm')
00052     security.declarePrivate('manage_changePropertyTypes')
00053 
00054     security.declareProtected(ViewManagementScreens, 'manage_doCustomize')
00055     def manage_doCustomize(self, folder_path, RESPONSE=None, \
00056                            root=None, obj=None):
00057         """Makes a ZODB Based clone with the same data.
00058 
00059         Calls _createZODBClone for the actual work.
00060         """
00061         # Overridden here to provide a different redirect target.
00062 
00063         FSObject.manage_doCustomize(self, folder_path, RESPONSE, \
00064                                     root=root, obj=obj)
00065 
00066         if RESPONSE is not None:
00067             if folder_path == '.':
00068                 fpath = ()
00069             else:
00070                 fpath = tuple(folder_path.split('/'))
00071             folder = self.restrictedTraverse(fpath)
00072             RESPONSE.redirect('%s/%s/manage_propertiesForm' % (
00073                 folder.absolute_url(), self.getId()))
00074 
00075     def _createZODBClone(self):
00076         """Create a ZODB (editable) equivalent of this object."""
00077         # Create a Folder to hold the properties.
00078         obj = Folder()
00079         obj.id = self.getId()
00080         map = []
00081         for p in self._properties:
00082             # This should be secure since the properties come
00083             # from the filesystem.
00084             setattr(obj, p['id'], getattr(self, p['id']))
00085             map.append({'id': p['id'],
00086                         'type': p['type'],
00087                         'mode': 'wd',})
00088         obj._properties = tuple(map)
00089 
00090         return obj
00091 
00092     def _readFile(self, reparse):
00093         """Read the data from the filesystem.
00094         """
00095         file = open(self._filepath, 'r') # not 'rb', as this is a text file!
00096         try:
00097             lines = file.readlines()
00098         finally:
00099             file.close()
00100 
00101         map = []
00102         lino=0
00103 
00104         for line in lines:
00105 
00106             lino = lino + 1
00107             line = line.strip()
00108 
00109             if not line or line[0] == '#':
00110                 continue
00111 
00112             try:
00113                 propname, proptv = line.split(':',1)
00114                 #XXX multi-line properties?
00115                 proptype, propvstr = proptv.split( '=', 1 )
00116                 propname = propname.strip()
00117                 proptype = proptype.strip()
00118                 propvstr = propvstr.strip()
00119                 converter = get_converter( proptype, lambda x: x )
00120                 propvalue = converter( propvstr )
00121                 # Should be safe since we're loading from
00122                 # the filesystem.
00123                 setattr(self, propname, propvalue)
00124                 map.append({'id':propname,
00125                             'type':proptype,
00126                             'mode':'',
00127                             'default_value':propvalue,
00128                             })
00129             except:
00130                 raise ValueError, ( 'Error processing line %s of %s:\n%s'
00131                                   % (lino,self._filepath,line) )
00132         self._properties = tuple(map)
00133 
00134     if Globals.DevelopmentMode:
00135         # Provide an opportunity to update the properties.
00136         def __of__(self, parent):
00137             self = ImplicitAcquisitionWrapper(self, parent)
00138             self._updateFromFS()
00139             return self
00140 
00141 Globals.InitializeClass(FSPropertiesObject)
00142 
00143 registerFileExtension('props', FSPropertiesObject)
00144 registerMetaType('Properties Object', FSPropertiesObject)