ddeutil-workflow 0.0.56__py3-none-any.whl → 0.0.57__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ddeutil/workflow/utils.py CHANGED
@@ -24,13 +24,45 @@ from .__types import DictData, Matrix
24
24
 
25
25
  T = TypeVar("T")
26
26
  UTC: Final[ZoneInfo] = ZoneInfo("UTC")
27
- NEWLINE: Final[str] = "\n\t| ...\t"
27
+ MARK_NL: Final[str] = "||"
28
+
29
+
30
+ def prepare_newline(msg: str) -> str:
31
+ """Prepare message that has multiple newline char.
32
+
33
+ :param msg: (str) A message that want to prepare.
34
+
35
+ :rtype: str
36
+ """
37
+ msg: str = msg.strip("\n").replace("\n", MARK_NL)
38
+ if MARK_NL not in msg:
39
+ return msg
40
+
41
+ msg_lines: list[str] = msg.split(MARK_NL)
42
+ msg_last: str = msg_lines[-1]
43
+ msg_body: str = (
44
+ "\n" + "\n".join(f" ... | \t{s}" for s in msg_lines[1:-1])
45
+ if len(msg_lines) > 2
46
+ else ""
47
+ )
48
+ return msg_lines[0] + msg_body + f"\n ... ╰─ \t{msg_last}"
28
49
 
29
50
 
30
51
  def replace_sec(dt: datetime) -> datetime:
52
+ """Replace second and microsecond values to 0.
53
+
54
+ :param dt: A datetime object that want to replace.
55
+
56
+ :rtype: datetime
57
+ """
31
58
  return dt.replace(second=0, microsecond=0)
32
59
 
33
60
 
61
+ def clear_tz(dt: datetime) -> datetime:
62
+ """Replace timezone info on an input datetime object to None."""
63
+ return dt.replace(tzinfo=None)
64
+
65
+
34
66
  def get_dt_now(tz: ZoneInfo | None = None, offset: float = 0.0) -> datetime:
35
67
  """Return the current datetime object.
36
68
 
@@ -40,7 +72,7 @@ def get_dt_now(tz: ZoneInfo | None = None, offset: float = 0.0) -> datetime:
40
72
  :rtype: datetime
41
73
  :return: The current datetime object that use an input timezone or UTC.
42
74
  """
43
- return datetime.now(tz=(tz or UTC)) - timedelta(seconds=offset)
75
+ return datetime.now(tz=tz) - timedelta(seconds=offset)
44
76
 
45
77
 
46
78
  def get_d_now(
@@ -54,7 +86,7 @@ def get_d_now(
54
86
  :rtype: date
55
87
  :return: The current date object that use an input timezone or UTC.
56
88
  """
57
- return (datetime.now(tz=(tz or UTC)) - timedelta(seconds=offset)).date()
89
+ return (datetime.now(tz=tz) - timedelta(seconds=offset)).date()
58
90
 
59
91
 
60
92
  def get_diff_sec(dt: datetime, offset: float = 0.0) -> int:
@@ -81,20 +113,19 @@ def reach_next_minute(dt: datetime, offset: float = 0.0) -> bool:
81
113
  :param offset: (float) An offset second value.
82
114
  """
83
115
  diff: float = (
84
- replace_sec(dt) - replace_sec(get_dt_now(tz=dt.tzinfo, offset=offset))
116
+ replace_sec(clear_tz(dt)) - replace_sec(get_dt_now(offset=offset))
85
117
  ).total_seconds()
86
118
  if diff >= 60:
87
119
  return True
88
120
  elif diff >= 0:
89
121
  return False
90
-
91
122
  raise ValueError(
92
123
  "Check reach the next minute function should check a datetime that not "
93
124
  "less than the current date"
94
125
  )
95
126
 
96
127
 
97
- def wait_to_next_minute(
128
+ def wait_until_next_minute(
98
129
  dt: datetime, second: float = 0
99
130
  ) -> None: # pragma: no cov
100
131
  """Wait with sleep to the next minute with an offset second value."""