Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit b408b14

Browse files
feat: Migrate API to use python micro-generator (#41)
* migrate API to use micro-generator * migrate API to use micro-generator * update * doc changes * add samples * add samples * add samples and readme * Update README.md * Update README.md * Update UPGRADING.md file * update synth.py Co-authored-by: arithmetic1728 <liusijun1985@gmail.com>
1 parent 8be89ed commit b408b14

File tree

82 files changed

+11359
-997
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+11359
-997
lines changed

.coveragerc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ omit =
2323
[report]
2424
fail_under = 100
2525
show_missing = True
26+
omit = google/cloud/language/__init__.py
2627
exclude_lines =
2728
# Re-enable the standard pragma
2829
pragma: NO COVER
2930
# Ignore debug-only repr
3031
def __repr__
31-
# Ignore abstract methods
32-
raise NotImplementedError
33-
omit =
34-
*/gapic/*.py
35-
*/proto/*.py
36-
*/core/*.py
37-
*/site-packages/*.py
38-
google/cloud/__init__.py
32+
# Ignore pkg_resources exceptions.
33+
# This is added at the module level as a safeguard for if someone
34+
# generates the code and tries to run it without pip installing. This
35+
# makes it virtually impossible to test properly.
36+
except pkg_resources.DistributionNotFound

.kokoro/samples/python3.6/common.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ env_vars: {
1313
value: "py-3.6"
1414
}
1515

16+
# Declare build specific Cloud project.
17+
env_vars: {
18+
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
19+
value: "python-docs-samples-tests-py36"
20+
}
21+
1622
env_vars: {
1723
key: "TRAMPOLINE_BUILD_FILE"
1824
value: "github/python-language/.kokoro/test-samples.sh"

.kokoro/samples/python3.7/common.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ env_vars: {
1313
value: "py-3.7"
1414
}
1515

16+
# Declare build specific Cloud project.
17+
env_vars: {
18+
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
19+
value: "python-docs-samples-tests-py37"
20+
}
21+
1622
env_vars: {
1723
key: "TRAMPOLINE_BUILD_FILE"
1824
value: "github/python-language/.kokoro/test-samples.sh"

.kokoro/samples/python3.8/common.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ env_vars: {
1313
value: "py-3.8"
1414
}
1515

16+
# Declare build specific Cloud project.
17+
env_vars: {
18+
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
19+
value: "python-docs-samples-tests-py38"
20+
}
21+
1622
env_vars: {
1723
key: "TRAMPOLINE_BUILD_FILE"
1824
value: "github/python-language/.kokoro/test-samples.sh"

README.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ dependencies.
6262

6363
Supported Python Versions
6464
^^^^^^^^^^^^^^^^^^^^^^^^^
65-
Python >= 3.5
65+
Python >= 3.6
6666

67-
Deprecated Python Versions
68-
^^^^^^^^^^^^^^^^^^^^^^^^^^
69-
Python == 2.7. Python 2.7 support will be removed on January 1, 2020.
67+
Unsupported Python Versions
68+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
69+
Python == 2.7.
70+
71+
The last version of this library compatible with Python 2.7 is google-cloud-language=1.3.0
7072

7173

7274
Mac/Linux

UPGRADING.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# 2.0.0 Migration Guide
2+
3+
The 2.0 release of the `google-cloud-language` client is a significant upgrade based on a [next-gen code generator](https://github.com/googleapis/gapic-generator-python), and includes substantial interface changes. Existing code written for earlier versions of this library will likely require updates to use this version. This document describes the changes that have been made, and what you need to do to update your usage.
4+
5+
If you experience issues or have questions, please file an [issue](https://github.com/googleapis/python-language/issues).
6+
7+
## Supported Python Versions
8+
9+
> **WARNING**: Breaking change
10+
The 2.0.0 release requires Python 3.6+.
11+
12+
## Method Calls
13+
14+
> **WARNING**: Breaking change
15+
Methods expect request objects. We provide a script that will convert most common use cases.
16+
* Install the library
17+
18+
```py
19+
python3 -m pip install google-cloud-language
20+
```
21+
22+
* The script `fixup_language_v1_keywords.py` is shipped with the library. It expects
23+
an input directory (with the code to convert) and an empty destination directory.
24+
25+
```sh
26+
$ fixup_language_v1_keywords.py --input-directory .samples/ --output-directory samples/
27+
```
28+
29+
**Before:**
30+
```py
31+
from google.cloud import language_v1
32+
language = language_v1.LanguageClient()
33+
return language.analyze_sentiment(document=document).document_sentiment
34+
```
35+
36+
37+
**After:**
38+
```py
39+
from google.cloud import language_v1
40+
language = language_v1.LanguageServiceClient()
41+
return language.analyze_sentiment(request={'document': document}).document_sentiment
42+
```
43+
44+
### More Details
45+
46+
In `google-cloud-language<2.0.0`, parameters required by the API were positional parameters and optional parameters were keyword parameters.
47+
48+
**Before:**
49+
```py
50+
def analyze_sentiment(
51+
self,
52+
document,
53+
encoding_type=None,
54+
retry=google.api_core.gapic_v1.method.DEFAULT,
55+
timeout=google.api_core.gapic_v1.method.DEFAULT,
56+
metadata=None,
57+
):
58+
```
59+
60+
In the 2.0.0 release, all methods have a single positional parameter `request`. Method docstrings indicate whether a parameter is required or optional.
61+
62+
Some methods have additional keyword only parameters. The available parameters depend on the `google.api.method_signature` annotation specified by the API producer.
63+
64+
65+
**After:**
66+
```py
67+
def analyze_sentiment(
68+
self,
69+
request: language_service.AnalyzeSentimentRequest = None,
70+
*,
71+
document: language_service.Document = None,
72+
encoding_type: language_service.EncodingType = None,
73+
retry: retries.Retry = gapic_v1.method.DEFAULT,
74+
timeout: float = None,
75+
metadata: Sequence[Tuple[str, str]] = (),
76+
) -> language_service.AnalyzeSentimentResponse:
77+
```
78+
79+
> **NOTE:** The `request` parameter and flattened keyword parameters for the API are mutually exclusive.
80+
> Passing both will result in an error.
81+
Both of these calls are valid:
82+
83+
```py
84+
response = client.analyze_sentiment(
85+
request={
86+
"document": document,
87+
"encoding_type": encoding_type
88+
}
89+
)
90+
```
91+
92+
```py
93+
response = client.analyze_sentiment(
94+
document=document,
95+
encoding_type=encoding_type
96+
) # Make an API request.
97+
```
98+
99+
This call is invalid because it mixes `request` with a keyword argument `entry_group`. Executing this code
100+
will result in an error.
101+
102+
```py
103+
response = client.analyze_sentiment(
104+
request={
105+
"document": document
106+
},
107+
encoding_type=encoding_type
108+
)
109+
```
110+
111+
112+
113+
## Enums and Types
114+
115+
116+
> **WARNING**: Breaking change
117+
The submodules `enums` and `types` have been removed.
118+
**Before:**
119+
```py
120+
from google.cloud import language_v1
121+
document = language_v1.types.Document(content=text, type=language_v1.enums.Document.Type.PLAIN_TEXT)
122+
encoding_type = language_v1.enums.EncodingType.UTF8
123+
```
124+
125+
126+
**After:**
127+
```py
128+
from google.cloud import language_v1
129+
document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
130+
encoding_type = language_v1.EncodingType.UTF8
131+
```
132+
133+
## Project Path Helper Methods
134+
135+
The project path helper method `project_path` has been removed. Please construct
136+
this path manually.
137+
138+
```py
139+
project = 'my-project'
140+
project_path = f'projects/{project}'

docs/UPGRADING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../UPGRADING.md

docs/api.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ API. By default, you will get ``v1``, the latest GA version.
77
.. toctree::
88
:maxdepth: 2
99

10-
gapic/v1/api
11-
gapic/v1/types
10+
language_v1/services
11+
language_v1/types
1212

1313
If you are interested in beta features ahead of the latest GA, you may
1414
opt-in to the v1.1 beta, which is spelled ``v1beta2``. In order to do this,
@@ -20,8 +20,18 @@ An API and type reference is provided for the v1.1 beta also:
2020
.. toctree::
2121
:maxdepth: 2
2222

23-
gapic/v1beta2/api
24-
gapic/v1beta2/types
23+
language_v1beta2/services
24+
language_v1beta2/types
25+
26+
Migration Guide
27+
---------------
28+
29+
See the guide below for instructions on migrating to the 2.x release of this library.
30+
31+
.. toctree::
32+
:maxdepth: 2
33+
34+
UPGRADING
2535

2636
.. note::
2737

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@
347347
intersphinx_mapping = {
348348
"python": ("http://python.readthedocs.org/en/latest/", None),
349349
"google-auth": ("https://google-auth.readthedocs.io/en/stable", None),
350-
"google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None),
350+
"google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,),
351351
"grpc": ("https://grpc.io/grpc/python/", None),
352352
}
353353

docs/gapic/v1/api.rst

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)