Compare commits

..

17 Commits

Author SHA1 Message Date
bfbc829f91 feat(naive-python): Add grammar via inflect, improve overall handling 2022-07-05 19:42:11 +02:00
fb835f4f40 refactor(debug): Increase log level 2022-07-05 19:38:53 +02:00
b6da0efa29 feat(debug): Load inflect to render grammatically correct text 2022-07-05 19:36:52 +02:00
dab22b8c96 fix(config): Remove duplicate Configparser loading 2022-07-05 19:36:45 +02:00
f2c8b15b84 fix(debug): Add exit code 7 definition 2022-07-05 19:36:42 +02:00
8bf8ae3def fix(debug): Print when Rich is unavailable 2022-07-05 19:36:37 +02:00
1d929f5fb3 refactor(build): Double quotes instead of single quotes 2022-07-05 19:36:33 +02:00
0971286823 feat(config): For empty vars check differentiate between print and rich 2022-07-05 19:11:16 +02:00
deee35fc32 feat(config): Add list of config options where empty value is okay 2022-07-05 19:10:47 +02:00
b830f84ebe feat(config): Add check if setting is incorrectly empty 2022-07-05 19:03:21 +02:00
b64dc5f595 refactor(config): Clearer Cookiecutter variable names 2022-07-05 18:58:11 +02:00
03f11d84ce refactor(config): use_rich_logging instead of rich_logging 2022-07-05 18:50:45 +02:00
10d4d8f718 feat(config): Given a LOGLEVEL env var let user change verbosity, default to INFO 2022-07-05 18:36:29 +02:00
a065550d50 feat(config): In systemd make logging output slimmer 2022-07-05 18:35:15 +02:00
87d5a5bf04 feat(config): CFG_KNOWN_DEFAULTS can now be empty and still valid 2022-07-05 18:32:12 +02:00
71d68a47ed refactor(config): By default lead user to sparingly set CFG_KNOWN_SECTION 2022-07-05 18:26:00 +02:00
687b5bf422 refactor(debug): Be more concise in config description 2022-07-05 18:24:51 +02:00
8 changed files with 201 additions and 81 deletions

View File

@@ -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"] "use_config_ini": ["yes", "no"],
"use_inflect": ["yes", "no"]
} }

View File

@@ -1 +1,2 @@
rich rich
inflect

View File

@@ -6,6 +6,8 @@
# #
commonmark==0.9.1 commonmark==0.9.1
# via rich # via rich
inflect==5.6.0
# via -r requirements.in
pygments==2.12.0 pygments==2.12.0
# via rich # via rich
rich==12.4.4 rich==12.4.4

View File

@@ -1,13 +1,20 @@
# Path and env manipulation
import os import os
# Use a config file
import configparser import configparser
# Exit with various exit codes
import sys import sys
# Manipulate style and content of logs
import logging import logging
from rich.logging import RichHandler from rich.logging import RichHandler
# Correctly generate plurals, singular nouns etc.
import inflect
# Exit codes # Exit codes
# 1: Config file invalid, it has no sections # 1: Config file invalid, it has no sections
# 2: Config file invalid, sections must define at least CONST.CFG_MANDATORY # 2: Config file invalid, sections must define at least CONST.CFG_MANDATORY
# 7 : An option that must have a non-null value is either unset or null
class CONST(object): class CONST(object):
@@ -20,42 +27,55 @@ class CONST(object):
# Values you don't have to set, these are their internal defaults. You may optionally add a key 'is_global' equal # Values you don't have to set, these are their internal defaults. You may optionally add a key 'is_global' equal
# to either True or False. By default if left off it'll be assumed False. Script will treat values where # to either True or False. By default if left off it'll be assumed False. Script will treat values where
# 'is_global' equals True as not being overridable in a '[section]'. It's a setting that only makes sense in a # 'is_global' equals True as not being overridable in a '[section]'. It's a setting that only makes sense in a
# global context for the entire script. # global context for the entire script. An option where 'empty_ok' equals True can safely be unset or set to
# an empty string. An example config.ini file may give a sane config example value here, removing that value
# still results in a valid file.
CFG_KNOWN_DEFAULTS = [ CFG_KNOWN_DEFAULTS = [
{"key": "self_name", "value": "rich-and-config"}, {"key": "self_name", "value": "rich-and-config", "empty_ok": False},
{"key": "tmp_base_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/tmp/%(self_name)s")}, {"key": "tmp_base_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/tmp/%(self_name)s"),
{"key": "state_base_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/var/lib/%(self_name)s")}, "empty_ok": False},
{"key": "state_files_dir", "value": "%(state_base_dir)s/state", "is_global": False}, {"key": "state_base_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/var/lib/%(self_name)s"),
{"key": "state_file_retention", "value": "50", "is_global": False}, "empty_ok": False},
{"key": "state_file_name_prefix", "value": "state-", "is_global": False}, {"key": "state_files_dir", "value": "%(state_base_dir)s/state", "is_global": False, "empty_ok": False},
{"key": "state_file_name_suffix", "value": ".log", "is_global": False}, {"key": "state_file_retention", "value": "50", "is_global": False, "empty_ok": True},
{"key": "rich_and_config_some_option", "value": "http://localhost:8000/api/query", "is_global": True}, {"key": "state_file_name_prefix", "value": "state-", "is_global": False, "empty_ok": True},
{"key": "another_option", "value": "first", "is_global": True} {"key": "state_file_name_suffix", "value": ".log", "is_global": False, "empty_ok": True},
{"key": "rich_and_config_some_option", "value": "http://localhost:8000/api/query", "is_global": True,
"empty_ok": False},
{"key": "another_option", "value": "first", "is_global": True, "empty_ok": True}
] ]
# In all sections other than 'default' the following settings are known and accepted. We silently ignore other # In all sections other than 'default' the following settings are known and accepted. We ignore other settings.
# settings. We use 'is_mandatory' to determine if we have to raise errors on missing settings. # Per CFG_KNOWN_DEFAULTS above most '[DEFAULT]' options are accepted by virtue of being defaults and overridable.
# The only exception are options where "is_global" equals True, they can't be overridden in '[sections]'; any
# attempt at doing it anyway will be ignored. The main purpose of this list is to name settings that do not have
# a default value but can - if set - influence how a '[section]' behaves. Repeating a '[DEFAULT]' here does not
# make sense. We use 'is_mandatory' to determine if we have to raise errors on missing settings. Here
# 'is_mandatory' means the setting must be given in a '[section]'. It may be empty.
CFG_KNOWN_SECTION = [ CFG_KNOWN_SECTION = [
{"key": "min_duration", "is_mandatory": False}, # {"key": "an_option", "is_mandatory": True},
{"key": "max_duration", "is_mandatory": False}, # {"key": "another_one", "is_mandatory": False}
{"key": "title_not_regex", "is_mandatory": False},
{"key": "query", "is_mandatory": True},
{"key": "dl_dir", "is_mandatory": True}
] ]
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"]]
is_systemd = any([systemd_env_var in os.environ for systemd_env_var in ["SYSTEMD_EXEC_PID", "INVOCATION_ID"]])
logging.basicConfig( logging.basicConfig(
# Default for all modules is NOTSET so log everything # Default for all modules is NOTSET so log everything
level="NOTSET", level="NOTSET",
format=CONST.LOG_FORMAT, format=CONST.LOG_FORMAT,
datefmt="[%X]", datefmt="[%X]",
handlers=[RichHandler( handlers=[RichHandler(
show_time=False if is_systemd else True,
show_path=False if is_systemd else True,
show_level=False if is_systemd else True,
rich_tracebacks=True rich_tracebacks=True
)] )]
) )
log = logging.getLogger("rich") log = logging.getLogger("rich")
# Our own code logs with this level # Our own code logs with this level
log.setLevel(logging.DEBUG) log.setLevel(os.environ.get("LOGLEVEL") if "LOGLEVEL" in [k for k, v in os.environ.items()] else logging.INFO)
p = inflect.engine()
# Use this version of class ConfigParser to log.debug contents of our config file. When parsing sections other than # Use this version of class ConfigParser to log.debug contents of our config file. When parsing sections other than
@@ -80,7 +100,9 @@ class ConfigParser(
ini_defaults = [] ini_defaults = []
internal_defaults = {default["key"]: default["value"] for default in CONST.CFG_KNOWN_DEFAULTS} internal_defaults = {default["key"]: default["value"] for default in CONST.CFG_KNOWN_DEFAULTS}
internal_globals = [default["key"] for default in CONST.CFG_KNOWN_DEFAULTS if default["is_global"]] internal_globals = [default["key"] for default in CONST.CFG_KNOWN_DEFAULTS if default["is_global"]]
config = ConfigParser(defaults=internal_defaults) internal_empty_ok = [default["key"] for default in CONST.CFG_KNOWN_DEFAULTS if default["empty_ok"]]
config = ConfigParser(defaults=internal_defaults,
converters={'list': lambda x: [i.strip() for i in x.split(',') if len(x) > 0]})
config.read(CONST.CFG_DEFAULT_ABS_PATH) config.read(CONST.CFG_DEFAULT_ABS_PATH)
@@ -139,13 +161,41 @@ def is_same_as_default(
return config_kv_pair in ini_defaults return config_kv_pair in ini_defaults
def we_have_unset_options(
config_obj: configparser.ConfigParser(),
section_name: str) -> list:
options_must_be_non_empty = []
for option in config_obj.options(section_name):
if not config_obj.get(section_name, option):
if option not in internal_empty_ok:
log.warning(f"In section '[{section_name}]' option '{option}' is empty, it mustn't be.")
options_must_be_non_empty.append(option)
return options_must_be_non_empty
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():
log.debug(print_section_header(this_section)) log.debug(print_section_header(this_section))
unset_options = we_have_unset_options(config_obj, this_section)
if unset_options:
log.error(f"""{p.plural("Option", len(unset_options))} {unset_options} """
f"""{p.plural("is", len(unset_options))} unset. """
f"""{p.singular_noun("They", len(unset_options))} """
f"must have a non-null value. "
f"""{p.plural("Default", len(unset_options))} {p.plural("is", len(unset_options))}:""")
for unset_option in unset_options:
log.error(f"{unset_option} = {internal_defaults[unset_option]}")
log.error(f"Exiting 7 ...")
sys.exit(7)
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)):
log.debug(f"Config section '[{this_section}]' does not have all mandatory options " log.warning(f"Config section '[{this_section}]' does not have all mandatory options "
f"{CONST.CFG_MANDATORY} set, skipping section ...") 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):
@@ -172,16 +222,16 @@ def an_important_function(
return ["I", "am", "a", "list"] return ["I", "am", "a", "list"]
if __name__ == '__main__': if __name__ == "__main__":
validate_default_section(config) validate_default_section(config)
if config_has_valid_section(config): if config_has_valid_section(config):
validate_config_sections(config) validate_config_sections(config)
else: else:
log.debug(f"No valid config section found. A valid config section has at least the mandatory options " log.error(f"No valid config section found. A valid config section has at least the mandatory options "
f"{CONST.CFG_MANDATORY} set. Exiting 2 ...") f"{CONST.CFG_MANDATORY} set. Exiting 2 ...")
sys.exit(2) sys.exit(2)
log.debug(f"Iterating over config sections ...") log.debug(f"Iterating over config sections ...")
for section in config.sections(): for section in config.sections():
log.debug(f"Processing section '[{section}]' ...") log.info(f"Processing section '[{section}]' ...")
# ... # ...

View File

@@ -6,7 +6,7 @@ config_ini_file_name = "config.ini.example"
examples_dir_abs = os.path.join(project_dir, examples_dir_name) examples_dir_abs = os.path.join(project_dir, examples_dir_name)
config_ini_file_abs = os.path.join(project_dir, examples_dir_name, config_ini_file_name) config_ini_file_abs = os.path.join(project_dir, examples_dir_name, config_ini_file_name)
if {% if cookiecutter.uses_config_ini == "yes" -%}False{% else -%}True{%- endif -%}: if {% if cookiecutter.use_config_ini == "yes" -%}False{% else -%}True{%- endif -%}:
try: try:
os.remove(config_ini_file_abs) os.remove(config_ini_file_abs)
try: try:

View File

@@ -1,3 +1,6 @@
{%- if cookiecutter.rich_logging == "yes" -%} {%- if cookiecutter.use_rich_logging == "yes" -%}
rich rich
{% endif -%} {% endif -%}
{%- if cookiecutter.use_inflect == "yes" -%}
inflect
{% endif -%}

View File

@@ -1,12 +1,20 @@
{%- if cookiecutter.rich_logging == "yes" -%} {%- if cookiecutter.use_rich_logging == "yes" or cookiecutter.use_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.use_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

View File

@@ -1,27 +1,36 @@
{% if cookiecutter.uses_config_ini == "yes" -%} {% if cookiecutter.use_config_ini == "yes" -%}
# Path and env manipulation
import os import os
# Use a config file
import configparser import configparser
# Exit with various exit codes
import sys import sys
{%- endif %} {%- endif %}
{%- if cookiecutter.rich_logging == "yes" %} {%- if cookiecutter.use_rich_logging == "yes" %}
# Manipulate style and content of logs
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.use_inflect == "yes" %}
# Correctly generate plurals, singular nouns etc.
import inflect
{%- endif %}
{%- if cookiecutter.use_rich_logging == "yes" or cookiecutter.use_config_ini == "yes" %}
# Exit codes # Exit codes
# 1: Config file invalid, it has no sections # 1: Config file invalid, it has no sections
# 2: Config file invalid, sections must define at least CONST.CFG_MANDATORY # 2: Config file invalid, sections must define at least CONST.CFG_MANDATORY
# 7 : An option that must have a non-null value is either unset or null
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.use_config_ini == "yes" %}
# How to find a config file # How to find a config file
CFG_THIS_FILE_DIRNAME = os.path.dirname(__file__) CFG_THIS_FILE_DIRNAME = os.path.dirname(__file__)
CFG_DEFAULT_FILENAME = "config.ini" CFG_DEFAULT_FILENAME = "config.ini"
@@ -29,49 +38,65 @@ class CONST(object):
# Values you don't have to set, these are their internal defaults. You may optionally add a key 'is_global' equal # Values you don't have to set, these are their internal defaults. You may optionally add a key 'is_global' equal
# to either True or False. By default if left off it'll be assumed False. Script will treat values where # to either True or False. By default if left off it'll be assumed False. Script will treat values where
# 'is_global' equals True as not being overridable in a '[section]'. It's a setting that only makes sense in a # 'is_global' equals True as not being overridable in a '[section]'. It's a setting that only makes sense in a
# global context for the entire script. # global context for the entire script. An option where 'empty_ok' equals True can safely be unset or set to
# an empty string. An example config.ini file may give a sane config example value here, removing that value
# still results in a valid file.
CFG_KNOWN_DEFAULTS = [ CFG_KNOWN_DEFAULTS = [
{"key": "self_name", "value": "{{ cookiecutter.__project_slug }}"}, {"key": "self_name", "value": "{{ cookiecutter.__project_slug }}", "empty_ok": False},
{"key": "tmp_base_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/tmp/%(self_name)s")}, {"key": "tmp_base_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/tmp/%(self_name)s"),
{"key": "state_base_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/var/lib/%(self_name)s")}, "empty_ok": False},
{"key": "state_files_dir", "value": "%(state_base_dir)s/state", "is_global": False}, {"key": "state_base_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/var/lib/%(self_name)s"),
{"key": "state_file_retention", "value": "50", "is_global": False}, "empty_ok": False},
{"key": "state_file_name_prefix", "value": "state-", "is_global": False}, {"key": "state_files_dir", "value": "%(state_base_dir)s/state", "is_global": False, "empty_ok": False},
{"key": "state_file_name_suffix", "value": ".log", "is_global": False}, {"key": "state_file_retention", "value": "50", "is_global": False, "empty_ok": True},
{"key": "{{ cookiecutter.__project_slug_under }}_some_option", "value": "http://localhost:8000/api/query", "is_global": True}, {"key": "state_file_name_prefix", "value": "state-", "is_global": False, "empty_ok": True},
{"key": "another_option", "value": "first", "is_global": True} {"key": "state_file_name_suffix", "value": ".log", "is_global": False, "empty_ok": True},
{"key": "{{ cookiecutter.__project_slug_under }}_some_option", "value": "http://localhost:8000/api/query", "is_global": True,
"empty_ok": False},
{"key": "another_option", "value": "first", "is_global": True, "empty_ok": True}
] ]
# In all sections other than 'default' the following settings are known and accepted. We silently ignore other # In all sections other than 'default' the following settings are known and accepted. We ignore other settings.
# settings. We use 'is_mandatory' to determine if we have to raise errors on missing settings. # Per CFG_KNOWN_DEFAULTS above most '[DEFAULT]' options are accepted by virtue of being defaults and overridable.
# The only exception are options where "is_global" equals True, they can't be overridden in '[sections]'; any
# attempt at doing it anyway will be ignored. The main purpose of this list is to name settings that do not have
# a default value but can - if set - influence how a '[section]' behaves. Repeating a '[DEFAULT]' here does not
# make sense. We use 'is_mandatory' to determine if we have to raise errors on missing settings. Here
# 'is_mandatory' means the setting must be given in a '[section]'. It may be empty.
CFG_KNOWN_SECTION = [ CFG_KNOWN_SECTION = [
{"key": "min_duration", "is_mandatory": False}, # {"key": "an_option", "is_mandatory": True},
{"key": "max_duration", "is_mandatory": False}, # {"key": "another_one", "is_mandatory": False}
{"key": "title_not_regex", "is_mandatory": False},
{"key": "query", "is_mandatory": True},
{"key": "dl_dir", "is_mandatory": True}
] ]
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"]])
logging.basicConfig( logging.basicConfig(
# Default for all modules is NOTSET so log everything # Default for all modules is NOTSET so log everything
level="NOTSET", level="NOTSET",
format=CONST.LOG_FORMAT, format=CONST.LOG_FORMAT,
datefmt="[%X]", datefmt="[%X]",
handlers=[RichHandler( handlers=[RichHandler(
show_time=False if is_systemd else True,
show_path=False if is_systemd else True,
show_level=False if is_systemd else True,
rich_tracebacks=True rich_tracebacks=True
)] )]
) )
log = logging.getLogger("rich") log = logging.getLogger("rich")
# Our own code logs with this level # Our own code logs with this level
log.setLevel(logging.DEBUG) log.setLevel(os.environ.get("LOGLEVEL") if "LOGLEVEL" in [k for k, v in os.environ.items()] else logging.INFO)
{%- endif %}{%- if cookiecutter.use_rich_logging == "no" %}
{% endif %}
{%- if cookiecutter.use_inflect == "yes" %}
p = inflect.engine()
{%- endif %} {%- endif %}
{%- if cookiecutter.uses_config_ini == "yes" %} {%- if cookiecutter.use_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):
@@ -93,7 +118,9 @@ class ConfigParser(
ini_defaults = [] ini_defaults = []
internal_defaults = {default["key"]: default["value"] for default in CONST.CFG_KNOWN_DEFAULTS} internal_defaults = {default["key"]: default["value"] for default in CONST.CFG_KNOWN_DEFAULTS}
internal_globals = [default["key"] for default in CONST.CFG_KNOWN_DEFAULTS if default["is_global"]] internal_globals = [default["key"] for default in CONST.CFG_KNOWN_DEFAULTS if default["is_global"]]
config = ConfigParser(defaults=internal_defaults) internal_empty_ok = [default["key"] for default in CONST.CFG_KNOWN_DEFAULTS if default["empty_ok"]]
config = ConfigParser(defaults=internal_defaults,
converters={'list': lambda x: [i.strip() for i in x.split(',') if len(x) > 0]})
config.read(CONST.CFG_DEFAULT_ABS_PATH) config.read(CONST.CFG_DEFAULT_ABS_PATH)
@@ -104,27 +131,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(
@@ -152,13 +179,41 @@ def is_same_as_default(
return config_kv_pair in ini_defaults return config_kv_pair in ini_defaults
def we_have_unset_options(
config_obj: configparser.ConfigParser(),
section_name: str) -> list:
options_must_be_non_empty = []
for option in config_obj.options(section_name):
if not config_obj.get(section_name, option):
if option not in internal_empty_ok:
{% if cookiecutter.use_rich_logging == "yes" -%}log.warning{%- else -%}print{%- endif %}(f"In section '[{section_name}]' option '{option}' is empty, it mustn't be.")
options_must_be_non_empty.append(option)
return options_must_be_non_empty
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))
unset_options = we_have_unset_options(config_obj, this_section)
if unset_options:
{% if cookiecutter.use_rich_logging == "yes" -%}log.error{%- else -%}print{%- endif %}(f"""{% if cookiecutter.use_inflect == "yes" %}{p.plural("Option", len(unset_options))}{% else %}Options{% endif %} {unset_options} """
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"""{% if cookiecutter.use_inflect == "yes" %}{p.plural("is", len(unset_options))}{% else %}are{% endif %} unset. """
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"""{% if cookiecutter.use_inflect == "yes" %}{p.singular_noun("They", len(unset_options))}{% else %}They{% endif %} """
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"must have a non-null value. "
{% if cookiecutter.use_rich_logging == "yes" %} {% endif %}f"""{% if cookiecutter.use_inflect == "yes" %}{p.plural("Default", len(unset_options))} {p.plural("is", len(unset_options))}{% else %}Defaults are{% endif %}:""")
for unset_option in unset_options:
{% if cookiecutter.use_rich_logging == "yes" -%}log.error{%- else -%}print{%- endif %}(f"{unset_option} = {internal_defaults[unset_option]}")
{% if cookiecutter.use_rich_logging == "yes" -%}log.error{%- else -%}print{%- endif %}(f"Exiting 7 ...")
sys.exit(7)
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.warning{%- 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):
@@ -171,7 +226,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 %}
@@ -179,11 +234,11 @@ def validate_config_sections(
def an_important_function( def an_important_function(
section_name: str, section_name: str,
{%- if cookiecutter.uses_config_ini == "yes" %} {%- if cookiecutter.use_config_ini == "yes" %}
config_obj: configparser.ConfigParser(), config_obj: configparser.ConfigParser(),
{%- endif %} {%- endif %}
whatever: str) -> list: whatever: str) -> list:
{%- if cookiecutter.uses_config_ini == "yes" %} {%- if cookiecutter.use_config_ini == "yes" %}
min_duration = config_obj.getint(section_name, "min_duration") min_duration = config_obj.getint(section_name, "min_duration")
max_duration = config_obj.getint(section_name, "max_duration") max_duration = config_obj.getint(section_name, "max_duration")
{%- else %} {%- else %}
@@ -193,19 +248,19 @@ def an_important_function(
return ["I", "am", "a", "list"] return ["I", "am", "a", "list"]
if __name__ == '__main__': if __name__ == "__main__":
{% if cookiecutter.uses_config_ini == "yes" -%} {% if cookiecutter.use_config_ini == "yes" -%}
validate_default_section(config) validate_default_section(config)
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.error{%- 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.info{%- else -%}print{%- endif %}(f"Processing section '[{section}]' ...")
# ... # ...
{%- else -%} {%- else -%}
pass pass