all repos — nasg @ f5c599cef923afe23974252f778dd4e4cb214b80

cache.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
import os
import json
import hashlib
import logging
import glob

class Cached(object):
    def __init__(self, hash='', text='', stime=0):

        if not os.path.isdir(glob.CACHE):
            os.mkdir(glob.CACHE)

        if hash:
            self._hbase = hash
        elif text:
            self._hbase = hashlib.sha1(text.encode('utf-8')).hexdigest()
        else:
            print("No identifier passed for Cached")
            raise

        self._cpath = os.path.join(glob.CACHE, self._hbase)
        self._stime = stime

        if os.path.isfile(self._cpath):
            self._ctime = os.stat(self._cpath)
        else:
            self._ctime = None

    def get(self):
        if not glob.CACHEENABLED:
            return None

        cached = ''
        if os.path.isfile(self._cpath):
            if self._stime and self._stime.st_mtime == self._ctime.st_mtime:
                logging.debug("Cache exists at %s; using it" % (self._cpath ))
                with open(self._cpath, 'r') as c:
                    cached = c.read()
                    c.close()
            # invalidate old
            elif self._stime and self._stime.st_mtime > self._ctime.st_mtime:
                logging.debug("invalidating cache at %s" % (self._cpath ))
                os.remove(self._cpath)

        return cached

    def set(self, content):
        if not glob.CACHEENABLED:
            return None

        with open(self._cpath, "w") as c:
            logging.debug("writing cache to %s" % (self._cpath ))
            c.write(content)
            c.close()
        if self._stime:
            os.utime(self._cpath, (self._stime.st_mtime, self._stime.st_mtime ))