Exemple #1
0
 def __init__(self, logger, path):
     self.logger = logger
     self.path = path
     self.locks = Locks()
Exemple #2
0
class DiskStorage(object):
    implements(IStorage)
    def __init__(self, logger, path):
        self.logger = logger
        self.path = path
        self.locks = Locks()
    
    @staticmethod
    def _safe_hash(string):
        sha1sum = sha1()
        sha1sum.update(string)
        return sha1sum.hexdigest()

    def _dirname(self, url):
        hashedname = self._safe_hash(url)
        return os.path.join(self.path, hashedname[0:2], hashedname[2:])

    def contents(self, url):
        subdir = self._dirname(url)
        if os.path.exists(subdir):
            for each in os.listdir(subdir):
                if each.lower().endswith(".body"):
                    continue
                yield os.path.join(subdir, each)

    def store(self, url, discriminators, expires, status, headers, **extras):
        subdir = self._dirname(url)
        os.makedirs(subdir)
        key = self._safe_hash(pickle.dumps(discriminators))
        filename = os.path.join(subdir, key)
        fp = open(filename, 'w')
        lock = self.locks.acquire(filename, fp)

        pickle.dump((discriminators, expires, status, headers, extras), fp)
        body_filename = filename + ".body"
        if os.path.exists(body_filename):
            os.unlink(body_filename)
        body = open(body_filename, "w")    
        
        fp.close()
        lock.release()

        class SimpleHandler:
            implements(IChunkHandler)
            def write(self, chunk):
                body.write(chunk)

            def close(self):
                body.close()

        return SimpleHandler()

    def fetch(self, url):
        for each in self.contents(url):
            fp = open(each, "r")
            lock = self.locks.acquire(each, fp)
            try:
                values = pickle.load(fp)
                (discrims, expires, status, headers, extras) = values
                body = open(each + ".body", "r")
                yield (discrims, expires, status, headers, body, extras)
            finally:
                lock.release()
                fp.close