|
83 | 83 | import logging |
84 | 84 | try: |
85 | 85 | class ANSIColors: |
| 86 | + """ |
| 87 | + A class that defines ANSI color codes for terminal output. |
| 88 | +
|
| 89 | + This class contains ANSI escape sequences for various colors and a |
| 90 | + mapping of logging levels to their corresponding colors. It also |
| 91 | + includes a nested class for a colored stream handler that formats |
| 92 | + log messages with the appropriate colors based on their severity. |
| 93 | +
|
| 94 | + Attributes: |
| 95 | + BLACK (str): ANSI code for black text. |
| 96 | + RED (str): ANSI code for red text. |
| 97 | + GREEN (str): ANSI code for green text. |
| 98 | + YELLOW (str): ANSI code for yellow text. |
| 99 | + BLUE (str): ANSI code for blue text. |
| 100 | + MAGENTA (str): ANSI code for magenta text. |
| 101 | + CYAN (str): ANSI code for cyan text. |
| 102 | + GREY (str): ANSI code for grey text. |
| 103 | + AMBER (str): ANSI code for amber text. |
| 104 | + REDBG (str): ANSI code for red background. |
| 105 | + ENDC (str): ANSI code to reset text formatting. |
| 106 | +
|
| 107 | + logging_color (dict): A dictionary mapping logging levels to their |
| 108 | + corresponding ANSI color codes. |
| 109 | + logging_level (dict): A dictionary mapping logging levels to their |
| 110 | + corresponding logging module constants. |
| 111 | + """ |
86 | 112 | # Define ANSI color codes |
87 | 113 | BLACK = """\033[30m""" |
88 | 114 | RED = """\033[31m""" |
@@ -112,7 +138,30 @@ class ANSIColors: |
112 | 138 | } |
113 | 139 |
|
114 | 140 | class ColoredStreamHandler(logging.StreamHandler): |
| 141 | + """ |
| 142 | + A custom logging handler that outputs with color formatting based on the log level. |
| 143 | +
|
| 144 | + This handler checks if the output stream is a terminal and applies |
| 145 | + the appropriate ANSI color codes to the log messages. If the output |
| 146 | + is not a terminal, it outputs plain text without color. |
| 147 | +
|
| 148 | + Methods: |
| 149 | + emit(record: logging.LogRecord) -> None: |
| 150 | + Formats and writes the log message to the stream with |
| 151 | + appropriate color based on the log level. |
| 152 | + """ |
| 153 | + |
115 | 154 | def emit(self, record: logging.LogRecord) -> None: |
| 155 | + """ |
| 156 | + Formats and writes the log message to the stream with color. |
| 157 | +
|
| 158 | + Args: |
| 159 | + record (logging.LogRecord): The log record containing |
| 160 | + information about the log event. |
| 161 | +
|
| 162 | + Raises: |
| 163 | + ValueError: If the log level is invalid or not recognized. |
| 164 | + """ |
116 | 165 | # Get the log level as a string |
117 | 166 | loglevel = record.levelname.lower() |
118 | 167 | # Validate the log level |
@@ -429,6 +478,21 @@ def get_test_suite( |
429 | 478 |
|
430 | 479 | # Helper function to add test cases to suite |
431 | 480 | def add_test_cases(test_cases: list) -> None: |
| 481 | + """ |
| 482 | + Adds a list of test cases to the test suite. |
| 483 | +
|
| 484 | + This function iterates over the provided list of test cases. If a test case |
| 485 | + is an instance of unittest.TestSuite, it adds the entire suite to the |
| 486 | + main suite. If the test case is a class, it loads the tests from that |
| 487 | + class and adds them to the main suite. |
| 488 | +
|
| 489 | + Args: |
| 490 | + test_cases (list): A list of test cases or test suites to be added |
| 491 | + to the main test suite. |
| 492 | +
|
| 493 | + Returns: |
| 494 | + None: This function does not return a value. |
| 495 | + """ |
432 | 496 | for test_case in test_cases: |
433 | 497 | if isinstance(test_case, unittest.TestSuite): |
434 | 498 | # Handle doctests |
|
0 commit comments