Coverage for taksonomia/__init__.py: 98.04%

45 statements  

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

1"""Taxonomy (Finnish: taksonomia) of a folder tree, 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.6.18+parent.a8561973' 

11# [[[end]]] (checksum: 403d4b77cdd1dbbd3a56d736185c108d) 

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 = ',' 

29DEFAULT_LF_ONLY = 'YES' 

30KNOWN_FORMATS = ('json', 'xml', 'yaml') 

31KNOWN_KEY_FUNCTIONS = ('blake2', 'elf', 'md5') 

32TS_FORMAT = '%Y-%m-%d %H:%M:%S.%f +00:00' 

33VERSION_INFO = __version_info__ 

34 

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

36LOG_FOLDER = pathlib.Path('logs') 

37LOG_FILE = f'{APP_ALIAS}.log' 

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

39LOG_LEVEL = logging.INFO 

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_NAME', 

48 'APP_VERSION', 

49 'ENCODING', 

50 'TS_FORMAT_PAYLOADS', 

51 'VERSION_INFO', 

52 'log', 

53] 

54 

55 

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

57 """DRY.""" 

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

59 

60 

61@no_type_check 

62def formatTime_RFC3339(self, record, datefmt=None): 

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

64 return dti.datetime.fromtimestamp(record.created, dti.timezone.utc).isoformat() 

65 

66 

67@no_type_check 

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

69 """Initialize module level logger""" 

70 global log # pylint: disable=global-statement 

71 

72 log_format = { 

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

74 'datefmt': TS_FORMAT_LOG, 

75 # 'filename': LOG_PATH, 

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

77 } 

78 logging.Formatter.formatTime = formatTime_RFC3339 

79 logging.basicConfig(**log_format) 

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

81 log.propagate = True 

82 

83 

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