Skip to content

Commit

Permalink
#737: Add support of third-party rules
Browse files Browse the repository at this point in the history
  • Loading branch information
nvbn committed Nov 23, 2017
1 parent d582159 commit 4847078
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,25 @@ export THEFUCK_PRIORITY='no_command=9999:apt_get=100'
export THEFUCK_HISTORY_LIMIT='2000'
```

## Third-party packages with rules

If you want to make very specific rules or rules, that you don't want to make public,
but share with other people.
You can create a special package with name `thefuck_contrib_*` with following structure:

```
thefuck_contrib_foo
thefuck_contrib_foo
rules
__init__.py
*third-party rules*
__init__.py
*third-party-utils*
setup.py
```

And thefuck will find all rules from `rules` module.

## Experimental instant mode

By default The Fuck reruns a previous command and that takes time,
Expand Down
27 changes: 22 additions & 5 deletions thefuck/corrector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from .conf import settings
from .types import Rule
from .system import Path
Expand All @@ -18,17 +19,33 @@ def get_loaded_rules(rules_paths):
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')
# Packages with third-party rules:
for path in sys.path:
for contrib_module in Path(path).glob('thefuck_contrib_*'):
contrib_rules = contrib_module.joinpath('rules')
if contrib_rules.is_dir():
yield contrib_rules


def get_rules():
"""Returns all enabled rules.
:rtype: [Rule]
"""
bundled = Path(__file__).parent \
.joinpath('rules') \
.glob('*.py')
user = settings.user_dir.joinpath('rules').glob('*.py')
return sorted(get_loaded_rules(sorted(bundled) + sorted(user)),
paths = [rule_path for path in get_rules_import_paths()
for rule_path in sorted(path.glob('*.py'))]
return sorted(get_loaded_rules(paths),
key=lambda rule: rule.priority)


Expand Down

0 comments on commit 4847078

Please sign in to comment.