1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-19 04:21:14 +00:00

#N/A: Move all consts to const

This commit is contained in:
nvbn 2016-02-22 18:40:28 +03:00
parent 9b260eb239
commit e9de01fa41
10 changed files with 81 additions and 78 deletions

View File

@ -1,7 +1,7 @@
from pathlib import Path from pathlib import Path
import pytest import pytest
from thefuck import shells from thefuck import shells
from thefuck import conf from thefuck import conf, const
shells.shell = shells.Generic() shells.shell = shells.Generic()
@ -22,7 +22,7 @@ def no_memoize(monkeypatch):
def settings(request): def settings(request):
def _reset_settings(): def _reset_settings():
conf.settings.clear() conf.settings.clear()
conf.settings.update(conf.DEFAULT_SETTINGS) conf.settings.update(const.DEFAULT_SETTINGS)
request.addfinalizer(_reset_settings) request.addfinalizer(_reset_settings)
conf.settings.user_dir = Path('~/.thefuck') conf.settings.user_dir = Path('~/.thefuck')

View File

@ -1,7 +1,7 @@
import pytest import pytest
import six import six
from mock import Mock from mock import Mock
from thefuck import conf from thefuck import const
@pytest.fixture @pytest.fixture
@ -20,7 +20,7 @@ def environ(monkeypatch):
def test_settings_defaults(load_source, settings): def test_settings_defaults(load_source, settings):
load_source.return_value = object() load_source.return_value = object()
settings.init() settings.init()
for key, val in conf.DEFAULT_SETTINGS.items(): for key, val in const.DEFAULT_SETTINGS.items():
assert getattr(settings, key) == val assert getattr(settings, key) == val
@ -42,13 +42,13 @@ class TestSettingsFromFile(object):
assert settings.exclude_rules == ['git'] assert settings.exclude_rules == ['git']
def test_from_file_with_DEFAULT(self, load_source, settings): def test_from_file_with_DEFAULT(self, load_source, settings):
load_source.return_value = Mock(rules=conf.DEFAULT_RULES + ['test'], load_source.return_value = Mock(rules=const.DEFAULT_RULES + ['test'],
wait_command=10, wait_command=10,
exclude_rules=[], exclude_rules=[],
require_confirmation=True, require_confirmation=True,
no_colors=True) no_colors=True)
settings.init() settings.init()
assert settings.rules == conf.DEFAULT_RULES + ['test'] assert settings.rules == const.DEFAULT_RULES + ['test']
@pytest.mark.usefixture('load_source') @pytest.mark.usefixture('load_source')
@ -71,7 +71,7 @@ class TestSettingsFromEnv(object):
def test_from_env_with_DEFAULT(self, environ, settings): def test_from_env_with_DEFAULT(self, environ, settings):
environ.update({'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'}) environ.update({'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'})
settings.init() settings.init()
assert settings.rules == conf.DEFAULT_RULES + ['bash', 'lisp'] assert settings.rules == const.DEFAULT_RULES + ['bash', 'lisp']
class TestInitializeSettingsFile(object): class TestInitializeSettingsFile(object):
@ -93,7 +93,7 @@ class TestInitializeSettingsFile(object):
settings_file_contents = settings_file.getvalue() settings_file_contents = settings_file.getvalue()
assert settings_path_mock.is_file.call_count == 1 assert settings_path_mock.is_file.call_count == 1
assert settings_path_mock.open.call_count == 1 assert settings_path_mock.open.call_count == 1
assert conf.SETTINGS_HEADER in settings_file_contents assert const.SETTINGS_HEADER in settings_file_contents
for setting in conf.DEFAULT_SETTINGS.items(): for setting in const.DEFAULT_SETTINGS.items():
assert '# {} = {}\n'.format(*setting) in settings_file_contents assert '# {} = {}\n'.format(*setting) in settings_file_contents
settings_file.close() settings_file.close()

View File

@ -2,7 +2,7 @@
import pytest import pytest
from pathlib import PosixPath from pathlib import PosixPath
from thefuck import corrector, conf from thefuck import corrector, const
from tests.utils import Rule, Command, CorrectedCommand from tests.utils import Rule, Command, CorrectedCommand
from thefuck.corrector import get_corrected_commands, organize_commands from thefuck.corrector import get_corrected_commands, organize_commands
@ -24,9 +24,9 @@ class TestGetRules(object):
assert {r.name for r in rules} == set(names) assert {r.name for r in rules} == set(names)
@pytest.mark.parametrize('paths, conf_rules, exclude_rules, loaded_rules', [ @pytest.mark.parametrize('paths, conf_rules, exclude_rules, loaded_rules', [
(['git.py', 'bash.py'], conf.DEFAULT_RULES, [], ['git', 'bash']), (['git.py', 'bash.py'], const.DEFAULT_RULES, [], ['git', 'bash']),
(['git.py', 'bash.py'], ['git'], [], ['git']), (['git.py', 'bash.py'], ['git'], [], ['git']),
(['git.py', 'bash.py'], conf.DEFAULT_RULES, ['git'], ['bash']), (['git.py', 'bash.py'], const.DEFAULT_RULES, ['git'], ['bash']),
(['git.py', 'bash.py'], ['git'], ['git'], [])]) (['git.py', 'bash.py'], ['git'], ['git'], [])])
def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules, def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules,
loaded_rules): loaded_rules):

View File

@ -5,7 +5,7 @@ from mock import Mock
from pathlib import Path from pathlib import Path
import pytest import pytest
from tests.utils import CorrectedCommand, Rule, Command from tests.utils import CorrectedCommand, Rule, Command
from thefuck import conf from thefuck import const
from thefuck.exceptions import EmptyCommand from thefuck.exceptions import EmptyCommand
@ -44,14 +44,14 @@ class TestRule(object):
load_source.assert_called_once_with('bash', '/rules/bash.py') load_source.assert_called_once_with('bash', '/rules/bash.py')
@pytest.mark.parametrize('rules, exclude_rules, rule, is_enabled', [ @pytest.mark.parametrize('rules, exclude_rules, rule, is_enabled', [
(conf.DEFAULT_RULES, [], Rule('git', enabled_by_default=True), True), (const.DEFAULT_RULES, [], Rule('git', enabled_by_default=True), True),
(conf.DEFAULT_RULES, [], Rule('git', enabled_by_default=False), False), (const.DEFAULT_RULES, [], Rule('git', enabled_by_default=False), False),
([], [], Rule('git', enabled_by_default=False), False), ([], [], Rule('git', enabled_by_default=False), False),
([], [], Rule('git', enabled_by_default=True), False), ([], [], Rule('git', enabled_by_default=True), False),
(conf.DEFAULT_RULES + ['git'], [], Rule('git', enabled_by_default=False), True), (const.DEFAULT_RULES + ['git'], [], Rule('git', enabled_by_default=False), True),
(['git'], [], Rule('git', enabled_by_default=False), True), (['git'], [], Rule('git', enabled_by_default=False), True),
(conf.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=True), False), (const.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=True), False),
(conf.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=False), False), (const.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=False), False),
([], ['git'], Rule('git', enabled_by_default=True), False), ([], ['git'], Rule('git', enabled_by_default=True), False),
([], ['git'], Rule('git', enabled_by_default=False), False)]) ([], ['git'], Rule('git', enabled_by_default=False), False)])
def test_is_enabled(self, settings, rules, exclude_rules, rule, is_enabled): def test_is_enabled(self, settings, rules, exclude_rules, rule, is_enabled):

View File

@ -31,7 +31,9 @@ def test_read_actions(patch_get_key):
# Ctrl+C: # Ctrl+C:
const.KEY_CTRL_C]) const.KEY_CTRL_C])
assert list(islice(ui.read_actions(), 5)) \ assert list(islice(ui.read_actions(), 5)) \
== [ui.SELECT, ui.SELECT, ui.PREVIOUS, ui.NEXT, ui.ABORT] == [const.ACTION_SELECT, const.ACTION_SELECT,
const.ACTION_PREVIOUS, const.ACTION_NEXT,
const.ACTION_ABORT]
def test_command_selector(): def test_command_selector():

View File

@ -1,5 +1,5 @@
from thefuck import types from thefuck import types
from thefuck.conf import DEFAULT_PRIORITY from thefuck.const import DEFAULT_PRIORITY
class Command(types.Command): class Command(types.Command):

View File

@ -3,44 +3,7 @@ import os
import sys import sys
from pathlib import Path from pathlib import Path
from six import text_type from six import text_type
from . import const
ALL_ENABLED = object()
DEFAULT_RULES = [ALL_ENABLED]
DEFAULT_PRIORITY = 1000
DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
'exclude_rules': [],
'wait_command': 3,
'require_confirmation': True,
'no_colors': False,
'debug': False,
'priority': {},
'history_limit': None,
'alter_history': True,
'env': {'LC_ALL': 'C', 'LANG': 'C', 'GIT_TRACE': '1'}}
ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
'THEFUCK_EXCLUDE_RULES': 'exclude_rules',
'THEFUCK_WAIT_COMMAND': 'wait_command',
'THEFUCK_REQUIRE_CONFIRMATION': 'require_confirmation',
'THEFUCK_NO_COLORS': 'no_colors',
'THEFUCK_DEBUG': 'debug',
'THEFUCK_PRIORITY': 'priority',
'THEFUCK_HISTORY_LIMIT': 'history_limit',
'THEFUCK_ALTER_HISTORY': 'alter_history'}
SETTINGS_HEADER = u"""# The Fuck settings file
#
# The rules are defined as in the example bellow:
#
# rules = ['cd_parent', 'git_push', 'python_command', 'sudo']
#
# The default values are as follows. Uncomment and change to fit your needs.
# See https://github.com/nvbn/thefuck#settings for more information.
#
"""
class Settings(dict): class Settings(dict):
@ -71,8 +34,8 @@ class Settings(dict):
settings_path = self.user_dir.joinpath('settings.py') settings_path = self.user_dir.joinpath('settings.py')
if not settings_path.is_file(): if not settings_path.is_file():
with settings_path.open(mode='w') as settings_file: with settings_path.open(mode='w') as settings_file:
settings_file.write(SETTINGS_HEADER) settings_file.write(const.SETTINGS_HEADER)
for setting in DEFAULT_SETTINGS.items(): for setting in const.DEFAULT_SETTINGS.items():
settings_file.write(u'# {} = {}\n'.format(*setting)) settings_file.write(u'# {} = {}\n'.format(*setting))
def _get_user_dir_path(self): def _get_user_dir_path(self):
@ -100,14 +63,14 @@ class Settings(dict):
settings = load_source( settings = load_source(
'settings', text_type(self.user_dir.joinpath('settings.py'))) 'settings', text_type(self.user_dir.joinpath('settings.py')))
return {key: getattr(settings, key) return {key: getattr(settings, key)
for key in DEFAULT_SETTINGS.keys() for key in const.DEFAULT_SETTINGS.keys()
if hasattr(settings, key)} if hasattr(settings, key)}
def _rules_from_env(self, val): def _rules_from_env(self, val):
"""Transforms rules list from env-string to python.""" """Transforms rules list from env-string to python."""
val = val.split(':') val = val.split(':')
if 'DEFAULT_RULES' in val: if 'DEFAULT_RULES' in val:
val = DEFAULT_RULES + [rule for rule in val if rule != 'DEFAULT_RULES'] val = const.DEFAULT_RULES + [rule for rule in val if rule != 'DEFAULT_RULES']
return val return val
def _priority_from_env(self, val): def _priority_from_env(self, val):
@ -139,8 +102,8 @@ class Settings(dict):
def _settings_from_env(self): def _settings_from_env(self):
"""Loads settings from env.""" """Loads settings from env."""
return {attr: self._val_from_env(env, attr) return {attr: self._val_from_env(env, attr)
for env, attr in ENV_TO_ATTR.items() for env, attr in const.ENV_TO_ATTR.items()
if env in os.environ} if env in os.environ}
settings = Settings(DEFAULT_SETTINGS) settings = Settings(const.DEFAULT_SETTINGS)

View File

@ -12,3 +12,45 @@ class _GenConst(object):
KEY_UP = _GenConst('') KEY_UP = _GenConst('')
KEY_DOWN = _GenConst('') KEY_DOWN = _GenConst('')
KEY_CTRL_C = _GenConst('Ctrl+C') KEY_CTRL_C = _GenConst('Ctrl+C')
ACTION_SELECT = _GenConst('select')
ACTION_ABORT = _GenConst('abort')
ACTION_PREVIOUS = _GenConst('previous')
ACTION_NEXT = _GenConst('next')
ALL_ENABLED = _GenConst('All rules enabled')
DEFAULT_RULES = [ALL_ENABLED]
DEFAULT_PRIORITY = 1000
DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
'exclude_rules': [],
'wait_command': 3,
'require_confirmation': True,
'no_colors': False,
'debug': False,
'priority': {},
'history_limit': None,
'alter_history': True,
'env': {'LC_ALL': 'C', 'LANG': 'C', 'GIT_TRACE': '1'}}
ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
'THEFUCK_EXCLUDE_RULES': 'exclude_rules',
'THEFUCK_WAIT_COMMAND': 'wait_command',
'THEFUCK_REQUIRE_CONFIRMATION': 'require_confirmation',
'THEFUCK_NO_COLORS': 'no_colors',
'THEFUCK_DEBUG': 'debug',
'THEFUCK_PRIORITY': 'priority',
'THEFUCK_HISTORY_LIMIT': 'history_limit',
'THEFUCK_ALTER_HISTORY': 'alter_history'}
SETTINGS_HEADER = u"""# The Fuck settings file
#
# The rules are defined as in the example bellow:
#
# rules = ['cd_parent', 'git_push', 'python_command', 'sudo']
#
# The default values are as follows. Uncomment and change to fit your needs.
# See https://github.com/nvbn/thefuck#settings for more information.
#
"""

View File

@ -6,7 +6,8 @@ import six
from psutil import Process, TimeoutExpired from psutil import Process, TimeoutExpired
from . import logs from . import logs
from .shells import shell from .shells import shell
from .conf import settings, DEFAULT_PRIORITY, ALL_ENABLED from .conf import settings
from .const import DEFAULT_PRIORITY, ALL_ENABLED
from .exceptions import EmptyCommand from .exceptions import EmptyCommand
from .utils import compatibility_call from .utils import compatibility_call

View File

@ -6,11 +6,6 @@ from .exceptions import NoRuleMatched
from .system import get_key from .system import get_key
from . import logs, const from . import logs, const
SELECT = 0
ABORT = 1
PREVIOUS = 2
NEXT = 3
def read_actions(): def read_actions():
"""Yields actions for pressed keys.""" """Yields actions for pressed keys."""
@ -18,13 +13,13 @@ def read_actions():
key = get_key() key = get_key()
if key in (const.KEY_UP, 'k'): if key in (const.KEY_UP, 'k'):
yield PREVIOUS yield const.ACTION_PREVIOUS
elif key in (const.KEY_DOWN, 'j'): elif key in (const.KEY_DOWN, 'j'):
yield NEXT yield const.ACTION_NEXT
elif key == const.KEY_CTRL_C: elif key == const.KEY_CTRL_C:
yield ABORT yield const.ACTION_ABORT
elif key in ('\n', '\r'): elif key in ('\n', '\r'):
yield SELECT yield const.ACTION_SELECT
class CommandSelector(object): class CommandSelector(object):
@ -83,15 +78,15 @@ def select_command(corrected_commands):
logs.confirm_text(selector.value) logs.confirm_text(selector.value)
for action in read_actions(): for action in read_actions():
if action == SELECT: if action == const.ACTION_SELECT:
sys.stderr.write('\n') sys.stderr.write('\n')
return selector.value return selector.value
elif action == ABORT: elif action == const.ACTION_ABORT:
logs.failed('\nAborted') logs.failed('\nAborted')
return return
elif action == PREVIOUS: elif action == const.ACTION_PREVIOUS:
selector.previous() selector.previous()
logs.confirm_text(selector.value) logs.confirm_text(selector.value)
elif action == NEXT: elif action == const.ACTION_NEXT:
selector.next() selector.next()
logs.confirm_text(selector.value) logs.confirm_text(selector.value)