Merge pull request #411 from scorphus/unicode

Support non-ascii content in Python 2
This commit is contained in:
Vladimir Iakovlev
2015-11-25 20:41:20 +08:00
15 changed files with 159 additions and 64 deletions
+1 -1
View File
@@ -55,7 +55,7 @@ def organize_commands(corrected_commands):
key=lambda corrected_command: corrected_command.priority)
logs.debug('Corrected commands: '.format(
', '.join(str(cmd) for cmd in [first_command] + sorted_commands)))
', '.join(u'{}'.format(cmd) for cmd in [first_command] + sorted_commands)))
for command in sorted_commands:
yield command
+6 -2
View File
@@ -1,6 +1,7 @@
"""Attempts to spellcheck and correct failed cd commands"""
import os
import six
from difflib import get_close_matches
from thefuck.specific.sudo import sudo_support
from thefuck.rules import cd_mkdir
@@ -36,7 +37,10 @@ def get_new_command(command):
dest = command.script_parts[1].split(os.sep)
if dest[-1] == '':
dest = dest[:-1]
cwd = os.getcwd()
if six.PY2:
cwd = os.getcwdu()
else:
cwd = os.getcwd()
for directory in dest:
if directory == ".":
continue
@@ -48,7 +52,7 @@ def get_new_command(command):
cwd = os.path.join(cwd, best_matches[0])
else:
return cd_mkdir.get_new_command(command)
return 'cd "{0}"'.format(cwd)
return u'cd "{0}"'.format(cwd)
enabled_by_default = True
+2 -2
View File
@@ -19,7 +19,7 @@ def _zip_file(command):
if c.endswith('.zip'):
return c
else:
return '{}.zip'.format(c)
return u'{}.zip'.format(c)
@for_app('unzip')
@@ -29,7 +29,7 @@ def match(command):
def get_new_command(command):
return '{} -d {}'.format(command.script, quote(_zip_file(command)[:-4]))
return u'{} -d {}'.format(command.script, quote(_zip_file(command)[:-4]))
def side_effect(old_cmd, command):
+2 -2
View File
@@ -38,7 +38,7 @@ patterns = (
def _make_pattern(pattern):
pattern = pattern.replace('{file}', '(?P<file>[^:\n]+)') \
.replace('{line}', '(?P<line>[0-9]+)') \
.replace('{col}', '(?P<col>[0-9]+)')
.replace('{col}', '(?P<col>[0-9]+)')
return re.compile(pattern, re.MULTILINE)
patterns = [_make_pattern(p).search for p in patterns]
@@ -58,7 +58,7 @@ def match(command):
return _search(command.stderr) or _search(command.stdout)
@default_settings({'fixlinecmd': '{editor} {file} +{line}',
@default_settings({'fixlinecmd': u'{editor} {file} +{line}',
'fixcolcmd': None})
def get_new_command(command):
m = _search(command.stderr) or _search(command.stdout)
+1 -1
View File
@@ -7,4 +7,4 @@ def match(command):
def get_new_command(command):
return 'grep -r {}'.format(command.script[5:])
return u'grep -r {}'.format(command.script[5:])
+1 -1
View File
@@ -13,6 +13,6 @@ def get_new_command(command):
command.stderr)
old_cmd = cmd.group(1)
suggestions = [cmd.strip() for cmd in cmd.group(2).split(',')]
suggestions = [c.strip() for c in cmd.group(2).split(',')]
return replace_command(command, old_cmd, suggestions)
+23 -11
View File
@@ -51,7 +51,11 @@ class Generic(object):
history_file_name = self._get_history_file_name()
if os.path.isfile(history_file_name):
with open(history_file_name, 'a') as history:
history.write(self._get_history_line(command_script))
entry = self._get_history_line(command_script)
if six.PY2:
history.write(entry.encode('utf-8'))
else:
history.write(entry)
def get_history(self):
"""Returns list of history entries."""
@@ -78,6 +82,8 @@ class Generic(object):
def split_command(self, command):
"""Split the command using shell-like syntax."""
if six.PY2:
return [s.decode('utf8') for s in shlex.split(command.encode('utf8'))]
return shlex.split(command)
def quote(self, s):
@@ -143,18 +149,18 @@ class Fish(Generic):
def app_alias(self, fuck):
return ('function {0} -d "Correct your previous console command"\n'
' set -l exit_code $status\n'
' set -x TF_ALIAS {0}\n'
' set -l fucked_up_command $history[1]\n'
' set -l exit_code $status\n'
' set -l fucked_up_command $history[1]\n'
' env TF_ALIAS={0} PYTHONIOENCODING=utf-8'
' thefuck $fucked_up_command | read -l unfucked_command\n'
' if [ "$unfucked_command" != "" ]\n'
' eval $unfucked_command\n'
' if test $exit_code -ne 0\n'
' history --delete $fucked_up_command\n'
' history --merge ^ /dev/null\n'
' return 0\n'
' end\n'
' if [ "$unfucked_command" != "" ]\n'
' eval $unfucked_command\n'
' if test $exit_code -ne 0\n'
' history --delete $fucked_up_command\n'
' history --merge ^ /dev/null\n'
' return 0\n'
' end\n'
' end\n'
'end').format(fuck)
@memoize
@@ -182,6 +188,12 @@ class Fish(Generic):
def _get_history_line(self, command_script):
return u'- cmd: {}\n when: {}\n'.format(command_script, int(time()))
def _script_from_history(self, line):
if '- cmd: ' in line:
return line.split('- cmd: ', 1)[1]
else:
return ''
def and_(self, *commands):
return u'; and '.join(commands)
+4 -3
View File
@@ -31,7 +31,7 @@ class Command(object):
try:
self._script_parts = shells.split_command(self.script)
except Exception:
logs.debug("Can't split command script {} because:\n {}".format(
logs.debug(u"Can't split command script {} because:\n {}".format(
self, sys.exc_info()))
self._script_parts = None
return self._script_parts
@@ -44,7 +44,7 @@ class Command(object):
return False
def __repr__(self):
return 'Command(script={}, stdout={}, stderr={})'.format(
return u'Command(script={}, stdout={}, stderr={})'.format(
self.script, self.stdout, self.stderr)
def update(self, **kwargs):
@@ -267,7 +267,7 @@ class CorrectedCommand(object):
return (self.script, self.side_effect).__hash__()
def __repr__(self):
return 'CorrectedCommand(script={}, side_effect={}, priority={})'.format(
return u'CorrectedCommand(script={}, side_effect={}, priority={})'.format(
self.script, self.side_effect, self.priority)
def run(self, old_cmd):
@@ -279,4 +279,5 @@ class CorrectedCommand(object):
if self.side_effect:
compatibility_call(self.side_effect, old_cmd, self.script)
shells.put_to_history(self.script)
# This depends on correct setting of PYTHONIOENCODING by the alias:
print(self.script)