diff --git a/powerline/lib/memoize.py b/powerline/lib/memoize.py index 00dbeef8..7bf75c13 100644 --- a/powerline/lib/memoize.py +++ b/powerline/lib/memoize.py @@ -1,11 +1,5 @@ # -*- coding: utf-8 -*- -try: - import cPickle as pickle -except ImportError: - import pickle -import os -import tempfile import time @@ -13,11 +7,9 @@ class memoize(object): '''Memoization decorator with timeout.''' _cache = {} - def __init__(self, timeout, additional_key=None, persistent=False, persistent_file=None): + def __init__(self, timeout, additional_key=None): self.timeout = timeout self.additional_key = additional_key - self.persistent = persistent - self.persistent_file = persistent_file or os.path.join(tempfile.gettempdir(), 'powerline-cache') def __call__(self, func): def decorated_function(*args, **kwargs): @@ -25,27 +17,11 @@ class memoize(object): key = (func.__name__, args, tuple(kwargs.items()), self.additional_key()) else: key = (func.__name__, args, tuple(kwargs.items())) - if self.persistent: - try: - with open(self.persistent_file, 'rb') as fileobj: - self._cache = pickle.load(fileobj) - except (IOError, EOFError, ValueError): - self._cache = {} cached = self._cache.get(key, None) if cached is None or time.time() - cached['time'] > self.timeout: cached = self._cache[key] = { 'result': func(*args, **kwargs), 'time': time.time(), } - if self.persistent: - try: - with open(self.persistent_file, 'wb') as fileobj: - pickle.dump(self._cache, fileobj) - except IOError: - # Unable to write to file - pass - except TypeError: - # Unable to pickle function result - pass return cached['result'] return decorated_function diff --git a/powerline/segments/common.py b/powerline/segments/common.py index 5598b956..e25b985e 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -101,7 +101,7 @@ def date(format='%Y-%m-%d'): return datetime.now().strftime(format) -@memoize(600, persistent=True) +@memoize(600) def external_ip(query_url='http://ipv4.icanhazip.com/'): return _urllib_read(query_url).strip() @@ -123,7 +123,7 @@ def uptime(format='{days:02d}d {hours:02d}h {minutes:02d}m'): return format.format(days=int(days), hours=hours, minutes=minutes) -@memoize(1800, persistent=True) +@memoize(1800) def weather(unit='c', location_query=None): import json @@ -232,7 +232,7 @@ def virtualenv(): return os.path.basename(os.environ.get('VIRTUAL_ENV', '')) or None -@memoize(60, persistent=True) +@memoize(60) def email_imap_alert(username, password, server='imap.gmail.com', port=993, folder='INBOX'): import imaplib import re