7
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-06-03 14:50:47 +00:00

feat: add CONFIG_LOADED action

By utilizing CONFIG LOADED, we can now verify if PREVIEW_LMS_HOST is a subdomain of LMS_HOST and display a warning message to the user if it is not.
This commit is contained in:
Emad Rad 2023-10-02 10:38:07 +03:30 committed by GitHub
parent 4a5e9d4271
commit 4eb2085023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 3 deletions

View File

@ -0,0 +1 @@
- 💥[Feature] New action introduced: CONFIG_LOADED. This actions is called whenever the config is loaded. (by @CodeWithEmad)

View File

@ -114,7 +114,7 @@ class ConfigListKeyValParamType(ConfigKeyValParamType):
type=ConfigListKeyValParamType(), type=ConfigListKeyValParamType(),
multiple=True, multiple=True,
metavar="KEY=VAL", metavar="KEY=VAL",
help="Append an item to a configuration value of type list. The value will only be added it it is not already present. (can be used multiple times)", help="Append an item to a configuration value of type list. The value will only be added if it is not already present. (can be used multiple times)",
) )
@click.option( @click.option(
"-A", "-A",
@ -147,16 +147,18 @@ def save(
env_only: bool, env_only: bool,
) -> None: ) -> None:
config = tutor_config.load_minimal(context.root) config = tutor_config.load_minimal(context.root)
config_full = tutor_config.load_full(context.root)
if interactive: if interactive:
interactive_config.ask_questions(config) interactive_config.ask_questions(config)
if set_vars: if set_vars:
for key, value in set_vars: for key, value in set_vars:
config[key] = env.render_unknown(config, value) config[key] = env.render_unknown(config, value)
if append_vars: if append_vars:
config_defaults = tutor_config.load_defaults()
for key, value in append_vars: for key, value in append_vars:
if key not in config: if key not in config:
config[key] = config_full.get(key, []) config[key] = config[key] = config.get(
key, config_defaults.get(key, [])
)
values = config[key] values = config[key]
if not isinstance(values, list): if not isinstance(values, list):
raise exceptions.TutorError( raise exceptions.TutorError(

View File

@ -1,4 +1,5 @@
from __future__ import annotations from __future__ import annotations
from copy import deepcopy
import os import os
@ -26,6 +27,15 @@ def load(root: str) -> Config:
return load_full(root) return load_full(root)
def load_defaults() -> Config:
"""
Load default configuration.
"""
config: Config = {}
update_with_defaults(config)
return config
def load_minimal(root: str) -> Config: def load_minimal(root: str) -> Config:
""" """
Load a minimal configuration composed of the user and the base config. Load a minimal configuration composed of the user and the base config.
@ -51,6 +61,7 @@ def load_full(root: str) -> Config:
update_with_base(config) update_with_base(config)
update_with_defaults(config) update_with_defaults(config)
render_full(config) render_full(config)
hooks.Actions.CONFIG_LOADED.do(deepcopy(config))
return config return config
@ -319,3 +330,19 @@ def _update_enabled_plugins_on_unload(_plugin: str, _root: str, config: Config)
Note that this action must be performed after the plugin has been unloaded, hence the low priority. Note that this action must be performed after the plugin has been unloaded, hence the low priority.
""" """
save_enabled_plugins(config) save_enabled_plugins(config)
@hooks.Actions.CONFIG_LOADED.add()
def _check_preview_lms_host(config: Config) -> None:
"""
This will check if the PREVIEW_LMS_HOST is a subdomain of LMS_HOST.
if not, prints a warning to notify the user.
"""
lms_host = get_typed(config, "LMS_HOST", str, "")
preview_lms_host = get_typed(config, "PREVIEW_LMS_HOST", str, "")
if not preview_lms_host.endswith("." + lms_host):
fmt.echo_alert(
f'Warning: PREVIEW_LMS_HOST="{preview_lms_host}" is not a subdomain of LMS_HOST="{lms_host}". '
"This configuration is not typically recommended and may lead to unexpected behavior."
)

View File

@ -57,6 +57,13 @@ class Actions:
#: :parameter str name: docker-compose project name. #: :parameter str name: docker-compose project name.
COMPOSE_PROJECT_STARTED: Action[[str, Config, str]] = Action() COMPOSE_PROJECT_STARTED: Action[[str, Config, str]] = Action()
#: This action is called at the end of the tutor.config.load_full function.
#: Modifying this object will not trigger changes in the configuration.
#: For all purposes, it should be considered read-only.
#:
#: :parameter dict config: project configuration.
CONFIG_LOADED: Action[[Config]] = Action()
#: Called whenever the core project is ready to run. This action is called as soon #: Called whenever the core project is ready to run. This action is called as soon
#: as possible. This is the right time to discover plugins, for instance. In #: as possible. This is the right time to discover plugins, for instance. In
#: particular, we auto-discover the following plugins: #: particular, we auto-discover the following plugins: