From f3438a3b2b7453fa5ede8a62209da244b6aede3f Mon Sep 17 00:00:00 2001 From: Yael Genish <62285310+yaelgen@users.noreply.github.com> Date: Mon, 16 Jan 2023 15:16:36 +0200 Subject: [PATCH] [Project] Add helper func to determine if url suffix is yaml/yml (#2925) --- mlrun/projects/project.py | 17 ++++++++++++----- mlrun/utils/helpers.py | 4 ++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/mlrun/projects/project.py b/mlrun/projects/project.py index c15d875a00..80ccc80f8d 100644 --- a/mlrun/projects/project.py +++ b/mlrun/projects/project.py @@ -45,7 +45,14 @@ from ..run import code_to_function, get_object, import_function, new_function from ..runtimes.utils import add_code_metadata from ..secrets import SecretsStore -from ..utils import is_ipython, is_legacy_artifact, is_relative_path, logger, update_in +from ..utils import ( + is_ipython, + is_legacy_artifact, + is_relative_path, + is_yaml_path, + logger, + update_in, +) from ..utils.clones import clone_git, clone_tgz, clone_zip, get_repo_url from ..utils.model_monitoring import set_project_model_monitoring_credentials from ..utils.notifications import CustomNotificationPusher, NotificationTypes @@ -248,7 +255,7 @@ def load_project( from_db = False if url: url = str(url) # to support path objects - if url.endswith(".yaml"): + if is_yaml_path(url): project = _load_project_file(url, name, secrets) project.spec.context = context elif url.startswith("git://"): @@ -1504,7 +1511,7 @@ def get_artifact(spec): item_path, _ = self.get_item_absolute_path(item_path) dataitem = mlrun.get_dataitem(item_path) - if item_path.endswith(".yaml") or item_path.endswith(".yml"): + if is_yaml_path(item_path): artifact_dict = yaml.load(dataitem.get(), Loader=yaml.FullLoader) artifact = get_artifact(artifact_dict) elif item_path.endswith(".json"): @@ -2846,7 +2853,7 @@ def _init_function_from_dict(f, project, name=None): name, image=image, kind=kind or "job", handler=handler, tag=tag ) - elif url.endswith(".yaml") or url.startswith("db://") or url.startswith("hub://"): + elif is_yaml_path(url) or url.startswith("db://") or url.startswith("hub://"): if tag: raise ValueError( "function with db:// or hub:// url or .yaml file, does not support tag value " @@ -2935,7 +2942,7 @@ def _init_function_from_dict_legacy(f, project): if "spec" in f: func = new_function(name, runtime=f["spec"]) - elif url.endswith(".yaml") or url.startswith("db://") or url.startswith("hub://"): + elif is_yaml_path(url) or url.startswith("db://") or url.startswith("hub://"): func = import_function(url) if image: func.spec.image = image diff --git a/mlrun/utils/helpers.py b/mlrun/utils/helpers.py index d790a0be6d..aead178c02 100644 --- a/mlrun/utils/helpers.py +++ b/mlrun/utils/helpers.py @@ -182,6 +182,10 @@ def tag_name_regex_as_string() -> str: return get_regex_list_as_string(mlrun.utils.regex.tag_name) +def is_yaml_path(url): + return url.endswith(".yaml") or url.endswith(".yml") + + # Verifying that a field input is of the expected type. If not the method raises a detailed MLRunInvalidArgumentError def verify_field_of_type(field_name: str, field_value, expected_type: type): if not isinstance(field_value, expected_type):