Skip to content

Commit

Permalink
core: migrate from deprecated appdirs to platformdirs
Browse files Browse the repository at this point in the history
resolves #377
  • Loading branch information
karlicoss committed Feb 1, 2025
1 parent cf8b031 commit f0cc3db
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion doc/SETUP.org
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ I understand that people who'd like to use this may not be super familiar with P
- when you are using ~pip install~, [[https://stackoverflow.com/a/42989020/706389][always pass]] =--user=, and *never install third party packages with sudo* (unless you know what you are doing)
- throughout the guide I'm assuming the user config directory is =~/.config=, but it's *different on Mac/Windows*.

See [[https://github.com/ActiveState/appdirs/blob/3fe6a83776843a46f20c2e5587afcffe05e03b39/appdirs.py#L187-L190][this]] if you're not sure what's your user config dir.
See [[https://github.com/tox-dev/platformdirs?tab=readme-ov-file#example-output][this]] if you're not sure what's your user config dir.

* Install main HPI package
This is a *required step*
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "HPI"
dependencies = [
"pytz" , # even though it's not needed by the core, it's so common anyway...
"typing-extensions" , # one of the most common pypi packages, ok to depend for core
"appdirs" , # very common, and makes it portable
"platformdirs" , # very common, and makes it portable
"more-itertools" , # it's just too useful and very common anyway
"decorator" , # less pain in writing correct decorators. very mature and stable, so worth keeping in core
"click>=8.1" , # for the CLI, printing colors, decorator-based - may allow extensions to CLI
Expand Down
10 changes: 3 additions & 7 deletions src/my/core/cachew.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from __future__ import annotations

from .internal import assert_subpackage

assert_subpackage(__name__)

import logging
import sys
from collections.abc import Iterator
Expand All @@ -19,7 +15,7 @@
overload,
)

import appdirs # type: ignore[import-untyped]
import platformdirs

from . import warnings

Expand Down Expand Up @@ -52,8 +48,8 @@ def disabled_cachew() -> Iterator[None]:
yield


def _appdirs_cache_dir() -> Path:
cd = Path(appdirs.user_cache_dir('my'))
def _hpi_cache_dir() -> Path:
cd = Path(platformdirs.user_cache_dir('my'))
cd.mkdir(exist_ok=True, parents=True)
return cd

Expand Down
6 changes: 3 additions & 3 deletions src/my/core/core_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class config:
'''
Base directory for cachew.
- if None , means cache is disabled
- if '' (empty string), use user cache dir (see https://github.com/ActiveState/appdirs for more info). This is the default.
- if '' (empty string), use user cache dir (see https://github.com/tox-dev/platformdirs?tab=readme-ov-file#example-output for more info). This is the default.
- otherwise , use the specified directory as base cache directory
NOTE: you shouldn't use this attribute in HPI modules directly, use Config.get_cache_dir()/cachew.cache_dir() instead
Expand Down Expand Up @@ -73,9 +73,9 @@ def get_cache_dir(self) -> Path | None:
if cdir is None:
return None
if cdir == _HPI_CACHE_DIR_DEFAULT:
from .cachew import _appdirs_cache_dir
from .cachew import _hpi_cache_dir

return _appdirs_cache_dir()
return _hpi_cache_dir()
else:
return Path(cdir).expanduser()

Expand Down
4 changes: 2 additions & 2 deletions src/my/core/preinit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
def get_mycfg_dir() -> Path:
import os

import appdirs # type: ignore[import-untyped]
import platformdirs

# not sure if that's necessary, i.e. could rely on PYTHONPATH instead
# on the other hand, by using MY_CONFIG we are guaranteed to load it from the desired path?
mvar = os.environ.get('MY_CONFIG')
if mvar is not None:
mycfg_dir = Path(mvar)
else:
mycfg_dir = Path(appdirs.user_config_dir('my'))
mycfg_dir = Path(platformdirs.user_config_dir('my'))
return mycfg_dir

0 comments on commit f0cc3db

Please sign in to comment.