Coverage for kiertotie/__init__.py: 0.00%
57 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 18:44:05 +00:00
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 18:44:05 +00:00
1"""Detour (Finnish: kiertotie) per rsync proxy to https mirror node."""
3import datetime as dti
4import json
5import logging
6import os
7import pathlib
8from typing import List, Union, no_type_check
10# [[[fill git_describe()]]]
11__version__ = '2023.1.5+parent.ccac1ba7'
12# [[[end]]] (checksum: 1658aff3b014e31e3ee3121c56f64e36)
13__version_info__ = tuple(
14 e if '-' not in e else e.split('-')[0] for part in __version__.split('+') for e in part.split('.') if e != 'parent'
15)
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'
28BASE_URL = 'https://master.qt.io/'
29DASH = '-'
30EASING = 3
31NL = '\n'
32SP = ' '
33ESP = '\\' + SP
34URL_ENC_SP = '%20'
35RATE = 2_000_000
36TS_FORMAT = '%Y-%m-%d %H:%M:%S +00:00'
37HTTP_404_FILE = """\
38<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
39<html><head>
40<title>404 Not Found</title>
41</head><body>
42<h1>Not Found</h1>
43<p>The requested URL was not found on this server.</p>
44</body></html>
45"""
46HTTP_404_SIZE_BYTES = 196
47HTTP_404_BYTES_TOKEN = b'<!DOCTYPE'
48HTTP_404_BYTES_TOKEN_LENGTH = len(HTTP_404_BYTES_TOKEN)
50DEFAULT_LF_ONLY = 'YES'
51log = logging.getLogger() # Module level logger is sufficient
52LOG_FOLDER = pathlib.Path('logs')
53LOG_FILE = f'{APP_ALIAS}.log'
54LOG_PATH = pathlib.Path(LOG_FOLDER, LOG_FILE) if LOG_FOLDER.is_dir() else pathlib.Path(LOG_FILE)
55LOG_LEVEL = logging.INFO
57TS_FORMAT_LOG = '%Y-%m-%dT%H:%M:%S'
58TS_FORMAT_PAYLOADS = '%Y-%m-%d %H:%M:%S.%f UTC'
60EntryType = dict[str, Union[int, str]]
61DimensionType = dict[str, list[EntryType]]
62ProxyType = dict[str, Union[int, DimensionType]]
64__all__: List[str] = [
65 'BASE_URL',
66 'DASH',
67 'EASING',
68 'RATE',
69 'ENCODING',
70 'NL',
71 'SP',
72 'ESP',
73 'URL_ENC_SP',
74 'TS_FORMAT',
75 'HTTP_404_FILE',
76 'HTTP_404_SIZE_BYTES',
77 'HTTP_404_BYTES_TOKEN',
78 'HTTP_404_BYTES_TOKEN_LENGTH',
79 'ProxyType',
80 'load',
81 'log',
82]
85@no_type_check
86def formatTime_RFC3339(self, record, datefmt=None): # noqa
87 """HACK A DID ACK we could inject .astimezone() to localize ..."""
88 return dti.datetime.fromtimestamp(record.created, dti.timezone.utc).isoformat() # pragma: no cover
91@no_type_check
92def init_logger(name=None, level=None):
93 """Initialize module level logger"""
94 global log # pylint: disable=global-statement
96 log_format = {
97 'format': '%(asctime)s %(levelname)s [%(name)s]: %(message)s',
98 'datefmt': TS_FORMAT_LOG,
99 # 'filename': LOG_PATH,
100 'level': LOG_LEVEL if level is None else level,
101 }
102 logging.Formatter.formatTime = formatTime_RFC3339
103 logging.basicConfig(**log_format)
104 log = logging.getLogger(APP_ENV if name is None else name)
105 log.propagate = True
108def load(data_path: Union[str, pathlib.Path]) -> ProxyType:
109 """Load the data from JSON."""
110 with open(data_path, 'rt', encoding=ENCODING) as handle:
111 data: ProxyType = json.load(handle)
112 return data
115init_logger(name=APP_ENV, level=logging.DEBUG if DEBUG else None)