Coverage for putki/__init__.py: 100.00%

41 statements  

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

1"""Pipeline (Finnish: putki) - discovering and executing a specific task description.""" 

2 

3import datetime as dti 

4import logging 

5import os 

6import pathlib 

7from typing import List, no_type_check 

8 

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

10__version__ = '2023.11.29+parent.g53f523fd' 

11# [[[end]]] (checksum: a851508854340075dd2880298dc6133b) 

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 

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

17APP_ENV = APP_ALIAS.upper() 

18APP_NAME = locals()['__doc__'] 

19DEBUG = bool(os.getenv(f'{APP_ENV}_DEBUG', '')) 

20VERBOSE = bool(os.getenv(f'{APP_ENV}_VERBOSE', '')) 

21QUIET = False 

22STRICT = bool(os.getenv(f'{APP_ENV}_STRICT', '')) 

23ENCODING = 'utf-8' 

24ENCODING_ERRORS_POLICY = 'ignore' 

25DEFAULT_CONFIG_NAME = f'.{APP_ALIAS}.json' 

26DEFAULT_LF_ONLY = 'YES' 

27DEFAULT_STRUCTURE_NAME = 'structure.yml' 

28DEFAULT_STRUCTURES_NAME = 'structures.yml' 

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 

34VERSION = __version__ 

35VERSION_DOTTED_TRIPLE = '.'.join(__version_info__[:3]) 

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

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

38TS_FORMAT_GENERATOR = '%Y-%m-%d %H:%M:%S.%f +00:00' 

39 

40__all__: List[str] = [ 

41 'APP_ALIAS', 

42 'APP_ENV', 

43 'APP_NAME', 

44 'DEBUG', 

45 'DEFAULT_CONFIG_NAME', 

46 'DEFAULT_STRUCTURE_NAME', 

47 'DEFAULT_STRUCTURES_NAME', 

48 'ENCODING', 

49 'TS_FORMAT_GENERATOR', 

50 'VERSION', 

51 'VERSION_DOTTED_TRIPLE', 

52 'log', 

53] 

54 

55 

56@no_type_check 

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

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

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

60 

61 

62@no_type_check 

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

64 """Initialize module level logger""" 

65 global log # pylint: disable=global-statement 

66 

67 log_format = { 

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

69 'datefmt': TS_FORMAT_LOG, 

70 # 'filename': LOG_PATH, 

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

72 } 

73 logging.Formatter.formatTime = formatTime_RFC3339 

74 logging.basicConfig(**log_format) 

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

76 log.propagate = True 

77 

78 

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