From 16f6b89e23b89144b055a9cd74d9bb8e35455a79 Mon Sep 17 00:00:00 2001 From: coldsofttech <161084795+coldsofttech@users.noreply.github.com> Date: Mon, 8 Apr 2024 21:48:41 +0100 Subject: [PATCH] Introduced Linting (#2) --- .flake8 | 3 +++ .github/workflows/pipeline.yml | 35 ++++++++++++++++++++++++-- pyloggermanager/__init__.py | 26 +++++++++++-------- pyloggermanager/__main__.py | 2 +- pyloggermanager/formatters/__init__.py | 6 ++--- pyloggermanager/formatters/__main__.py | 10 ++++---- pyloggermanager/handlers/__init__.py | 4 +-- pyloggermanager/handlers/__main__.py | 24 +++++++++--------- pyloggermanager/streams/__init__.py | 10 ++++---- pyloggermanager/textstyles/__init__.py | 12 ++++----- tests/handlers/test_handler.py | 2 +- tests/test_colorization.py | 4 +-- tests/test_pyloggermanager.py | 2 +- tests/test_pyloggermanager_config.py | 2 +- tests/test_pyloggermanager_critical.py | 2 +- tests/test_pyloggermanager_debug.py | 2 +- tests/test_pyloggermanager_error.py | 2 +- tests/test_pyloggermanager_info.py | 2 +- tests/test_pyloggermanager_log.py | 2 +- tests/test_pyloggermanager_warning.py | 2 +- 20 files changed, 96 insertions(+), 58 deletions(-) create mode 100644 .flake8 diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..b13159a --- /dev/null +++ b/.flake8 @@ -0,0 +1,3 @@ +[flake8] +max-line-length = 120 +exclude = .git,__pycache__,venv \ No newline at end of file diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index a14ee8c..be1b18c 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -6,15 +6,46 @@ on: - '*' jobs: + lint: + name: Python Linux (flake8) + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + + - name: Install Dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 + + - name: Run Lint + run: flake8 --verbose --color auto --count --statistics --format=json --output-file=flake8-report.json || echo "::set-output name=flake8_failed::true" + continue-on-error: true + + - name: Upload Report + if: ${{ always() }} + uses: actions/upload-artifact@v4 + with: + name: flake8-report + path: flake8-report.json + build_and_test: + name: Build and Test runs-on: ubuntu-latest + needs: lint steps: - name: Checkout Repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: '3.10' diff --git a/pyloggermanager/__init__.py b/pyloggermanager/__init__.py index 15f88cd..0db7c2a 100644 --- a/pyloggermanager/__init__.py +++ b/pyloggermanager/__init__.py @@ -39,6 +39,10 @@ "log", "disable", "shutdown", + "formatters", + "handlers", + "streams", + "textstyles", "__author__", "__description__", "__name__", @@ -46,24 +50,24 @@ ] __author__ = "coldsofttech" __description__ = """ -The pyloggermanager package is a vital logging framework for Python applications, providing developers with essential -tools to streamline logging operations. Its primary function is to simplify the recording and organization of log -messages, including critical information, debugging messages, errors, and warnings. By offering a centralized interface +The pyloggermanager package is a vital logging framework for Python applications, providing developers with essential +tools to streamline logging operations. Its primary function is to simplify the recording and organization of log +messages, including critical information, debugging messages, errors, and warnings. By offering a centralized interface and robust functionalities, the package facilitates efficient monitoring and troubleshooting processes. -With its intuitive interface, the pyloggermanager package enables developers to seamlessly integrate logging mechanisms -into their applications. This allows for systematic recording and categorization of log entries based on severity -levels, enhancing readability and prioritization of issues. Moreover, the package offers flexibility in customizing -logging configurations to suit specific project requirements, including formatting, output destinations, and thread +With its intuitive interface, the pyloggermanager package enables developers to seamlessly integrate logging mechanisms +into their applications. This allows for systematic recording and categorization of log entries based on severity +levels, enhancing readability and prioritization of issues. Moreover, the package offers flexibility in customizing +logging configurations to suit specific project requirements, including formatting, output destinations, and thread safety. -Beyond technical capabilities, the pyloggermanager package contributes to the reliability and maintainability of Python -applications. It establishes consistent logging practices, simplifying collaboration, code reviews, and issue -resolution across development teams. Overall, the pyloggermanager package is an invaluable asset for developers aiming +Beyond technical capabilities, the pyloggermanager package contributes to the reliability and maintainability of Python +applications. It establishes consistent logging practices, simplifying collaboration, code reviews, and issue +resolution across development teams. Overall, the pyloggermanager package is an invaluable asset for developers aiming to implement robust logging solutions, ensuring efficient and resilient application performance. """ __name__ = "pyloggermanager" -__version__ = "0.1.0" +__version__ = "0.1.1" from pyloggermanager import formatters from pyloggermanager import handlers diff --git a/pyloggermanager/__main__.py b/pyloggermanager/__main__.py index 4824ec2..612fe08 100644 --- a/pyloggermanager/__main__.py +++ b/pyloggermanager/__main__.py @@ -187,7 +187,7 @@ def is_colorization_supported(cls) -> bool: if term is None: return False - if 'color' in os.popen(f'tput colors').read(): + if 'color' in os.popen('tput colors').read(): return True try: diff --git a/pyloggermanager/formatters/__init__.py b/pyloggermanager/formatters/__init__.py index 99dac63..cbf3f3b 100644 --- a/pyloggermanager/formatters/__init__.py +++ b/pyloggermanager/formatters/__init__.py @@ -35,11 +35,11 @@ as CSV (Comma-Separated Values), JSON (JavaScript Object Notation), and the default text format. Below listed formatter classes enable users to customize the appearance and structure of log messages -according to their requirements. By supporting different formats such as CSV and JSON, users have the -flexibility to choose the most suitable format for their logging needs, whether it's for human-readable +according to their requirements. By supporting different formats such as CSV and JSON, users have the +flexibility to choose the most suitable format for their logging needs, whether it's for human-readable output, structured data storage, or integration with external systems. -Overall, the pyloggermanager.formatters package enhances the logger manager framework by offering +Overall, the pyloggermanager.formatters package enhances the logger manager framework by offering versatile formatting options for log messages, catering to a wide range of logging use cases and preferences. """ diff --git a/pyloggermanager/formatters/__main__.py b/pyloggermanager/formatters/__main__.py index 6904b16..09d9d1f 100644 --- a/pyloggermanager/formatters/__main__.py +++ b/pyloggermanager/formatters/__main__.py @@ -111,7 +111,7 @@ def format_str(self, value: str | dict) -> None: self._format_str = value - def format(self, record: 'Record') -> str: + def format(self, record) -> str: """ Formats the log record into a string based on the provided record object. @@ -172,7 +172,7 @@ def format_exception( else: return '' - def _log_attributes(self, record: 'Record', date_format: str) -> dict: + def _log_attributes(self, record, date_format: str) -> dict: """ Extracts and organizes various attributes of a 'Record' object into a dictionary format. @@ -226,7 +226,7 @@ def __init__(self, format_str: str = DEFAULT_FORMAT, date_format: str = DATE_FOR super().__init__(format_str, date_format) - def format(self, record: 'Record') -> str: + def format(self, record) -> str: """ Formats the given log record according to the format string. @@ -293,7 +293,7 @@ def _is_valid_csv_format(format_str: str) -> bool: """ return len(format_str.split(',')) >= 2 - def format(self, record: 'Record') -> str: + def format(self, record) -> str: """ Formats the given log record into a CSV string based on the specified format string. @@ -356,7 +356,7 @@ def _validate_format_str(format_str: dict) -> None: except ValueError: raise ValueError("Invalid JSON format string.") - def format(self, record: 'Record') -> str: + def format(self, record) -> str: """ Formats the given log record into a JSON string. diff --git a/pyloggermanager/handlers/__init__.py b/pyloggermanager/handlers/__init__.py index 8c1dd25..c4fb41f 100644 --- a/pyloggermanager/handlers/__init__.py +++ b/pyloggermanager/handlers/__init__.py @@ -28,11 +28,11 @@ __name__ = "pyloggermanager.handlers" __description__ = """ The pyloggermanager.handlers package provides classes responsible for handling log records -generated within the logger manager framework. It includes various handlers for processing log +generated within the logger manager framework. It includes various handlers for processing log messages, directing them to different destinations, and performing actions based on logging levels. Below listed handler classes offer flexibility and customization options for managing log records -within the logger manager framework. They enable users to define how log messages are processed, +within the logger manager framework. They enable users to define how log messages are processed, where they are directed, and how they are formatted, catering to various logging scenarios and deployment environments. diff --git a/pyloggermanager/handlers/__main__.py b/pyloggermanager/handlers/__main__.py index 8bddd93..5daee9e 100644 --- a/pyloggermanager/handlers/__main__.py +++ b/pyloggermanager/handlers/__main__.py @@ -44,7 +44,7 @@ def __init__( self, name: str = None, level: int = 20, - colorization: 'Colorization' = None, + colorization=None, formatter: Formatter = DefaultFormatter() ) -> None: """ @@ -79,7 +79,7 @@ def __init__( self._release_lock() @property - def colorization(self) -> 'Colorization': + def colorization(self): """ Gets the colorization object for the handler. @@ -88,7 +88,7 @@ def colorization(self) -> 'Colorization': return self._colorization @colorization.setter - def colorization(self, value: 'Colorization') -> None: + def colorization(self, value) -> None: """ Sets the colorization object for the handler. @@ -199,7 +199,7 @@ def close(self) -> None: finally: self._release_lock() - def emit(self, record: 'Record', ignore_display: bool) -> None: + def emit(self, record, ignore_display: bool) -> None: """ Abstract method to emit a log record. @@ -208,7 +208,7 @@ def emit(self, record: 'Record', ignore_display: bool) -> None: """ raise NotImplementedError('emit() method must be implemented in subclasses.') - def format(self, record: 'Record') -> str: + def format(self, record) -> str: """ Formats a log record using the handler's formatter. @@ -232,7 +232,7 @@ def get_handlers() -> list[Any]: """ return _handlersList - def handle(self, record: 'Record', ignore_display: bool) -> None: + def handle(self, record, ignore_display: bool) -> None: """ Handles a log record. @@ -262,7 +262,7 @@ def __init__( self, name: str = None, level: int = 20, - colorization: 'Colorization' = None, + colorization=None, formatter: Formatter = DefaultFormatter(), stream: Stream = TerminalStream() ) -> None: @@ -317,7 +317,7 @@ def close(self) -> None: self._stream.close() super().close() - def emit(self, record: 'Record', ignore_display: bool = True) -> None: + def emit(self, record, ignore_display: bool = True) -> None: """ Emits the log record by formatting it, colorizing the message, and writing it to the stream. @@ -356,7 +356,7 @@ def __init__( self, name: str = None, level: int = 20, - colorization: 'Colorization' = None, + colorization=None, formatter: Formatter = DefaultFormatter(), stream: Stream = StdoutStream() ) -> None: @@ -416,7 +416,7 @@ def close(self) -> None: self._stream.close() super().close() - def emit(self, record: 'Record', ignore_display: bool) -> None: + def emit(self, record, ignore_display: bool) -> None: """ Emits a log record to the stream. @@ -461,7 +461,7 @@ def __init__( self, name: str = None, level: int = 20, - colorization: 'Colorization' = None, + colorization=None, formatter: Formatter = DefaultFormatter(), file_name: str = 'default.log', file_mode: str = 'a', @@ -596,7 +596,7 @@ def close(self) -> None: self._close_file_stream() super().close() - def emit(self, record: 'Record', ignore_display: bool) -> None: + def emit(self, record, ignore_display: bool) -> None: """ Emits a log record by writing it to the log file. diff --git a/pyloggermanager/streams/__init__.py b/pyloggermanager/streams/__init__.py index b4c110c..90bc5b3 100644 --- a/pyloggermanager/streams/__init__.py +++ b/pyloggermanager/streams/__init__.py @@ -31,14 +31,14 @@ can be directed to, allowing for flexible and customizable logging behaviour. Below listed stream classes offer versatility in directing log messages to different output -channels, allowing users to customize logging behavior based on their application's requirements -and environment configuration. By supporting various stream types, the logger manager framework -enables users to control where log records are displayed or stored, facilitating effective logging +channels, allowing users to customize logging behavior based on their application's requirements +and environment configuration. By supporting various stream types, the logger manager framework +enables users to control where log records are displayed or stored, facilitating effective logging and troubleshooting processes. -Overall, the pyloggermanager.streams package enhances the functionality of the logger manager framework +Overall, the pyloggermanager.streams package enhances the functionality of the logger manager framework by providing a range of stream classes for directing log messages to different output channels. Users can -leverage these classes to tailor their logging setup to suit their specific needs and preferences, ensuring +leverage these classes to tailor their logging setup to suit their specific needs and preferences, ensuring efficient management and processing of log records. """ diff --git a/pyloggermanager/textstyles/__init__.py b/pyloggermanager/textstyles/__init__.py index 9e4ed79..192f948 100644 --- a/pyloggermanager/textstyles/__init__.py +++ b/pyloggermanager/textstyles/__init__.py @@ -26,17 +26,17 @@ __name__ = "pyloggermanager.textstyles" __description__ = """ The pyloggermanager.textstyles package provides utilities for defining and applying text styles -to log messages within the logger manager framework. It includes classes for specifying text -colors, background colors, and text effects, allowing users to customize the appearance of log +to log messages within the logger manager framework. It includes classes for specifying text +colors, background colors, and text effects, allowing users to customize the appearance of log messages according to their preferences. -This package is designed to enhance the visual representation of log messages by providing a -flexible and intuitive way to apply various text styles. By incorporating these text styles +This package is designed to enhance the visual representation of log messages by providing a +flexible and intuitive way to apply various text styles. By incorporating these text styles into log messages, users can improve readability, emphasize important information, and differentiate between different types of log entries. -Overall, the pyloggermanager.textstyles package complements the logger manager framework by -offering tools for creating visually appealing and informative log messages, contributing to a +Overall, the pyloggermanager.textstyles package complements the logger manager framework by +offering tools for creating visually appealing and informative log messages, contributing to a more effective logging experience. """ diff --git a/tests/handlers/test_handler.py b/tests/handlers/test_handler.py index d24d984..82d9b3f 100644 --- a/tests/handlers/test_handler.py +++ b/tests/handlers/test_handler.py @@ -242,7 +242,7 @@ def test_get_handlers_none(self): def test_get_handlers_valid(self): """Test if the get handlers returns expected values.""" - handler = Handler() + Handler() assert len(Handler.get_handlers()) > 0 def test_handle_valid(self): diff --git a/tests/test_colorization.py b/tests/test_colorization.py index 0111c8a..9cf7705 100644 --- a/tests/test_colorization.py +++ b/tests/test_colorization.py @@ -105,7 +105,7 @@ def test_colorize_message_regex(self): f'Test error message' f'{Colorization.RESET}' ) if Colorization.is_colorization_supported() else ( - f'Test error message' + 'Test error message' ) self.assertEqual(Colorization.colorize_message('Test error message'), expected_output) @@ -114,7 +114,7 @@ def test_colorize_message_regex(self): f'Test warning message' f'{Colorization.RESET}' ) if Colorization.is_colorization_supported() else ( - f'Test warning message' + 'Test warning message' ) self.assertEqual(Colorization.colorize_message('Test warning message'), expected_output) diff --git a/tests/test_pyloggermanager.py b/tests/test_pyloggermanager.py index 80c9aba..628121c 100644 --- a/tests/test_pyloggermanager.py +++ b/tests/test_pyloggermanager.py @@ -39,7 +39,7 @@ def tearDown(self) -> None: if self.logger is not None: for handler in self.logger.handlers: handler.close() - if type(handler) == FileHandler: + if handler is FileHandler: try: file_name = str(handler.filename) os.remove(file_name) diff --git a/tests/test_pyloggermanager_config.py b/tests/test_pyloggermanager_config.py index 1d1cbdd..103d392 100644 --- a/tests/test_pyloggermanager_config.py +++ b/tests/test_pyloggermanager_config.py @@ -35,7 +35,7 @@ def cleanup(logger): """Clean up method""" for handler in logger.handlers: handler.close() - if type(handler) == FileHandler: + if handler is FileHandler: try: file_name = str(handler.filename) os.remove(file_name) diff --git a/tests/test_pyloggermanager_critical.py b/tests/test_pyloggermanager_critical.py index 57611fc..698b1f7 100644 --- a/tests/test_pyloggermanager_critical.py +++ b/tests/test_pyloggermanager_critical.py @@ -36,7 +36,7 @@ def cleanup(logger): """Clean up method""" for handler in logger.handlers: handler.close() - if type(handler) == FileHandler: + if handler is FileHandler: try: file_name = str(handler.filename) os.remove(file_name) diff --git a/tests/test_pyloggermanager_debug.py b/tests/test_pyloggermanager_debug.py index 707748e..9b5bee8 100644 --- a/tests/test_pyloggermanager_debug.py +++ b/tests/test_pyloggermanager_debug.py @@ -36,7 +36,7 @@ def cleanup(logger): """Clean up method""" for handler in logger.handlers: handler.close() - if type(handler) == FileHandler: + if handler is FileHandler: try: file_name = str(handler.filename) os.remove(file_name) diff --git a/tests/test_pyloggermanager_error.py b/tests/test_pyloggermanager_error.py index 9d6c4b9..a2b5713 100644 --- a/tests/test_pyloggermanager_error.py +++ b/tests/test_pyloggermanager_error.py @@ -36,7 +36,7 @@ def cleanup(logger): """Clean up method""" for handler in logger.handlers: handler.close() - if type(handler) == FileHandler: + if handler is FileHandler: try: file_name = str(handler.filename) os.remove(file_name) diff --git a/tests/test_pyloggermanager_info.py b/tests/test_pyloggermanager_info.py index 75f8097..be7a331 100644 --- a/tests/test_pyloggermanager_info.py +++ b/tests/test_pyloggermanager_info.py @@ -36,7 +36,7 @@ def cleanup(logger): """Clean up method""" for handler in logger.handlers: handler.close() - if type(handler) == FileHandler: + if handler is FileHandler: try: file_name = str(handler.filename) os.remove(file_name) diff --git a/tests/test_pyloggermanager_log.py b/tests/test_pyloggermanager_log.py index 31a890b..4d2631d 100644 --- a/tests/test_pyloggermanager_log.py +++ b/tests/test_pyloggermanager_log.py @@ -36,7 +36,7 @@ def cleanup(logger): """Clean up method""" for handler in logger.handlers: handler.close() - if type(handler) == FileHandler: + if handler is FileHandler: try: file_name = str(handler.filename) os.remove(file_name) diff --git a/tests/test_pyloggermanager_warning.py b/tests/test_pyloggermanager_warning.py index d586a87..fcc4b7f 100644 --- a/tests/test_pyloggermanager_warning.py +++ b/tests/test_pyloggermanager_warning.py @@ -36,7 +36,7 @@ def cleanup(logger): """Clean up method""" for handler in logger.handlers: handler.close() - if type(handler) == FileHandler: + if handler is FileHandler: try: file_name = str(handler.filename) os.remove(file_name)