From e9de01fa4134c1e38162602688bed4c19371a33d Mon Sep 17 00:00:00 2001 From: nvbn Date: Mon, 22 Feb 2016 18:40:28 +0300 Subject: [PATCH] #N/A: Move all consts to `const` --- tests/conftest.py | 4 ++-- tests/test_conf.py | 14 +++++------ tests/test_corrector.py | 6 ++--- tests/test_types.py | 12 +++++----- tests/test_ui.py | 4 +++- tests/utils.py | 2 +- thefuck/conf.py | 51 ++++++----------------------------------- thefuck/const.py | 42 +++++++++++++++++++++++++++++++++ thefuck/types.py | 3 ++- thefuck/ui.py | 21 +++++++---------- 10 files changed, 81 insertions(+), 78 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index ad1f5394..c089af71 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ from pathlib import Path import pytest from thefuck import shells -from thefuck import conf +from thefuck import conf, const shells.shell = shells.Generic() @@ -22,7 +22,7 @@ def no_memoize(monkeypatch): def settings(request): def _reset_settings(): conf.settings.clear() - conf.settings.update(conf.DEFAULT_SETTINGS) + conf.settings.update(const.DEFAULT_SETTINGS) request.addfinalizer(_reset_settings) conf.settings.user_dir = Path('~/.thefuck') diff --git a/tests/test_conf.py b/tests/test_conf.py index 86b2bf01..db894031 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -1,7 +1,7 @@ import pytest import six from mock import Mock -from thefuck import conf +from thefuck import const @pytest.fixture @@ -20,7 +20,7 @@ def environ(monkeypatch): def test_settings_defaults(load_source, settings): load_source.return_value = object() settings.init() - for key, val in conf.DEFAULT_SETTINGS.items(): + for key, val in const.DEFAULT_SETTINGS.items(): assert getattr(settings, key) == val @@ -42,13 +42,13 @@ class TestSettingsFromFile(object): assert settings.exclude_rules == ['git'] 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, exclude_rules=[], require_confirmation=True, no_colors=True) settings.init() - assert settings.rules == conf.DEFAULT_RULES + ['test'] + assert settings.rules == const.DEFAULT_RULES + ['test'] @pytest.mark.usefixture('load_source') @@ -71,7 +71,7 @@ class TestSettingsFromEnv(object): def test_from_env_with_DEFAULT(self, environ, settings): environ.update({'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'}) settings.init() - assert settings.rules == conf.DEFAULT_RULES + ['bash', 'lisp'] + assert settings.rules == const.DEFAULT_RULES + ['bash', 'lisp'] class TestInitializeSettingsFile(object): @@ -93,7 +93,7 @@ class TestInitializeSettingsFile(object): settings_file_contents = settings_file.getvalue() assert settings_path_mock.is_file.call_count == 1 assert settings_path_mock.open.call_count == 1 - assert conf.SETTINGS_HEADER in settings_file_contents - for setting in conf.DEFAULT_SETTINGS.items(): + assert const.SETTINGS_HEADER in settings_file_contents + for setting in const.DEFAULT_SETTINGS.items(): assert '# {} = {}\n'.format(*setting) in settings_file_contents settings_file.close() diff --git a/tests/test_corrector.py b/tests/test_corrector.py index 529025b5..ac95e54a 100644 --- a/tests/test_corrector.py +++ b/tests/test_corrector.py @@ -2,7 +2,7 @@ import pytest from pathlib import PosixPath -from thefuck import corrector, conf +from thefuck import corrector, const from tests.utils import Rule, Command, CorrectedCommand 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) @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'], conf.DEFAULT_RULES, ['git'], ['bash']), + (['git.py', 'bash.py'], const.DEFAULT_RULES, ['git'], ['bash']), (['git.py', 'bash.py'], ['git'], ['git'], [])]) def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules, loaded_rules): diff --git a/tests/test_types.py b/tests/test_types.py index d68dc465..3ff3fccb 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -5,7 +5,7 @@ from mock import Mock from pathlib import Path import pytest from tests.utils import CorrectedCommand, Rule, Command -from thefuck import conf +from thefuck import const from thefuck.exceptions import EmptyCommand @@ -44,14 +44,14 @@ class TestRule(object): load_source.assert_called_once_with('bash', '/rules/bash.py') @pytest.mark.parametrize('rules, exclude_rules, rule, is_enabled', [ - (conf.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=True), True), + (const.DEFAULT_RULES, [], Rule('git', enabled_by_default=False), False), ([], [], Rule('git', enabled_by_default=False), 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), - (conf.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=True), 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=False), False)]) def test_is_enabled(self, settings, rules, exclude_rules, rule, is_enabled): diff --git a/tests/test_ui.py b/tests/test_ui.py index 310dd376..5ac78cbc 100644 --- a/tests/test_ui.py +++ b/tests/test_ui.py @@ -31,7 +31,9 @@ def test_read_actions(patch_get_key): # Ctrl+C: const.KEY_CTRL_C]) 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(): diff --git a/tests/utils.py b/tests/utils.py index 378187d2..62a84749 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,5 +1,5 @@ from thefuck import types -from thefuck.conf import DEFAULT_PRIORITY +from thefuck.const import DEFAULT_PRIORITY class Command(types.Command): diff --git a/thefuck/conf.py b/thefuck/conf.py index 4e47d9c1..8e1a2a94 100644 --- a/thefuck/conf.py +++ b/thefuck/conf.py @@ -3,44 +3,7 @@ import os import sys from pathlib import Path from six import text_type - - -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. -# - -""" +from . import const class Settings(dict): @@ -71,8 +34,8 @@ class Settings(dict): settings_path = self.user_dir.joinpath('settings.py') if not settings_path.is_file(): with settings_path.open(mode='w') as settings_file: - settings_file.write(SETTINGS_HEADER) - for setting in DEFAULT_SETTINGS.items(): + settings_file.write(const.SETTINGS_HEADER) + for setting in const.DEFAULT_SETTINGS.items(): settings_file.write(u'# {} = {}\n'.format(*setting)) def _get_user_dir_path(self): @@ -100,14 +63,14 @@ class Settings(dict): settings = load_source( 'settings', text_type(self.user_dir.joinpath('settings.py'))) return {key: getattr(settings, key) - for key in DEFAULT_SETTINGS.keys() + for key in const.DEFAULT_SETTINGS.keys() if hasattr(settings, key)} def _rules_from_env(self, val): """Transforms rules list from env-string to python.""" val = val.split(':') 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 def _priority_from_env(self, val): @@ -139,8 +102,8 @@ class Settings(dict): def _settings_from_env(self): """Loads settings from env.""" 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} -settings = Settings(DEFAULT_SETTINGS) +settings = Settings(const.DEFAULT_SETTINGS) diff --git a/thefuck/const.py b/thefuck/const.py index c7ffaa0e..6c4d3491 100644 --- a/thefuck/const.py +++ b/thefuck/const.py @@ -12,3 +12,45 @@ class _GenConst(object): KEY_UP = _GenConst('↑') KEY_DOWN = _GenConst('↓') 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. +# + +""" diff --git a/thefuck/types.py b/thefuck/types.py index 5e0e368d..dcd99b6b 100644 --- a/thefuck/types.py +++ b/thefuck/types.py @@ -6,7 +6,8 @@ import six from psutil import Process, TimeoutExpired from . import logs 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 .utils import compatibility_call diff --git a/thefuck/ui.py b/thefuck/ui.py index e2eb920f..417058da 100644 --- a/thefuck/ui.py +++ b/thefuck/ui.py @@ -6,11 +6,6 @@ from .exceptions import NoRuleMatched from .system import get_key from . import logs, const -SELECT = 0 -ABORT = 1 -PREVIOUS = 2 -NEXT = 3 - def read_actions(): """Yields actions for pressed keys.""" @@ -18,13 +13,13 @@ def read_actions(): key = get_key() if key in (const.KEY_UP, 'k'): - yield PREVIOUS + yield const.ACTION_PREVIOUS elif key in (const.KEY_DOWN, 'j'): - yield NEXT + yield const.ACTION_NEXT elif key == const.KEY_CTRL_C: - yield ABORT + yield const.ACTION_ABORT elif key in ('\n', '\r'): - yield SELECT + yield const.ACTION_SELECT class CommandSelector(object): @@ -83,15 +78,15 @@ def select_command(corrected_commands): logs.confirm_text(selector.value) for action in read_actions(): - if action == SELECT: + if action == const.ACTION_SELECT: sys.stderr.write('\n') return selector.value - elif action == ABORT: + elif action == const.ACTION_ABORT: logs.failed('\nAborted') return - elif action == PREVIOUS: + elif action == const.ACTION_PREVIOUS: selector.previous() logs.confirm_text(selector.value) - elif action == NEXT: + elif action == const.ACTION_NEXT: selector.next() logs.confirm_text(selector.value)