-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (47 loc) · 1.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
import logging
from argparse import Namespace
from pathlib import Path
from cli import get_argv
from shaen_logger import slog
from shaen_logger.log import set_log_level
from constants import REQ_PY_VER
def get_log_level(ns: Namespace):
return (ns.verbose - ns.quiet) * 10
def read_all_input_files(ns: Namespace,
read_in: str = ""):
"""Read in all infile data iteratively using helper generator"""
for inp in _read_input_helper(ns):
read_in += inp.read()
return read_in
def _read_input_helper(ns: Namespace):
"""Iterate over input files."""
for path in ns.input:
if path == '-':
yield sys.stdin
else:
yield Path(path).open('r', encoding='utf-8')
def main(argv: Namespace):
set_log_level(get_log_level(argv))
slog.info("=" * 20)
slog.info("STARTING NEW EXECUTION...")
slog.info("Running main.py")
slog.info("=" * 20)
slog.debug(f"slog: handlers={slog.handlers}, level={slog.level}")
slog.info(f"Writing {argv.input} files to {argv.output}...")
with argv.output as fo:
fo.write(
read_all_input_files(argv)
)
if __name__ == '__main__':
if sys.version_info < REQ_PY_VER:
sys.stderr.write(
f"Use python {REQ_PY_VER[0]}.{REQ_PY_VER[1]} or later to run this "
)
sys.exit(1)
try:
if isinstance(slog, logging.Logger):
main(get_argv())
except ValueError as e:
print(f"There is a failure referencing slog from shaen_logger: {e}")
sys.exit(1)