From 4a48108c698e6d76908070cc802d120c539931bc Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 16 Oct 2015 16:52:03 +0200 Subject: [PATCH 1/2] Follow the XDG Base Directory Specification --- README.md | 6 +++--- thefuck/conf.py | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 199f37b8..e06fb366 100644 --- a/README.md +++ b/README.md @@ -224,7 +224,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 @@ -241,7 +241,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`: @@ -271,7 +271,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) From 8f25c95f06f03b75ddca190141b2a630e1a2b811 Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 16 Oct 2015 17:33:52 +0200 Subject: [PATCH 2/2] Use XDG_CACHE_HOME for cache --- thefuck/utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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])