From e0be27cddf8869eaefe903ac2bb497921a754039 Mon Sep 17 00:00:00 2001 From: mainrs <5113257+mainrs@users.noreply.github.com> Date: Mon, 3 Jan 2022 23:23:43 +0100 Subject: [PATCH] refactor: remove xdg dependency The minimal functionality that was needed has been implemented by hand. --- requirements.txt | 1 - thefuck/shells/fish.py | 5 ++--- thefuck/utils.py | 48 ++++++++++++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/requirements.txt b/requirements.txt index aed860c8..33ae7a9d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,3 @@ pypandoc pytest-benchmark pytest-docker-pexpect twine -xdg diff --git a/thefuck/shells/fish.py b/thefuck/shells/fish.py index b06b7c0d..2b8be6c9 100644 --- a/thefuck/shells/fish.py +++ b/thefuck/shells/fish.py @@ -6,9 +6,8 @@ import six from .. import logs from ..conf import settings from ..const import ARGUMENT_PLACEHOLDER -from ..utils import DEVNULL, cache +from ..utils import DEVNULL, cache, data_dir from .generic import Generic -from xdg import xdg_data_home @cache('~/.config/fish/config.fish', '~/.config/fish/functions') @@ -84,7 +83,7 @@ class Fish(Generic): return command_script def _get_history_file_name(self): - return xdg_data_home().joinpath("fish").joinpath("fish_history") + return data_dir().joinpath("fish").joinpath("fish_history") def _get_history_line(self, command_script): return u'- cmd: {}\n when: {}\n'.format(command_script, int(time())) diff --git a/thefuck/utils.py b/thefuck/utils.py index 466e4ba5..e4df043d 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -196,6 +196,37 @@ def for_app(*app_names, **kwargs): return decorator(_for_app) +def _xdg_dir(env, default): + """ + Returns the XDG user directory by first checking the given environment variable + and then resulting to the default filepath. + + # Note + + The directory gets created if it doesn't exist. + """ + + default_xdg_dir = os.path.expanduser(default) + dir = os.getenv(env, default_xdg_dir) + + # Ensure the cache_path exists, Python 2 does not have the exist_ok + # parameter + try: + os.makedirs(dir) + except OSError: + if not os.path.isdir(dir): + raise + + return dir + +def cache_dir(): + """Returns the user's XDG cache directory.""" + return Path(_xdg_dir("XDG_CACHE_HOME", "~/.cache")) + +def data_dir(): + """Returns the user's XDG data directory.""" + return Path(_xdg_dir("XDG_DATA_HOME", "~/.local/share")) + class Cache(object): """Lazy read cache and save changes at exit.""" @@ -210,8 +241,7 @@ class Cache(object): self._db = {} def _setup_db(self): - cache_dir = self._get_cache_dir() - cache_path = Path(cache_dir).joinpath('thefuck').as_posix() + cache_path = cache_dir().joinpath('thefuck').as_posix() try: self._db = shelve.open(cache_path) @@ -223,20 +253,6 @@ class Cache(object): atexit.register(self._db.close) - def _get_cache_dir(self): - default_xdg_cache_dir = os.path.expanduser("~/.cache") - cache_dir = os.getenv("XDG_CACHE_HOME", default_xdg_cache_dir) - - # 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 - - return cache_dir - def _get_mtime(self, path): try: return str(os.path.getmtime(path))