From ca93d4572d5d393d950c4e8a29550487240eda07 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 25 Oct 2024 12:36:16 +0200 Subject: [PATCH 1/3] WIP --- doc/source/conf.py | 50 +++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/doc/source/conf.py b/doc/source/conf.py index e445672ba..3ef36c3e1 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -1,13 +1,13 @@ # Configuration file for the Sphinx_PyAEDT documentation builder. -# -- Project information ----------------------------------------------------- +import colorama +from colorama import Fore, Style import datetime from importlib import import_module +import logging import os from pathlib import Path -from pprint import pformat import shutil -from typing import Any from sphinx.application import Sphinx from sphinx.config import Config @@ -18,17 +18,14 @@ latex, watermark, ) -from docutils import nodes -from docutils.parsers.rst import Directive import numpy as np import pyvista -from sphinx import addnodes from sphinx.builders.latex import LaTeXBuilder -from sphinx.util import logging +from sphinx.util import logging as sphinx_logging LaTeXBuilder.supported_image_types = ["image/png", "image/pdf", "image/svg+xml"] -logger = logging.getLogger(__name__) +logger: logging.Logger = sphinx_logging.getLogger(__name__) path = Path(__file__).parent.parent.parent / "examples" EXAMPLES_DIRECTORY = path.resolve() REPOSITORY_NAME = "pyaedt-examples" @@ -43,26 +40,21 @@ release = version = "0.1.dev0" -# -- Connect functions (hooks) to Sphinx events ----------------------------- +# # -- Connect functions (hooks) to Sphinx events ----------------------------- -class PrettyPrintDirective(Directive): - """Renders a constant using ``pprint.pformat`` and inserts into the document.""" +class ColoredFormatter(logging.ColorizeFormatter): + """Custom log formatter to add colors to different log levels.""" - required_arguments = 1 - - def run(self): - module_path, member_name = self.arguments[0].rsplit(".", 1) - - member_data = getattr(import_module(module_path), member_name) - code = pformat(member_data, 2, width=68) - - literal = nodes.literal_block(code, code) - literal["language"] = "python" - - return [addnodes.desc_name(text=member_name), addnodes.desc_content("", literal)] - - -# Sphinx generic event hooks + def format(self, record: logging.LogRecord): + if record.levelno == logging.ERROR: + record.msg = f"{Fore.RED}{record.msg}{Style.RESET_ALL}" + if record.levelno == logging.WARNING: + record.msg = f"{Fore.YELLOW}{record.msg}{Style.RESET_ALL}" + if record.levelno == logging.INFO: + record.msg = f"{Fore.GREEN}{record.msg}{Style.RESET_ALL}" + else: + record.msg = f"{Fore.CYAN}{record.msg}{Style.RESET_ALL}" + return super().format(record) def directory_size(directory_path: Path): @@ -305,7 +297,11 @@ def convert_examples_into_notebooks(app): def setup(app): """Run different hook functions during the documentation build.""" - app.add_directive("pprint", PrettyPrintDirective) + colored_formatter = ColoredFormatter('%(levelname)s: %(message)s') + for handler in logger.handlers: + handler.setFormatter(colored_formatter) + logger.setLevel(logging.INFO) + # Configuration inited hooks app.connect("config-inited", check_pandoc_installed) app.connect("config-inited", copy_examples_structure) From 300c89d52e4673d1468799ca15cff488928ca648 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 25 Oct 2024 13:08:22 +0200 Subject: [PATCH 2/3] DOCS: Improve coloring --- doc/make.bat | 2 +- doc/source/conf.py | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/doc/make.bat b/doc/make.bat index a153eb9d5..c987d2eec 100644 --- a/doc/make.bat +++ b/doc/make.bat @@ -5,7 +5,7 @@ pushd %~dp0 REM Command file for Sphinx documentation if "%SPHINXOPTS%" == "" ( - set SPHINXOPTS=-j auto --color + set SPHINXOPTS=-j auto --color -W ) if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build diff --git a/doc/source/conf.py b/doc/source/conf.py index 3ef36c3e1..eb62b652f 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -3,7 +3,6 @@ import colorama from colorama import Fore, Style import datetime -from importlib import import_module import logging import os from pathlib import Path @@ -21,11 +20,9 @@ import numpy as np import pyvista from sphinx.builders.latex import LaTeXBuilder -from sphinx.util import logging as sphinx_logging LaTeXBuilder.supported_image_types = ["image/png", "image/pdf", "image/svg+xml"] -logger: logging.Logger = sphinx_logging.getLogger(__name__) path = Path(__file__).parent.parent.parent / "examples" EXAMPLES_DIRECTORY = path.resolve() REPOSITORY_NAME = "pyaedt-examples" @@ -40,22 +37,34 @@ release = version = "0.1.dev0" -# # -- Connect functions (hooks) to Sphinx events ----------------------------- - -class ColoredFormatter(logging.ColorizeFormatter): +class ColoredFormatter(logging.Formatter): """Custom log formatter to add colors to different log levels.""" def format(self, record: logging.LogRecord): if record.levelno == logging.ERROR: record.msg = f"{Fore.RED}{record.msg}{Style.RESET_ALL}" + record.levelname = f"{Fore.RED}{record.levelname}{Style.RESET_ALL}" if record.levelno == logging.WARNING: - record.msg = f"{Fore.YELLOW}{record.msg}{Style.RESET_ALL}" + record.msg = f"{Fore.LIGHTYELLOW_EX}{record.msg}{Style.RESET_ALL}" + record.levelname = f"{Fore.LIGHTYELLOW_EX}{record.levelname}{Style.RESET_ALL}" if record.levelno == logging.INFO: record.msg = f"{Fore.GREEN}{record.msg}{Style.RESET_ALL}" + record.levelname = f"{Fore.GREEN}{record.levelname}{Style.RESET_ALL}" else: record.msg = f"{Fore.CYAN}{record.msg}{Style.RESET_ALL}" + record.levelname = f"{Fore.CYAN}{record.levelname}{Style.RESET_ALL}" return super().format(record) +colorama.init(autoreset=True) +root_logger = logging.getLogger() +root_logger.setLevel(logging.INFO) +colored_formatter = ColoredFormatter('%(levelname)s: %(message)s') +stream_handler = logging.StreamHandler() +stream_handler.setFormatter(colored_formatter) +root_logger.addHandler(stream_handler) +logger = logging.getLogger(__name__) + +# # -- Connect functions (hooks) to Sphinx events ----------------------------- def directory_size(directory_path: Path): """Compute the size (in mega bytes) of a directory. @@ -297,10 +306,6 @@ def convert_examples_into_notebooks(app): def setup(app): """Run different hook functions during the documentation build.""" - colored_formatter = ColoredFormatter('%(levelname)s: %(message)s') - for handler in logger.handlers: - handler.setFormatter(colored_formatter) - logger.setLevel(logging.INFO) # Configuration inited hooks app.connect("config-inited", check_pandoc_installed) From c69711ad25498a682bca36a4a74a4a7ec19d6145 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 25 Oct 2024 13:09:24 +0200 Subject: [PATCH 3/3] WIP --- doc/make.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/make.bat b/doc/make.bat index c987d2eec..a153eb9d5 100644 --- a/doc/make.bat +++ b/doc/make.bat @@ -5,7 +5,7 @@ pushd %~dp0 REM Command file for Sphinx documentation if "%SPHINXOPTS%" == "" ( - set SPHINXOPTS=-j auto --color -W + set SPHINXOPTS=-j auto --color ) if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build