|
| 1 | +"""User configuration management.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import xdg.BaseDirectory # type: ignore |
| 8 | + |
| 9 | +from .logging import get_logger |
| 10 | +from .settings import PACKAGE_NAME |
| 11 | + |
| 12 | +logger = get_logger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +def config_get(key: str, default: Any | None = None) -> Any | None: |
| 16 | + """Reads a config from the first level of a JSON file by key. |
| 17 | +
|
| 18 | + :param key: the name of the top-level JSON property. |
| 19 | +
|
| 20 | + :param default: |
| 21 | + a default value if the property is not present in the JSON |
| 22 | + or if its value is `null`. |
| 23 | + Defaults to `None`. |
| 24 | +
|
| 25 | + :return: |
| 26 | + the value as a Python object. |
| 27 | + Returns `None` if the JSON value is `null` or if `key` does |
| 28 | + not exist as a property at the top-level. |
| 29 | + """ |
| 30 | + path = os.path.join(config_dir(), f'{PACKAGE_NAME}.json') |
| 31 | + if not os.path.exists(path): |
| 32 | + logger.debug( |
| 33 | + 'File %s does not exist; interpreting %s == ', path, default |
| 34 | + ) |
| 35 | + return default |
| 36 | + with open(path, 'r', encoding='utf-8') as stream: |
| 37 | + config_dict = json.load(stream) |
| 38 | + return config_dict.get(key, default) |
| 39 | + |
| 40 | + |
| 41 | +def config_set(key: str, value: Any) -> None: |
| 42 | + """Writes a key-value pair to the config JSON file. |
| 43 | +
|
| 44 | + Overwrites an existing property by the given key. |
| 45 | +
|
| 46 | + :param key: the name of the top-level JSON property to store. |
| 47 | +
|
| 48 | + :param value: the value as a Python object, or `None` for `null`. |
| 49 | + """ |
| 50 | + path = os.path.join(config_dir(), f'{PACKAGE_NAME}.json') |
| 51 | + if os.path.exists(path): |
| 52 | + with open(path, 'r+', encoding='utf-8') as stream: |
| 53 | + config_dict = json.load(stream) or {} |
| 54 | + if value and value == config_dict.get(key, None): |
| 55 | + logger.debug('Configuration unchanged for key %s', key) |
| 56 | + return |
| 57 | + config_dict[key] = value |
| 58 | + logger.info('Writing new value for key %s: %s', key, value) |
| 59 | + stream.seek(0) |
| 60 | + stream.truncate(0) |
| 61 | + json.dump(config_dict, stream) |
| 62 | + os.fsync(stream) |
| 63 | + else: |
| 64 | + with open(path, 'x', encoding='utf-8') as stream: |
| 65 | + logger.info( |
| 66 | + 'Creating configuration file %s with value %s: %s', |
| 67 | + path, |
| 68 | + key, |
| 69 | + value, |
| 70 | + ) |
| 71 | + json.dump({key: value}, stream) |
| 72 | + os.fsync(stream) |
| 73 | + |
| 74 | + logger.info('New value written') |
| 75 | + |
| 76 | + |
| 77 | +def config_dir(subdir: str | None = None) -> str: |
| 78 | + """Returns the configuration directory for this package. |
| 79 | +
|
| 80 | + Respects the XDG settings. Also creates the directory if it does |
| 81 | + not exist. |
| 82 | +
|
| 83 | + :param subdir: an optional subdirectory, relative to the |
| 84 | + configuration directory. |
| 85 | +
|
| 86 | + :return: the absolute path to the directory. |
| 87 | + """ |
| 88 | + return str( |
| 89 | + xdg.BaseDirectory.save_config_path( |
| 90 | + os.path.join(PACKAGE_NAME, subdir) |
| 91 | + if subdir |
| 92 | + else PACKAGE_NAME |
| 93 | + ) |
| 94 | + ) |
0 commit comments