From 372a91ae924cdb9bc3ad478d3386df8f1dcb9ffd Mon Sep 17 00:00:00 2001 From: funilrys Date: Fri, 27 Dec 2024 14:00:25 +0100 Subject: [PATCH] Introduction of a way to display the test datetime. This patch fixes #361. Users can now use the --display-datetime to allow the "Tested At" column and --display-datetime-fmt to control the format of the datetime. --- PyFunceble/cli/entry_points/pyfunceble/cli.py | 19 ++++++++++++++ PyFunceble/cli/filesystem/printer/base.py | 25 ++++++++++++++----- PyFunceble/cli/processes/workers/producer.py | 17 +++++++++++-- .../.PyFunceble_production.yaml | 14 +++++++++++ PyFunceble/storage.py | 2 +- .../configuration/parameters/cli-testing.md | 14 +++++++++++ version.yaml | 2 +- 7 files changed, 83 insertions(+), 10 deletions(-) diff --git a/PyFunceble/cli/entry_points/pyfunceble/cli.py b/PyFunceble/cli/entry_points/pyfunceble/cli.py index 473c4fcc..fe6ee539 100644 --- a/PyFunceble/cli/entry_points/pyfunceble/cli.py +++ b/PyFunceble/cli/entry_points/pyfunceble/cli.py @@ -833,6 +833,25 @@ def get_output_control_group_data() -> List[Tuple[List[str], dict]]: "default": "all", }, ), + ( + ["--display-datetime"], + { + "dest": "cli_testing.display_mode.datetime", + "action": "store_true", + "help": "Activates or disables the display of the datetime of the\n" + "test. %s" % get_configured_value("cli_testing.display_mode.datetime"), + }, + ), + ( + ["--display-datetime-fmt"], + { + "dest": "cli_testing.display_mode.datetime_format", + "type": str, + "help": "Sets the datetime format to use when displaying the\n" + "datetime of the test. %s" + % get_configured_value("cli_testing.display_mode.datetime_format"), + }, + ), ( [ "--dots", diff --git a/PyFunceble/cli/filesystem/printer/base.py b/PyFunceble/cli/filesystem/printer/base.py index 7d4866a8..dae129ac 100644 --- a/PyFunceble/cli/filesystem/printer/base.py +++ b/PyFunceble/cli/filesystem/printer/base.py @@ -53,7 +53,7 @@ import copy import functools import string -from typing import Dict, List, Optional +from typing import Any, Callable, Dict, List, Optional class PrinterBase: @@ -81,14 +81,15 @@ class PrinterBase: "minutes": 2, "seconds": 6, "registrar": 30, + "tested_at": 19, } TEMPLATES: Dict[str, string.Template] = { "all": string.Template( "$idna_subject $status $status_source $expiration_date $registrar " - "$http_status_code $checker_type" + "$http_status_code $checker_type $tested_at" ), - "less": string.Template("$idna_subject $status $status_source"), + "less": string.Template("$idna_subject $status $status_source $tested_at"), "simple": string.Template("$idna_subject $status"), "percentage": string.Template("$status $percentage $amount"), "hosts": string.Template("$ip $idna_subject"), @@ -114,8 +115,11 @@ class PrinterBase: "minutes": "Minutes", "seconds": "Seconds", "registrar": "Registrar", + "tested_at": "Tested At", } + extra_formatters: Dict[str, Callable[..., Any]] = {} + _template_to_use: Optional[str] = None _dataset: Optional[Dict[str, str]] = None _skip_column: Optional[List[str]] = [] @@ -126,6 +130,7 @@ def __init__( *, dataset: Optional[Dict[str, str]] = None, skip_column: Optional[List[str]] = None, + extra_formatters: Optional[Dict[str, Callable[..., Any]]] = None, ) -> None: if template_to_use is not None: self.template_to_use = template_to_use @@ -136,6 +141,9 @@ def __init__( if skip_column is not None: self.skip_column = skip_column + if extra_formatters is not None: + self.extra_formatters.update(extra_formatters) + def ensure_template_to_use_is_given(func): # pylint: disable=no-self-argument """ Ensures that the template to use is given before launching the @@ -315,9 +323,11 @@ def get_header_to_print(self) -> str: continue if key in self.skip_column: - self.TEMPLATES[self.template_to_use].template = self.TEMPLATES[ - self.template_to_use - ].template.replace(f"${key} ", "") + self.TEMPLATES[self.template_to_use].template = ( + self.TEMPLATES[self.template_to_use] + .template.replace(f"${key} ", "") + .replace(f" ${key}", "") + ) continue to_print_data[0][key] = f"{value:<{self.STD_LENGTH[key]}}" @@ -354,6 +364,9 @@ def get_line_to_print(self) -> str: if not value and value != 0: value = self.STD_UNKNOWN + if key in self.extra_formatters: + value = self.extra_formatters[key](value) + if self.template_to_use not in ignore_length: to_print[key] = f"{value:<{self.STD_LENGTH[key]}}" else: diff --git a/PyFunceble/cli/processes/workers/producer.py b/PyFunceble/cli/processes/workers/producer.py index e887b0e7..de9c882d 100644 --- a/PyFunceble/cli/processes/workers/producer.py +++ b/PyFunceble/cli/processes/workers/producer.py @@ -106,12 +106,25 @@ class ProducerWorker(WorkerBase): def __post_init__(self) -> None: skip_columns = [] + extra_formatters = {} if not PyFunceble.storage.CONFIGURATION.cli_testing.display_mode.registrar: skip_columns.append("registrar") - self.stdout_printer = StdoutPrinter(skip_column=skip_columns) - self.file_printer = FilePrinter(skip_column=skip_columns) + if not PyFunceble.storage.CONFIGURATION.cli_testing.display_mode.datetime: + skip_columns.append("tested_at") + else: + # pylint: disable=line-too-long + extra_formatters["tested_at"] = lambda x: x.strftime( + PyFunceble.storage.CONFIGURATION.cli_testing.display_mode.datetime_format + ) + + self.stdout_printer = StdoutPrinter( + skip_column=skip_columns, extra_formatters=extra_formatters + ) + self.file_printer = FilePrinter( + skip_column=skip_columns, extra_formatters=extra_formatters + ) self.whois_dataset = get_whois_dataset_object(db_session=self.db_session) self.inactive_dataset = get_inactive_dataset_object(db_session=self.db_session) self.continue_dataset = get_continue_databaset_object( diff --git a/PyFunceble/data/infrastructure/.PyFunceble_production.yaml b/PyFunceble/data/infrastructure/.PyFunceble_production.yaml index a6f67010..ba78e96d 100644 --- a/PyFunceble/data/infrastructure/.PyFunceble_production.yaml +++ b/PyFunceble/data/infrastructure/.PyFunceble_production.yaml @@ -377,6 +377,20 @@ cli_testing: # CLI Argument: --max-registrar max_registrar: 15 + # Enable/Disable the printing of the datetime of the test. + # + # CLI Argument: --display-datetime + datetime: no + + # The format to use when displaying the datetime of the test. + # + # WARNING: + # This parameter is only taken into consideration when `datetime` is set + # to `yes`. + # + # CLI Argument: --display-datetime-fmt + datetime_format: "%Y-%m-%d %H:%M:%S" + testing_mode: # Provides and select the testing mode. # diff --git a/PyFunceble/storage.py b/PyFunceble/storage.py index 4b041b28..6fd6cfb0 100644 --- a/PyFunceble/storage.py +++ b/PyFunceble/storage.py @@ -60,7 +60,7 @@ from PyFunceble.storage_facility import get_config_directory PROJECT_NAME: str = "PyFunceble" -PROJECT_VERSION: str = "4.3.0a10.dev (Blue Duckling: Tulip)" +PROJECT_VERSION: str = "4.3.0a11.dev (Blue Duckling: Tulip)" DISTRIBUTED_CONFIGURATION_FILENAME: str = ".PyFunceble_production.yaml" diff --git a/docs/use/configuration/parameters/cli-testing.md b/docs/use/configuration/parameters/cli-testing.md index f05ec649..95ade678 100644 --- a/docs/use/configuration/parameters/cli-testing.md +++ b/docs/use/configuration/parameters/cli-testing.md @@ -300,6 +300,20 @@ cli_testing: # CLI Argument: --max-registrar max_registrar: 15 + # Enable/Disable the printing of the datetime of the test. + # + # CLI Argument: --display-datetime + datetime: no + + # The format to use when displaying the datetime of the test. + # + # WARNING: + # This parameter is only taken into consideration when `datetime` is set + # to `yes`. + # + # CLI Argument: --display-datetime-fmt + datetime_format: "%Y-%m-%d %H:%M:%S" + testing_mode: # Provides and select the testing mode. # diff --git a/version.yaml b/version.yaml index 3e9095ce..516aac64 100644 --- a/version.yaml +++ b/version.yaml @@ -1,4 +1,4 @@ -current_version: '4.3.0a10.dev (Blue Duckling: Tulip)' +current_version: '4.3.0a11.dev (Blue Duckling: Tulip)' deprecated: - 3.0.21 - 3.1.20