|
15 | 15 | # limitations under the License.
|
16 | 16 | #
|
17 | 17 |
|
18 |
| -import json |
19 | 18 | from typing import Any, Dict, Optional
|
20 | 19 |
|
21 | 20 | from google.auth import credentials as auth_credentials
|
22 | 21 | from google.cloud import storage
|
23 | 22 |
|
24 | 23 |
|
25 |
| -def load_json( |
| 24 | +def load_yaml( |
26 | 25 | path: str,
|
27 | 26 | project: Optional[str] = None,
|
28 | 27 | credentials: Optional[auth_credentials.Credentials] = None,
|
29 | 28 | ) -> Dict[str, Any]:
|
30 |
| - """Loads data from a JSON document. |
| 29 | + """Loads data from a YAML document. |
31 | 30 |
|
32 | 31 | Args:
|
33 | 32 | path (str):
|
34 |
| - Required. The path of the JSON document in Google Cloud Storage or |
| 33 | + Required. The path of the YAML document in Google Cloud Storage or |
35 | 34 | local.
|
36 | 35 | project (str):
|
37 | 36 | Optional. Project to initiate the Storage client with.
|
38 | 37 | credentials (auth_credentials.Credentials):
|
39 | 38 | Optional. Credentials to use with Storage Client.
|
40 | 39 |
|
41 | 40 | Returns:
|
42 |
| - A Dict object representing the JSON document. |
| 41 | + A Dict object representing the YAML document. |
43 | 42 | """
|
44 | 43 | if path.startswith("gs://"):
|
45 |
| - return _load_json_from_gs_uri(path, project, credentials) |
| 44 | + return _load_yaml_from_gs_uri(path, project, credentials) |
46 | 45 | else:
|
47 |
| - return _load_json_from_local_file(path) |
| 46 | + return _load_yaml_from_local_file(path) |
48 | 47 |
|
49 | 48 |
|
50 |
| -def _load_json_from_gs_uri( |
| 49 | +def _load_yaml_from_gs_uri( |
51 | 50 | uri: str,
|
52 | 51 | project: Optional[str] = None,
|
53 | 52 | credentials: Optional[auth_credentials.Credentials] = None,
|
54 | 53 | ) -> Dict[str, Any]:
|
55 |
| - """Loads data from a JSON document referenced by a GCS URI. |
| 54 | + """Loads data from a YAML document referenced by a GCS URI. |
56 | 55 |
|
57 | 56 | Args:
|
58 | 57 | path (str):
|
59 |
| - Required. GCS URI for JSON document. |
| 58 | + Required. GCS URI for YAML document. |
60 | 59 | project (str):
|
61 | 60 | Optional. Project to initiate the Storage client with.
|
62 | 61 | credentials (auth_credentials.Credentials):
|
63 | 62 | Optional. Credentials to use with Storage Client.
|
64 | 63 |
|
65 | 64 | Returns:
|
66 |
| - A Dict object representing the JSON document. |
| 65 | + A Dict object representing the YAML document. |
67 | 66 | """
|
| 67 | + try: |
| 68 | + import yaml |
| 69 | + except ImportError: |
| 70 | + raise ImportError( |
| 71 | + "pyyaml is not installed and is required to parse PipelineJob or PipelineSpec files. " |
| 72 | + 'Please install the SDK using "pip install google-cloud-aiplatform[pipelines]"' |
| 73 | + ) |
68 | 74 | storage_client = storage.Client(project=project, credentials=credentials)
|
69 | 75 | blob = storage.Blob.from_string(uri, storage_client)
|
70 |
| - return json.loads(blob.download_as_bytes()) |
| 76 | + return yaml.safe_load(blob.download_as_bytes()) |
71 | 77 |
|
72 | 78 |
|
73 |
| -def _load_json_from_local_file(file_path: str) -> Dict[str, Any]: |
74 |
| - """Loads data from a JSON local file. |
| 79 | +def _load_yaml_from_local_file(file_path: str) -> Dict[str, Any]: |
| 80 | + """Loads data from a YAML local file. |
75 | 81 |
|
76 | 82 | Args:
|
77 | 83 | file_path (str):
|
78 |
| - Required. The local file path of the JSON document. |
| 84 | + Required. The local file path of the YAML document. |
79 | 85 |
|
80 | 86 | Returns:
|
81 |
| - A Dict object representing the JSON document. |
| 87 | + A Dict object representing the YAML document. |
82 | 88 | """
|
| 89 | + try: |
| 90 | + import yaml |
| 91 | + except ImportError: |
| 92 | + raise ImportError( |
| 93 | + "pyyaml is not installed and is required to parse PipelineJob or PipelineSpec files. " |
| 94 | + 'Please install the SDK using "pip install google-cloud-aiplatform[pipelines]"' |
| 95 | + ) |
83 | 96 | with open(file_path) as f:
|
84 |
| - return json.load(f) |
| 97 | + return yaml.safe_load(f) |
0 commit comments