1212# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313# See the License for the specific language governing permissions and
1414# limitations under the License.
15-
16-
1715import yaml
18- from typing import List , Optional , Dict
16+ from typing import Optional
1917from library_generation .model .library_config import LibraryConfig
2018from library_generation .model .gapic_config import GapicConfig
2119
20+ REPO_LEVEL_PARAMETER = "Repo level parameter"
21+ LIBRARY_LEVEL_PARAMETER = "Library level parameter"
22+ GAPIC_LEVEL_PARAMETER = "GAPIC level parameter"
23+
2224
2325class GenerationConfig :
2426 """
@@ -32,8 +34,8 @@ def __init__(
3234 libraries_bom_version : str ,
3335 owlbot_cli_image : str ,
3436 synthtool_commitish : str ,
35- template_excludes : List [str ],
36- libraries : List [LibraryConfig ],
37+ template_excludes : list [str ],
38+ libraries : list [LibraryConfig ],
3739 grpc_version : Optional [str ] = None ,
3840 protoc_version : Optional [str ] = None ,
3941 ):
@@ -46,6 +48,7 @@ def __init__(
4648 self .libraries = libraries
4749 self .grpc_version = grpc_version
4850 self .protoc_version = protoc_version
51+ self .__validate ()
4952
5053 def get_proto_path_to_library_name (self ) -> dict [str , str ]:
5154 """
@@ -62,6 +65,19 @@ def get_proto_path_to_library_name(self) -> dict[str, str]:
6265 def is_monorepo (self ) -> bool :
6366 return len (self .libraries ) > 1
6467
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+
6581
6682def from_yaml (path_to_yaml : str ) -> GenerationConfig :
6783 """
@@ -72,15 +88,19 @@ def from_yaml(path_to_yaml: str) -> GenerationConfig:
7288 with open (path_to_yaml , "r" ) as file_stream :
7389 config = yaml .safe_load (file_stream )
7490
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 } ." )
7694
7795 parsed_libraries = list ()
7896 for library in libraries :
7997 gapics = __required (library , "GAPICs" )
8098
8199 parsed_gapics = list ()
100+ if not gapics :
101+ raise ValueError (f"GAPICs is None in { library } ." )
82102 for gapic in gapics :
83- proto_path = __required (gapic , "proto_path" )
103+ proto_path = __required (gapic , "proto_path" , GAPIC_LEVEL_PARAMETER )
84104 new_gapic = GapicConfig (proto_path )
85105 parsed_gapics .append (new_gapic )
86106
@@ -114,27 +134,40 @@ def from_yaml(path_to_yaml: str) -> GenerationConfig:
114134 parsed_libraries .append (new_library )
115135
116136 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+ ),
118140 grpc_version = __optional (config , "grpc_version" , None ),
119141 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 ),
125153 libraries = parsed_libraries ,
126154 )
127155
128156 return parsed_config
129157
130158
131- def __required (config : Dict , key : str ):
159+ def __required (config : dict , key : str , level : str = LIBRARY_LEVEL_PARAMETER ):
132160 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 )
134167 return config [key ]
135168
136169
137- def __optional (config : Dict , key : str , default : any ):
170+ def __optional (config : dict , key : str , default : any ):
138171 if key not in config :
139172 return default
140173 return config [key ]
0 commit comments