|
1 | | -# Copyright © 2016-2021 Scott Stevenson <[email protected]> |
2 | | -# Modifications copyright © 2022 onwards Jeremy Howard |
| 1 | +# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/09_xdg.ipynb. |
3 | 2 |
|
4 | | -"""XDG Base Directory Specification variables. |
| 3 | +# %% auto 0 |
| 4 | +__all__ = ['xdg_cache_home', 'xdg_config_dirs', 'xdg_config_home', 'xdg_data_dirs', 'xdg_data_home', 'xdg_runtime_dir', |
| 5 | + 'xdg_state_home'] |
5 | 6 |
|
6 | | -xdg_cache_home(), xdg_config_home(), xdg_data_home(), and xdg_state_home() |
7 | | -return pathlib.Path objects containing the value of the environment variable |
8 | | -named XDG_CACHE_HOME, XDG_CONFIG_HOME, XDG_DATA_HOME, and XDG_STATE_HOME |
9 | | -respectively, or the default defined in the specification if the environment |
10 | | -variable is unset, empty, or contains a relative path rather than absolute |
11 | | -path. |
| 7 | +# %% ../nbs/09_xdg.ipynb 3 |
| 8 | +from .utils import * |
12 | 9 |
|
13 | | -xdg_config_dirs() and xdg_data_dirs() return a list of pathlib.Path |
14 | | -objects containing the value, split on colons, of the environment |
15 | | -variable named XDG_CONFIG_DIRS and XDG_DATA_DIRS respectively, or the |
16 | | -default defined in the specification if the environment variable is |
17 | | -unset or empty. Relative paths are ignored, as per the specification. |
18 | | -
|
19 | | -xdg_runtime_dir() returns a pathlib.Path object containing the value of |
20 | | -the XDG_RUNTIME_DIR environment variable, or None if the environment |
21 | | -variable is not set, or contains a relative path rather than absolute path. |
22 | | -""" |
23 | | - |
24 | | -import os |
25 | | -from pathlib import Path |
26 | | -from typing import List, Optional |
27 | | - |
28 | | -__all__ = [ "xdg_cache_home", "xdg_config_dirs", "xdg_config_home", "xdg_data_dirs", "xdg_data_home", "xdg_runtime_dir", "xdg_state_home", |
29 | | - "XDG_CACHE_HOME", "XDG_CONFIG_DIRS", "XDG_CONFIG_HOME", "XDG_DATA_DIRS", "XDG_DATA_HOME", "XDG_RUNTIME_DIR" ] |
30 | | - |
31 | | - |
32 | | -def _path_from_env(variable: str, default: Path) -> Path: |
| 10 | +# %% ../nbs/09_xdg.ipynb 10 |
| 11 | +def _path_from_env(variable, default): |
33 | 12 | value = os.environ.get(variable) |
34 | 13 | if value and os.path.isabs(value): return Path(value) |
35 | 14 | return default |
36 | 15 |
|
37 | | -def _paths_from_env(variable: str, default: List[Path]) -> List[Path]: |
| 16 | +# %% ../nbs/09_xdg.ipynb 11 |
| 17 | +def _paths_from_env(variable, default): |
38 | 18 | value = os.environ.get(variable) |
39 | 19 | if value: |
40 | | - paths = [ Path(path) for path in value.split(":") if os.path.isabs(path) ] |
| 20 | + paths = [Path(o) for o in value.split(":") if os.path.isabs(o)] |
41 | 21 | if paths: return paths |
42 | 22 | return default |
43 | 23 |
|
44 | | -def xdg_cache_home() -> Path: |
45 | | - """Path corresponding to XDG_CACHE_HOME.""" |
| 24 | +# %% ../nbs/09_xdg.ipynb 12 |
| 25 | +def xdg_cache_home(): |
| 26 | + "Path corresponding to `XDG_CACHE_HOME`" |
46 | 27 | return _path_from_env("XDG_CACHE_HOME", Path.home()/".cache") |
47 | 28 |
|
48 | | -def xdg_config_dirs() -> List[Path]: |
49 | | - """Paths corresponding to XDG_CONFIG_DIRS.""" |
| 29 | +# %% ../nbs/09_xdg.ipynb 15 |
| 30 | +def xdg_config_dirs(): |
| 31 | + "Paths corresponding to `XDG_CONFIG_DIRS`" |
50 | 32 | return _paths_from_env("XDG_CONFIG_DIRS", [Path("/etc/xdg")]) |
51 | 33 |
|
52 | | -def xdg_config_home() -> Path: |
53 | | - """Path corresponding to XDG_CONFIG_HOME.""" |
| 34 | +# %% ../nbs/09_xdg.ipynb 17 |
| 35 | +def xdg_config_home(): |
| 36 | + "Path corresponding to `XDG_CONFIG_HOME`" |
54 | 37 | return _path_from_env("XDG_CONFIG_HOME", Path.home()/".config") |
55 | 38 |
|
56 | | -def xdg_data_dirs() -> List[Path]: |
57 | | - """Paths corresponding to XDG_DATA_DIRS.""" |
58 | | - return _paths_from_env( "XDG_DATA_DIRS", [Path(path) for path in "/usr/local/share/:/usr/share/".split(":")]) |
| 39 | +# %% ../nbs/09_xdg.ipynb 19 |
| 40 | +def xdg_data_dirs(): |
| 41 | + "Paths corresponding to XDG_DATA_DIRS`" |
| 42 | + return _paths_from_env( "XDG_DATA_DIRS", [Path(o) for o in "/usr/local/share/:/usr/share/".split(":")]) |
59 | 43 |
|
60 | | -def xdg_data_home() -> Path: |
61 | | - """Path corresponding to XDG_DATA_HOME.""" |
| 44 | +# %% ../nbs/09_xdg.ipynb 21 |
| 45 | +def xdg_data_home(): |
| 46 | + "Path corresponding to `XDG_DATA_HOME`" |
62 | 47 | return _path_from_env("XDG_DATA_HOME", Path.home()/".local"/"share") |
63 | 48 |
|
64 | | -def xdg_runtime_dir() -> Optional[Path]: |
65 | | - """Path corresponding to XDG_RUNTIME_DIR. """ |
| 49 | +# %% ../nbs/09_xdg.ipynb 23 |
| 50 | +def xdg_runtime_dir(): |
| 51 | + "Path corresponding to `XDG_RUNTIME_DIR`" |
66 | 52 | value = os.getenv("XDG_RUNTIME_DIR") |
67 | 53 | return Path(value) if value and os.path.isabs(value) else None |
68 | 54 |
|
69 | | -def xdg_state_home() -> Path: |
70 | | - """Path corresponding to XDG_STATE_HOME.""" |
| 55 | +# %% ../nbs/09_xdg.ipynb 25 |
| 56 | +def xdg_state_home(): |
| 57 | + "Path corresponding to `XDG_STATE_HOME`" |
71 | 58 | return _path_from_env("XDG_STATE_HOME", Path.home()/".local"/"state") |
72 | | - |
0 commit comments