view wikked/cache.py @ 0:c946f4facfa2

Initial commit.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 10 Dec 2012 22:40:52 -0800
parents
children ef9cf617d76f
line wrap: on
line source

import os
import os.path

try:
    import simplejson as json
except ImportError:
    import json


class Cache(object):
    def __init__(self, root):
        self.cache_dir = root

    def read(self, url, time):
        path, valid = self._getCachePathAndValidity(url, time)
        if valid:
            with open(path, 'r') as f:
                return json.load(f)
        return None

    def write(self, url, data):
        path = self._getCachePath(url)
        if not os.path.isdir(os.path.dirname(path)):
            os.makedirs(os.path.dirname(path))
        with open(path, 'w') as f:
            json.dump(data, f)

    def remove(self, url):
        path = self._getCachePath(url)
        if os.path.isfile(path):
            os.remove(path)

    def getTime(self, url):
        path = self._getCachePath(url)
        if not os.path.isfile(path):
            return None
        return os.path.getmtime(path)
        
    def _getCachePath(self, url):
        return os.path.join(self.cache_dir, url)

    def _getCachePathAndValidity(self, url, time):
        cache_path = self._getCachePath(url)
        if not os.path.isfile(cache_path):
            return cache_path, False

        if time >= os.path.getmtime(cache_path):
            return cache_path, False

        return cache_path, True