Coverage for omforme/__init__.py: 100.00%

36 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 21:04:04 +00:00

1"""Reshape (Danish: omforme).""" 

2 

3import datetime as dti 

4import logging 

5import os 

6import pathlib 

7from typing import no_type_check 

8 

9# [[[fill git_describe()]]] 

10__version__ = '2023.6.18+parent.d616e4ac' 

11# [[[end]]] (checksum: 28766419b9118cb56ee687dec300d5d8) 

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) 

15__all__: list[str] = [] 

16 

17APP_ALIAS = str(pathlib.Path(__file__).parent.name) 

18APP_ENV = APP_ALIAS.upper() 

19APP_NAME = locals()['__doc__'] 

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' 

27 

28DEFAULT_LF_ONLY = 'YES' 

29log = logging.getLogger() # Module level logger is sufficient 

30LOG_FOLDER = pathlib.Path('logs') 

31LOG_FILE = f'{APP_ALIAS}.log' 

32LOG_PATH = pathlib.Path(LOG_FOLDER, LOG_FILE) if LOG_FOLDER.is_dir() else pathlib.Path(LOG_FILE) 

33LOG_LEVEL = logging.INFO 

34 

35TS_FORMAT_LOG = '%Y-%m-%dT%H:%M:%S' 

36TS_FORMAT_PAYLOADS = '%Y-%m-%d %H:%M:%S.%f UTC' 

37 

38 

39@no_type_check 

40def formatTime_RFC3339(self, record, datefmt=None): # noqa 

41 """HACK A DID ACK we could inject .astimezone() to localize ...""" 

42 return dti.datetime.fromtimestamp(record.created, dti.timezone.utc).isoformat() # pragma: no cover 

43 

44 

45@no_type_check 

46def init_logger(name=None, level=None): 

47 """Initialize module level logger""" 

48 global log # pylint: disable=global-statement 

49 

50 log_format = { 

51 'format': '%(asctime)s %(levelname)s [%(name)s]: %(message)s', 

52 'datefmt': TS_FORMAT_LOG, 

53 # 'filename': LOG_PATH, 

54 'level': LOG_LEVEL if level is None else level, 

55 } 

56 logging.Formatter.formatTime = formatTime_RFC3339 

57 logging.basicConfig(**log_format) 

58 log = logging.getLogger(APP_ENV if name is None else name) 

59 log.propagate = True 

60 

61 

62init_logger(name=APP_ENV, level=logging.DEBUG if DEBUG else None)