# Path: thefuck/conf.py
# class Settings(dict):
#     def __getattr__(self, item):
#     def __setattr__(self, key, value):
#     def init(self, args=None):
#     def _init_settings_file(self):
#     def _get_user_dir_path(self):
#     def _setup_user_dir(self):
#     def _settings_from_file(self):
#     def _rules_from_env(self, val):
#     def _priority_from_env(self, val):
#     def _val_from_env(self, env, attr):
#     def _settings_from_env(self):
#     def _settings_from_args(self, args):
# 
# Path: thefuck/types.py
# class Rule(object):
#     """Rule for fixing commands."""
# 
#     def __init__(self, name, match, get_new_command,
#                  enabled_by_default, side_effect,
#                  priority, requires_output):
#         """Initializes rule with given fields.
# 
#         :type name: basestring
#         :type match: (Command) -> bool
#         :type get_new_command: (Command) -> (basestring | [basestring])
#         :type enabled_by_default: boolean
#         :type side_effect: (Command, basestring) -> None
#         :type priority: int
#         :type requires_output: bool
# 
#         """
#         self.name = name
#         self.match = match
#         self.get_new_command = get_new_command
#         self.enabled_by_default = enabled_by_default
#         self.side_effect = side_effect
#         self.priority = priority
#         self.requires_output = requires_output
# 
#     def __eq__(self, other):
#         if isinstance(other, Rule):
#             return ((self.name, self.match, self.get_new_command,
#                      self.enabled_by_default, self.side_effect,
#                      self.priority, self.requires_output)
#                     == (other.name, other.match, other.get_new_command,
#                         other.enabled_by_default, other.side_effect,
#                         other.priority, other.requires_output))
#         else:
#             return False
# 
#     def __repr__(self):
#         return 'Rule(name={}, match={}, get_new_command={}, ' \
#                'enabled_by_default={}, side_effect={}, ' \
#                'priority={}, requires_output={})'.format(
#                    self.name, self.match, self.get_new_command,
#                    self.enabled_by_default, self.side_effect,
#                    self.priority, self.requires_output)
# 
#     @classmethod
#     def from_path(cls, path):
#         """Creates rule instance from path.
# 
#         :type path: pathlib.Path
#         :rtype: Rule
# 
#         """
#         name = path.name[:-3]
#         if name in settings.exclude_rules:
#             logs.debug(u'Ignoring excluded rule: {}'.format(name))
#             return
#         with logs.debug_time(u'Importing rule: {};'.format(name)):
#             try:
#                 rule_module = load_source(name, str(path))
#             except Exception:
#                 logs.exception(u"Rule {} failed to load".format(name), sys.exc_info())
#                 return
#         priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY)
#         return cls(name, rule_module.match,
#                    rule_module.get_new_command,
#                    getattr(rule_module, 'enabled_by_default', True),
#                    getattr(rule_module, 'side_effect', None),
#                    settings.priority.get(name, priority),
#                    getattr(rule_module, 'requires_output', True))
# 
#     @property
#     def is_enabled(self):
#         """Returns `True` when rule enabled.
# 
#         :rtype: bool
# 
#         """
#         return (
#             self.name in settings.rules
#             or self.enabled_by_default
#             and ALL_ENABLED in settings.rules
#         )
# 
#     def is_match(self, command):
#         """Returns `True` if rule matches the command.
# 
#         :type command: Command
#         :rtype: bool
# 
#         """
#         if command.output is None and self.requires_output:
#             return False
# 
#         try:
#             with logs.debug_time(u'Trying rule: {};'.format(self.name)):
#                 if self.match(command):
#                     return True
#         except Exception:
#             logs.rule_failed(self, sys.exc_info())
# 
#     def get_corrected_commands(self, command):
#         """Returns generator with corrected commands.
# 
#         :type command: Command
#         :rtype: Iterable[CorrectedCommand]
# 
#         """
#         new_commands = self.get_new_command(command)
#         if not isinstance(new_commands, list):
#             new_commands = (new_commands,)
#         for n, new_command in enumerate(new_commands):
#             yield CorrectedCommand(script=new_command,
#                                    side_effect=self.side_effect,
#                                    priority=(n + 1) * self.priority)


import sys
from .conf import settings
from .types import Rule
from .system import Path
from . import logs


def get_loaded_rules(rules_paths):
    """Yields all available rules.

    :type rules_paths: [Path]
    :rtype: Iterable[Rule]

    """
    for path in rules_paths:
        if path.name != '__init__.py':
            rule = Rule.from_path(path)
            if rule and rule.is_enabled:
                yield rule


def get_rules_import_paths():
    """Yields all rules import paths.

    :rtype: Iterable[Path]

    """
    # Bundled rules:
    yield Path(__file__).parent.joinpath('rules')
    # Rules defined by user:

# Path: thefuck/conf.py
# class Settings(dict):
#     def __getattr__(self, item):
#     def __setattr__(self, key, value):
#     def init(self, args=None):
#     def _init_settings_file(self):
#     def _get_user_dir_path(self):
#     def _setup_user_dir(self):
#     def _settings_from_file(self):
#     def _rules_from_env(self, val):
#     def _priority_from_env(self, val):
#     def _val_from_env(self, env, attr):
#     def _settings_from_env(self):
#     def _settings_from_args(self, args):
# 
# Path: thefuck/types.py
# class Rule(object):
#     """Rule for fixing commands."""
# 
#     def __init__(self, name, match, get_new_command,
#                  enabled_by_default, side_effect,
#                  priority, requires_output):
#         """Initializes rule with given fields.
# 
#         :type name: basestring
#         :type match: (Command) -> bool
#         :type get_new_command: (Command) -> (basestring | [basestring])
#         :type enabled_by_default: boolean
#         :type side_effect: (Command, basestring) -> None
#         :type priority: int
#         :type requires_output: bool
# 
#         """
#         self.name = name
#         self.match = match
#         self.get_new_command = get_new_command
#         self.enabled_by_default = enabled_by_default
#         self.side_effect = side_effect
#         self.priority = priority
#         self.requires_output = requires_output
# 
#     def __eq__(self, other):
#         if isinstance(other, Rule):
#             return ((self.name, self.match, self.get_new_command,
#                      self.enabled_by_default, self.side_effect,
#                      self.priority, self.requires_output)
#                     == (other.name, other.match, other.get_new_command,
#                         other.enabled_by_default, other.side_effect,
#                         other.priority, other.requires_output))
#         else:
#             return False
# 
#     def __repr__(self):
#         return 'Rule(name={}, match={}, get_new_command={}, ' \
#                'enabled_by_default={}, side_effect={}, ' \
#                'priority={}, requires_output={})'.format(
#                    self.name, self.match, self.get_new_command,
#                    self.enabled_by_default, self.side_effect,
#                    self.priority, self.requires_output)
# 
#     @classmethod
#     def from_path(cls, path):
#         """Creates rule instance from path.
# 
#         :type path: pathlib.Path
#         :rtype: Rule
# 
#         """
#         name = path.name[:-3]
#         if name in settings.exclude_rules:
#             logs.debug(u'Ignoring excluded rule: {}'.format(name))
#             return
#         with logs.debug_time(u'Importing rule: {};'.format(name)):
#             try:
#                 rule_module = load_source(name, str(path))
#             except Exception:
#                 logs.exception(u"Rule {} failed to load".format(name), sys.exc_info())
#                 return
#         priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY)
#         return cls(name, rule_module.match,
#                    rule_module.get_new_command,
#                    getattr(rule_module, 'enabled_by_default', True),
#                    getattr(rule_module, 'side_effect', None),
#                    settings.priority.get(name, priority),
#                    getattr(rule_module, 'requires_output', True))
# 
#     @property
#     def is_enabled(self):
#         """Returns `True` when rule enabled.
# 
#         :rtype: bool
# 
#         """
#         return (
#             self.name in settings.rules
#             or self.enabled_by_default
#             and ALL_ENABLED in settings.rules
#         )
# 
#     def is_match(self, command):
#         """Returns `True` if rule matches the command.
# 
#         :type command: Command
#         :rtype: bool
# 
#         """
#         if command.output is None and self.requires_output:
#             return False
# 
#         try:
#             with logs.debug_time(u'Trying rule: {};'.format(self.name)):
#                 if self.match(command):
#                     return True
#         except Exception:
#             logs.rule_failed(self, sys.exc_info())
# 
#     def get_corrected_commands(self, command):
#         """Returns generator with corrected commands.
# 
#         :type command: Command
#         :rtype: Iterable[CorrectedCommand]
# 
#         """
#         new_commands = self.get_new_command(command)
#         if not isinstance(new_commands, list):
#             new_commands = (new_commands,)
#         for n, new_command in enumerate(new_commands):
#             yield CorrectedCommand(script=new_command,
#                                    side_effect=self.side_effect,
#                                    priority=(n + 1) * self.priority)


# Path: thefuck/corrector.py
import sys
from .conf import settings
from .types import Rule
from .system import Path
from . import logs



def get_loaded_rules(rules_paths):
    """Yields all available rules.

    :type rules_paths: [Path]
    :rtype: Iterable[Rule]

    """
    for path in rules_paths:
        if path.name != '__init__.py':
            rule = Rule.from_path(path)
            if rule and rule.is_enabled:
                yield rule


def get_rules_import_paths():
    """Yields all rules import paths.

    :rtype: Iterable[Path]

    """
    # Bundled rules:
    yield Path(__file__).parent.joinpath('rules')
    # Rules defined by user:

    yield settings.user_dir.joinpath('rules')