refactor(config): use_rich_logging instead of rich_logging
This commit is contained in:
		| @@ -3,11 +3,14 @@ import os | ||||
| import configparser | ||||
| import sys | ||||
| {%- endif %} | ||||
| {%- if cookiecutter.rich_logging == "yes" %} | ||||
| {%- if cookiecutter.use_rich_logging == "yes" %} | ||||
| import logging | ||||
| from rich.logging import RichHandler | ||||
| {%- 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 | ||||
| @@ -18,7 +21,7 @@ from rich.logging import RichHandler | ||||
| class CONST(object): | ||||
|     __slots__ = () | ||||
| {%- endif %} | ||||
|     {%- if cookiecutter.rich_logging == "yes" %} | ||||
|     {%- if cookiecutter.use_rich_logging == "yes" %} | ||||
|     LOG_FORMAT = "%(message)s" | ||||
|     {%- endif %} | ||||
|     {%- 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"]] | ||||
|     {%- 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"]]) | ||||
| @@ -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" %} | ||||
|  | ||||
|  | ||||
| # 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. | ||||
| class ConfigParser( | ||||
|         configparser.ConfigParser): | ||||
| @@ -115,27 +118,27 @@ def print_section_header( | ||||
|  | ||||
| def validate_default_section( | ||||
|         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 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) | ||||
|     if config.defaults(): | ||||
|         {% if cookiecutter.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.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.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.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 %}(f"Symbol legend:\n" | ||||
|               {% if cookiecutter.use_rich_logging == "yes" %}    {% endif %}f"* Default from section '[{config_obj.default_section}]'\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.use_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 override of a value from '[{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.use_rich_logging == "yes" %}    {% endif %}f"# Local attempt at overriding a global, will be ignored") | ||||
|         {% if cookiecutter.use_rich_logging == "yes" -%}log.debug{%- else -%}print{%- endif %}(print_section_header(config_obj.default_section)) | ||||
|         for default in config_obj.defaults(): | ||||
|             ini_defaults.append({default: config_obj[config_obj.default_section][default]}) | ||||
|             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: | ||||
|                 {% 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: | ||||
|         {% 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( | ||||
| @@ -166,10 +169,10 @@ def is_same_as_default( | ||||
| def validate_config_sections( | ||||
|         config_obj: configparser.ConfigParser()) -> None: | ||||
|     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 cookiecutter.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" -%}log.debug{%- else -%}print{%- endif %}(f"Config section '[{this_section}]' does not have all mandatory options " | ||||
|                   {% if cookiecutter.use_rich_logging == "yes" %}    {% endif %}f"{CONST.CFG_MANDATORY} set, skipping section ...") | ||||
|             config_obj.remove_section(this_section) | ||||
|         else: | ||||
|             for key in config_obj.options(this_section, no_defaults=True): | ||||
| @@ -182,7 +185,7 @@ def validate_config_sections( | ||||
|                     kv_prefix = "+" | ||||
|                     if is_same_as_default({key: config_obj[this_section][key]}): | ||||
|                         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: | ||||
|                     config_obj.remove_option(this_section, key) | ||||
| {%- endif %} | ||||
| @@ -210,13 +213,13 @@ if __name__ == '__main__': | ||||
|     if config_has_valid_section(config): | ||||
|         validate_config_sections(config) | ||||
|     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.rich_logging == "yes" %}    {% endif %}f"{CONST.CFG_MANDATORY} set. Exiting 2 ...") | ||||
|         {% 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.use_rich_logging == "yes" %}    {% endif %}f"{CONST.CFG_MANDATORY} set. Exiting 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(): | ||||
|         {% 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 -%} | ||||
|     pass | ||||
|   | ||||
		Reference in New Issue
	
	Block a user