forked from redcanaryco/surveyor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.py
43 lines (32 loc) · 1.15 KB
/
help.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
import logging
import re
from datetime import datetime
import click
# regular expression that detects ANSI color codes
from tqdm import tqdm
ansi_escape_regex = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', re.VERBOSE)
def _strip_ansi_codes(message: str) -> str:
"""
Strip ANSI sequences from a log string
"""
return ansi_escape_regex.sub('', message)
def log_echo(message: str, log: logging.Logger, level: int = logging.DEBUG, use_tqdm: bool = False):
"""
Write a command to STDOUT and the debug log stream.
"""
color_message = message
if level == logging.WARNING:
color_message = f'\u001b[33m{color_message}\u001b[0m'
elif level >= logging.ERROR:
color_message = f'\u001b[31m{color_message}\u001b[0m'
if use_tqdm:
tqdm.write(color_message)
else:
click.echo(color_message)
# strip ANSI sequences from log string
log.log(level, _strip_ansi_codes(message))
def datetime_to_epoch_millis(date: datetime) -> int:
"""
Convert a datetime object to an epoch timestamp in milliseconds.
"""
return int((date - datetime.utcfromtimestamp(0)).total_seconds() * 1000)