File: persistent_settings.py

package info (click to toggle)
python-bumps 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,200 kB
  • sloc: python: 24,517; xml: 493; ansic: 373; makefile: 211; javascript: 99; sh: 94
file content (28 lines) | stat: -rw-r--r-- 788 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# persist settings to disk

import sys
from pathlib import Path
from typing import Optional

ETC_PATH = Path(sys.prefix, "etc")
DEFAULT_APPLICATION = "bumps"


def set_value(key: str, value: str, application: str = DEFAULT_APPLICATION):
    settings_path = ETC_PATH / application / key
    try:
        settings_path.parent.mkdir(parents=True, exist_ok=True)
        with open(settings_path, "w") as f:
            f.write(value)
        return True
    except Exception as e:
        return False


def get_value(key: str, default_value: Optional[str] = None, application: str = DEFAULT_APPLICATION):
    settings_path = ETC_PATH / application / key
    try:
        with open(settings_path, "r") as f:
            return f.read()
    except Exception as e:
        return default_value