12
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
-
16
-
17
15
import yaml
18
- from typing import List , Optional , Dict
16
+ from typing import Optional
19
17
from library_generation .model .library_config import LibraryConfig
20
18
from library_generation .model .gapic_config import GapicConfig
21
19
20
+ REPO_LEVEL_PARAMETER = "Repo level parameter"
21
+ LIBRARY_LEVEL_PARAMETER = "Library level parameter"
22
+ GAPIC_LEVEL_PARAMETER = "GAPIC level parameter"
23
+
22
24
23
25
class GenerationConfig :
24
26
"""
@@ -32,8 +34,8 @@ def __init__(
32
34
libraries_bom_version : str ,
33
35
owlbot_cli_image : str ,
34
36
synthtool_commitish : str ,
35
- template_excludes : List [str ],
36
- libraries : List [LibraryConfig ],
37
+ template_excludes : list [str ],
38
+ libraries : list [LibraryConfig ],
37
39
grpc_version : Optional [str ] = None ,
38
40
protoc_version : Optional [str ] = None ,
39
41
):
@@ -46,6 +48,7 @@ def __init__(
46
48
self .libraries = libraries
47
49
self .grpc_version = grpc_version
48
50
self .protoc_version = protoc_version
51
+ self .__validate ()
49
52
50
53
def get_proto_path_to_library_name (self ) -> dict [str , str ]:
51
54
"""
@@ -62,6 +65,19 @@ def get_proto_path_to_library_name(self) -> dict[str, str]:
62
65
def is_monorepo (self ) -> bool :
63
66
return len (self .libraries ) > 1
64
67
68
+ def __validate (self ) -> None :
69
+ seen_library_names = dict ()
70
+ for library in self .libraries :
71
+ library_name = library .get_library_name ()
72
+ if library_name in seen_library_names :
73
+ raise ValueError (
74
+ f"Both { library .name_pretty } and "
75
+ f"{ seen_library_names .get (library_name )} have the same "
76
+ f"library name: { library_name } , please update one of the "
77
+ f"library to have a different library name."
78
+ )
79
+ seen_library_names [library_name ] = library .name_pretty
80
+
65
81
66
82
def from_yaml (path_to_yaml : str ) -> GenerationConfig :
67
83
"""
@@ -72,15 +88,19 @@ def from_yaml(path_to_yaml: str) -> GenerationConfig:
72
88
with open (path_to_yaml , "r" ) as file_stream :
73
89
config = yaml .safe_load (file_stream )
74
90
75
- libraries = __required (config , "libraries" )
91
+ libraries = __required (config , "libraries" , REPO_LEVEL_PARAMETER )
92
+ if not libraries :
93
+ raise ValueError (f"Library is None in { path_to_yaml } ." )
76
94
77
95
parsed_libraries = list ()
78
96
for library in libraries :
79
97
gapics = __required (library , "GAPICs" )
80
98
81
99
parsed_gapics = list ()
100
+ if not gapics :
101
+ raise ValueError (f"GAPICs is None in { library } ." )
82
102
for gapic in gapics :
83
- proto_path = __required (gapic , "proto_path" )
103
+ proto_path = __required (gapic , "proto_path" , GAPIC_LEVEL_PARAMETER )
84
104
new_gapic = GapicConfig (proto_path )
85
105
parsed_gapics .append (new_gapic )
86
106
@@ -114,27 +134,40 @@ def from_yaml(path_to_yaml: str) -> GenerationConfig:
114
134
parsed_libraries .append (new_library )
115
135
116
136
parsed_config = GenerationConfig (
117
- gapic_generator_version = __required (config , "gapic_generator_version" ),
137
+ gapic_generator_version = __required (
138
+ config , "gapic_generator_version" , REPO_LEVEL_PARAMETER
139
+ ),
118
140
grpc_version = __optional (config , "grpc_version" , None ),
119
141
protoc_version = __optional (config , "protoc_version" , None ),
120
- googleapis_commitish = __required (config , "googleapis_commitish" ),
121
- libraries_bom_version = __required (config , "libraries_bom_version" ),
122
- owlbot_cli_image = __required (config , "owlbot_cli_image" ),
123
- synthtool_commitish = __required (config , "synthtool_commitish" ),
124
- template_excludes = __required (config , "template_excludes" ),
142
+ googleapis_commitish = __required (
143
+ config , "googleapis_commitish" , REPO_LEVEL_PARAMETER
144
+ ),
145
+ libraries_bom_version = __required (
146
+ config , "libraries_bom_version" , REPO_LEVEL_PARAMETER
147
+ ),
148
+ owlbot_cli_image = __required (config , "owlbot_cli_image" , REPO_LEVEL_PARAMETER ),
149
+ synthtool_commitish = __required (
150
+ config , "synthtool_commitish" , REPO_LEVEL_PARAMETER
151
+ ),
152
+ template_excludes = __required (config , "template_excludes" , REPO_LEVEL_PARAMETER ),
125
153
libraries = parsed_libraries ,
126
154
)
127
155
128
156
return parsed_config
129
157
130
158
131
- def __required (config : Dict , key : str ):
159
+ def __required (config : dict , key : str , level : str = LIBRARY_LEVEL_PARAMETER ):
132
160
if key not in config :
133
- raise ValueError (f"required key { key } not found in yaml" )
161
+ message = (
162
+ f"{ level } , { key } , is not found in { config } in yaml."
163
+ if level != REPO_LEVEL_PARAMETER
164
+ else f"{ level } , { key } , is not found in yaml."
165
+ )
166
+ raise ValueError (message )
134
167
return config [key ]
135
168
136
169
137
- def __optional (config : Dict , key : str , default : any ):
170
+ def __optional (config : dict , key : str , default : any ):
138
171
if key not in config :
139
172
return default
140
173
return config [key ]
0 commit comments