forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynth.py
More file actions
358 lines (315 loc) · 13 KB
/
synth.py
File metadata and controls
358 lines (315 loc) · 13 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# Copyright 2018 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.
"""This script is used to synthesize generated parts of this library."""
import synthtool as s
import synthtool.gcp as gcp
import logging
logging.basicConfig(level=logging.DEBUG)
gapic = gcp.GAPICGenerator()
common = gcp.CommonTemplates()
# ----------------------------------------------------------------------------
# Generate Grafeas GAPIC layer
# ----------------------------------------------------------------------------
library = gapic.py_library(
"grafeas", "v1", config_path="/grafeas/artman_grafeas_v1.yaml", include_protos=True
)
excludes = ["README.rst", "nox.py", "setup.py", "docs/index.rst"]
# Make 'grafeas' a namespace
s.move(library / "grafeas", excludes=["__init__.py"])
s.move(library / "docs", excludes=["conf.py", "index.rst"])
s.move(
library / "google/cloud/grafeas_v1/proto",
"grafeas/grafeas_v1/proto",
excludes=excludes,
)
s.move(library / "tests")
# Fix proto imports
s.replace(
["grafeas/**/*.py", "tests/**/*.py"],
"from grafeas\.v1( import \w*_pb2)",
"from grafeas.grafeas_v1.proto\g<1>",
)
s.replace(
"grafeas/**/*_pb2.py",
"from grafeas_v1\.proto( import \w*_pb2)",
"from grafeas.grafeas_v1.proto\g<1>",
)
s.replace(
"grafeas/**/grafeas_pb2_grpc.py",
"from grafeas_v1\.proto",
"from grafeas.grafeas_v1.proto",
)
# Make package name 'grafeas'
s.replace(
"grafeas/grafeas_v1/gapic/grafeas_client.py", "google-cloud-grafeas", "grafeas"
)
# Fix docstrings with no summary lines
s.replace(
"grafeas/grafeas_v1/proto/vulnerability_pb2.py",
r"""(\s+)__doc__ = \"\"\"Attributes:""",
"""\g<1>__doc__=\"\"\"
Attributes:""",
)
# Replace mentions of 'Container Analysis' with 'Grafeas' in the docs
s.replace("docs/**/v*/*.rst", "Container Analysis", "Grafeas")
# ----------------------------------------------------------------------------
# Remove google-specific portions of library
# ----------------------------------------------------------------------------
# Please see this PR https://github.com/googleapis/google-cloud-python/pull/8186/
# Remove default service address, default scopes, default credentials
# Update tests and code in docstrings showing client instantiation.
s.replace(
"grafeas/**/grafeas_client.py",
r""" SERVICE_ADDRESS = 'containeranalysis\.googleapis\.com:443'
\"\"\"The default address of the service\.\"\"\"""",
"",
)
s.replace(
"grafeas/**/grafeas_client.py",
r""" def __init__\(self, transport=None, channel=None, credentials=None,
client_config=None, client_info=None, client_options=None\):""",
" def __init__(self, transport, client_config=None, client_info=None):",
)
s.replace(
"grafeas/**/grafeas_client.py",
r"""Union\[~\.GrafeasGrpcTransport,
Callable\[\[~\.Credentials, type], ~\.GrafeasGrpcTransport\]""",
"""~.GrafeasGrpcTransport""",
)
s.replace(
"grafeas/**/grafeas_client.py",
r""" channel \(grpc\.Channel\): DEPRECATED\. A ``Channel`` instance
through which to make calls\. This argument is mutually exclusive
with ``credentials``; providing both will raise an exception\.
credentials \(google\.auth\.credentials\.Credentials\): The
authorization credentials to attach to requests\. These
credentials identify this application to the service\. If none
are specified, the client will attempt to ascertain the
credentials from the environment\.
This argument is mutually exclusive with providing a
transport instance to ``transport``; doing so will raise
an exception\.""",
"",
)
# Remove client_options
# api_endpoint is currently the only option and doesn't make sense for Grafeas.
s.replace("grafeas/**/grafeas_client.py", "import google.api_core.client_options\n", "")
s.replace(
"grafeas/**/grafeas_client.py",
r""" client_options \(Union\[dict, google\.api_core\.client_options\.ClientOptions\]\):
Client options used to set user options on the client\. API Endpoint
should be set through client_options\.
\"\"\"""",
" \"\"\""
)
s.replace(
"grafeas/**/grafeas_client.py",
r"""if channel:
warnings\.warn\('The `channel` argument is deprecated; use '
'`transport` instead\.',
PendingDeprecationWarning, stacklevel=2\)
api_endpoint = self\.SERVICE_ADDRESS
if client_options:
if type\(client_options\) == dict:
client_options = google\.api_core\.client_options\.from_dict\(client_options\)
if client_options\.api_endpoint:
api_endpoint = client_options\.api_endpoint
\# Instantiate the transport\.
\# The transport is responsible for handling serialization and
\# deserialization and actually sending data to the service\.
if transport:
if callable\(transport\):
self\.transport = transport\(
credentials=credentials,
default_class=grafeas_grpc_transport\.GrafeasGrpcTransport,
address=api_endpoint,
\)
else:
if credentials:
raise ValueError\(
'Received both a transport instance and '
'credentials; these are mutually exclusive\.'
\)
self\.transport = transport
else:
self\.transport = grafeas_grpc_transport\.GrafeasGrpcTransport\(
address=api_endpoint,
channel=channel,
credentials=credentials,
\)""",
"""# Instantiate the transport.
# The transport is responsible for handling serialization and
# deserialization and actually sending data to the service.
self.transport = transport""",
)
s.replace(
"grafeas/**/grafeas_client.py",
r""" Example:
>>> from grafeas import grafeas_v1
>>>
>>> client = grafeas_v1\.GrafeasClient\(\)""",
""" Example:
>>> from grafeas import grafeas_v1
>>> from grafeas.grafeas_v1.gapic.transports import grafeas_grpc_transport
>>>
>>> address = "[SERVICE_ADDRESS]"
>>> scopes = ("[SCOPE]")
>>> transport = grafeas_grpc_transport.GrafeasGrpcTransport(address, scopes)
>>> client = grafeas_v1.GrafeasClient(transport)""",
)
s.replace(
"grafeas/**/grafeas_client.py",
r''' @classmethod
def from_service_account_file\(cls, filename, \*args, \*\*kwargs\):
"""Creates an instance of this client using the provided credentials
file\.
Args:
filename \(str\): The path to the service account private key json
file\.
args: Additional arguments to pass to the constructor\.
kwargs: Additional arguments to pass to the constructor\.
Returns:
GrafeasClient: The constructed client\.
"""
credentials = service_account\.Credentials\.from_service_account_file\(
filename\)
kwargs\['credentials'\] = credentials
return cls\(\*args, \*\*kwargs\)
from_service_account_json = from_service_account_file''',
"")
s.replace(
"grafeas/**/grafeas_grpc_transport.py",
r""" \# The scopes needed to make gRPC calls to all of the methods defined
\# in this service\.
_OAUTH_SCOPES = \(
'https://www\.googleapis\.com/auth/cloud-platform',
\)""",
"",
)
s.replace(
"grafeas/**/grafeas_grpc_transport.py",
r""" def __init__\(self, channel=None, credentials=None,
address='containeranalysis\.googleapis\.com:443'\):""",
""" def __init__(self, address, scopes, channel=None, credentials=None):""",
)
s.replace(
"grafeas/**/grafeas_grpc_transport.py",
r""" \# Create the channel\.
if channel is None:
channel = self\.create_channel\(
address=address,
credentials=credentials,
""",
""" # Create the channel.
if channel is None:
channel = self.create_channel(
address,
scopes,
credentials=credentials,
""",
)
s.replace(
"grafeas/**/grafeas_grpc_transport.py",
r""" def create_channel\(
cls,
address='containeranalysis\.googleapis\.com:443',
credentials=None,
\*\*kwargs\):""",
""" def create_channel(
cls,
address,
scopes,
credentials=None,
**kwargs):""",
)
s.replace(
"grafeas/**/grafeas_grpc_transport.py",
r""" Args:
address \(str\): The host for the channel to use\.
credentials \(~\.Credentials\): The
authorization credentials to attach to requests\. These
credentials identify this application to the service\. If
none are specified, the client will attempt to ascertain
the credentials from the environment\.""",
""" Args:
address (str): The host for the channel to use.
scopes (Sequence[str]): The scopes needed to make gRPC calls.
credentials (~.Credentials): The
authorization credentials to attach to requests. These
credentials identify this application to the service. If
none are specified, the client will attempt to ascertain
the credentials from the environment.""",
)
s.replace(
"grafeas/**/grafeas_grpc_transport.py",
r""" return google\.api_core\.grpc_helpers\.create_channel\(
address,
credentials=credentials,
scopes=cls\._OAUTH_SCOPES,
\*\*kwargs
\)""",
""" return google.api_core.grpc_helpers.create_channel(
address,
credentials=credentials,
scopes=scopes,
**kwargs
)""",
)
s.replace(
"grafeas/**/grafeas_grpc_transport.py",
r""" \"\"\"Instantiate the transport class\.
Args:
channel \(grpc\.Channel\): A ``Channel`` instance through
which to make calls\. This argument is mutually exclusive
with ``credentials``; providing both will raise an exception\.
credentials \(google\.auth\.credentials\.Credentials\): The
authorization credentials to attach to requests\. These
credentials identify this application to the service\. If none
are specified, the client will attempt to ascertain the
credentials from the environment\.
address \(str\): The address where the service is hosted\.""",
''' """Instantiate the transport class.
Args:
address (str): The address where the service is hosted.
scopes (Sequence[str]): The scopes needed to make gRPC calls.
channel (grpc.Channel): A ``Channel`` instance through
which to make calls. This argument is mutually exclusive
with ``credentials``; providing both will raise an exception.
credentials (google.auth.credentials.Credentials): The
authorization credentials to attach to requests. These
credentials identify this application to the service. If none
are specified, the client will attempt to ascertain the
credentials from the environment.
''',
)
s.replace(
"tests/**/test_grafeas_client_v1.py",
r"""from grafeas\.grafeas_v1\.proto import grafeas_pb2""",
r"""from grafeas.grafeas_v1.proto import grafeas_pb2
from grafeas.grafeas_v1.gapic.transports import grafeas_grpc_transport""",
)
s.replace(
"tests/**/test_grafeas_client_v1.py",
r"(\s+)client = grafeas_v1\.GrafeasClient\(\)",
r"""\g<1>address = "[SERVICE_ADDRESS]"
\g<1>scopes = ("SCOPE")
\g<1>transport = grafeas_grpc_transport.GrafeasGrpcTransport(address, scopes)
\g<1>client=grafeas_v1.GrafeasClient(transport)""",
)
# ----------------------------------------------------------------------------
# Add templated files
# ----------------------------------------------------------------------------
templated_files = common.py_library(unit_cov_level=78, cov_level=78)
s.move(templated_files, excludes=["noxfile.py"])
s.shell.run(["nox", "-s", "blacken"], hide_output=False)