diff --git a/README.md b/README.md index d3a58681..f6143223 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,7 @@ Bundled, but not enabled by default: ## Creating your own rules For adding your own rule you should create `your-rule-name.py` -in `~/.thefuck/rules`. The rule should contain two functions: +in `~/.config/thefuck/rules`. The rule should contain two functions: ```python match(command: Command) -> bool @@ -242,7 +242,7 @@ and optional `enabled_by_default`, `requires_output` and `priority` variables. `Command` has three attributes: `script`, `stdout` and `stderr`. *Rules api changed in 3.0:* For accessing settings in rule you need to import it with `from thefuck.conf import settings`. -`settings` is a special object filled with `~/.thefuck/settings.py` and values from env ([see more below](#settings)). +`settings` is a special object filled with `~/.config/thefuck/settings.py` and values from env ([see more below](#settings)). Simple example of the rule for running script with `sudo`: @@ -272,7 +272,7 @@ requires_output = True ## Settings -The Fuck has a few settings parameters which can be changed in `~/.thefuck/settings.py`: +The Fuck has a few settings parameters which can be changed in `$XDG_CONFIG_HOME/thefuck/settings.py` (`$XDG_CONFIG_HOME` defaults to `~/.config`): * `rules` – list of enabled rules, by default `thefuck.conf.DEFAULT_RULES`; * `exclude_rules` – list of disabled rules, by default `[]`; diff --git a/thefuck/conf.py b/thefuck/conf.py index 3e6e55f5..54b2d370 100644 --- a/thefuck/conf.py +++ b/thefuck/conf.py @@ -26,7 +26,7 @@ ENV_TO_ATTR = {'THEFUCK_RULES': 'rules', 'THEFUCK_PRIORITY': 'priority', 'THEFUCK_DEBUG': 'debug'} -SETTINGS_HEADER = u"""# ~/.thefuck/settings.py: The Fuck settings file +SETTINGS_HEADER = u"""# The Fuck settings file # # The rules are defined as in the example bellow: # @@ -73,7 +73,15 @@ class Settings(dict): def _setup_user_dir(self): """Returns user config dir, create it when it doesn't exist.""" + + # for backward compatibility, use `~/.thefuck` if it exists user_dir = Path(os.path.expanduser('~/.thefuck')) + + if not user_dir.is_dir(): + default_xdg_config_dir = os.path.expanduser("~/.config") + xdg_config_dir = os.getenv("XDG_CONFIG_HOME", default_xdg_config_dir) + user_dir = Path(os.path.join(xdg_config_dir, 'thefuck')) + rules_dir = user_dir.joinpath('rules') if not rules_dir.is_dir(): rules_dir.mkdir(parents=True) diff --git a/thefuck/utils.py b/thefuck/utils.py index 06ec9342..fba72a3f 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -185,7 +185,18 @@ def cache(*depends_on): if cache.disabled: return fn(*args, **kwargs) - cache_path = settings.user_dir.joinpath('.thefuck-cache').as_posix() + default_xdg_cache_dir = os.path.expanduser("~/.cache") + cache_dir = os.getenv("XDG_CACHE_HOME", default_xdg_cache_dir) + cache_path = Path(cache_dir).joinpath('thefuck').as_posix() + + # Ensure the cache_path exists, Python 2 does not have the exist_ok + # parameter + try: + os.makedirs(cache_dir) + except OSError: + if not os.path.isdir(cache_dir): + raise + # A bit obscure, but simplest way to generate unique key for # functions and methods in python 2 and 3: key = '{}.{}'.format(fn.__module__, repr(fn).split('at')[0])