-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommons.py
49 lines (33 loc) · 1.14 KB
/
commons.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
import click
import time
import dataclasses
@dataclasses.dataclass(init=True, repr=True, frozen=True)
class _Config():
"""Global configuration object for the CLI"""
show_debug: bool
dry_run: bool
force: bool
copy_tmpdir: bool
# static global config instance
_instance: '_Config' = None
def get_config():
if _Config._instance is None:
_Config._instance = _Config(False, True, False, False)
return _Config._instance
def set_config(show_debug, dry_run, force, copy_tmpdir):
_Config._instance = _Config(show_debug, dry_run, force, copy_tmpdir)
return _Config._instance
def _now():
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
def debug(message):
if get_config().show_debug:
click.echo(click.style(f"{_now()} D {message}", fg='magenta'))
def error(message):
click.echo(click.style(f"{_now()} E {message}", fg='red'))
def errorWithException(message):
error(message)
raise EnvironmentError(message)
def info(message):
click.echo(click.style(f"{_now()} I {message}", fg='blue'))
def warn(message):
click.echo(click.style(f"{_now()} W {message}", fg='yellow'))