7
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-06-01 22:00:48 +00:00

Minor code refactoring, for naming clarity

This commit is contained in:
Régis Behmo 2020-02-27 17:14:00 +01:00
parent 5e626114de
commit 7eb99a3811
7 changed files with 29 additions and 23 deletions

View File

@ -54,7 +54,7 @@ class ConfigTests(unittest.TestCase):
password1 = config1["MYSQL_ROOT_PASSWORD"]
config1.pop("MYSQL_ROOT_PASSWORD")
tutor_config.save(root, config1)
tutor_config.save_config_file(root, config1)
mock_random_string.return_value = "efgh"
config2, _ = tutor_config.load_all(root)

View File

@ -58,7 +58,7 @@ def save(context, interactive, set_, unset):
tutor_config.merge(config, dict(set_), force=True)
for key in unset:
config.pop(key, None)
tutor_config.save(context.root, config)
tutor_config.save_config_file(context.root, config)
tutor_config.merge(config, defaults)
env.save(context.root, config)
@ -78,7 +78,9 @@ def save(context, interactive, set_, unset):
def render(context, extra_configs, src, dst):
config = tutor_config.load(context.root)
for extra_config in extra_configs:
tutor_config.merge(config, tutor_config.load_file(extra_config), force=True)
tutor_config.merge(
config, tutor_config.load_config_file(extra_config), force=True
)
renderer = env.Renderer(config, [src])
renderer.render_all_to(dst)

View File

@ -44,7 +44,7 @@ def enable(context, plugin_names):
for plugin in plugin_names:
plugins.enable(config, plugin)
fmt.echo_info("Plugin {} enabled".format(plugin))
tutor_config.save(context.root, config)
tutor_config.save_config_file(context.root, config)
fmt.echo_info(
"You should now re-generate your environment with `tutor config save`."
)
@ -63,7 +63,7 @@ def disable(context, plugin_names):
shutil.rmtree(plugin_dir)
fmt.echo_info("Plugin {} disabled".format(plugin))
tutor_config.save(context.root, config)
tutor_config.save_config_file(context.root, config)
fmt.echo_info(
"You should now re-generate your environment with `tutor config save`."
)

View File

@ -87,7 +87,7 @@ def start(context, port, host):
)
@click.pass_obj
def configure(context, user, password):
save_config(context.root, {"user": user, "password": password})
save_webui_config_file(context.root, {"user": user, "password": password})
fmt.echo_info(
"The web UI configuration has been updated. "
"If at any point you wish to reset your username and password, "
@ -122,12 +122,12 @@ def check_gotty_binary(root):
def load_config(root):
path = config_path(root)
if not os.path.exists(path):
save_config(root, {"user": None, "password": None})
save_webui_config_file(root, {"user": None, "password": None})
with open(config_path(root)) as f:
return serialize.load(f)
def save_config(root, config):
def save_webui_config_file(root, config):
path = config_path(root)
directory = os.path.dirname(path)
if not os.path.exists(directory):

View File

@ -14,7 +14,7 @@ def update(root):
Load and save the configuration.
"""
config, defaults = load_all(root)
save(root, config)
save_config_file(root, config)
merge(config, defaults)
return config
@ -53,10 +53,10 @@ def merge(config, defaults, force=False):
def load_defaults():
return serialize.load(env.read("config.yml"))
return serialize.load(env.read_template_file("config.yml"))
def load_file(path):
def load_config_file(path):
with open(path) as f:
return serialize.load(f.read())
@ -79,8 +79,7 @@ def load_user(root):
if not os.path.exists(path):
return {}
with open(path) as fi:
config = serialize.load(fi.read())
config = load_config_file(path)
upgrade_obsolete(config)
return config
@ -164,16 +163,15 @@ def convert_json2yml(root):
root
)
)
with open(json_path) as fi:
config = json.load(fi)
save(root, config)
config = load_config_file(json_path)
save_config_file(root, config)
os.remove(json_path)
fmt.echo_info(
"File config.json detected in {} and converted to config.yml".format(root)
)
def save(root, config):
def save_config_file(root, config):
path = config_path(root)
utils.ensure_file_directory_exists(path)
with open(path, "w") as of:

View File

@ -120,6 +120,10 @@ class Renderer:
return self.__render(template)
def render_file(self, path):
"""
Render a template file. Return the corresponding string. If it's a binary file
(as indicated by its path), return bytes.
"""
if is_binary_file(path):
# Don't try to render binary files
with open(self.find_path(path), "rb") as f:
@ -205,7 +209,7 @@ def save_all_from(prefix, root, config):
def write_to(content, path):
"""
Content can be either str or bytes.
Write some content to a path. Content can be either str or bytes.
"""
open_mode = "w"
if isinstance(content, bytes):
@ -267,17 +271,19 @@ def check_is_up_to_date(root):
"\n"
" tutor config save"
)
fmt.echo_alert(message.format(base_dir(root), version(root), __version__))
fmt.echo_alert(
message.format(base_dir(root), current_version(root), __version__)
)
def is_up_to_date(root):
"""
Check if the currently rendered version is equal to the current tutor version.
"""
return version(root) == __version__
return current_version(root) == __version__
def version(root):
def current_version(root):
"""
Return the current environment version. If the current environment has no version,
return "0".
@ -288,7 +294,7 @@ def version(root):
return open(path).read().strip()
def read(*path):
def read_template_file(*path):
"""
Read raw content of template located at `path`.
"""

View File

@ -13,7 +13,7 @@ def update(root, interactive=True):
Load and save the configuration.
"""
config, defaults = load_all(root, interactive=interactive)
tutor_config.save(root, config)
tutor_config.save_config_file(root, config)
tutor_config.merge(config, defaults)
return config