entrypickle.py :  » Blog » PyBlosxom » pyblosxom-1.5-rc1 » Pyblosxom » cache » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Blog » PyBlosxom 
PyBlosxom » pyblosxom 1.5 rc1 » Pyblosxom » cache » entrypickle.py
#######################################################################
# This file is part of PyBlosxom.
#
# Copyright (c) 2003-2006 Wari Wahab
# Copyright (c) 2003-2010 Will Kahn-Greene
#
# PyBlosxom is distributed under the MIT license.  See the file
# LICENSE for distribution details.
#######################################################################
"""
This cache driver creates pickled data as cache in a directory.

To use this driver, add the following configuration options in your config.py

py['cacheDriver'] = 'entrypickle'
py['cacheConfig'] = '/path/to/a/cache/directory'

If successful, you will see the cache directory filled up with files that ends
with .entryplugin extention in the drectory.
"""

from Pyblosxom import tools
from Pyblosxom.cache.base import BlosxomCacheBase

import cPickle as pickle
import os
from os import makedirs
from os.path import normpath,dirname,exists,abspath

class BlosxomCache(BlosxomCacheBase):
    """
    This cache stores each entry as a separate pickle file of the
    entry's contents.
    """
    def __init__(self, req, config):
        """
        Takes in a PyBlosxom request object and a configuration string
        which determines where to store the pickle files.
        """
        BlosxomCacheBase.__init__(self, req, config)
        self._cachefile = ""

    def load(self, entryid):
        """
        Takes an entryid and keeps track of the filename.  We only
        open the file when it's requested with getEntry.
        """
        BlosxomCacheBase.load(self, entryid)
        filename = os.path.join(self._config, entryid.replace('/', '_'))
        self._cachefile = filename + '.entrypickle'

    def getEntry(self):
        """
        Open the pickle file and return the data therein.  If this
        fails, then we return None.
        """
        filep = None
        try:
            filep = open(self._cachefile, 'rb')
            data = pickle.load(filep)
            filep.close()
            return data
        except IOError:
            return None

        if filep:
            filep.close()


    def isCached(self):
        """
        Check to see if the file is updated.
        """
        return os.path.isfile(self._cachefile) and \
            os.stat(self._cachefile)[8] >= os.stat(self._entryid)[8]


    def saveEntry(self, entrydata):
        """
        Save the data in the entry object to a pickle file.
        """
        filep = None
        try:
            self.__makepath(self._cachefile)
            filep = open(self._cachefile, "w+b")
            entrydata.update({'realfilename': self._entryid})
            pickle.dump(entrydata, filep, 1)
        except IOError:
            pass

        if filep:
            filep.close()

    def rmEntry(self):
        """
        Removes the pickle file for this entry if it exists.
        """
        if os.path.isfile(self._cachefile):
            os.remove(self._cachefile)

    def keys(self):
        """
        Returns a list of the keys found in this entrypickle instance.
        This corresponds to the list of entries that are cached.

        @returns: list of full paths to entries that are cached
        @rtype: list of strings
        """
        import re
        keys = []
        cached = []
        if os.path.isdir(self._config):
            cached = tools.walk(self._request,
                                self._config,
                                1,
                                re.compile(r'.*\.entrypickle$'))
        for cache in cached:
            cache_data = pickle.load(open(cache))
            key = cache_data.get('realfilename', '')
            if not key and os.path.isfile(cache):
                os.remove(cache)
            self.load(key)
            if not self.isCached():
                self.rmEntry()
            else:
                keys.append(key)
        return keys

    def __makepath(self, path):
        """
        Creates the directory and all parent directories for a
        specified path.

        @param path: the path to create
        @type  path: string

        @returns: the normalized absolute path
        @rtype: string
        """
        dpath = normpath(dirname(path))
        if not exists(dpath):
            makedirs(dpath)
        return normpath(abspath(path))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.