def change_environment(environment: str, config_path=Configuration.get_config_path()): """Change the environment (branch) of the configuration""" io.execute("git stash", config_path) io.execute("git fetch origin", config_path) io.execute(f"git checkout {environment}", config_path) io.execute(f"git reset --hard origin/{environment}", config_path)
def get_project_config(config_path: str = Configuration.get_config_path() ) -> dict: """Load the global configuration of the project""" project_config_path = os.path.join( config_path, Configuration.get_config_project_filename()) if not io.exists(project_config_path): raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), project_config_path) project_config = yaml_lib.read_yaml(project_config_path) return _resolve_variables_deep(project_config)
def get_project_config() -> dict: """Load the configuration of the current environment (global configuration)""" project_config_path = os.path.join( Configuration.get_config_path(), Configuration.get_config_projet_filename()) if not io.exists(project_config_path): raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), project_config_path) project_config = io.from_yaml(project_config_path) return _resolve_variables_deep(project_config)
def get_app_config(app_name: str) -> dict: """Load the configuration of an app""" app_config_path = os.path.join( Configuration.get_config_path(), Configuration.get_config_app_folder(), f"{app_name}.yaml", ) if not io.exists(app_config_path): raise AppConfigurationNotFoundError(app_name) app_config = io.from_yaml(app_config_path) environment_config = get_project_config() config = dict_utils.deep_merge(environment_config, app_config) # Awaiting for implementation # validate configuration using nestor-config-validator return _resolve_variables_deep(config)
def list_apps_config(config_path: str = Configuration.get_config_path() ) -> dict: """Retrieves all of the apps configurations keyed by app names.""" apps_path = os.path.join(config_path, Configuration.get_config_app_folder()) if not os.path.isdir(apps_path): raise ValueError(apps_path) apps_config_hashmap = {} for file_path in os.listdir(apps_path): basename = os.path.basename(file_path) filename = PurePath(basename) file_extension = "".join(filename.suffixes) app_name = filename.name.replace(file_extension, "") # Prevent parsing other files than configuration ones (directories, incorrect extension) if file_extension not in [".yml", ".yaml"]: continue apps_config_hashmap[app_name] = get_app_config(app_name) return apps_config_hashmap
def test_get_config_path(monkeypatch): monkeypatch.setenv("NESTOR_CONFIG_PATH", "/a-custom-config-path") assert Configuration.get_config_path() == "/a-custom-config-path"
def create_temporary_config_copy() -> str: """Copy the configuration into a temporary directory and returns its path""" return io.create_temporary_copy(Configuration.get_config_path(), "config")
def test_get_config_path(self): self.assertEqual(Configuration.get_config_path(), "/a-custom-config-path")