From 5737adc253be898aa2e328cfd1f70334812a21f8 Mon Sep 17 00:00:00 2001 From: pictuga Date: Sun, 3 Nov 2013 13:26:56 +0100 Subject: [PATCH] Improve Cache() Add the ability to create new Cache() objects from the current one (in the same directory). Allow the storage of int() (via str()). Don't store the key in the feed (useless, and not keeping it might make this service a little more less privacy-infrighting). --- morss.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/morss.py b/morss.py index 1da6355..e5a9f70 100644 --- a/morss.py +++ b/morss.py @@ -146,7 +146,7 @@ class Cache: return None def set(self, key, content): - self._cache[key] = b64encode(content or '') + self._cache[key] = b64encode(str(content) or '') def save(self): if len(self._cache) == 0: @@ -155,7 +155,6 @@ class Cache: out = [] for (key, bdata) in self._cache.iteritems(): out.append(str(key) + "\t" + bdata) - out.append("_key\t" + self._key) txt = "\n".join(out) if not os.path.exists(self._dir): @@ -170,6 +169,13 @@ class Cache: return time.time() - os.path.getmtime(self._file) < sec + def new(self, key): + """ Returns a Cache object in the same directory """ + if key != self._key: + return Cache(self._dir, key) + else: + return self + class SimpleDownload(urllib2.HTTPCookieProcessor): """ Custom urllib2 handler to download a page, using etag/last-modified headers,