-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathgeneration_config.py
118 lines (101 loc) · 4.57 KB
/
generation_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import yaml
from typing import List, Optional, Dict
from library_generation.model.library_config import LibraryConfig
from library_generation.model.gapic_config import GapicConfig
class GenerationConfig:
"""
Class that represents the root of a generation_config.yaml
"""
def __init__(
self,
gapic_generator_version: str,
googleapis_commitish: str,
owlbot_cli_image: str,
synthtool_commitish: str,
template_excludes: str,
path_to_yaml: str,
libraries: List[LibraryConfig],
grpc_version: Optional[str] = None,
protobuf_version: Optional[str] = None,
):
self.gapic_generator_version = gapic_generator_version
self.googleapis_commitish = googleapis_commitish
self.owlbot_cli_image = owlbot_cli_image
self.synthtool_commitish = synthtool_commitish
self.template_excludes = template_excludes
self.path_to_yaml = path_to_yaml
self.libraries = libraries
self.grpc_version = grpc_version
self.protobuf_version = protobuf_version
def from_yaml(path_to_yaml: str):
"""
Parses a yaml located in path_to_yaml. Returns the parsed configuration
represented by the "model" classes
"""
with open(path_to_yaml, "r") as file_stream:
config = yaml.safe_load(file_stream)
libraries = __required(config, "libraries")
parsed_libraries = list()
for library in libraries:
gapics = __required(library, "GAPICs")
parsed_gapics = list()
for gapic in gapics:
proto_path = __required(gapic, "proto_path")
new_gapic = GapicConfig(proto_path)
parsed_gapics.append(new_gapic)
new_library = LibraryConfig(
api_shortname=__required(library, "api_shortname"),
api_description=__required(library, "api_description"),
name_pretty=__required(library, "name_pretty"),
product_documentation=__required(library, "product_documentation"),
gapic_configs=parsed_gapics,
library_type=__optional(library, "library_type", "GAPIC_AUTO"),
release_level=__optional(library, "release_level", "preview"),
api_id=__optional(library, "api_id", None),
api_reference=__optional(library, "api_reference", None),
client_documentation=__optional(library, "client_documentation", None),
distribution_name=__optional(library, "distribution_name", None),
googleapis_commitish=__optional(library, "googleapis_commitish", None),
group_id=__optional(library, "group_id", "com.google.cloud"),
issue_tracker=__optional(library, "issue_tracker", None),
library_name=__optional(library, "library_name", None),
rest_documentation=__optional(library, "rest_documentation", None),
rpc_documentation=__optional(library, "rpc_documentation", None),
cloud_api=__optional(library, "cloud_api", True),
requires_billing=__optional(library, "requires_billing", True),
)
parsed_libraries.append(new_library)
parsed_config = GenerationConfig(
gapic_generator_version=__required(config, "gapic_generator_version"),
grpc_version=__optional(config, "grpc_version", None),
protobuf_version=__optional(config, "protobuf_version", None),
googleapis_commitish=__required(config, "googleapis_commitish"),
owlbot_cli_image=__required(config, "owlbot_cli_image"),
synthtool_commitish=__required(config, "synthtool_commitish"),
template_excludes=__required(config, "template_excludes"),
path_to_yaml=path_to_yaml,
libraries=parsed_libraries,
)
return parsed_config
def __required(config: Dict, key: str):
if key not in config:
raise ValueError(f"required key {key} not found in yaml")
return config[key]
def __optional(config: Dict, key: str, default: any):
if key not in config:
return default
return config[key]