diff --git a/tests/test_config.py b/tests/test_config.py index 52d6382..105b2b7 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index f55ad13..bc11f85 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -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) diff --git a/tutor/commands/plugins.py b/tutor/commands/plugins.py index 6d2a832..0137c1d 100644 --- a/tutor/commands/plugins.py +++ b/tutor/commands/plugins.py @@ -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`." ) diff --git a/tutor/commands/webui.py b/tutor/commands/webui.py index c3a2f2b..896910e 100644 --- a/tutor/commands/webui.py +++ b/tutor/commands/webui.py @@ -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): diff --git a/tutor/config.py b/tutor/config.py index 7ef1951..ddd5960 100644 --- a/tutor/config.py +++ b/tutor/config.py @@ -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: diff --git a/tutor/env.py b/tutor/env.py index c1f7df2..16fd726 100644 --- a/tutor/env.py +++ b/tutor/env.py @@ -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`. """ diff --git a/tutor/interactive.py b/tutor/interactive.py index 36181a6..493f309 100644 --- a/tutor/interactive.py +++ b/tutor/interactive.py @@ -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