logger-36 2023.13__py3-none-any.whl → 2024.2__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.
Files changed (41) hide show
  1. logger_36/__init__.py +3 -6
  2. logger_36/catalog/config/console_rich.py +49 -0
  3. logger_36/catalog/handler/console.py +80 -0
  4. logger_36/{type/console.py → catalog/handler/console_rich.py} +77 -52
  5. logger_36/catalog/handler/file.py +90 -0
  6. logger_36/catalog/handler/generic.py +150 -0
  7. logger_36/catalog/logging/chronos.py +39 -0
  8. logger_36/catalog/logging/gpu.py +58 -0
  9. logger_36/catalog/logging/memory.py +105 -0
  10. logger_36/catalog/logging/system.py +62 -0
  11. logger_36/config/issue.py +32 -0
  12. logger_36/config/memory.py +33 -0
  13. logger_36/config/message.py +48 -0
  14. logger_36/{config.py → config/system.py} +0 -17
  15. logger_36/constant/generic.py +37 -0
  16. logger_36/constant/handler.py +35 -0
  17. logger_36/constant/issue.py +35 -0
  18. logger_36/{type/file.py → constant/logger.py} +13 -15
  19. logger_36/constant/memory.py +39 -0
  20. logger_36/{constant.py → constant/message.py} +11 -20
  21. logger_36/constant/record.py +35 -0
  22. logger_36/constant/system.py +39 -0
  23. logger_36/instance.py +34 -0
  24. logger_36/main.py +133 -124
  25. logger_36/{measure → task/format}/memory.py +35 -27
  26. logger_36/task/format/message.py +112 -0
  27. logger_36/task/format/rule.py +47 -0
  28. logger_36/task/inspection.py +18 -18
  29. logger_36/{measure → task/measure}/chronos.py +4 -2
  30. logger_36/task/measure/memory.py +50 -0
  31. logger_36/task/storage.py +144 -0
  32. logger_36/type/extension.py +73 -61
  33. logger_36/type/issue.py +57 -0
  34. logger_36/type/logger.py +349 -0
  35. logger_36/version.py +1 -1
  36. {logger_36-2023.13.dist-info → logger_36-2024.2.dist-info}/METADATA +1 -2
  37. logger_36-2024.2.dist-info/RECORD +39 -0
  38. {logger_36-2023.13.dist-info → logger_36-2024.2.dist-info}/WHEEL +1 -1
  39. logger_36/type/generic.py +0 -116
  40. logger_36-2023.13.dist-info/RECORD +0 -16
  41. {logger_36-2023.13.dist-info → logger_36-2024.2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,144 @@
1
+ # Copyright CNRS/Inria/UCA
2
+ # Contributor(s): Eric Debreuve (since 2023)
3
+ #
4
+ # eric.debreuve@cnrs.fr
5
+ #
6
+ # This software is governed by the CeCILL license under French law and
7
+ # abiding by the rules of distribution of free software. You can use,
8
+ # modify and/ or redistribute the software under the terms of the CeCILL
9
+ # license as circulated by CEA, CNRS and INRIA at the following URL
10
+ # "http://www.cecill.info".
11
+ #
12
+ # As a counterpart to the access to the source code and rights to copy,
13
+ # modify and redistribute granted by the license, users are provided only
14
+ # with a limited warranty and the software's author, the holder of the
15
+ # economic rights, and the successive licensors have only limited
16
+ # liability.
17
+ #
18
+ # In this respect, the user's attention is drawn to the risks associated
19
+ # with loading, using, modifying and/or developing or reproducing the
20
+ # software by the user in light of its specific status of free software,
21
+ # that may mean that it is complicated to manipulate, and that also
22
+ # therefore means that it is reserved for developers and experienced
23
+ # professionals having in-depth computer knowledge. Users are therefore
24
+ # encouraged to load and test the software's suitability as regards their
25
+ # requirements in conditions enabling the security of their systems and/or
26
+ # data to be ensured and, more generally, to use and operate it in the
27
+ # same conditions as regards security.
28
+ #
29
+ # The fact that you are presently reading this means that you have had
30
+ # knowledge of the CeCILL license and that you accept its terms.
31
+
32
+ import dataclasses as dtcl
33
+ import logging as lggg
34
+ import re as regx
35
+ import typing as h
36
+ from html.parser import HTMLParser as html_parser_t
37
+ from pathlib import Path as path_t
38
+
39
+ try:
40
+ from rich.console import Console as console_t
41
+ except ModuleNotFoundError:
42
+ console_t = None
43
+
44
+ from logger_36.instance import LOGGER
45
+
46
+
47
+ @dtcl.dataclass(slots=True, repr=False, eq=False)
48
+ class html_reader_t(html_parser_t):
49
+ BODY_END_PATTERN: h.ClassVar[str] = r"</[bB][oO][dD][yY]>(.|\n)*$"
50
+
51
+ source: str = ""
52
+ inside_body: bool = dtcl.field(init=False, default=False)
53
+ body_position_start: tuple[int, int] = dtcl.field(init=False, default=(-1, -1))
54
+ body_position_end: tuple[int, int] = dtcl.field(init=False, default=(-1, -1))
55
+ pieces: list[str] = dtcl.field(init=False, default_factory=list)
56
+
57
+ def __post_init__(self) -> None:
58
+ """"""
59
+ html_parser_t.__init__(self)
60
+ self.source = self.source.strip()
61
+ self.feed(self.source)
62
+
63
+ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]], /) -> None:
64
+ """"""
65
+ if tag == "body":
66
+ self.body_position_start = self.getpos()
67
+ self.inside_body = True
68
+
69
+ def handle_endtag(self, tag: str, /) -> None:
70
+ """"""
71
+ if tag == "body":
72
+ self.body_position_end = self.getpos()
73
+ self.inside_body = False
74
+
75
+ def handle_data(self, data: str, /) -> None:
76
+ """"""
77
+ if self.inside_body:
78
+ self.pieces.append(data)
79
+
80
+ @property
81
+ def body(self) -> str:
82
+ """"""
83
+ output = self.source.splitlines()
84
+ output = "\n".join(
85
+ output[self.body_position_start[0] : (self.body_position_end[0] + 1)]
86
+ )
87
+ output = output[self.body_position_start[1] :]
88
+ output = regx.sub(self.__class__.BODY_END_PATTERN, "", output, count=1)
89
+
90
+ return output.strip()
91
+
92
+ @property
93
+ def body_as_text(self) -> str:
94
+ """"""
95
+ return "".join(self.pieces).strip()
96
+
97
+
98
+ def SaveLOGasHTML(path: str | path_t | h.TextIO = None) -> None:
99
+ """
100
+ From first console handler found.
101
+ """
102
+ cannot_save = "Cannot save logging record as HTML"
103
+
104
+ if console_t is None:
105
+ LOGGER.warning(f"{cannot_save}: The Rich console cannot be imported.")
106
+ return
107
+
108
+ if path is None:
109
+ for handler in LOGGER.handlers:
110
+ if isinstance(handler, lggg.FileHandler):
111
+ path = path_t(handler.baseFilename).with_suffix(".htm")
112
+ break
113
+ if path is None:
114
+ LOGGER.warning(f"{cannot_save}: No file handler to build a filename from.")
115
+ return
116
+ if path.exists():
117
+ LOGGER.warning(
118
+ f'{cannot_save}: Automatically generated path "{path}" already exists.'
119
+ )
120
+ return
121
+ elif isinstance(path, str):
122
+ path = path_t(path)
123
+
124
+ actual_file = isinstance(path, path_t)
125
+ if actual_file and path.exists():
126
+ LOGGER.warning(f'{cannot_save}: File "{path}" already exists.')
127
+ return
128
+
129
+ console = None
130
+ found = False
131
+ for handler in LOGGER.handlers:
132
+ console = getattr(handler, "console", None)
133
+ if found := isinstance(console, console_t):
134
+ break
135
+
136
+ if found:
137
+ html = console.export_html()
138
+ if actual_file:
139
+ with open(path, "w") as accessor:
140
+ accessor.write(html)
141
+ else:
142
+ path.write(html)
143
+ else:
144
+ LOGGER.warning(f"{cannot_save}: No handler has a RICH console.")
@@ -29,94 +29,106 @@
29
29
  # The fact that you are presently reading this means that you have had
30
30
  # knowledge of the CeCILL license and that you accept its terms.
31
31
 
32
+ import dataclasses as dtcl
32
33
  import logging as lggg
33
34
  import sys as sstm
34
- from typing import Callable
35
+ import typing as h
35
36
 
36
- from logger_36.config import DATE_TIME_FORMAT, MESSAGE_FORMAT
37
- from logger_36.constant import LOG_LEVEL_LENGTH, NEXT_LINE_PROLOGUE
38
- from logger_36.measure.chronos import ElapsedTime
39
- from logger_36.measure.memory import CurrentUsage as CurrentMemoryUsage
40
- from logger_36.measure.memory import WithAutoUnit as MemoryWithAutoUnit
37
+ from logger_36.config.message import TIME_FORMAT, WHERE_FORMAT
38
+ from logger_36.constant.handler import HANDLER_CODES
39
+ from logger_36.constant.memory import MEMORY_MEASURE_ERROR
40
+ from logger_36.constant.message import NEXT_LINE_PROLOGUE
41
+ from logger_36.constant.record import HIDE_WHERE_ATTR, SHOW_WHERE_ATTR
42
+ from logger_36.task.format.message import FormattedMessage, MessageFormat
43
+ from logger_36.task.measure.chronos import TimeStamp
44
+ from logger_36.task.measure.memory import CanCheckMemory
41
45
 
42
46
 
47
+ @dtcl.dataclass(slots=True, repr=False, eq=False)
43
48
  class handler_extension_t:
44
- __slots__ = ("formatter", "show_memory_usage", "max_memory_usage", "exit_on_error")
45
- formatter: lggg.Formatter
46
- show_memory_usage: bool
47
- max_memory_usage: int
48
- exit_on_error: bool
49
+ name: str | None = None
50
+ show_where: bool = True
51
+ show_memory_usage: bool = False
52
+ FormattedRecord: h.Callable[[lggg.LogRecord], str] = dtcl.field(init=False)
49
53
 
50
- def __init__(self) -> None:
54
+ handler: dtcl.InitVar[lggg.Handler | None] = None
55
+ level: dtcl.InitVar[int] = lggg.NOTSET
56
+ formatter: dtcl.InitVar[lggg.Formatter | None] = None
57
+
58
+ def __post_init__(
59
+ self, handler: lggg.Handler | None, level: int, formatter: lggg.Formatter | None
60
+ ) -> None:
51
61
  """"""
52
- self.formatter = lggg.Formatter(fmt=MESSAGE_FORMAT, datefmt=DATE_TIME_FORMAT)
53
- self.show_memory_usage = False
54
- self.max_memory_usage = -1
55
- self.exit_on_error = False
62
+ if self.name in HANDLER_CODES:
63
+ raise ValueError(
64
+ FormattedMessage(
65
+ "Invalid handler name",
66
+ actual=self.name,
67
+ expected=f"a name not in {str(HANDLER_CODES)[1:-1]}",
68
+ )
69
+ )
70
+
71
+ if self.name is None:
72
+ self.name = TimeStamp()
73
+
74
+ if self.show_memory_usage and not CanCheckMemory():
75
+ self.show_memory_usage = False
76
+ print(MEMORY_MEASURE_ERROR, file=sstm.stderr)
77
+
78
+ handler.setLevel(level)
56
79
 
57
- def MessageLines(
80
+ if formatter is None:
81
+ message_format = MessageFormat(self.show_where, self.show_memory_usage)
82
+ formatter = lggg.Formatter(fmt=message_format, datefmt=TIME_FORMAT)
83
+ handler.setFormatter(formatter)
84
+ self.FormattedRecord = handler.formatter.format
85
+
86
+ def FormattedLines(
58
87
  self,
59
88
  record: lggg.LogRecord,
60
89
  /,
61
90
  *,
62
- PreProcessed: Callable[[str], str] | None = None,
63
- should_fully_format: bool = False,
91
+ PreProcessed: h.Callable[[str], str] | None = None,
92
+ should_join_lines: bool = False,
64
93
  ) -> tuple[str, str | None]:
65
94
  """
66
- Note: "message" is not yet an attribute of record (it will be set by format());
67
- Use "msg" instead.
95
+ See logger_36.catalog.handler.README.txt.
68
96
  """
69
- if not isinstance(record.msg, str):
70
- record.msg = str(record.msg)
97
+ record.level_first_letter = record.levelname[0]
98
+
99
+ message = record.msg
100
+ if not isinstance(message, str):
101
+ message = str(message)
102
+ original_message = message
103
+
71
104
  if PreProcessed is not None:
72
- record.msg = PreProcessed(record.msg)
73
- if "\n" in record.msg:
74
- original_message = record.msg
75
- lines = original_message.splitlines()
76
- record.msg = lines[0]
105
+ message = PreProcessed(message)
106
+ if "\n" in message:
107
+ lines = message.splitlines()
77
108
  next_lines = NEXT_LINE_PROLOGUE.join(lines[1:])
78
109
  next_lines = f"{NEXT_LINE_PROLOGUE}{next_lines}"
110
+ message = lines[0]
79
111
  else:
80
- original_message = next_lines = None
81
-
82
- # The record is shared between handlers, so do not re-assign if already there.
83
- if not hasattr(record, "elapsed_time"):
84
- record.elapsed_time = ElapsedTime()
85
- # Re-assign for each handler in case they have different show properties.
86
- if self.show_memory_usage:
87
- record.memory_usage = self.FormattedMemoryUsage()
88
- else:
89
- record.memory_usage = ""
112
+ next_lines = None
90
113
 
91
- padding = (LOG_LEVEL_LENGTH - record.levelname.__len__() - 1) * " "
92
- first_line = self.formatter.format(record).replace("\t", padding)
114
+ record.msg = message
115
+ if self.show_where and not hasattr(record, SHOW_WHERE_ATTR):
116
+ hide_where = getattr(record, HIDE_WHERE_ATTR, False)
117
+ if hide_where:
118
+ record.where = ""
119
+ else:
120
+ record.where = WHERE_FORMAT.format(
121
+ module=record.module, funcName=record.funcName, lineno=record.lineno
122
+ )
123
+ first_line = self.FormattedRecord(record).replace("\t", " ")
93
124
 
94
125
  # Revert the record message to its original value for subsequent handlers.
95
- if original_message is not None:
96
- record.msg = original_message
126
+ record.msg = original_message
97
127
 
98
- if should_fully_format:
128
+ if should_join_lines:
99
129
  if next_lines is None:
100
130
  return first_line, None
101
131
  else:
102
132
  return f"{first_line}{next_lines}", None
103
133
  else:
104
134
  return first_line, next_lines
105
-
106
- def FormattedMemoryUsage(self) -> str:
107
- """"""
108
- if self.show_memory_usage:
109
- usage = CurrentMemoryUsage()
110
- if usage > self.max_memory_usage:
111
- self.max_memory_usage = usage
112
-
113
- usage, unit = MemoryWithAutoUnit(usage, 1)
114
-
115
- return f" :{usage}{unit}"
116
- else:
117
- return ""
118
-
119
- def ExitOrNotIfError(self, record: lggg.LogRecord, /) -> None:
120
- """"""
121
- if self.exit_on_error and (record.levelno in (lggg.ERROR, lggg.CRITICAL)):
122
- sstm.exit(1)
@@ -0,0 +1,57 @@
1
+ # Copyright CNRS/Inria/UCA
2
+ # Contributor(s): Eric Debreuve (since 2023)
3
+ #
4
+ # eric.debreuve@cnrs.fr
5
+ #
6
+ # This software is governed by the CeCILL license under French law and
7
+ # abiding by the rules of distribution of free software. You can use,
8
+ # modify and/ or redistribute the software under the terms of the CeCILL
9
+ # license as circulated by CEA, CNRS and INRIA at the following URL
10
+ # "http://www.cecill.info".
11
+ #
12
+ # As a counterpart to the access to the source code and rights to copy,
13
+ # modify and redistribute granted by the license, users are provided only
14
+ # with a limited warranty and the software's author, the holder of the
15
+ # economic rights, and the successive licensors have only limited
16
+ # liability.
17
+ #
18
+ # In this respect, the user's attention is drawn to the risks associated
19
+ # with loading, using, modifying and/or developing or reproducing the
20
+ # software by the user in light of its specific status of free software,
21
+ # that may mean that it is complicated to manipulate, and that also
22
+ # therefore means that it is reserved for developers and experienced
23
+ # professionals having in-depth computer knowledge. Users are therefore
24
+ # encouraged to load and test the software's suitability as regards their
25
+ # requirements in conditions enabling the security of their systems and/or
26
+ # data to be ensured and, more generally, to use and operate it in the
27
+ # same conditions as regards security.
28
+ #
29
+ # The fact that you are presently reading this means that you have had
30
+ # knowledge of the CeCILL license and that you accept its terms.
31
+
32
+ import typing as h
33
+
34
+ from logger_36.config.issue import BASE_CONTEXT
35
+ from logger_36.constant.generic import NOT_PASSED
36
+ from logger_36.task.format.message import FormattedMessage
37
+
38
+ issue_t = str
39
+
40
+
41
+ def NewIssue(
42
+ context: str,
43
+ separator: str,
44
+ message: str,
45
+ /,
46
+ *,
47
+ actual: h.Any = NOT_PASSED,
48
+ expected: h.Any | None = None,
49
+ ) -> issue_t:
50
+ """"""
51
+ if context.__len__() == 0:
52
+ context = BASE_CONTEXT
53
+ message = FormattedMessage(
54
+ message, actual=actual, expected=expected, with_final_dot=False
55
+ )
56
+
57
+ return f"{context}{separator}{message}"