Skip to content

Commit c9875c6

Browse files
Merge pull request #60 from Hyperloop-UPV/ADJ-Validator-compatibility
feat: add flag `--no-color` to remove ANSI colours
2 parents daa482b + b24c5e8 commit c9875c6

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

.github/workflows/scripts/adj-tester/main.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
ADJ Validator
44
5-
Version: 11.1.2
5+
Version: 11.1.3
66
77
JavierRibaldelRio
88
@@ -13,6 +13,7 @@
1313
(e.g. uniqueness constraints and IPv4 validation).
1414
"""
1515

16+
import argparse
1617
import sys
1718
from pathlib import Path
1819

@@ -23,7 +24,7 @@
2324
is_valid_ipv4,
2425
print_results,
2526
logError,
26-
info_message,
27+
set_color_enabled,
2728
validate_with_schema,
2829
)
2930

@@ -452,10 +453,10 @@ def check_packet_json(
452453
has_period_type = "period_type" in pkt
453454
has_period = "period" in pkt
454455
has_socket = "socket" in pkt
455-
456+
456457
# Count how many of the three fields are present
457458
fields_present = sum([has_period_type, has_period, has_socket])
458-
459+
459460
# If any field is present, all three must be present
460461
if fields_present > 0 and fields_present < 3:
461462
error_list.append(
@@ -598,6 +599,16 @@ def main():
598599
non-zero status code if any validation step fails.
599600
"""
600601

602+
parser = argparse.ArgumentParser(description="Validate ADJ JSON schemas")
603+
parser.add_argument(
604+
"--no-color",
605+
action="store_true",
606+
help="Disable ANSI color codes in output",
607+
)
608+
args = parser.parse_args()
609+
610+
set_color_enabled(not args.no_color)
611+
601612
# App header
602613
print_header("JSON Validation Script")
603614

.github/workflows/scripts/adj-tester/utils.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
from pathlib import Path
1111
from jsonschema import Draft7Validator
1212

13+
# Global switch to control whether ANSI colors are used in output.
14+
# This is controlled from CLI via the `--no-color` flag.
15+
ENABLE_COLOR = True
16+
17+
18+
def set_color_enabled(enabled: bool):
19+
"""Enable or disable ANSI color output.
20+
21+
Args:
22+
enabled: If False, all helper output functions will omit ANSI
23+
escape sequences.
24+
"""
25+
26+
global ENABLE_COLOR
27+
ENABLE_COLOR = enabled
28+
1329

1430
def print_header(header: str):
1531
"""Print a simple boxed header to stdout.
@@ -39,10 +55,14 @@ def format_status(message: str, validated: bool = False):
3955
red "Failed".
4056
"""
4157

58+
if not ENABLE_COLOR:
59+
result = "Passed" if validated else "Failed"
60+
return f"{message} {result}"
61+
4262
# ANSI color codes used for terminal output
4363
if not validated:
4464
result = "\033[31mFailed\033[0m" # red
45-
elif validated:
65+
else:
4666
result = "\033[32mPassed\033[0m" # green
4767

4868
return f"{message} {result}"
@@ -51,6 +71,9 @@ def format_status(message: str, validated: bool = False):
5171
def info_message(message: str):
5272
"""format to blue color and return the message string."""
5373

74+
if not ENABLE_COLOR:
75+
return message
76+
5477
return f"\033[34m{message}\033[0m"
5578

5679

@@ -69,6 +92,10 @@ def log_message(message: str, status: int = 0):
6992
(success).
7093
"""
7194

95+
if not ENABLE_COLOR:
96+
print(message)
97+
return
98+
7299
colors = {-1: "\033[31m", 1: "\033[32m"}
73100
color = colors.get(status, "")
74101
reset = "\033[0m" if color else ""
@@ -143,6 +170,9 @@ def logError(label: str, path: str, message: str):
143170
single formatted string instead of printing directly.
144171
"""
145172

173+
if not ENABLE_COLOR:
174+
return f"{label}: {path}{message}"
175+
146176
return f"\033[31m{label}: {path}{message}\033[0m"
147177

148178

0 commit comments

Comments
 (0)