generated from dbt-labs/dbt-oss-template
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathformat.py
54 lines (40 loc) · 1.56 KB
/
format.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
from dbt_common import ui
from typing import Optional, Union
from datetime import datetime
from dbt_common.events.interfaces import LoggableDbtObject
def format_fancy_output_line(
msg: str,
status: str,
index: Optional[int],
total: Optional[int],
execution_time: Optional[float] = None,
truncate: bool = False,
) -> str:
if index is None or total is None:
progress = ""
else:
progress = "{} of {} ".format(index, total)
prefix = "{progress}{message} ".format(progress=progress, message=msg)
truncate_width = ui.printer_width() - 3
justified = prefix.ljust(ui.printer_width(), ".")
if truncate and len(justified) > truncate_width:
justified = justified[:truncate_width] + "..."
if execution_time is None:
status_time = ""
else:
status_time = " in {execution_time:0.2f}s".format(execution_time=execution_time)
output = "{justified} [{status}{status_time}]".format(justified=justified, status=status, status_time=status_time)
return output
def _pluralize(string: Union[str, LoggableDbtObject]) -> str:
if isinstance(string, LoggableDbtObject):
return string.pluralize()
else:
return f"{string}s"
def pluralize(count, string: Union[str, LoggableDbtObject]) -> str:
pluralized: str = str(string)
if count != 1:
pluralized = _pluralize(string)
return f"{count} {pluralized}"
def timestamp_to_datetime_string(ts) -> str:
timestamp_dt = datetime.fromtimestamp(ts.seconds + ts.nanos / 1e9)
return timestamp_dt.strftime("%H:%M:%S.%f")