Move special data types to types
This commit is contained in:
+7
-28
@@ -3,17 +3,10 @@ from imp import load_source
|
||||
import os
|
||||
import sys
|
||||
from six import text_type
|
||||
from . import logs
|
||||
from . import logs, types
|
||||
|
||||
|
||||
class RulesNamesList(list):
|
||||
"""Wrapper a top of list for string rules names."""
|
||||
|
||||
def __contains__(self, item):
|
||||
return super(RulesNamesList, self).__contains__(item.name)
|
||||
|
||||
|
||||
class _DefaultRulesNames(RulesNamesList):
|
||||
class _DefaultRulesNames(types.RulesNamesList):
|
||||
def __add__(self, items):
|
||||
return _DefaultRulesNames(list(self) + items)
|
||||
|
||||
@@ -31,20 +24,6 @@ class _DefaultRulesNames(RulesNamesList):
|
||||
DEFAULT_RULES = _DefaultRulesNames([])
|
||||
|
||||
|
||||
class Settings(object):
|
||||
def __init__(self, conf):
|
||||
self._conf = conf
|
||||
|
||||
def __getattr__(self, item):
|
||||
return self._conf.get(item)
|
||||
|
||||
def update(self, **kwargs):
|
||||
"""Returns new settings with new values from `kwargs`."""
|
||||
conf = copy(self._conf)
|
||||
conf.update(kwargs)
|
||||
return Settings(conf)
|
||||
|
||||
|
||||
DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
|
||||
'wait_command': 3,
|
||||
'require_confirmation': False,
|
||||
@@ -100,16 +79,16 @@ def get_settings(user_dir):
|
||||
except Exception:
|
||||
logs.exception("Can't load settings from file",
|
||||
sys.exc_info(),
|
||||
Settings(conf))
|
||||
types.Settings(conf))
|
||||
|
||||
try:
|
||||
conf.update(_settings_from_env())
|
||||
except Exception:
|
||||
logs.exception("Can't load settings from env",
|
||||
sys.exc_info(),
|
||||
Settings(conf))
|
||||
types.Settings(conf))
|
||||
|
||||
if not isinstance(conf['rules'], RulesNamesList):
|
||||
conf['rules'] = RulesNamesList(conf['rules'])
|
||||
if not isinstance(conf['rules'], types.RulesNamesList):
|
||||
conf['rules'] = types.RulesNamesList(conf['rules'])
|
||||
|
||||
return Settings(conf)
|
||||
return types.Settings(conf)
|
||||
|
||||
+9
-15
@@ -1,4 +1,3 @@
|
||||
from collections import namedtuple
|
||||
from imp import load_source
|
||||
from pathlib import Path
|
||||
from os.path import expanduser
|
||||
@@ -7,12 +6,7 @@ import os
|
||||
import sys
|
||||
from psutil import Process, TimeoutExpired
|
||||
import colorama
|
||||
from . import logs, conf
|
||||
|
||||
|
||||
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
||||
Rule = namedtuple('Rule', ('name', 'match', 'get_new_command',
|
||||
'enabled_by_default'))
|
||||
from . import logs, conf, types
|
||||
|
||||
|
||||
def setup_user_dir():
|
||||
@@ -28,16 +22,16 @@ def setup_user_dir():
|
||||
def load_rule(rule):
|
||||
"""Imports rule module and returns it."""
|
||||
rule_module = load_source(rule.name[:-3], str(rule))
|
||||
return Rule(rule.name[:-3], rule_module.match,
|
||||
rule_module.get_new_command,
|
||||
getattr(rule_module, 'enabled_by_default', True))
|
||||
return types.Rule(rule.name[:-3], rule_module.match,
|
||||
rule_module.get_new_command,
|
||||
getattr(rule_module, 'enabled_by_default', True))
|
||||
|
||||
|
||||
def get_rules(user_dir, settings):
|
||||
"""Returns all enabled rules."""
|
||||
bundled = Path(__file__).parent\
|
||||
.joinpath('rules')\
|
||||
.glob('*.py')
|
||||
bundled = Path(__file__).parent \
|
||||
.joinpath('rules') \
|
||||
.glob('*.py')
|
||||
user = user_dir.joinpath('rules').glob('*.py')
|
||||
for rule in sorted(list(bundled)) + list(user):
|
||||
if rule.name != '__init__.py':
|
||||
@@ -77,8 +71,8 @@ def get_command(settings, args):
|
||||
result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE,
|
||||
env=dict(os.environ, LANG='C'))
|
||||
if wait_output(settings, result):
|
||||
return Command(script, result.stdout.read().decode('utf-8'),
|
||||
result.stderr.read().decode('utf-8'))
|
||||
return types.Command(script, result.stdout.read().decode('utf-8'),
|
||||
result.stderr.read().decode('utf-8'))
|
||||
|
||||
|
||||
def get_matched_rule(command, rules, settings):
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
||||
|
||||
Rule = namedtuple('Rule', ('name', 'match', 'get_new_command',
|
||||
'enabled_by_default'))
|
||||
|
||||
|
||||
class RulesNamesList(list):
|
||||
"""Wrapper a top of list for string rules names."""
|
||||
|
||||
def __contains__(self, item):
|
||||
return super(RulesNamesList, self).__contains__(item.name)
|
||||
|
||||
|
||||
class Settings(dict):
|
||||
|
||||
def __getattr__(self, item):
|
||||
return self.get(item)
|
||||
|
||||
def update(self, **kwargs):
|
||||
"""Returns new settings with new values from `kwargs`."""
|
||||
conf = dict(self)
|
||||
conf.update(kwargs)
|
||||
return Settings(conf)
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
from functools import wraps
|
||||
import os
|
||||
import six
|
||||
from thefuck.main import Command
|
||||
from .types import Command
|
||||
|
||||
|
||||
def which(program):
|
||||
|
||||
Reference in New Issue
Block a user