Coverage for vitriini/__init__.py: 92.31%

44 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-05 19:54:27 +00:00

1"""Showcase (Finnish: vitriini) some packaged content - guided by conventions.""" 

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.10.22+parent.737a503c' 

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

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' 

26 

27APP_VERSION = __version__ 

28COMMA = ',' 

29 

30MAX_PACKED_BYTES = int(os.getenv(f'{APP_ENV}_MAX_PACKED_BYTES', '20_000_000')) 

31MAX_UNPACKED_BYTES = int(os.getenv(f'{APP_ENV}_MAX_UNPACKED_BYTES', '200_000_000')) 

32 

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

34LOG_FOLDER = pathlib.Path('logs') 

35LOG_FILE = f'{APP_ALIAS}.log' 

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

37LOG_LEVEL = logging.INFO 

38LOG_SEPARATOR = '- ' * 80 

39 

40 

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

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

43 

44__all__: List[str] = [ 

45 'APP_ALIAS', 

46 'APP_ENV', 

47 'APP_VERSION', 

48 'ENCODING', 

49 'LOG_SEPARATOR', 

50 'MAX_PACKED_BYTES', 

51 'MAX_UNPACKED_BYTES', 

52 'TS_FORMAT_PAYLOADS', 

53 'log', 

54 'parse_csl', 

55] 

56 

57 

58def parse_csl(csl: str) -> List[str]: 

59 """DRY.""" 

60 return [fmt.strip().lower() for fmt in csl.split(COMMA) if fmt.strip()] 

61 

62 

63def parse_csl_as_is(csl: str) -> tuple[str, ...]: 

64 """DRY.""" 

65 return tuple(fmt.strip() for fmt in csl.split(COMMA) if fmt.strip())[:2] 

66 

67 

68@no_type_check 

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

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

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

72 

73 

74@no_type_check 

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

76 """Initialize module level logger""" 

77 global log # pylint: disable=global-statement 

78 

79 log_format = { 

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

81 'datefmt': TS_FORMAT_LOG, 

82 # 'filename': LOG_PATH, 

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

84 } 

85 logging.Formatter.formatTime = formatTime_RFC3339 

86 logging.basicConfig(**log_format) 

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

88 log.propagate = True 

89 

90 

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