mirror of
https://github.com/powerline/powerline.git
synced 2025-07-16 18:34:57 +02:00
Remove support for persistent memoization
A different form of persistent memoization should be reimplemented later. Refs #159.
This commit is contained in:
parent
260e40a544
commit
4cda38e214
@ -1,11 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
try:
|
|
||||||
import cPickle as pickle
|
|
||||||
except ImportError:
|
|
||||||
import pickle
|
|
||||||
import os
|
|
||||||
import tempfile
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
@ -13,11 +7,9 @@ class memoize(object):
|
|||||||
'''Memoization decorator with timeout.'''
|
'''Memoization decorator with timeout.'''
|
||||||
_cache = {}
|
_cache = {}
|
||||||
|
|
||||||
def __init__(self, timeout, additional_key=None, persistent=False, persistent_file=None):
|
def __init__(self, timeout, additional_key=None):
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.additional_key = additional_key
|
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 __call__(self, func):
|
||||||
def decorated_function(*args, **kwargs):
|
def decorated_function(*args, **kwargs):
|
||||||
@ -25,27 +17,11 @@ class memoize(object):
|
|||||||
key = (func.__name__, args, tuple(kwargs.items()), self.additional_key())
|
key = (func.__name__, args, tuple(kwargs.items()), self.additional_key())
|
||||||
else:
|
else:
|
||||||
key = (func.__name__, args, tuple(kwargs.items()))
|
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)
|
cached = self._cache.get(key, None)
|
||||||
if cached is None or time.time() - cached['time'] > self.timeout:
|
if cached is None or time.time() - cached['time'] > self.timeout:
|
||||||
cached = self._cache[key] = {
|
cached = self._cache[key] = {
|
||||||
'result': func(*args, **kwargs),
|
'result': func(*args, **kwargs),
|
||||||
'time': time.time(),
|
'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 cached['result']
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
@ -101,7 +101,7 @@ def date(format='%Y-%m-%d'):
|
|||||||
return datetime.now().strftime(format)
|
return datetime.now().strftime(format)
|
||||||
|
|
||||||
|
|
||||||
@memoize(600, persistent=True)
|
@memoize(600)
|
||||||
def external_ip(query_url='http://ipv4.icanhazip.com/'):
|
def external_ip(query_url='http://ipv4.icanhazip.com/'):
|
||||||
return _urllib_read(query_url).strip()
|
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)
|
return format.format(days=int(days), hours=hours, minutes=minutes)
|
||||||
|
|
||||||
|
|
||||||
@memoize(1800, persistent=True)
|
@memoize(1800)
|
||||||
def weather(unit='c', location_query=None):
|
def weather(unit='c', location_query=None):
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@ -232,7 +232,7 @@ def virtualenv():
|
|||||||
return os.path.basename(os.environ.get('VIRTUAL_ENV', '')) or None
|
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'):
|
def email_imap_alert(username, password, server='imap.gmail.com', port=993, folder='INBOX'):
|
||||||
import imaplib
|
import imaplib
|
||||||
import re
|
import re
|
||||||
|
Loading…
x
Reference in New Issue
Block a user