Coverage for paikalta/__init__.py: 98.25%
47 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 21:11:26 +00:00
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 21:11:26 +00:00
1"""From the place (Finnish: paikalta) we derive the name."""
3import datetime as dti
4import logging
5import os
6import pathlib
7from typing import List, no_type_check
9# [[[fill git_describe()]]]
10__version__ = '2023.6.18+parent.65b57909'
11# [[[end]]] (checksum: 8dab14bac1372382ee45751b9d630a91)
12__version_info__ = tuple(
13 e if '-' not in e else e.split('-')[0] for part in __version__.split('+') for e in part.split('.') if e != 'parent'
14)
16APP_ALIAS = str(pathlib.Path(__file__).parent.name)
17APP_ENV = APP_ALIAS.upper()
18APP_NAME = locals()['__doc__']
19APP_VERSION = __version__
20DEBUG = bool(os.getenv(f'{APP_ENV}_DEBUG', ''))
21VERBOSE = bool(os.getenv(f'{APP_ENV}_VERBOSE', ''))
22QUIET = False
23STRICT = bool(os.getenv(f'{APP_ENV}_STRICT', ''))
24ENCODING = 'utf-8'
25ENCODING_ERRORS_POLICY = 'ignore'
26DEFAULT_CONFIG_NAME = f'.{APP_ALIAS}.json'
28COMMA = ','
29INVALID_ID = '_invalid'
30VALID_NAME_PAT = r'([^+\-a-z0-9]+)'
31SUCC = os.getenv(f'{APP_ENV}_SUCC', 'TRUE')
32FAIL = os.getenv(f'{APP_ENV}_FAIL', 'FALSE')
34log = logging.getLogger() # Module level logger is sufficient
35LOG_FOLDER = pathlib.Path('logs')
36LOG_FILE = f'{APP_ALIAS}.log'
37LOG_PATH = pathlib.Path(LOG_FOLDER, LOG_FILE) if LOG_FOLDER.is_dir() else pathlib.Path(LOG_FILE)
38LOG_LEVEL = logging.INFO
39LOG_SEPARATOR = '- ' * 80
42TS_FORMAT_LOG = '%Y-%m-%dT%H:%M:%S'
43TS_FORMAT_PAYLOADS = '%Y-%m-%d %H:%M:%S.%f UTC'
45__all__: List[str] = [
46 'APP_ALIAS',
47 'APP_ENV',
48 'APP_VERSION',
49 'ENCODING',
50 'FAIL',
51 'INVALID_ID',
52 'LOG_SEPARATOR',
53 'SUCC',
54 'TS_FORMAT_PAYLOADS',
55 'log',
56 'parse_csl',
57]
60def parse_csl(csl: str) -> List[str]:
61 """DRY."""
62 return [fmt.strip().lower() for fmt in csl.split(COMMA) if fmt.strip()]
65@no_type_check
66def parse_csl_as_is(csl: str) -> List[str]:
67 """DRY."""
68 return tuple(fmt.strip() for fmt in csl.split(COMMA) if fmt.strip())[:2]
71@no_type_check
72def formatTime_RFC3339(self, record, datefmt=None): # noqa
73 """HACK A DID ACK we could inject .astimezone() to localize ..."""
74 return dti.datetime.fromtimestamp(record.created, dti.timezone.utc).isoformat() # pragma: no cover
77@no_type_check
78def init_logger(name=None, level=None):
79 """Initialize module level logger"""
80 global log # pylint: disable=global-statement
82 log_format = {
83 'format': '%(asctime)s %(levelname)s [%(name)s]: %(message)s',
84 'datefmt': TS_FORMAT_LOG,
85 # 'filename': LOG_PATH,
86 'level': LOG_LEVEL if level is None else level,
87 }
88 logging.Formatter.formatTime = formatTime_RFC3339
89 logging.basicConfig(**log_format)
90 log = logging.getLogger(APP_ENV if name is None else name)
91 log.propagate = True
94init_logger(name=APP_ENV, level=logging.DEBUG if DEBUG else None)