14
14
from typing import Any
15
15
16
16
from docutils .nodes import Node
17
+ from sphinx_needs import logging
17
18
from sphinx_needs .data import NeedsInfoType
18
19
from sphinx_needs .logging import SphinxLoggerAdapter
19
20
21
+ Location = str | tuple [str | None , int | None ] | Node | None
22
+ NewCheck = tuple [str , Location ]
23
+ logger = logging .get_logger (__name__ )
24
+
20
25
21
26
class CheckLogger :
22
27
def __init__ (self , log : SphinxLoggerAdapter , prefix : str ):
23
28
self ._log = log
24
29
self ._info_count = 0
25
30
self ._warning_count = 0
26
31
self ._prefix = prefix
32
+ self ._new_checks : list [NewCheck ] = []
27
33
28
34
@staticmethod
29
35
def _location (need : NeedsInfoType , prefix : str ):
@@ -43,47 +49,45 @@ def get(key: str) -> Any:
43
49
return None
44
50
45
51
def warning_for_option (
46
- self , need : NeedsInfoType , option : str , msg : str , new_check : bool = False
52
+ self , need : NeedsInfoType , option : str , msg : str , is_new_check : bool = False
47
53
):
48
54
full_msg = f"{ need ['id' ]} .{ option } ({ need .get (option , None )} ): { msg } "
49
55
location = CheckLogger ._location (need , self ._prefix )
50
- self ._log_message (full_msg , location , new_check )
56
+ self ._log_message (full_msg , location , is_new_check )
51
57
52
- def warning_for_need (self , need : NeedsInfoType , msg : str , new_check : bool = False ):
58
+ def warning_for_need (
59
+ self , need : NeedsInfoType , msg : str , is_new_check : bool = False
60
+ ):
53
61
full_msg = f"{ need ['id' ]} : { msg } "
54
62
location = CheckLogger ._location (need , self ._prefix )
55
- self ._log_message (full_msg , location , new_check )
63
+ self ._log_message (full_msg , location , is_new_check )
56
64
57
65
def _log_message (
58
66
self ,
59
67
msg : str ,
60
- location : None | str | tuple [ str | None , int | None ] | Node = None ,
61
- is_info : bool = False ,
68
+ location : Location ,
69
+ is_new_check : bool = False ,
62
70
):
63
- if is_info :
64
- msg += (
65
- "\n Please fix this warning related to the new check "
66
- "before the release of the next version of Docs-As-Code."
67
- )
68
- self .info (msg , location )
71
+ if is_new_check :
72
+ self ._new_checks .append ((msg , location ))
73
+ self ._info_count += 1
69
74
else :
70
75
self .warning (msg , location )
76
+ self ._warning_count += 1
71
77
72
78
def info (
73
79
self ,
74
80
msg : str ,
75
- location : None | str | tuple [ str | None , int | None ] | Node = None ,
81
+ location : Location ,
76
82
):
77
83
self ._log .info (msg , type = "score_metamodel" , location = location )
78
- self ._info_count += 1
79
84
80
85
def warning (
81
86
self ,
82
87
msg : str ,
83
- location : None | str | tuple [ str | None , int | None ] | Node = None ,
88
+ location : Location ,
84
89
):
85
90
self ._log .warning (msg , type = "score_metamodel" , location = location )
86
- self ._warning_count += 1
87
91
88
92
@property
89
93
def has_warnings (self ):
@@ -92,3 +96,27 @@ def has_warnings(self):
92
96
@property
93
97
def has_infos (self ):
94
98
return self ._info_count > 0
99
+
100
+ def flush_new_checks (self ):
101
+ """Log all new-check messages together at once."""
102
+
103
+ def make_header_line (text : str , width : int = 80 ) -> str :
104
+ """Center a header inside '=' padding so line length stays fixed."""
105
+ text = f" { text } "
106
+ return text .center (width , "=" )
107
+
108
+ if not self ._new_checks :
109
+ return
110
+
111
+ info_header = make_header_line ("[INFO MESSAGE]" )
112
+ separator = "=" * 80
113
+ warning_header = make_header_line (
114
+ f"[New Checks] has { len (self ._new_checks )} warnings"
115
+ )
116
+
117
+ logger .info (info_header )
118
+ logger .info (separator )
119
+ logger .info (warning_header )
120
+
121
+ for msg , location in self ._new_checks :
122
+ self .info (msg , location )
0 commit comments