Skip to content

Commit 4bb904d

Browse files
[FEATURE] Slight improvement to new tool (- WIP #265 -)
Changes in file .github/tools/cioutput.py: * minor improvements * related work Changes in file pytest.ini: * related work
1 parent 6d3706a commit 4bb904d

File tree

2 files changed

+114
-41
lines changed

2 files changed

+114
-41
lines changed

.github/tools/cioutput.py

Lines changed: 113 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -116,44 +116,85 @@ def format(self, record: logging.LogRecord) -> str:
116116
Formatted string in GitHub Actions annotation format
117117
"""
118118
# Extract file/line info if available in extra attributes
119-
is_boundry = getattr(record, "is_boundry", False)
120-
title = getattr(record, "title", None)
119+
is_boundary = getattr(record, "is_boundry", False)
120+
message = super().format(record)
121+
122+
if is_boundary:
123+
return self._format_boundary(message)
124+
125+
annotation_level = self.LEVEL_MAPPING.get(record.levelno, "notice")
126+
command = self._create_annotation_command(record, annotation_level)
127+
return f"{command}:: {message}"
128+
129+
def _format_boundary(self, message: str) -> str:
130+
"""Format the boundary message for GitHub Actions.
131+
132+
Args:
133+
message: The log message
134+
135+
Returns:
136+
Formatted boundary string
137+
"""
138+
if message:
139+
return f"::group::{message}"
140+
return "::endgroup::"
141+
142+
def _create_annotation_command(self, record: logging.LogRecord, annotation_level: str) -> str:
143+
"""Create the annotation command based on the log record.
144+
145+
Args:
146+
record: The log record
147+
annotation_level: The level of the annotation
148+
149+
Returns:
150+
The constructed annotation command
151+
"""
152+
command = f"::{annotation_level}"
153+
if annotation_level != "debug":
154+
command += self._add_location_parameters(record)
155+
command += self._add_title_parameter(record)
156+
return command
157+
158+
def _add_location_parameters(self, record: logging.LogRecord) -> str:
159+
"""Add location parameters to the annotation command.
160+
161+
Args:
162+
record: The log record
163+
164+
Returns:
165+
Location parameters as a string
166+
"""
167+
params = []
121168
file_path = getattr(record, "file_path", None)
122169
line_num = getattr(record, "line_num", None)
123170
end_line_num = getattr(record, "end_line_num", None)
124171
col_num = getattr(record, "col_num", None)
125172
end_col_num = getattr(record, "end_col_num", None)
126-
# Get the annotation level based on log level
127-
annotation_level = self.LEVEL_MAPPING.get(record.levelno, "notice")
128-
# Format the basic message
129-
message = super().format(record)
130-
if is_boundry:
131-
if message and (len(message) > 0):
132-
annotation_level = "group"
133-
return f"::group::{message}"
134-
else:
135-
return "::endgroup::"
136-
# Create the annotation command
137-
command = f"::{annotation_level}"
138-
if annotation_level not in "debug":
139-
# Add location parameters if available
140-
if file_path:
141-
command += f" file={file_path}"
142-
if line_num:
143-
command += f",line={line_num}"
144-
if end_line_num:
145-
command += f",endLine={end_line_num}"
146-
if col_num:
147-
command += f",col={col_num}"
148-
if end_col_num:
149-
command += f",endCol={end_col_num}"
150-
if title:
151-
if file_path:
152-
command += f",title={title}"
153-
else:
154-
command += f" title={title}"
155-
# Complete the command with the message
156-
return f"{command}:: {message}"
173+
if file_path:
174+
params.append(f"file={file_path}")
175+
if line_num:
176+
params.append(f"line={line_num}")
177+
if end_line_num:
178+
params.append(f"endLine={end_line_num}")
179+
if col_num:
180+
params.append(f"col={col_num}")
181+
if end_col_num:
182+
params.append(f"endCol={end_col_num}")
183+
return ",".join(params) if params else ""
184+
185+
def _add_title_parameter(self, record: logging.LogRecord) -> str:
186+
"""Add the title parameter to the annotation command.
187+
188+
Args:
189+
record: The log record
190+
191+
Returns:
192+
Title parameter as a string
193+
"""
194+
title = getattr(record, "title", None)
195+
if title:
196+
return f",title={title}" if getattr(record, "file_path", None) else f"title={title}"
197+
return ""
157198

158199

159200
class MarkdownFormatter(logging.Formatter):
@@ -366,23 +407,54 @@ def log(
366407
col_num: Optional column number for annotations
367408
end_col_num: Optional end-column number for annotations
368409
"""
410+
extra = self._build_extra_info(
411+
is_boundry, title, file_path,
412+
line_num, end_line_num, col_num, end_col_num,
413+
)
414+
self._log_message(message, level, extra)
415+
self._handle_github_actions_summary(level, message, file_path, line_num, extra)
416+
417+
def _build_extra_info(
418+
self,
419+
is_boundry: Optional[bool],
420+
title: Optional[str],
421+
file_path: Optional[str],
422+
line_num: Optional[int],
423+
end_line_num: Optional[int],
424+
col_num: Optional[int],
425+
end_col_num: Optional[int]
426+
) -> dict:
427+
"""Build extra information for logging."""
369428
extra = {}
370-
if is_boundry:
429+
if is_boundry is not None:
371430
extra["is_boundry"] = is_boundry
372-
if file_path:
431+
if title:
373432
extra["title"] = title
374433
if file_path:
375434
extra["file_path"] = file_path
376-
if line_num:
435+
if line_num is not None:
377436
extra["line_num"] = line_num
378-
if end_line_num:
437+
if end_line_num is not None:
379438
extra["end_line_num"] = end_line_num
380-
if col_num:
439+
if col_num is not None:
381440
extra["col_num"] = col_num
382-
if end_col_num:
441+
if end_col_num is not None:
383442
extra["end_col_num"] = end_col_num
443+
return extra
444+
445+
def _log_message(self, message: str, level: LogLevel, extra: dict) -> None:
446+
"""Log the message with the specified level and extra information."""
384447
self.logger.log(level.value, message, extra=extra if extra else None)
385-
# Also add ERROR and higher messages to GitHub Actions summary
448+
449+
def _handle_github_actions_summary(
450+
self,
451+
level: LogLevel,
452+
message: str,
453+
file_path: Optional[str],
454+
line_num: Optional[int],
455+
extra: dict
456+
) -> None:
457+
"""Handle logging for GitHub Actions summary."""
386458
if level.value >= logging.ERROR and self.format_type == OutputFormat.MARKDOWN:
387459
if "GITHUB_STEP_SUMMARY" in os.environ:
388460
md_formatter = MarkdownFormatter()
@@ -395,7 +467,7 @@ def log(
395467
args=(),
396468
exc_info=None
397469
)
398-
for key, value in extra.items() if extra else {}:
470+
for key, value in extra.items():
399471
setattr(record, key, value)
400472
self.add_to_summary(md_formatter.format(record))
401473

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[pytest]
22
addopts = --cache-clear --doctest-glob=**/*.py --doctest-modules --cov=multicast --cov-append --cov-report=xml --rootdir=.
3+
#testpaths = tests
34
pythonpath = multicast tests
45
python_files = test_*.py
56
python_classes = *TestSuite

0 commit comments

Comments
 (0)