Skip to content

Commit ae2dbfa

Browse files
committed
Prevent reloading filters for every template validation
1 parent 933dab3 commit ae2dbfa

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/opentaskpy/config/loader.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self, config_dir: str) -> None:
3333
self.logger = opentaskpy.otflogging.init_logging(__name__)
3434
self.config_dir = config_dir
3535
self.global_variables: dict = {}
36+
self.loaded_filters: dict = {}
3637

3738
self.logger.log(12, f"Looking in {self.config_dir}")
3839

@@ -41,6 +42,7 @@ def __init__(self, config_dir: str) -> None:
4142
self.template_env = jinja2.Environment(undefined=jinja2.StrictUndefined)
4243

4344
self._load_filters(self.template_env.filters)
45+
self.loaded_filters = self.template_env.filters
4446

4547
self._load_global_variables()
4648

@@ -56,6 +58,11 @@ def _load_filters(self, destination: dict) -> None:
5658
Args:
5759
destination (dict): The destination dictionary to load the filters into
5860
"""
61+
# Prevent multiple loads
62+
if self.loaded_filters:
63+
destination.update(self.loaded_filters)
64+
return
65+
5966
# Check what functions exist in the module
6067
for name, func in inspect.getmembers(default_filters, inspect.isfunction):
6168
destination[name] = func
@@ -79,31 +86,46 @@ def _load_filters(self, destination: dict) -> None:
7986
else:
8087
self.logger.log(12, f"Couldn't import custom filter: {filter_file}")
8188

89+
destination = self.loaded_filters
90+
8291
def _override_variables_from_env(self, variables: dict, variable_type: str) -> None:
8392
"""Overrides variables with environment variables."""
84-
for env_var_name, env_var_value in os.environ.items():
93+
for ( # pylint: disable=too-many-nested-blocks
94+
env_var_name,
95+
env_var_value,
96+
) in os.environ.items():
8597
if "." in env_var_name:
8698
key_path = env_var_name.split(".")
8799
current_dict = variables
100+
self.logger.log(12, f"Searching for {env_var_name}")
88101
for i, key in enumerate(key_path):
89102
if isinstance(current_dict, dict) and key in current_dict:
90103
if i == len(key_path) - 1:
91104
# It's the final key, override the value
92105
self.logger.info(
93106
f"Overriding nested {variable_type} variable '{env_var_name}' with environment variable."
94107
)
95-
current_dict[key] = env_var_value
108+
# Check the original type of the variable, if it was an int, then cast it to an int
109+
if isinstance(current_dict[key], int):
110+
current_dict[key] = int(env_var_value)
111+
else:
112+
current_dict[key] = env_var_value
96113
else:
97114
# Traverse deeper
98115
current_dict = current_dict[key]
99116
else:
100117
# The key path does not exist in the dictionary
101118
break
119+
102120
elif env_var_name in variables:
103121
self.logger.info(
104122
f"Overriding {variable_type} variable ({env_var_name}: {variables[env_var_name]}) with environment variable ({env_var_value})"
105123
)
106-
variables[env_var_name] = env_var_value
124+
# Check the original type of the variable, if it was an int, then cast it to an int
125+
if isinstance(variables[env_var_name], int):
126+
variables[env_var_name] = int(env_var_value)
127+
else:
128+
variables[env_var_name] = env_var_value
107129

108130
def get_global_variables(self) -> dict:
109131
"""Return the set of global variables that have been assigned via config files.

0 commit comments

Comments
 (0)