Add information about writting yourself rules, revert no_command changes

This commit is contained in:
nvbn
2015-04-17 17:01:30 +02:00
parent 1503dcf294
commit 1de9c5f77b
5 changed files with 82 additions and 16 deletions
@@ -1,25 +1,26 @@
from subprocess import Popen, PIPE
import re
from thefuck.utils import which
from thefuck.utils import which, wrap_settings
def _get_bin(settings):
return getattr(settings, 'command_not_found', '/usr/lib/command-not-found')
local_settings = {'command_not_found': '/usr/lib/command-not-found'}
def _get_output(command, settings):
name = command.script.split(' ')[command.script.startswith('sudo')]
check_script = '{} {}'.format(_get_bin(settings), name)
check_script = '{} {}'.format(settings.command_not_found, name)
result = Popen(check_script, shell=True, stderr=PIPE)
return result.stderr.read().decode()
@wrap_settings(local_settings)
def match(command, settings):
if which('apt-get') and which(_get_bin(settings)):
if which(settings.command_not_found):
output = _get_output(command, settings)
return "No command" in output and "from package" in output
@wrap_settings(local_settings)
def get_new_command(command, settings):
output = _get_output(command, settings)
broken_name = re.findall(r"No command '([^']*)' found",
+24
View File
@@ -1,7 +1,10 @@
from functools import wraps
import os
def which(program):
"""Returns `program` path or `None`."""
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
@@ -17,3 +20,24 @@ def which(program):
return exe_file
return None
def wrap_settings(params):
"""Adds default values to settings if it not presented.
Usage:
@wrap_settings({'apt': '/usr/bin/apt'})
def match(command, settings):
print(settings.apt)
"""
def decorator(fn):
@wraps(fn)
def wrapper(command, settings):
for key, val in params.items():
if not hasattr(settings, key):
setattr(settings, key, val)
return fn(command, settings)
return wrapper
return decorator