[Zope-CVS] CVS: Products/FileCacheManager - permissions.py:1.1
FileCacheManager.py:1.11
Jens Vagelpohl
jens at dataflake.org
Sun Aug 15 07:44:24 EDT 2004
Update of /cvs-repository/Products/FileCacheManager
In directory cvs.zope.org:/tmp/cvs-serv31473
Modified Files:
FileCacheManager.py
Added Files:
permissions.py
Log Message:
- add permissions module
- checkpoint checkin for the TAL naming expression bit
=== Added File Products/FileCacheManager/permissions.py ===
##############################################################################
#
# Copyright (c) 2004 Chris McDonough, Paul Winkler, Jens Vagelpohl and
# Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
change_cache_managers = 'Change cache managers'
=== Products/FileCacheManager/FileCacheManager.py 1.10 => 1.11 ===
--- Products/FileCacheManager/FileCacheManager.py:1.10 Fri Aug 13 15:55:54 2004
+++ Products/FileCacheManager/FileCacheManager.py Sun Aug 15 07:43:53 2004
@@ -22,6 +22,7 @@
from thread import allocate_lock
from AccessControl import ClassSecurityInfo
+from AccessControl.SecurityManagement import getSecurityManager
from AccessControl.Permissions import view_management_screens
from Globals import InitializeClass
from BTrees import OOBTree
@@ -30,6 +31,11 @@
from Products.StandardCacheManagers.RAMCacheManager import RAMCache, \
CacheEntry, RAMCacheManager
from Acquisition import aq_base
+from Products.PageTemplates.Expressions import getEngine, \
+ SecureModuleImporter
+from Products.PageTemplates.TALES import CompilerError
+
+from permissions import change_cache_managers
caches = {}
@@ -43,16 +49,46 @@
self.next_cleanup = 0
self.setDir(path)
self._makeDirs()
+ self._naming_expr = None
def _fileName(self, ob):
""" Compute a filename based on an MD5 hash: doesn't preserve
human-readable path, but otherwise makes life much easier """
- phys_path = '/'.join(ob.getPhysicalPath())[1:]
- hashed = md5.new(phys_path).hexdigest()
- fname = os.path.join(self._dir, hashed[:2], hashed)
+ if self._naming_expr is not None:
+ expr_context = self._getExprContext(self, ob)
+ munged = self._naming_expr(expr_context)
+ fname = os.path.join(self._dir, munged)
+ else:
+ phys_path = '/'.join(ob.getPhysicalPath())[1:]
+ hashed = md5.new(phys_path).hexdigest()
+ fname = os.path.join(self._dir, hashed[:2], hashed)
return fname
+ def getExprContext(self, ob=None):
+ """ Create a context for evaluating the TAL naming expression """
+ return { 'user' : getSecurityManager.getAuthenticatedUser()
+ , 'request' : getattr(ob, 'REQUEST', None)
+ , 'modules' : SecureModuleImporter
+ , 'object' : ob
+ , 'object_url' : ob.absolute_url()
+ , 'nothing' : None
+ }
+
+ def setNamingExpression(self, naming_expression=''):
+ """ Set a new file naming expression """
+ msg = ''
+
+ if naming_expression:
+ try:
+ self._naming_expr = getEngine().compile(naming_expression)
+ except CompilerError:
+ msg = 'Invalid TAL expression "%s"' % naming_expression
+ else:
+ self._naming_expr = None
+
+ return msg
+
def getDir(self):
""" Retrieve the filesystem directory used for caching """
return self._dir
@@ -169,6 +205,7 @@
self.id = ob_id
self._dir = path
self.title = title
+ self._naming_expr_text = ''
self._settings = {
'threshold': 1000,
'cleanup_interval': 3600,
@@ -188,6 +225,7 @@
try:
cache.setDir(self._dir)
+ cache.setNamingExpression(self._naming_expr_text)
except ValueError:
pass
@@ -202,7 +240,7 @@
return cache.getDir()
- security.declareProtected('Change cache managers', 'setDir')
+ security.declareProtected(change_cache_managers, 'setDir')
def setDir(self, path):
""" Set the cache directory path """
cache = self.ZCacheManager_getCache()
@@ -213,18 +251,36 @@
except ValueError:
pass
- security.declareProtected('Change cache managers', 'manage_edit')
- def manage_edit(self, path='/tmp', REQUEST=None):
+ security.declareProtected(view_management_screens, 'getNamingExpression')
+ def getNamingExpression(self):
+ """ Retrieve the TAL expression for naming file system files """
+ return self._naming_expr_text
+
+ security.declareProtected(change_cache_managers, 'setNamingExpression')
+ def setNamingExpression(self, naming_expression_text=''):
+ """ Set a naming expression for filesystem files """
+ cache = self.ZCacheManager_getCache()
+ error = cache.setNamingExpression(naming_expression_text)
+ if not error:
+ self._naming_expr_text = naming_expression_text
+
+ return error
+
+ security.declareProtected(change_cache_managers, 'manage_edit')
+ def manage_edit(self, path='/tmp', naming_expression_text='', REQUEST=None):
""" Edit the filesystem-related values """
- if os.access(path, os.W_OK):
- msg = 'Changes saved.'
- self.setDir(path)
- else:
- msg = 'Invalid directory "%s"' % path
+ msg = self.setNamingExpression(naming_expression_text)
+
+ if not msg:
+ if os.access(path, os.W_OK):
+ msg = 'Changes saved.'
+ self.setDir(path)
+ else:
+ msg = 'Invalid directory "%s"' % path
if REQUEST is not None:
return self.manage_fs(self, REQUEST, manage_tabs_message=msg)
-
+
InitializeClass(FileCacheManager)
More information about the Zope-CVS
mailing list