#154 Add ability to override priority in settings
This commit is contained in:
+21
-6
@@ -28,12 +28,14 @@ DEFAULT_PRIORITY = 1000
|
||||
DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
|
||||
'wait_command': 3,
|
||||
'require_confirmation': False,
|
||||
'no_colors': False}
|
||||
'no_colors': False,
|
||||
'priority': {}}
|
||||
|
||||
ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
|
||||
'THEFUCK_WAIT_COMMAND': 'wait_command',
|
||||
'THEFUCK_REQUIRE_CONFIRMATION': 'require_confirmation',
|
||||
'THEFUCK_NO_COLORS': 'no_colors'}
|
||||
'THEFUCK_NO_COLORS': 'no_colors',
|
||||
'THEFUCK_PRIORITY': 'priority'}
|
||||
|
||||
|
||||
SETTINGS_HEADER = u"""# ~/.thefuck/settings.py: The Fuck settings file
|
||||
@@ -66,16 +68,29 @@ def _rules_from_env(val):
|
||||
return val
|
||||
|
||||
|
||||
def _priority_from_env(val):
|
||||
"""Gets priority pairs from env."""
|
||||
for part in val.split(':'):
|
||||
try:
|
||||
rule, priority = part.split('=')
|
||||
yield rule, int(priority)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
|
||||
def _val_from_env(env, attr):
|
||||
"""Transforms env-strings to python."""
|
||||
val = os.environ[env]
|
||||
if attr == 'rules':
|
||||
val = _rules_from_env(val)
|
||||
return _rules_from_env(val)
|
||||
elif attr == 'priority':
|
||||
return dict(_priority_from_env(val))
|
||||
elif attr == 'wait_command':
|
||||
val = int(val)
|
||||
return int(val)
|
||||
elif attr in ('require_confirmation', 'no_colors'):
|
||||
val = val.lower() == 'true'
|
||||
return val
|
||||
return val.lower() == 'true'
|
||||
else:
|
||||
return val
|
||||
|
||||
|
||||
def _settings_from_env():
|
||||
|
||||
+2
-1
@@ -46,7 +46,8 @@ def get_rules(user_dir, settings):
|
||||
.glob('*.py')
|
||||
user = user_dir.joinpath('rules').glob('*.py')
|
||||
rules = _get_loaded_rules(sorted(bundled) + sorted(user), settings)
|
||||
return sorted(rules, key=lambda rule: rule.priority)
|
||||
return sorted(rules, key=lambda rule: settings.priority.get(
|
||||
rule.name, rule.priority))
|
||||
|
||||
|
||||
def wait_output(settings, popen):
|
||||
|
||||
Reference in New Issue
Block a user