mirror of
https://github.com/nvbn/thefuck.git
synced 2025-03-20 01:28:56 +00:00
Replaced print with sys.stdout.write
This commit is contained in:
parent
6975d30818
commit
9233086d2e
516
thefuck/types.py
516
thefuck/types.py
@ -1,258 +1,258 @@
|
|||||||
from imp import load_source
|
from imp import load_source
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from . import logs
|
from . import logs
|
||||||
from .shells import shell
|
from .shells import shell
|
||||||
from .conf import settings
|
from .conf import settings
|
||||||
from .const import DEFAULT_PRIORITY, ALL_ENABLED
|
from .const import DEFAULT_PRIORITY, ALL_ENABLED
|
||||||
from .exceptions import EmptyCommand
|
from .exceptions import EmptyCommand
|
||||||
from .utils import get_alias, format_raw_script
|
from .utils import get_alias, format_raw_script
|
||||||
from .output_readers import get_output
|
from .output_readers import get_output
|
||||||
|
|
||||||
|
|
||||||
class Command(object):
|
class Command(object):
|
||||||
"""Command that should be fixed."""
|
"""Command that should be fixed."""
|
||||||
|
|
||||||
def __init__(self, script, output):
|
def __init__(self, script, output):
|
||||||
"""Initializes command with given values.
|
"""Initializes command with given values.
|
||||||
|
|
||||||
:type script: basestring
|
:type script: basestring
|
||||||
:type output: basestring
|
:type output: basestring
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.script = script
|
self.script = script
|
||||||
self.output = output
|
self.output = output
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def stdout(self):
|
def stdout(self):
|
||||||
logs.warn('`stdout` is deprecated, please use `output` instead')
|
logs.warn('`stdout` is deprecated, please use `output` instead')
|
||||||
return self.output
|
return self.output
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def stderr(self):
|
def stderr(self):
|
||||||
logs.warn('`stderr` is deprecated, please use `output` instead')
|
logs.warn('`stderr` is deprecated, please use `output` instead')
|
||||||
return self.output
|
return self.output
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def script_parts(self):
|
def script_parts(self):
|
||||||
if not hasattr(self, '_script_parts'):
|
if not hasattr(self, '_script_parts'):
|
||||||
try:
|
try:
|
||||||
self._script_parts = shell.split_command(self.script)
|
self._script_parts = shell.split_command(self.script)
|
||||||
except Exception:
|
except Exception:
|
||||||
logs.debug(u"Can't split command script {} because:\n {}".format(
|
logs.debug(u"Can't split command script {} because:\n {}".format(
|
||||||
self, sys.exc_info()))
|
self, sys.exc_info()))
|
||||||
self._script_parts = []
|
self._script_parts = []
|
||||||
|
|
||||||
return self._script_parts
|
return self._script_parts
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, Command):
|
if isinstance(other, Command):
|
||||||
return (self.script, self.output) == (other.script, other.output)
|
return (self.script, self.output) == (other.script, other.output)
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return u'Command(script={}, output={})'.format(
|
return u'Command(script={}, output={})'.format(
|
||||||
self.script, self.output)
|
self.script, self.output)
|
||||||
|
|
||||||
def update(self, **kwargs):
|
def update(self, **kwargs):
|
||||||
"""Returns new command with replaced fields.
|
"""Returns new command with replaced fields.
|
||||||
|
|
||||||
:rtype: Command
|
:rtype: Command
|
||||||
|
|
||||||
"""
|
"""
|
||||||
kwargs.setdefault('script', self.script)
|
kwargs.setdefault('script', self.script)
|
||||||
kwargs.setdefault('output', self.output)
|
kwargs.setdefault('output', self.output)
|
||||||
return Command(**kwargs)
|
return Command(**kwargs)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_raw_script(cls, raw_script):
|
def from_raw_script(cls, raw_script):
|
||||||
"""Creates instance of `Command` from a list of script parts.
|
"""Creates instance of `Command` from a list of script parts.
|
||||||
|
|
||||||
:type raw_script: [basestring]
|
:type raw_script: [basestring]
|
||||||
:rtype: Command
|
:rtype: Command
|
||||||
:raises: EmptyCommand
|
:raises: EmptyCommand
|
||||||
|
|
||||||
"""
|
"""
|
||||||
script = format_raw_script(raw_script)
|
script = format_raw_script(raw_script)
|
||||||
if not script:
|
if not script:
|
||||||
raise EmptyCommand
|
raise EmptyCommand
|
||||||
|
|
||||||
expanded = shell.from_shell(script)
|
expanded = shell.from_shell(script)
|
||||||
output = get_output(script, expanded)
|
output = get_output(script, expanded)
|
||||||
return cls(expanded, output)
|
return cls(expanded, output)
|
||||||
|
|
||||||
|
|
||||||
class Rule(object):
|
class Rule(object):
|
||||||
"""Rule for fixing commands."""
|
"""Rule for fixing commands."""
|
||||||
|
|
||||||
def __init__(self, name, match, get_new_command,
|
def __init__(self, name, match, get_new_command,
|
||||||
enabled_by_default, side_effect,
|
enabled_by_default, side_effect,
|
||||||
priority, requires_output):
|
priority, requires_output):
|
||||||
"""Initializes rule with given fields.
|
"""Initializes rule with given fields.
|
||||||
|
|
||||||
:type name: basestring
|
:type name: basestring
|
||||||
:type match: (Command) -> bool
|
:type match: (Command) -> bool
|
||||||
:type get_new_command: (Command) -> (basestring | [basestring])
|
:type get_new_command: (Command) -> (basestring | [basestring])
|
||||||
:type enabled_by_default: boolean
|
:type enabled_by_default: boolean
|
||||||
:type side_effect: (Command, basestring) -> None
|
:type side_effect: (Command, basestring) -> None
|
||||||
:type priority: int
|
:type priority: int
|
||||||
:type requires_output: bool
|
:type requires_output: bool
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.match = match
|
self.match = match
|
||||||
self.get_new_command = get_new_command
|
self.get_new_command = get_new_command
|
||||||
self.enabled_by_default = enabled_by_default
|
self.enabled_by_default = enabled_by_default
|
||||||
self.side_effect = side_effect
|
self.side_effect = side_effect
|
||||||
self.priority = priority
|
self.priority = priority
|
||||||
self.requires_output = requires_output
|
self.requires_output = requires_output
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, Rule):
|
if isinstance(other, Rule):
|
||||||
return ((self.name, self.match, self.get_new_command,
|
return ((self.name, self.match, self.get_new_command,
|
||||||
self.enabled_by_default, self.side_effect,
|
self.enabled_by_default, self.side_effect,
|
||||||
self.priority, self.requires_output)
|
self.priority, self.requires_output)
|
||||||
== (other.name, other.match, other.get_new_command,
|
== (other.name, other.match, other.get_new_command,
|
||||||
other.enabled_by_default, other.side_effect,
|
other.enabled_by_default, other.side_effect,
|
||||||
other.priority, other.requires_output))
|
other.priority, other.requires_output))
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'Rule(name={}, match={}, get_new_command={}, ' \
|
return 'Rule(name={}, match={}, get_new_command={}, ' \
|
||||||
'enabled_by_default={}, side_effect={}, ' \
|
'enabled_by_default={}, side_effect={}, ' \
|
||||||
'priority={}, requires_output)'.format(
|
'priority={}, requires_output)'.format(
|
||||||
self.name, self.match, self.get_new_command,
|
self.name, self.match, self.get_new_command,
|
||||||
self.enabled_by_default, self.side_effect,
|
self.enabled_by_default, self.side_effect,
|
||||||
self.priority, self.requires_output)
|
self.priority, self.requires_output)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_path(cls, path):
|
def from_path(cls, path):
|
||||||
"""Creates rule instance from path.
|
"""Creates rule instance from path.
|
||||||
|
|
||||||
:type path: pathlib.Path
|
:type path: pathlib.Path
|
||||||
:rtype: Rule
|
:rtype: Rule
|
||||||
|
|
||||||
"""
|
"""
|
||||||
name = path.name[:-3]
|
name = path.name[:-3]
|
||||||
with logs.debug_time(u'Importing rule: {};'.format(name)):
|
with logs.debug_time(u'Importing rule: {};'.format(name)):
|
||||||
rule_module = load_source(name, str(path))
|
rule_module = load_source(name, str(path))
|
||||||
priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY)
|
priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY)
|
||||||
return cls(name, rule_module.match,
|
return cls(name, rule_module.match,
|
||||||
rule_module.get_new_command,
|
rule_module.get_new_command,
|
||||||
getattr(rule_module, 'enabled_by_default', True),
|
getattr(rule_module, 'enabled_by_default', True),
|
||||||
getattr(rule_module, 'side_effect', None),
|
getattr(rule_module, 'side_effect', None),
|
||||||
settings.priority.get(name, priority),
|
settings.priority.get(name, priority),
|
||||||
getattr(rule_module, 'requires_output', True))
|
getattr(rule_module, 'requires_output', True))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_enabled(self):
|
def is_enabled(self):
|
||||||
"""Returns `True` when rule enabled.
|
"""Returns `True` when rule enabled.
|
||||||
|
|
||||||
:rtype: bool
|
:rtype: bool
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if self.name in settings.exclude_rules:
|
if self.name in settings.exclude_rules:
|
||||||
return False
|
return False
|
||||||
elif self.name in settings.rules:
|
elif self.name in settings.rules:
|
||||||
return True
|
return True
|
||||||
elif self.enabled_by_default and ALL_ENABLED in settings.rules:
|
elif self.enabled_by_default and ALL_ENABLED in settings.rules:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def is_match(self, command):
|
def is_match(self, command):
|
||||||
"""Returns `True` if rule matches the command.
|
"""Returns `True` if rule matches the command.
|
||||||
|
|
||||||
:type command: Command
|
:type command: Command
|
||||||
:rtype: bool
|
:rtype: bool
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if command.output is None and self.requires_output:
|
if command.output is None and self.requires_output:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with logs.debug_time(u'Trying rule: {};'.format(self.name)):
|
with logs.debug_time(u'Trying rule: {};'.format(self.name)):
|
||||||
if self.match(command):
|
if self.match(command):
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
logs.rule_failed(self, sys.exc_info())
|
logs.rule_failed(self, sys.exc_info())
|
||||||
|
|
||||||
def get_corrected_commands(self, command):
|
def get_corrected_commands(self, command):
|
||||||
"""Returns generator with corrected commands.
|
"""Returns generator with corrected commands.
|
||||||
|
|
||||||
:type command: Command
|
:type command: Command
|
||||||
:rtype: Iterable[CorrectedCommand]
|
:rtype: Iterable[CorrectedCommand]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
new_commands = self.get_new_command(command)
|
new_commands = self.get_new_command(command)
|
||||||
if not isinstance(new_commands, list):
|
if not isinstance(new_commands, list):
|
||||||
new_commands = (new_commands,)
|
new_commands = (new_commands,)
|
||||||
for n, new_command in enumerate(new_commands):
|
for n, new_command in enumerate(new_commands):
|
||||||
yield CorrectedCommand(script=new_command,
|
yield CorrectedCommand(script=new_command,
|
||||||
side_effect=self.side_effect,
|
side_effect=self.side_effect,
|
||||||
priority=(n + 1) * self.priority)
|
priority=(n + 1) * self.priority)
|
||||||
|
|
||||||
|
|
||||||
class CorrectedCommand(object):
|
class CorrectedCommand(object):
|
||||||
"""Corrected by rule command."""
|
"""Corrected by rule command."""
|
||||||
|
|
||||||
def __init__(self, script, side_effect, priority):
|
def __init__(self, script, side_effect, priority):
|
||||||
"""Initializes instance with given fields.
|
"""Initializes instance with given fields.
|
||||||
|
|
||||||
:type script: basestring
|
:type script: basestring
|
||||||
:type side_effect: (Command, basestring) -> None
|
:type side_effect: (Command, basestring) -> None
|
||||||
:type priority: int
|
:type priority: int
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.script = script
|
self.script = script
|
||||||
self.side_effect = side_effect
|
self.side_effect = side_effect
|
||||||
self.priority = priority
|
self.priority = priority
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
"""Ignores `priority` field."""
|
"""Ignores `priority` field."""
|
||||||
if isinstance(other, CorrectedCommand):
|
if isinstance(other, CorrectedCommand):
|
||||||
return (other.script, other.side_effect) == \
|
return (other.script, other.side_effect) == \
|
||||||
(self.script, self.side_effect)
|
(self.script, self.side_effect)
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return (self.script, self.side_effect).__hash__()
|
return (self.script, self.side_effect).__hash__()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return u'CorrectedCommand(script={}, side_effect={}, priority={})'.format(
|
return u'CorrectedCommand(script={}, side_effect={}, priority={})'.format(
|
||||||
self.script, self.side_effect, self.priority)
|
self.script, self.side_effect, self.priority)
|
||||||
|
|
||||||
def _get_script(self):
|
def _get_script(self):
|
||||||
"""Returns fixed commands script.
|
"""Returns fixed commands script.
|
||||||
|
|
||||||
If `settings.repeat` is `True`, appends command with second attempt
|
If `settings.repeat` is `True`, appends command with second attempt
|
||||||
of running fuck in case fixed command fails again.
|
of running fuck in case fixed command fails again.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if settings.repeat:
|
if settings.repeat:
|
||||||
repeat_fuck = '{} --repeat {}--force-command {}'.format(
|
repeat_fuck = '{} --repeat {}--force-command {}'.format(
|
||||||
get_alias(),
|
get_alias(),
|
||||||
'--debug ' if settings.debug else '',
|
'--debug ' if settings.debug else '',
|
||||||
shell.quote(self.script))
|
shell.quote(self.script))
|
||||||
return shell.or_(self.script, repeat_fuck)
|
return shell.or_(self.script, repeat_fuck)
|
||||||
else:
|
else:
|
||||||
return self.script
|
return self.script
|
||||||
|
|
||||||
def run(self, old_cmd):
|
def run(self, old_cmd):
|
||||||
"""Runs command from rule for passed command.
|
"""Runs command from rule for passed command.
|
||||||
|
|
||||||
:type old_cmd: Command
|
:type old_cmd: Command
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if self.side_effect:
|
if self.side_effect:
|
||||||
self.side_effect(old_cmd, self.script)
|
self.side_effect(old_cmd, self.script)
|
||||||
if settings.alter_history:
|
if settings.alter_history:
|
||||||
shell.put_to_history(self.script)
|
shell.put_to_history(self.script)
|
||||||
# This depends on correct setting of PYTHONIOENCODING by the alias:
|
# This depends on correct setting of PYTHONIOENCODING by the alias:
|
||||||
logs.debug(u'PYTHONIOENCODING: {}'.format(
|
logs.debug(u'PYTHONIOENCODING: {}'.format(
|
||||||
os.environ.get('PYTHONIOENCODING', '!!not-set!!')))
|
os.environ.get('PYTHONIOENCODING', '!!not-set!!')))
|
||||||
|
|
||||||
print(self._get_script())
|
sys.stdout.write(self._get_script())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user