#353 Cache aliases in a temporary file
This commit is contained in:
+3
-1
@@ -9,7 +9,7 @@ from subprocess import Popen, PIPE
|
||||
from time import time
|
||||
import io
|
||||
import os
|
||||
from .utils import DEVNULL, memoize
|
||||
from .utils import DEVNULL, memoize, cache
|
||||
|
||||
|
||||
class Generic(object):
|
||||
@@ -85,6 +85,7 @@ class Bash(Generic):
|
||||
return name, value
|
||||
|
||||
@memoize
|
||||
@cache('.bashrc', '.bash_profile')
|
||||
def get_aliases(self):
|
||||
proc = Popen(['bash', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL)
|
||||
return dict(
|
||||
@@ -169,6 +170,7 @@ class Zsh(Generic):
|
||||
return name, value
|
||||
|
||||
@memoize
|
||||
@cache('.zshrc')
|
||||
def get_aliases(self):
|
||||
proc = Popen(['zsh', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL)
|
||||
return dict(
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from difflib import get_close_matches
|
||||
from functools import wraps
|
||||
import shelve
|
||||
from decorator import decorator
|
||||
import tempfile
|
||||
|
||||
import os
|
||||
import pickle
|
||||
@@ -150,3 +152,40 @@ def for_app(*app_names):
|
||||
return False
|
||||
|
||||
return decorator(_for_app)
|
||||
|
||||
|
||||
def cache(*depends_on):
|
||||
"""Caches function result in temporary file.
|
||||
|
||||
Cache will be expired when modification date of files from `depends_on`
|
||||
will be changed.
|
||||
|
||||
Function wrapped in `cache` should be arguments agnostic.
|
||||
|
||||
"""
|
||||
def _get_mtime(name):
|
||||
path = os.path.join(os.path.expanduser('~'), name)
|
||||
try:
|
||||
return str(os.path.getmtime(path))
|
||||
except OSError:
|
||||
return '0'
|
||||
|
||||
@decorator
|
||||
def _cache(fn, *args, **kwargs):
|
||||
if cache.disabled:
|
||||
return fn(*args, **kwargs)
|
||||
|
||||
cache_path = os.path.join(tempfile.gettempdir(), '.thefuck-cache')
|
||||
key = '{}.{}'.format(fn.__module__, repr(fn).split('at')[0])
|
||||
|
||||
etag = '.'.join(_get_mtime(name) for name in depends_on)
|
||||
|
||||
with shelve.open(cache_path) as db:
|
||||
if db.get(key, {}).get('etag') == etag:
|
||||
return db[key]['value']
|
||||
else:
|
||||
value = fn(*args, **kwargs)
|
||||
db[key] = {'etag': etag, 'value': value}
|
||||
return value
|
||||
return _cache
|
||||
cache.disabled = False
|
||||
|
||||
Reference in New Issue
Block a user