mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-21 20:38:54 +00:00
Use decorator library
This commit is contained in:
parent
0c283ff2b8
commit
ebe53f0d18
2
setup.py
2
setup.py
@ -22,7 +22,7 @@ elif (3, 0) < version < (3, 3):
|
|||||||
|
|
||||||
VERSION = '2.8'
|
VERSION = '2.8'
|
||||||
|
|
||||||
install_requires = ['psutil', 'colorama', 'six']
|
install_requires = ['psutil', 'colorama', 'six', 'decorator']
|
||||||
extras_require = {':python_version<"3.4"': ['pathlib']}
|
extras_require = {':python_version<"3.4"': ['pathlib']}
|
||||||
|
|
||||||
setup(name='thefuck',
|
setup(name='thefuck',
|
||||||
|
@ -13,6 +13,8 @@ from tests.utils import Command
|
|||||||
(False, 'sudo ls', 'ls', False),
|
(False, 'sudo ls', 'ls', False),
|
||||||
(False, 'ls', 'ls', False)])
|
(False, 'ls', 'ls', False)])
|
||||||
def test_sudo_support(return_value, command, called, result):
|
def test_sudo_support(return_value, command, called, result):
|
||||||
fn = Mock(return_value=return_value, __name__='')
|
def fn(command, settings):
|
||||||
|
assert command == Command(called)
|
||||||
|
return return_value
|
||||||
|
|
||||||
assert sudo_support(fn)(Command(command), None) == result
|
assert sudo_support(fn)(Command(command), None) == result
|
||||||
fn.assert_called_once_with(Command(called), None)
|
|
||||||
|
@ -1,34 +1,32 @@
|
|||||||
from functools import wraps
|
|
||||||
import re
|
import re
|
||||||
from shlex import split
|
from shlex import split
|
||||||
|
from decorator import decorator
|
||||||
from ..types import Command
|
from ..types import Command
|
||||||
from ..utils import quote, for_app
|
from ..utils import quote, is_app
|
||||||
|
|
||||||
|
|
||||||
def git_support(fn):
|
@decorator
|
||||||
|
def git_support(fn, command, settings):
|
||||||
"""Resolves git aliases and supports testing for both git and hub."""
|
"""Resolves git aliases and supports testing for both git and hub."""
|
||||||
# supports GitHub's `hub` command
|
# supports GitHub's `hub` command
|
||||||
# which is recommended to be used with `alias git=hub`
|
# which is recommended to be used with `alias git=hub`
|
||||||
# but at this point, shell aliases have already been resolved
|
# but at this point, shell aliases have already been resolved
|
||||||
|
if not is_app(command, 'git', 'hub'):
|
||||||
|
return False
|
||||||
|
|
||||||
@for_app('git', 'hub')
|
# perform git aliases expansion
|
||||||
@wraps(fn)
|
if 'trace: alias expansion:' in command.stderr:
|
||||||
def wrapper(command, settings):
|
search = re.search("trace: alias expansion: ([^ ]*) => ([^\n]*)",
|
||||||
# perform git aliases expansion
|
command.stderr)
|
||||||
if 'trace: alias expansion:' in command.stderr:
|
alias = search.group(1)
|
||||||
search = re.search("trace: alias expansion: ([^ ]*) => ([^\n]*)",
|
|
||||||
command.stderr)
|
|
||||||
alias = search.group(1)
|
|
||||||
|
|
||||||
# by default git quotes everything, for example:
|
# by default git quotes everything, for example:
|
||||||
# 'commit' '--amend'
|
# 'commit' '--amend'
|
||||||
# which is surprising and does not allow to easily test for
|
# which is surprising and does not allow to easily test for
|
||||||
# eg. 'git commit'
|
# eg. 'git commit'
|
||||||
expansion = ' '.join(map(quote, split(search.group(2))))
|
expansion = ' '.join(map(quote, split(search.group(2))))
|
||||||
new_script = command.script.replace(alias, expansion)
|
new_script = command.script.replace(alias, expansion)
|
||||||
|
|
||||||
command = Command._replace(command, script=new_script)
|
command = Command._replace(command, script=new_script)
|
||||||
|
|
||||||
return fn(command, settings)
|
return fn(command, settings)
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
@ -1,24 +1,22 @@
|
|||||||
from functools import wraps
|
|
||||||
import six
|
import six
|
||||||
|
from decorator import decorator
|
||||||
from ..types import Command
|
from ..types import Command
|
||||||
|
|
||||||
|
|
||||||
def sudo_support(fn):
|
@decorator
|
||||||
|
def sudo_support(fn, command, settings):
|
||||||
"""Removes sudo before calling fn and adds it after."""
|
"""Removes sudo before calling fn and adds it after."""
|
||||||
@wraps(fn)
|
if not command.script.startswith('sudo '):
|
||||||
def wrapper(command, settings):
|
return fn(command, settings)
|
||||||
if not command.script.startswith('sudo '):
|
|
||||||
return fn(command, settings)
|
|
||||||
|
|
||||||
result = fn(Command(command.script[5:],
|
result = fn(Command(command.script[5:],
|
||||||
command.stdout,
|
command.stdout,
|
||||||
command.stderr),
|
command.stderr),
|
||||||
settings)
|
settings)
|
||||||
|
|
||||||
if result and isinstance(result, six.string_types):
|
if result and isinstance(result, six.string_types):
|
||||||
return u'sudo {}'.format(result)
|
return u'sudo {}'.format(result)
|
||||||
elif isinstance(result, list):
|
elif isinstance(result, list):
|
||||||
return [u'sudo {}'.format(x) for x in result]
|
return [u'sudo {}'.format(x) for x in result]
|
||||||
else:
|
else:
|
||||||
return result
|
return result
|
||||||
return wrapper
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from difflib import get_close_matches
|
from difflib import get_close_matches
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from decorator import decorator
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
@ -47,12 +48,9 @@ def wrap_settings(params):
|
|||||||
print(settings.apt)
|
print(settings.apt)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def decorator(fn):
|
def _wrap_settings(fn, command, settings):
|
||||||
@wraps(fn)
|
return fn(command, settings.update(**params))
|
||||||
def wrapper(command, settings):
|
return decorator(_wrap_settings)
|
||||||
return fn(command, settings.update(**params))
|
|
||||||
return wrapper
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
|
|
||||||
def memoize(fn):
|
def memoize(fn):
|
||||||
@ -110,11 +108,9 @@ def replace_argument(script, from_, to):
|
|||||||
u' {} '.format(from_), u' {} '.format(to), 1)
|
u' {} '.format(from_), u' {} '.format(to), 1)
|
||||||
|
|
||||||
|
|
||||||
def eager(fn):
|
@decorator
|
||||||
@wraps(fn)
|
def eager(fn, *args, **kwargs):
|
||||||
def wrapper(*args, **kwargs):
|
return list(fn(*args, **kwargs))
|
||||||
return list(fn(*args, **kwargs))
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
@eager
|
@eager
|
||||||
@ -146,13 +142,10 @@ def is_app(command, *app_names):
|
|||||||
|
|
||||||
def for_app(*app_names):
|
def for_app(*app_names):
|
||||||
"""Specifies that matching script is for on of app names."""
|
"""Specifies that matching script is for on of app names."""
|
||||||
def decorator(fn):
|
def _for_app(fn, command, settings):
|
||||||
@wraps(fn)
|
if is_app(command, *app_names):
|
||||||
def wrapper(command, settings):
|
return fn(command, settings)
|
||||||
if is_app(command, *app_names):
|
else:
|
||||||
return fn(command, settings)
|
return False
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return wrapper
|
return decorator(_for_app)
|
||||||
return decorator
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user