13
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
- #
17
16
18
- import re
17
+ from types import ModuleType
19
18
from typing import Any , Dict , Optional
20
19
from urllib import request
21
20
22
21
from google .auth import credentials as auth_credentials
23
22
from google .auth import transport
24
23
from google .cloud import storage
24
+ from google .cloud .aiplatform .constants import pipeline as pipeline_constants
25
25
26
26
# Pattern for an Artifact Registry URL.
27
- _VALID_AR_URL = re .compile (r"^https:\/\/([\w-]+)-kfp\.pkg\.dev\/.*" )
27
+ _VALID_AR_URL = pipeline_constants ._VALID_AR_URL
28
+
29
+ # Pattern for any JSON or YAML file over HTTPS.
30
+ _VALID_HTTPS_URL = pipeline_constants ._VALID_HTTPS_URL
28
31
29
32
30
33
def load_yaml (
@@ -36,8 +39,8 @@ def load_yaml(
36
39
37
40
Args:
38
41
path (str):
39
- Required. The path of the YAML document in Google Cloud Storage or
40
- local .
42
+ Required. The path of the YAML document. It can be a local path, a
43
+ Google Cloud Storage URI, an Artifact Registry URI, or an HTTPS URI .
41
44
project (str):
42
45
Optional. Project to initiate the Storage client with.
43
46
credentials (auth_credentials.Credentials):
@@ -48,12 +51,31 @@ def load_yaml(
48
51
"""
49
52
if path .startswith ("gs://" ):
50
53
return _load_yaml_from_gs_uri (path , project , credentials )
51
- elif _VALID_AR_URL .match (path ):
52
- return _load_yaml_from_ar_uri (path , credentials )
54
+ elif path .startswith ("http://" ) or path .startswith ("https://" ):
55
+ if _VALID_AR_URL .match (path ) or _VALID_HTTPS_URL .match (path ):
56
+ return _load_yaml_from_https_uri (path , credentials )
57
+ else :
58
+ raise ValueError (
59
+ "Invalid HTTPS URI. If not using Artifact Registry, please "
60
+ "ensure the URI ends with .json, .yaml, or .yml."
61
+ )
53
62
else :
54
63
return _load_yaml_from_local_file (path )
55
64
56
65
66
+ def _maybe_import_yaml () -> ModuleType :
67
+ """Tries to import the PyYAML module."""
68
+ try :
69
+ import yaml
70
+ except ImportError :
71
+ raise ImportError (
72
+ "PyYAML is not installed and is required to parse PipelineJob or "
73
+ 'PipelineSpec files. Please install the SDK using "pip install '
74
+ 'google-cloud-aiplatform[pipelines]"'
75
+ )
76
+ return yaml
77
+
78
+
57
79
def _load_yaml_from_gs_uri (
58
80
uri : str ,
59
81
project : Optional [str ] = None ,
@@ -72,13 +94,7 @@ def _load_yaml_from_gs_uri(
72
94
Returns:
73
95
A Dict object representing the YAML document.
74
96
"""
75
- try :
76
- import yaml
77
- except ImportError :
78
- raise ImportError (
79
- "pyyaml is not installed and is required to parse PipelineJob or PipelineSpec files. "
80
- 'Please install the SDK using "pip install google-cloud-aiplatform[pipelines]"'
81
- )
97
+ yaml = _maybe_import_yaml ()
82
98
storage_client = storage .Client (project = project , credentials = credentials )
83
99
blob = storage .Blob .from_string (uri , storage_client )
84
100
return yaml .safe_load (blob .download_as_bytes ())
@@ -94,39 +110,27 @@ def _load_yaml_from_local_file(file_path: str) -> Dict[str, Any]:
94
110
Returns:
95
111
A Dict object representing the YAML document.
96
112
"""
97
- try :
98
- import yaml
99
- except ImportError :
100
- raise ImportError (
101
- "pyyaml is not installed and is required to parse PipelineJob or PipelineSpec files. "
102
- 'Please install the SDK using "pip install google-cloud-aiplatform[pipelines]"'
103
- )
113
+ yaml = _maybe_import_yaml ()
104
114
with open (file_path ) as f :
105
115
return yaml .safe_load (f )
106
116
107
117
108
- def _load_yaml_from_ar_uri (
118
+ def _load_yaml_from_https_uri (
109
119
uri : str ,
110
120
credentials : Optional [auth_credentials .Credentials ] = None ,
111
121
) -> Dict [str , Any ]:
112
122
"""Loads data from a YAML document referenced by a Artifact Registry URI.
113
123
114
124
Args:
115
- path (str):
125
+ uri (str):
116
126
Required. Artifact Registry URI for YAML document.
117
127
credentials (auth_credentials.Credentials):
118
128
Optional. Credentials to use with Artifact Registry.
119
129
120
130
Returns:
121
131
A Dict object representing the YAML document.
122
132
"""
123
- try :
124
- import yaml
125
- except ImportError :
126
- raise ImportError (
127
- "pyyaml is not installed and is required to parse PipelineJob or PipelineSpec files. "
128
- 'Please install the SDK using "pip install google-cloud-aiplatform[pipelines]"'
129
- )
133
+ yaml = _maybe_import_yaml ()
130
134
req = request .Request (uri )
131
135
132
136
if credentials :
0 commit comments