1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-13 22:28:33 +00:00

refactor: remove xdg dependency

The minimal functionality that was needed has been implemented by hand.
This commit is contained in:
mainrs 2022-01-03 23:23:43 +01:00
parent 6512c8da4e
commit e0be27cddf
3 changed files with 34 additions and 20 deletions

View File

@ -9,4 +9,3 @@ pypandoc
pytest-benchmark
pytest-docker-pexpect
twine
xdg

View File

@ -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()))

View File

@ -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))