Skip to content

Commit

Permalink
Merge pull request #223 from sgeist-ionos/development
Browse files Browse the repository at this point in the history
PR development - 3.3.28
  • Loading branch information
GentleGhostCoder authored Oct 26, 2023
2 parents ac1e448 + 10b58c2 commit bcda97b
Show file tree
Hide file tree
Showing 10 changed files with 568 additions and 478 deletions.
2 changes: 1 addition & 1 deletion cornflakes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

__author__ = "Semjon Geist"
__email__ = "semjon.geist@ionos.com"
__version__ = "3.3.27" # <<FORCE_BUMP>>
__version__ = "3.3.28" # <<FORCE_BUMP>>

__all__ = [
"ini_load",
Expand Down
1 change: 1 addition & 0 deletions cornflakes/cli/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"--log-config",
"--version",
"--verbose",
"--verbose-all",
"--silent",
"--background-process",
"--install-completion",
Expand Down
23 changes: 11 additions & 12 deletions cornflakes/decorator/click/commands/_bg_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from os.path import exists
import re
import sys
from typing import Optional, Union
from typing import List, Optional

from click import Command
from rich.syntax import Syntax
from rich.table import Table

Expand All @@ -29,10 +28,10 @@ def _analyze_logs(file, files: list, error_list: list, warning_list: list):
logs = f.read()
if not logs:
return 0
logs = logs.split("\n")
logs_len += len(logs) + 1
warning_list.extend([line for line in logs if "WARNING" in line.upper()])
error_list.extend([line for line in logs if "ERROR" in line.upper()])
logs_list = logs.split("\n")
logs_len += len(logs_list) + 1
warning_list.extend([line for line in logs_list if "WARNING" in line.upper()])
error_list.extend([line for line in logs_list if "ERROR" in line.upper()])

return logs_len

Expand Down Expand Up @@ -78,10 +77,10 @@ def command_stop(id: Optional[str]):


@command("status")
def command_status(self: Union[RichCommand, Command]):
def command_status(self: RichCommand):
"""Default Command to get status Table of all running processes stated from the parent group."""

group_name = self.parent.name.replace(" ", "_").replace("-", "_")
group_name = str(self.parent.name).replace(" ", "_").replace("-", "_")

log_dir = f".log_{group_name}"

Expand Down Expand Up @@ -112,17 +111,17 @@ def command_status(self: Union[RichCommand, Command]):

idx = 0
for stdout_file in log_files:
stderr_file = stdout_file.replace("_stderr_", "_stdout_")
stderr_file = stdout_file.replace("_stdout_", "_stderr_")
pid = stdout_file.split("_stdout_", 1)[-1].split("_", 1)[0]
is_running = pid in processes
command = processes[pid] if is_running else "-"
status = "RUNNING" if is_running else "NOT RUNNING"
stat = [idx, pid, command, status]
idx += 1

files = []
error_list = []
warning_list = []
files: List[str] = []
error_list: List[str] = []
warning_list: List[str] = []

stderr_log_len = _analyze_logs(stderr_file, files, error_list, warning_list)
stdout_log_len = _analyze_logs(stdout_file, files, error_list, warning_list)
Expand Down
3 changes: 1 addition & 2 deletions cornflakes/decorator/click/options/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from cornflakes.decorator.click.options._config_option import config_option
from cornflakes.decorator.click.options._bg_process import bg_process_option
from cornflakes.decorator.click.options._global import global_option
from cornflakes.decorator.click.options._verbose import verbose_option, verbose_option_all
from cornflakes.decorator.click.options._verbose import verbose_option

__all__ = [
"verbose_option",
"verbose_option_all",
"bg_process_option",
"global_option",
"config_option",
Expand Down
16 changes: 12 additions & 4 deletions cornflakes/decorator/click/options/_config_option.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from functools import reduce, wraps
from inspect import isclass, signature
import logging
from os.path import exists
from typing import Any, Callable, List, Optional, Type, Union
from typing import Any, Callable, List, Optional, Type, Union, get_args

from click import Command, Group, option

Expand Down Expand Up @@ -59,7 +60,12 @@ def config_option( # noqa: C901

def _set_passed_key(wrapper, config, passing_key):
params = signature(wrapper).parameters
passing_keys = [param.name for param in params.values() if param.annotation == config]
passing_keys = [
param.name
for param in params.values()
if param.annotation == config
or (getattr(param.annotation, "__origin__", None) is list and get_args(param.annotation)[0] == config)
]

# Check if there are multiple passing_keys
if len(passing_keys) > 1:
Expand All @@ -76,7 +82,7 @@ def _set_passed_key(wrapper, config, passing_key):
all(param.kind not in [param.VAR_KEYWORD, param.VAR_POSITIONAL] for param in params.values())
and passing_key not in params
):
raise ValueError(
logging.error(
f"Method parameter for {config.__name__} is required, when using config_option, but not provided in the parameters! You can pass the config with the key {passing_key} or by a custom parameter that has the provided config class annotation."
)

Expand Down Expand Up @@ -347,7 +353,7 @@ def _validate_and_set_config(func_params, passed_key, config_type, config_name,

if is_group(config_type):
kwargs[passed_key] = check_type(config_type, kwargs[passed_key], passed_key, skip=False)
elif is_config(config_type):
elif is_config(config_type) or config_type == list:
return _handle_config_type_validation(passed_key, config_type, config_name, **kwargs)
return kwargs

Expand All @@ -366,6 +372,8 @@ def _handle_config_type_validation(passed_key, config_type, config_name, **kwarg

if isinstance(config_value, list):
kwargs[passed_key] = check_type(list, config_value, passed_key, skip=False)
if config_type != list:
kwargs[passed_key] = kwargs[passed_key][0]
else:
kwargs[passed_key] = check_type(config_type, config_value, passed_key, skip=False)

Expand Down
107 changes: 91 additions & 16 deletions cornflakes/decorator/click/options/_verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,102 @@

@global_option(
["-v", "--verbose"],
help="Base logging level is set to logging.DEBUG.",
# option_group="Basic Options",
help="Set logging level is set to logging.DEBUG for module-level loggers.",
is_flag=True,
)
def verbose_option(verbose, self):
"""Default Option for verbose logging."""
@global_option(
["-vv", "--verbose-all"],
help="Set logging level is set to logging.DEBUG for all loggers.",
is_flag=True,
)
@global_option(
["--log-config"],
help="Change The default logging configuration file path (current only yaml format supported).",
type=str,
default="logging.yaml",
)
def verbose_option(verbose, verbose_all, log_config, self):
"""Default Option for verbose logging.
Example logging.yaml:
.. code-block:: yaml
version: 1
disable_existing_loggers: False
formatters:
simple:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
ecs_formatter:
(): ecs_logging.StdlibFormatter
handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: simple
stream: ext://sys.stdout
info_file_handler:
class: logging.handlers.RotatingFileHandler
level: INFO
formatter: simple
filename: info.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
error_file_handler:
class: logging.handlers.RotatingFileHandler
level: ERROR
formatter: simple
filename: errors.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
loggers:
<logger-name>:
level: DEBUG
handlers: [ console ]
propagate: no
root:
level: DEBUG
handlers: [console]
Default logging.yaml:
.. code-block:: yaml
version: 1
disable_existing_loggers: False
formatters:
default:
format: '[%(name)s] - %(funcName)s() - %(message)s'
datefmt: '[%Y-%m-%d %H:%M:%S.%f]'
handlers:
default:
class: rich.logging.RichHandler
level: !py "default_level or logging.root.level"
formatter: default
root:
level: !py "default_level or logging.root.level"
handlers:
- default
"""
if verbose_all:
setup_logging(default_level=logging.DEBUG, force=True)
return

loggers = []
if self.callback.__module__:
main_module_name = self.callback.__module__.split(".", 1)[0] if self.callback.__module__ else None
loggers = [logger for logger in logging.root.manager.loggerDict if main_module_name in logger]
if hasattr(self, "config") and hasattr(self.config, "VERBOSE_LOGGER") and len(self.config.VERBOSE_LOGGER):
loggers.extend(self.config.VERBOSE_LOGGER)
setup_logging(default_level=logging.DEBUG if verbose else logging.root.level, loggers=loggers)


@global_option(
["-vv", "--verbose-all"],
help="Base logging level is set to logging.DEBUG.",
# option_group="Basic Options",
is_flag=True,
)
def verbose_option_all(verbose_all):
"""Default Option for verbose logging."""
setup_logging(default_level=logging.DEBUG if verbose_all else logging.root.level, force=True)
setup_logging(
default_level=logging.DEBUG if verbose else logging.root.level, loggers=loggers, default_path=log_config
)
7 changes: 4 additions & 3 deletions cornflakes/decorator/click/rich/_rich_command.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import sys
from typing import Any, List, Optional

from click import ClickException, Command, Context, HelpFormatter, Parameter, exceptions
from click import ClickException, Command, Context, Group, HelpFormatter, Parameter, exceptions
from rich.console import Console

from cornflakes.decorator.click.rich._rich_click import rich_abort_error, rich_format_error, rich_format_help
from cornflakes.decorator.click.rich._rich_config import RichConfig as RichConfig
Expand All @@ -21,7 +22,7 @@ class RichCommand(Command):
ignore_unknown_options = False
name = ""
context_settings: dict
parent = None
parent: Group
config: RichConfig

def callback(self):
Expand All @@ -33,7 +34,7 @@ def __init__(self, *args, config: Optional[RichConfig] = None, **kwargs):
config = RichConfig()
super().__init__(*args, **kwargs)
self.config = config
self.console = None
self.console: Console

def main(self, *args, standalone_mode: bool = True, **kwargs) -> Any: # noqa: C901
"""Main function of RichGroup."""
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@
# the built documents.
#
# The short X.Y version.
version = "3.3.27" # <<FORCE_BUMP>>
version = "3.3.28" # <<FORCE_BUMP>>
# The full version, including alpha/beta/rc tags.
release = "3.3.27" # <<FORCE_BUMP>>
release = "3.3.28" # <<FORCE_BUMP>>

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
Loading

0 comments on commit bcda97b

Please sign in to comment.