Skip to content

Commit

Permalink
Introduction of a way to display the test datetime.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
funilrys committed Dec 27, 2024
1 parent eb3a43b commit 372a91a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 10 deletions.
19 changes: 19 additions & 0 deletions PyFunceble/cli/entry_points/pyfunceble/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 19 additions & 6 deletions PyFunceble/cli/filesystem/printer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"),
Expand All @@ -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]] = []
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]}}"
Expand Down Expand Up @@ -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:
Expand Down
17 changes: 15 additions & 2 deletions PyFunceble/cli/processes/workers/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions PyFunceble/data/infrastructure/.PyFunceble_production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
2 changes: 1 addition & 1 deletion PyFunceble/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
14 changes: 14 additions & 0 deletions docs/use/configuration/parameters/cli-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
2 changes: 1 addition & 1 deletion version.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 372a91a

Please sign in to comment.