Skip to content

Commit

Permalink
Remove dependency on xdg package
Browse files Browse the repository at this point in the history
Implements and improves the functionality provided by the `xdg` package
in `getenv_dir()` and `get_cache_dir()`.
  • Loading branch information
superatomic committed Jul 23, 2023
1 parent 9c9f801 commit 012fac9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 32 deletions.
13 changes: 1 addition & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ include = ["generate_completions.sh"]
python = "^3.10.4"
click = "^8.1.3"
click-help-colors = "^0.9.1"
xdg = "^5.1.1"
requests = "^2.28.1"

[build-system]
Expand Down
6 changes: 3 additions & 3 deletions src/tldr_man/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from click import Context

from tldr_man.pages import TLDR_CACHE_HOME, language_directory_to_code
from tldr_man.pages import CACHE_DIR, language_directory_to_code
from tldr_man.util import exit_with


Expand All @@ -32,7 +32,7 @@ def all_language_codes() -> Iterator[str]:
"""Returns an iterator of all language codes, based on all language directories."""
return (
language_directory_to_code(pages_dir)
for pages_dir in TLDR_CACHE_HOME.iterdir()
for pages_dir in CACHE_DIR.iterdir()
if pages_dir.is_dir()
)

Expand Down Expand Up @@ -83,7 +83,7 @@ def get_language_directory(language_code: str) -> str:
return 'pages'
else:
full_locale = f'pages.{language}_{region}'
if (TLDR_CACHE_HOME / full_locale).is_dir():
if (CACHE_DIR / full_locale).is_dir():
return full_locale
else:
return f'pages.{language}'
Expand Down
33 changes: 20 additions & 13 deletions src/tldr_man/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@
from concurrent.futures import ThreadPoolExecutor
from contextlib import suppress
from pathlib import Path
from os import remove, makedirs
from os import remove, makedirs, getenv
from shutil import rmtree, move
from subprocess import run, PIPE, DEVNULL
from typing import Optional
from collections.abc import Iterable

import requests
from click import secho, progressbar, style, echo
from xdg import XDG_CACHE_HOME

from tldr_man.util import mkstemp_path, mkdtemp_path, eprint, exit_with

TLDR_CACHE_DIR_NAME = 'tldr-man'
CACHE_DIR_NAME = 'tldr-man'

ZIP_ARCHIVE_URL = "https://tldr.sh/assets/tldr.zip"

Expand Down Expand Up @@ -78,11 +77,19 @@
"""[1:-1]


def tldr_cache_home(location: Path = XDG_CACHE_HOME) -> Path:
return location / TLDR_CACHE_DIR_NAME
def getenv_dir(key: str, default: Optional[Path] = None) -> Optional[Path]:
if value := getenv(key):
path = Path(value)
if path.is_absolute():
return path.resolve()
return default


TLDR_CACHE_HOME: Path = tldr_cache_home()
def get_cache_dir() -> Path:
return getenv_dir('XDG_CACHE_HOME', Path.home() / '.cache') / CACHE_DIR_NAME


CACHE_DIR: Path = get_cache_dir()


def download_archive(location: Path, url: str = ZIP_ARCHIVE_URL) -> None:
Expand Down Expand Up @@ -138,8 +145,8 @@ def update_cache() -> None:
# Get the directory where the old versions of the manpages are located,
# to compare it with the new versions that are generated:
original_dir = (
TLDR_CACHE_HOME / language_dir.name / sections_dir.name / ('man' + MANPAGE_SECTION)
if TLDR_CACHE_HOME.exists() else None
CACHE_DIR / language_dir.name / sections_dir.name / ('man' + MANPAGE_SECTION)
if CACHE_DIR.exists() else None
)

# Create the label for the progress bars that are shown.
Expand Down Expand Up @@ -194,10 +201,10 @@ def to_manpage(tldr_page: zipfile.Path) -> tuple[str, str]:
# and move the new cache into the correct directory from the temporary directory.

with suppress(FileNotFoundError):
rmtree(TLDR_CACHE_HOME)
rmtree(CACHE_DIR)

makedirs(TLDR_CACHE_HOME.parent, exist_ok=True)
move(temp_cache_dir, TLDR_CACHE_HOME)
makedirs(CACHE_DIR.parent, exist_ok=True)
move(temp_cache_dir, CACHE_DIR)

finally:
# Clean up any temporary files that aren't gone.
Expand Down Expand Up @@ -266,7 +273,7 @@ def pandoc_exists() -> bool:

def verify_tldr_cache_exists():
"""Display a specific message if the tldr manpage cache doesn't exist yet, and then exit."""
if not TLDR_CACHE_HOME.exists():
if not CACHE_DIR.exists():
exit_with(CACHE_DOES_NOT_EXIST_MESSAGE)


Expand All @@ -292,7 +299,7 @@ def find_page(page_name: str, /, locales: Iterable[str], page_sections: Iterable

def get_dir_search_order(locales: Iterable[str], page_sections: Iterable[str]) -> Iterable[Path]:
return (
TLDR_CACHE_HOME / locale / section / ('man' + MANPAGE_SECTION)
CACHE_DIR / locale / section / ('man' + MANPAGE_SECTION)
for locale in locales
for section in page_sections
)
Expand Down
6 changes: 3 additions & 3 deletions src/tldr_man/shell_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from click import Context, Parameter
from click.shell_completion import CompletionItem

from tldr_man.pages import TLDR_CACHE_HOME, get_dir_search_order
from tldr_man.pages import CACHE_DIR, get_dir_search_order
from tldr_man.languages import get_locales, all_language_codes
from tldr_man.platforms import get_page_sections


def page_shell_complete(ctx: Context, param: Parameter, incomplete: str) -> list[CompletionItem]:
if not TLDR_CACHE_HOME.exists() or param.name is None: # the `param.name is None` check makes the type checker happy
if not CACHE_DIR.exists() or param.name is None: # the `param.name is None` check makes the type checker happy
return []

locales: list[str] = get_locales(ctx)
Expand All @@ -41,6 +41,6 @@ def page_shell_complete(ctx: Context, param: Parameter, incomplete: str) -> list


def language_shell_complete(_ctx: Context, _param: Parameter, _incomplete: str) -> list[CompletionItem]:
if not TLDR_CACHE_HOME.exists():
if not CACHE_DIR.exists():
return []
return [CompletionItem(code) for code in all_language_codes()]

0 comments on commit 012fac9

Please sign in to comment.