omdev 0.0.0.dev210__py3-none-any.whl → 0.0.0.dev212__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
omdev/ci/utils.py CHANGED
@@ -1,10 +1,14 @@
1
1
  # ruff: noqa: UP006 UP007
2
2
  # @omlish-lite
3
3
  import hashlib
4
+ import logging
4
5
  import os.path
5
6
  import tempfile
7
+ import time
6
8
  import typing as ta
7
9
 
10
+ from omlish.lite.logs import log
11
+
8
12
 
9
13
  ##
10
14
 
@@ -30,3 +34,48 @@ def read_yaml_file(yaml_file: str) -> ta.Any:
30
34
 
31
35
  def sha256_str(s: str) -> str:
32
36
  return hashlib.sha256(s.encode('utf-8')).hexdigest()
37
+
38
+
39
+ ##
40
+
41
+
42
+ class LogTimingContext:
43
+ DEFAULT_LOG: ta.ClassVar[logging.Logger] = log
44
+
45
+ def __init__(
46
+ self,
47
+ description: str,
48
+ *,
49
+ log: ta.Optional[logging.Logger] = None, # noqa
50
+ level: int = logging.DEBUG,
51
+ ) -> None:
52
+ super().__init__()
53
+
54
+ self._description = description
55
+ self._log = log if log is not None else self.DEFAULT_LOG
56
+ self._level = level
57
+
58
+ def set_description(self, description: str) -> 'LogTimingContext':
59
+ self._description = description
60
+ return self
61
+
62
+ _begin_time: float
63
+ _end_time: float
64
+
65
+ def __enter__(self) -> 'LogTimingContext':
66
+ self._begin_time = time.time()
67
+
68
+ self._log.log(self._level, f'Begin {self._description}') # noqa
69
+
70
+ return self
71
+
72
+ def __exit__(self, exc_type, exc_val, exc_tb):
73
+ self._end_time = time.time()
74
+
75
+ self._log.log(
76
+ self._level,
77
+ f'End {self._description} - {self._end_time - self._begin_time:0.2f} s elapsed',
78
+ )
79
+
80
+
81
+ log_timing_context = LogTimingContext