Skip to content

Commit

Permalink
Add appdirs support
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
cthoyt committed Nov 12, 2021
1 parent 27cbb2f commit ec10149
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ rdf_graph: rdflib.Graph = pystow.ensure_rdf('rhea', url=url)

Also see `pystow.ensure_excel()`, `pystow.ensure_rdf()`, `pystow.ensure_zip_df()`, and `pystow.ensure_tar_df()`.

### ⚙️️ Configuration
## ⚙️️ Configuration

By default, data is stored in the `$HOME/.data` directory. By default, the `<app>` app will create the
`$HOME/.data/<app>` folder.
Expand Down Expand Up @@ -144,6 +144,18 @@ pykeen_directory = pystow.join('pykeen')

Note: if you set `PYSTOW_HOME`, then `PYSTOW_NAME` is disregarded.

### XGD

While PyStow's main goal is to make application data less opaque and less
hidden, some users might want to use the
[XGD spec](http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html)
for storing their app data.

If you set `PYSTOW_USE_APPDIRS` to `true` or `True`, then the
[`appdirs`](https://pypi.org/project/appdirs/) package will be used to choose
the base directory based on the `user data dir` option. This can still be
overridden by `PYSTOW_HOME`.

## 🚀 Installation

The most recent release can be installed from
Expand Down
19 changes: 17 additions & 2 deletions src/pystow/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

PYSTOW_NAME_ENVVAR = "PYSTOW_NAME"
PYSTOW_HOME_ENVVAR = "PYSTOW_HOME"
PYSTOW_USE_APPDIRS = "PYSTOW_USE_APPDIRS"
PYSTOW_NAME_DEFAULT = ".data"


Expand All @@ -50,9 +51,18 @@ def get_name() -> str:
return os.getenv(PYSTOW_NAME_ENVVAR, default=PYSTOW_NAME_DEFAULT)


def _use_appdirs() -> bool:
return os.getenv(PYSTOW_USE_APPDIRS) in {"true", "True"}


def get_home(ensure_exists: bool = True) -> Path:
"""Get the PyStow home directory."""
default = Path.home() / get_name()
if _use_appdirs():
from appdirs import user_data_dir

default = Path(user_data_dir())
else:
default = Path.home() / get_name()
return getenv_path(PYSTOW_HOME_ENVVAR, default, ensure_exists=ensure_exists)


Expand All @@ -71,7 +81,12 @@ def get_base(key: str, ensure_exists: bool = True) -> Path:
"""
_assert_valid(key)
envvar = f"{key.upper()}_HOME"
default = get_home(ensure_exists=False) / key
if _use_appdirs():
from appdirs import user_data_dir

default = Path(user_data_dir(appname=key))
else:
default = get_home(ensure_exists=False) / key
return getenv_path(envvar, default, ensure_exists=ensure_exists)


Expand Down

0 comments on commit ec10149

Please sign in to comment.