-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
64 lines (49 loc) · 2 KB
/
common.py
File metadata and controls
64 lines (49 loc) · 2 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import json
import logging
import os
from typing import Any
class Config: # pylint: disable=too-few-public-methods
_HOME = os.path.expanduser("~")
_XDG_CONFIG_HOME = os.environ.get("XDG_CONFIG_HOME") or os.path.join(
_HOME, ".config"
)
_CONFIG_PATH = os.path.join(_XDG_CONFIG_HOME, "scripts")
def __init__(self, config_file: str):
self._config: dict[str, Any] | None = None
if os.path.isfile(config_file):
self._config_file = config_file
elif os.path.isfile(os.path.join(self._CONFIG_PATH, config_file)):
self._config_file = os.path.join(self._CONFIG_PATH, config_file)
else:
self._config_file = None
def get_config(self) -> dict[str, Any]:
if self._config is None:
self._config = self._read_config()
return self._config
def set_config(self, config: dict[str, Any]) -> None:
self._config = config
with open(self._config_file, "w", encoding="utf-8") as file:
return json.dump(config, file)
def get_value(self, key: str) -> Any | None:
return self.get_config().get(key, None)
def _read_config(self) -> dict[str, Any]:
if self._config_file:
with open(self._config_file, "r", encoding="utf-8") as file:
return json.load(file)
else:
return {}
class Logger:
def __init__(self) -> None:
logging.basicConfig(level=logging.INFO, format="%(message)s")
self._logger = logging.getLogger("scripts")
def verbose(self):
self._logger.setLevel(logging.DEBUG)
def debug(self, msg, *args, **kwargs) -> None:
self._logger.debug(msg, *args, **kwargs)
def info(self, msg, *args, **kwargs) -> None:
self._logger.info(msg, *args, **kwargs)
def warning(self, msg, *args, **kwargs) -> None:
self._logger.warning(msg, *args, **kwargs)
def error(self, msg, *args, **kwargs) -> None:
self._logger.error(msg, *args, **kwargs)