refactor(config): use_rich_logging instead of rich_logging
This commit is contained in:
parent
10d4d8f718
commit
03f11d84ce
@ -2,6 +2,7 @@
|
|||||||
"project_slug": "project-slug",
|
"project_slug": "project-slug",
|
||||||
"__project_slug": "{{ cookiecutter.project_slug.lower().replace(' ', '-').replace('_', '-') }}",
|
"__project_slug": "{{ cookiecutter.project_slug.lower().replace(' ', '-').replace('_', '-') }}",
|
||||||
"__project_slug_under": "{{ cookiecutter.project_slug.lower().replace(' ', '_').replace('-', '_') }}",
|
"__project_slug_under": "{{ cookiecutter.project_slug.lower().replace(' ', '_').replace('-', '_') }}",
|
||||||
"rich_logging": ["yes", "no"],
|
"use_rich_logging": ["yes", "no"],
|
||||||
"uses_config_ini": ["yes", "no"]
|
"uses_config_ini": ["yes", "no"],
|
||||||
|
"uses_inflect": ["yes", "no"]
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
{%- if cookiecutter.rich_logging == "yes" -%}
|
{%- if cookiecutter.use_rich_logging == "yes" -%}
|
||||||
rich
|
rich
|
||||||
{% endif -%}
|
{% endif -%}
|
||||||
|
{%- if cookiecutter.uses_inflect == "yes" -%}
|
||||||
|
inflect
|
||||||
|
{% endif -%}
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
{%- if cookiecutter.rich_logging == "yes" -%}
|
{%- if cookiecutter.use_rich_logging == "yes" or cookiecutter.uses_inflect == "yes" -%}
|
||||||
#
|
#
|
||||||
# This file is autogenerated by pip-compile with python 3.10
|
# This file is autogenerated by pip-compile with python 3.10
|
||||||
# To update, run:
|
# To update, run:
|
||||||
#
|
#
|
||||||
# pip-compile
|
# pip-compile
|
||||||
#
|
#
|
||||||
|
{% endif -%}
|
||||||
|
{%- if cookiecutter.use_rich_logging == "yes" -%}
|
||||||
commonmark==0.9.1
|
commonmark==0.9.1
|
||||||
# via rich
|
# via rich
|
||||||
|
{% endif -%}
|
||||||
|
{%- if cookiecutter.uses_inflect == "yes" -%}
|
||||||
|
inflect==5.6.0
|
||||||
|
# via -r requirements.in
|
||||||
|
{% endif -%}
|
||||||
|
{%- if cookiecutter.use_rich_logging == "yes" -%}
|
||||||
pygments==2.12.0
|
pygments==2.12.0
|
||||||
# via rich
|
# via rich
|
||||||
rich==12.4.4
|
rich==12.4.4
|
||||||
|
@ -3,11 +3,14 @@ import os
|
|||||||
import configparser
|
import configparser
|
||||||
import sys
|
import sys
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{%- if cookiecutter.rich_logging == "yes" %}
|
{%- if cookiecutter.use_rich_logging == "yes" %}
|
||||||
import logging
|
import logging
|
||||||
from rich.logging import RichHandler
|
from rich.logging import RichHandler
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{%- if cookiecutter.rich_logging == "yes" or cookiecutter.uses_config_ini == "yes" %}
|
{%- if cookiecutter.uses_inflect == "yes" %}
|
||||||
|
import inflect
|
||||||
|
{%- endif %}
|
||||||
|
{%- if cookiecutter.use_rich_logging == "yes" or cookiecutter.uses_config_ini == "yes" %}
|
||||||
|
|
||||||
|
|
||||||
# Exit codes
|
# Exit codes
|
||||||
@ -18,7 +21,7 @@ from rich.logging import RichHandler
|
|||||||
class CONST(object):
|
class CONST(object):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{%- if cookiecutter.rich_logging == "yes" %}
|
{%- if cookiecutter.use_rich_logging == "yes" %}
|
||||||
LOG_FORMAT = "%(message)s"
|
LOG_FORMAT = "%(message)s"
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{%- if cookiecutter.uses_config_ini == "yes" %}
|
{%- if cookiecutter.uses_config_ini == "yes" %}
|
||||||
@ -59,7 +62,7 @@ class CONST(object):
|
|||||||
]
|
]
|
||||||
CFG_MANDATORY = [section_cfg["key"] for section_cfg in CFG_KNOWN_SECTION if section_cfg["is_mandatory"]]
|
CFG_MANDATORY = [section_cfg["key"] for section_cfg in CFG_KNOWN_SECTION if section_cfg["is_mandatory"]]
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{%- if cookiecutter.rich_logging == "yes" %}
|
{%- if cookiecutter.use_rich_logging == "yes" %}
|
||||||
|
|
||||||
|
|
||||||
is_systemd = any([systemd_env_var in os.environ for systemd_env_var in ["SYSTEMD_EXEC_PID", "INVOCATION_ID"]])
|
is_systemd = any([systemd_env_var in os.environ for systemd_env_var in ["SYSTEMD_EXEC_PID", "INVOCATION_ID"]])
|
||||||
@ -82,7 +85,7 @@ log.setLevel(os.environ.get("LOGLEVEL") if "LOGLEVEL" in [k for k, v in os.envir
|
|||||||
{%- if cookiecutter.uses_config_ini == "yes" %}
|
{%- if cookiecutter.uses_config_ini == "yes" %}
|
||||||
|
|
||||||
|
|
||||||
# Use this version of class ConfigParser to {% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %} contents of our config file. When parsing sections other than
|
# Use this version of class ConfigParser to {% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %} contents of our config file. When parsing sections other than
|
||||||
# 'default' we don't want to reprint defaults over and over again. This custom class achieves that.
|
# 'default' we don't want to reprint defaults over and over again. This custom class achieves that.
|
||||||
class ConfigParser(
|
class ConfigParser(
|
||||||
configparser.ConfigParser):
|
configparser.ConfigParser):
|
||||||
@ -115,27 +118,27 @@ def print_section_header(
|
|||||||
|
|
||||||
def validate_default_section(
|
def validate_default_section(
|
||||||
config_obj: configparser.ConfigParser()) -> None:
|
config_obj: configparser.ConfigParser()) -> None:
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Loading config from file '{CONST.CFG_DEFAULT_ABS_PATH}' ...")
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Loading config from file '{CONST.CFG_DEFAULT_ABS_PATH}' ...")
|
||||||
if not config_obj.sections():
|
if not config_obj.sections():
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"No config sections found in '{CONST.CFG_DEFAULT_ABS_PATH}'. Exiting 1 ...")
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"No config sections found in '{CONST.CFG_DEFAULT_ABS_PATH}'. Exiting 1 ...")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if config.defaults():
|
if config.defaults():
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Symbol legend:\n"
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Symbol legend:\n"
|
||||||
{% if cookiecutter.rich_logging == "yes" %} {% endif %}f"* Default from section '[{config_obj.default_section}]'\n"
|
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"* Default from section '[{config_obj.default_section}]'\n"
|
||||||
{% if cookiecutter.rich_logging == "yes" %} {% endif %}f": Global option from '[{config_obj.default_section}]', can not be overridden in local sections\n"
|
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f": Global option from '[{config_obj.default_section}]', can not be overridden in local sections\n"
|
||||||
{% if cookiecutter.rich_logging == "yes" %} {% endif %}f"~ Local option, doesn't exist in '[{config_obj.default_section}]'\n"
|
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"~ Local option, doesn't exist in '[{config_obj.default_section}]'\n"
|
||||||
{% if cookiecutter.rich_logging == "yes" %} {% endif %}f"+ Local override of a value from '[{config_obj.default_section}]'\n"
|
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"+ Local override of a value from '[{config_obj.default_section}]'\n"
|
||||||
{% if cookiecutter.rich_logging == "yes" %} {% endif %}f"= Local override, same value as in '[{config_obj.default_section}]'\n"
|
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"= Local override, same value as in '[{config_obj.default_section}]'\n"
|
||||||
{% if cookiecutter.rich_logging == "yes" %} {% endif %}f"# Local attempt at overriding a global, will be ignored")
|
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"# Local attempt at overriding a global, will be ignored")
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(print_section_header(config_obj.default_section))
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(print_section_header(config_obj.default_section))
|
||||||
for default in config_obj.defaults():
|
for default in config_obj.defaults():
|
||||||
ini_defaults.append({default: config_obj[config_obj.default_section][default]})
|
ini_defaults.append({default: config_obj[config_obj.default_section][default]})
|
||||||
if default in internal_globals:
|
if default in internal_globals:
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f": {default} = {config_obj[config_obj.default_section][default]}")
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f": {default} = {config_obj[config_obj.default_section][default]}")
|
||||||
else:
|
else:
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"* {default} = {config_obj[config_obj.default_section][default]}")
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"* {default} = {config_obj[config_obj.default_section][default]}")
|
||||||
else:
|
else:
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"No defaults defined")
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"No defaults defined")
|
||||||
|
|
||||||
|
|
||||||
def config_has_valid_section(
|
def config_has_valid_section(
|
||||||
@ -166,10 +169,10 @@ def is_same_as_default(
|
|||||||
def validate_config_sections(
|
def validate_config_sections(
|
||||||
config_obj: configparser.ConfigParser()) -> None:
|
config_obj: configparser.ConfigParser()) -> None:
|
||||||
for this_section in config_obj.sections():
|
for this_section in config_obj.sections():
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(print_section_header(this_section))
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(print_section_header(this_section))
|
||||||
if not set(CONST.CFG_MANDATORY).issubset(config_obj.options(this_section, no_defaults=True)):
|
if not set(CONST.CFG_MANDATORY).issubset(config_obj.options(this_section, no_defaults=True)):
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Config section '[{this_section}]' does not have all mandatory options "
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Config section '[{this_section}]' does not have all mandatory options "
|
||||||
{% if cookiecutter.rich_logging == "yes" %} {% endif %}f"{CONST.CFG_MANDATORY} set, skipping section ...")
|
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"{CONST.CFG_MANDATORY} set, skipping section ...")
|
||||||
config_obj.remove_section(this_section)
|
config_obj.remove_section(this_section)
|
||||||
else:
|
else:
|
||||||
for key in config_obj.options(this_section, no_defaults=True):
|
for key in config_obj.options(this_section, no_defaults=True):
|
||||||
@ -182,7 +185,7 @@ def validate_config_sections(
|
|||||||
kv_prefix = "+"
|
kv_prefix = "+"
|
||||||
if is_same_as_default({key: config_obj[this_section][key]}):
|
if is_same_as_default({key: config_obj[this_section][key]}):
|
||||||
kv_prefix = "="
|
kv_prefix = "="
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"{kv_prefix} {key} = {config_obj[this_section][key]}")
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"{kv_prefix} {key} = {config_obj[this_section][key]}")
|
||||||
if remove_from_section:
|
if remove_from_section:
|
||||||
config_obj.remove_option(this_section, key)
|
config_obj.remove_option(this_section, key)
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
@ -210,13 +213,13 @@ if __name__ == '__main__':
|
|||||||
if config_has_valid_section(config):
|
if config_has_valid_section(config):
|
||||||
validate_config_sections(config)
|
validate_config_sections(config)
|
||||||
else:
|
else:
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"No valid config section found. A valid config section has at least the mandatory options "
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"No valid config section found. A valid config section has at least the mandatory options "
|
||||||
{% if cookiecutter.rich_logging == "yes" %} {% endif %}f"{CONST.CFG_MANDATORY} set. Exiting 2 ...")
|
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"{CONST.CFG_MANDATORY} set. Exiting 2 ...")
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Iterating over config sections ...")
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Iterating over config sections ...")
|
||||||
for section in config.sections():
|
for section in config.sections():
|
||||||
{% if cookiecutter.rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Processing section '[{section}]' ...")
|
{% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(f"Processing section '[{section}]' ...")
|
||||||
# ...
|
# ...
|
||||||
{%- else -%}
|
{%- else -%}
|
||||||
pass
|
pass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user