diff --git a/docs/language/client.rst b/docs/language/client.rst deleted file mode 100644 index 310e7b5bf828..000000000000 --- a/docs/language/client.rst +++ /dev/null @@ -1,6 +0,0 @@ -Natural Language Client -======================= - -.. automodule:: google.cloud.language.client - :members: - :show-inheritance: diff --git a/docs/language/document.rst b/docs/language/document.rst deleted file mode 100644 index e879b11e590a..000000000000 --- a/docs/language/document.rst +++ /dev/null @@ -1,6 +0,0 @@ -Document -~~~~~~~~ - -.. automodule:: google.cloud.language.document - :members: - :show-inheritance: diff --git a/docs/language/gapic/v1/api.rst b/docs/language/gapic/v1/api.rst new file mode 100644 index 000000000000..2c5fd4fd76ea --- /dev/null +++ b/docs/language/gapic/v1/api.rst @@ -0,0 +1,6 @@ +Natural Language Client API +=========================== + +.. automodule:: google.cloud.language_v1 + :members: + :inherited-members: diff --git a/docs/language/gapic/v1/types.rst b/docs/language/gapic/v1/types.rst new file mode 100644 index 000000000000..90d27a4b96fb --- /dev/null +++ b/docs/language/gapic/v1/types.rst @@ -0,0 +1,5 @@ +Natural Language Client Types +============================= + +.. automodule:: google.cloud.language_v1.types + :members: diff --git a/docs/language/gapic/v1beta2/api.rst b/docs/language/gapic/v1beta2/api.rst new file mode 100644 index 000000000000..330d7e6e7a78 --- /dev/null +++ b/docs/language/gapic/v1beta2/api.rst @@ -0,0 +1,6 @@ +Natural Language Beta Client API +================================ + +.. automodule:: google.cloud.language_v1beta2 + :members: + :inherited-members: diff --git a/docs/language/gapic/v1beta2/types.rst b/docs/language/gapic/v1beta2/types.rst new file mode 100644 index 000000000000..d9a7eb171f00 --- /dev/null +++ b/docs/language/gapic/v1beta2/types.rst @@ -0,0 +1,5 @@ +Natural Language Beta Client Types +================================== + +.. automodule:: google.cloud.language_v1beta2.types + :members: diff --git a/docs/language/responses.rst b/docs/language/responses.rst deleted file mode 100644 index 5584cbcdcfab..000000000000 --- a/docs/language/responses.rst +++ /dev/null @@ -1,37 +0,0 @@ -Natural Language Response Classes -================================= - -Responses -~~~~~~~~~ - -.. automodule:: google.cloud.language.api_responses - :members: - :show-inheritance: - -Sentences -~~~~~~~~~ - -.. automodule:: google.cloud.language.sentence - :members: - :show-inheritance: - -Entity -~~~~~~ - -.. automodule:: google.cloud.language.entity - :members: - :show-inheritance: - -Sentiment -~~~~~~~~~ - -.. automodule:: google.cloud.language.sentiment - :members: - :show-inheritance: - -Syntax -~~~~~~ - -.. automodule:: google.cloud.language.syntax - :members: - :show-inheritance: diff --git a/docs/language/usage.rst b/docs/language/usage.rst index 2a8c9ddba589..31d4bb20b95c 100644 --- a/docs/language/usage.rst +++ b/docs/language/usage.rst @@ -1,14 +1,6 @@ Natural Language ================ -.. toctree:: - :maxdepth: 2 - :hidden: - - client - document - responses - The `Google Natural Language`_ API can be used to reveal the structure and meaning of text via powerful machine learning models. You can use it to extract information about @@ -21,40 +13,43 @@ with your document storage on Google Cloud Storage. .. _Google Natural Language: https://cloud.google.com/natural-language/docs/getting-started -Client ------- -:class:`~google.cloud.language.client.Client` objects provide a -means to configure your application. Each instance holds -an authenticated connection to the Natural Language service. +******************************** +Authentication and Configuration +******************************** -For an overview of authentication in ``google-cloud-python``, see -:doc:`/core/auth`. +- For an overview of authentication in ``google-cloud-python``, + see :doc:`/core/auth`. -Assuming your environment is set up as described in that document, -create an instance of :class:`~google.cloud.language.client.Client`. +- In addition to any authentication configuration, you should also set the + :envvar:`GOOGLE_CLOUD_PROJECT` environment variable for the project you'd + like to interact with. If the :envvar:`GOOGLE_CLOUD_PROJECT` environment + variable is not present, the project ID from JSON file credentials is used. - .. code-block:: python + If you are using Google App Engine or Google Compute Engine + this will be detected automatically. - >>> from google.cloud import language - >>> client = language.Client() +- After configuring your environment, create a + :class:`~google.cloud.language_v1.LanguageServiceClient`. -By default the ``language`` is ``'en-US'`` and the ``encoding`` is -UTF-8. To over-ride these values: +.. code-block:: python - .. code-block:: python + >>> from google.cloud import language + >>> client = language.LanguageServiceClient() - >>> document = client.document_from_text( - ... text_content, language='es', encoding=language.Encoding.UTF16) +or pass in ``credentials`` explicitly. +.. code-block:: python + + >>> from google.cloud import language + >>> client = language.LanguageServiceClient( + ... credentials=creds, + ... ) -The encoding can be one of -:attr:`Encoding.UTF8 `, -:attr:`Encoding.UTF16 `, or -:attr:`Encoding.UTF32 `. -Methods -------- +********* +Documents +********* The Google Natural Language API has three supported methods @@ -62,109 +57,90 @@ The Google Natural Language API has three supported methods - `analyzeSentiment`_ - `annotateText`_ -and each method uses a `Document`_ for representing text. To -create a :class:`~google.cloud.language.document.Document`, +and each method uses a :class:`~.language_v1.types.Document` for representing +text. .. code-block:: python - >>> text_content = ( - ... 'Google, headquartered in Mountain View, unveiled the ' - ... 'new Android phone at the Consumer Electronic Show. ' - ... 'Sundar Pichai said in his keynote that users love ' - ... 'their new Android phones.') - >>> document = client.document_from_text(text_content) + >>> document = language.types.Document( + ... content='Google, headquartered in Mountain View, unveiled the ' + ... 'new Android phone at the Consumer Electronic Show. ' + ... 'Sundar Pichai said in his keynote that users love ' + ... 'their new Android phones.', + ... language='en', + ... type='PLAIN_TEXT', + ... ) -By using :meth:`~google.cloud.language.client.Client.document_from_text`, -the document's type is plain text: - - .. code-block:: python - - >>> document.doc_type == language.Document.PLAIN_TEXT - True The document's language defaults to ``None``, which will cause the API to auto-detect the language. -In addition, the -:meth:`~google.cloud.language.client.Client.document_from_html`, -factory can be used to created an HTML document. In this -method and the from text method, the language can be -over-ridden: +In addition, you can construct an HTML document: .. code-block:: python - >>> html_content = """\ - ... - ... - ... El Tiempo de las Historias</time> - ... </head> - ... <body> - ... <p>La vaca saltó sobre la luna.</p> - ... </body> - ... </html> - ... """ - >>> document = client.document_from_html(html_content, - ... language='es') + >>> html_content = """\ + ... <html> + ... <head> + ... <title>El Tiempo de las Historias</time> + ... </head> + ... <body> + ... <p>La vaca saltó sobre la luna.</p> + ... </body> + ... </html> + ... """ + >>> document = language.types.Document( + ... content=html_content, + ... language='es', + ... type='HTML', + ... ) The ``language`` argument can be either ISO-639-1 or BCP-47 language -codes; at the time, only English, Spanish, and Japanese `are supported`_. -However, the ``analyzeSentiment`` method `only supports`_ English text. +codes. The API reference page contains the full list of `supported languages`_. -.. _are supported: https://cloud.google.com/natural-language/docs/ -.. _only supports: https://cloud.google.com/natural-language/docs/reference/rest/v1beta1/documents/analyzeSentiment#body.request_body.FIELDS.document +.. _supported languages: https://cloud.google.com/natural-language/docs/languages -The document type (``doc_type``) value can be one of -:attr:`Document.PLAIN_TEXT <google.cloud.language.document.Document.PLAIN_TEXT>` or -:attr:`Document.HTML <google.cloud.language.document.Document.HTML>`. In addition to supplying the text / HTML content, a document can refer -to content stored in `Google Cloud Storage`_. We can use the -:meth:`~google.cloud.language.client.Client.document_from_url` method: - - .. code-block:: python - - >>> gcs_url = 'gs://my-text-bucket/sentiment-me.txt' - >>> document = client.document_from_url( - ... gcs_url, doc_type=language.Document.HTML) - >>> document.gcs_url == gcs_url - True - >>> document.doc_type == language.Document.PLAIN_TEXT - True - -The document type can be specified with the ``doc_type`` argument: +to content stored in `Google Cloud Storage`_. .. code-block:: python - >>> document = client.document_from_url( - ... gcs_url, doc_type=language.Document.HTML) + >>> document = language.types.Document( + ... gcs_content_uri='gs://my-text-bucket/sentiment-me.txt', + ... type=language.enums.HTML, + ... ) .. _analyzeEntities: https://cloud.google.com/natural-language/docs/reference/rest/v1beta1/documents/analyzeEntities .. _analyzeSentiment: https://cloud.google.com/natural-language/docs/reference/rest/v1beta1/documents/analyzeSentiment .. _annotateText: https://cloud.google.com/natural-language/docs/reference/rest/v1beta1/documents/annotateText -.. _Document: https://cloud.google.com/natural-language/reference/rest/v1beta1/Document .. _Google Cloud Storage: https://cloud.google.com/storage/ +**************** Analyze Entities ----------------- +**************** -The :meth:`~google.cloud.language.document.Document.analyze_entities` method -finds named entities (i.e. proper names) in the text and returns them -as a :class:`list` of :class:`~google.cloud.language.entity.Entity` objects. -Each entity has a corresponding type, salience (prominence), associated -metadata and other properties. +The :meth:`~.language_v1.LanguageServiceClient.analyze_entities` +method finds named entities (i.e. proper names) in the text. This method +returns a :class:`~.language_v1.types.AnalyzeEntitiesResponse`. .. code-block:: python - >>> text_content = ("Michelangelo Caravaggio, Italian painter, is " - ... "known for 'The Calling of Saint Matthew'.") - >>> document = client.document_from_text(text_content) - >>> entity_response = document.analyze_entities() - >>> for entity in entity_response.entities: + >>> document = language.types.Document( + ... content='Michelangelo Caravaggio, Italian painter, is ' + ... 'known for "The Calling of Saint Matthew".', + ... type=language.enums.Type.PLAIN_TEXT, + ... ) + >>> response = client.analyze_entities( + ... document=document, + ... encoding_type='UTF32', + ... ) + >>> for entity in response.entities: ... print('=' * 20) - ... print(' name: %s' % (entity.name,)) - ... print(' type: %s' % (entity.entity_type,)) - ... print(' metadata: %s' % (entity.metadata,)) - ... print(' salience: %s' % (entity.salience,)) + ... print(' name: {0}'.format(entity.name)) + ... print(' type: {0}'.format(entity.entity_type)) + ... print(' metadata: {0}'.format(entity.metadata)) + ... print(' salience: {0}'.format(entity.salience)) ==================== name: Michelangelo Caravaggio type: PERSON @@ -181,90 +157,84 @@ metadata and other properties. metadata: {'wikipedia_url': 'http://en.wikipedia.org/wiki/Caravaggio'} salience: 0.038798928 +.. note:: + + It is recommended to send an ``encoding_type`` argument to Natural + Language methods, so they provide useful offsets for the data they return. + While the correct value varies by environment, in Python you *usually* + want ``UTF32``. + + +***************** Analyze Sentiment ------------------ +***************** -The :meth:`~google.cloud.language.document.Document.analyze_sentiment` method -analyzes the sentiment of the provided text and returns a -:class:`~google.cloud.language.sentiment.Sentiment`. Currently, this method -only supports English text. +The :meth:`~.language_v1.LanguageServiceClient.analyze_sentiment` method +analyzes the sentiment of the provided text. This method returns a +:class:`~.language_v1.types.AnalyzeSentimentResponse`. .. code-block:: python - >>> text_content = "Jogging isn't very fun." - >>> document = client.document_from_text(text_content) - >>> sentiment_response = document.analyze_sentiment() - >>> sentiment = sentiment_response.sentiment + >>> document = language.types.Document( + ... content='Jogging is not very fun.', + ... type='PLAIN_TEXT', + ... ) + >>> response = client.analyze_sentiment( + ... document=document, + ... encoding_type='UTF32', + ... ) + >>> sentiment = response.document_sentiment >>> print(sentiment.score) -1 >>> print(sentiment.magnitude) 0.8 +.. note:: + + It is recommended to send an ``encoding_type`` argument to Natural + Language methods, so they provide useful offsets for the data they return. + While the correct value varies by environment, in Python you *usually* + want ``UTF32``. + + +************* Annotate Text -------------- +************* -The :meth:`~google.cloud.language.document.Document.annotate_text` method +The :meth:`~.language_v1.LanguageServiceClient.annotate_text` method analyzes a document and is intended for users who are familiar with -machine learning and need in-depth text features to build upon. - -The method returns a named tuple with four entries: - -* ``sentences``: A :class:`list` of sentences in the text -* ``tokens``: A :class:`list` of :class:`~google.cloud.language.syntax.Token` - object (e.g. words, punctuation) -* ``sentiment``: The :class:`~google.cloud.language.sentiment.Sentiment` of - the text (as returned by - :meth:`~google.cloud.language.document.Document.analyze_sentiment`) -* ``entities``: :class:`list` of :class:`~google.cloud.language.entity.Entity` - objects extracted from the text (as returned by - :meth:`~google.cloud.language.document.Document.analyze_entities`) - -By default :meth:`~google.cloud.language.document.Document.annotate_text` has -three arguments ``include_syntax``, ``include_entities`` and -``include_sentiment`` which are all :data:`True`. However, each of these -`Features`_ can be selectively turned off by setting the corresponding -arguments to :data:`False`. - -When ``include_syntax=False``, ``sentences`` and ``tokens`` in the -response is :data:`None`. When ``include_sentiment=False``, ``sentiment`` in -the response is :data:`None`. When ``include_entities=False``, ``entities`` in -the response is :data:`None`. +machine learning and need in-depth text features to build upon. This method +returns a :class:`~.language_v1.types.AnnotateTextResponse`. - .. code-block:: python - >>> text_content = 'The cow jumped over the Moon.' - >>> document = client.document_from_text(text_content) - >>> annotations = document.annotate_text() - >>> # Sentences present if include_syntax=True - >>> print(annotations.sentences) - ['The cow jumped over the Moon.'] - >>> # Tokens present if include_syntax=True - >>> for token in annotations.tokens: - ... msg = '%11s: %s' % (token.part_of_speech, token.text_content) - ... print(msg) - DETERMINER: The - NOUN: cow - VERB: jumped - ADPOSITION: over - DETERMINER: the - NOUN: Moon - PUNCTUATION: . - >>> # Sentiment present if include_sentiment=True - >>> print(annotations.sentiment.score) - 1 - >>> print(annotations.sentiment.magnitude) - 0.1 - >>> # Entities present if include_entities=True - >>> for entity in annotations.entities: - ... print('=' * 20) - ... print(' name: %s' % (entity.name,)) - ... print(' type: %s' % (entity.entity_type,)) - ... print(' metadata: %s' % (entity.metadata,)) - ... print(' salience: %s' % (entity.salience,)) - ==================== - name: Moon - type: LOCATION - metadata: {'wikipedia_url': 'http://en.wikipedia.org/wiki/Natural_satellite'} - salience: 0.11793101 +************* +API Reference +************* + +This package includes clients for multiple versions of the Natural Language +API. By default, you will get ``v1``, the latest GA version. + +.. toctree:: + :maxdepth: 2 + + gapic/v1/api + gapic/v1/types + +If you are interested in beta features ahead of the latest GA, you may +opt-in to the v1.1 beta, which is spelled ``v1beta2``. In order to do this, +you will want to import from ``google.cloud.language_v1beta2`` in lieu of +``google.cloud.language``. + +An API and type reference is provided for the v1.1 beta also: + +.. toctree:: + :maxdepth: 2 + + gapic/v1beta2/api + gapic/v1beta2/types + +.. note:: -.. _Features: https://cloud.google.com/natural-language/docs/reference/rest/v1beta1/documents/annotateText#Features + The client for the beta API is provided on a provisional basis. The API + surface is subject to change, and it is possible that this client will be + deprecated or removed after its features become GA. diff --git a/docs/vision/gapic/api.rst b/docs/vision/gapic/v1/api.rst similarity index 100% rename from docs/vision/gapic/api.rst rename to docs/vision/gapic/v1/api.rst diff --git a/docs/vision/gapic/types.rst b/docs/vision/gapic/v1/types.rst similarity index 100% rename from docs/vision/gapic/types.rst rename to docs/vision/gapic/v1/types.rst diff --git a/docs/vision/index.rst b/docs/vision/index.rst index 49f90d502d46..6a124a935f5e 100644 --- a/docs/vision/index.rst +++ b/docs/vision/index.rst @@ -33,19 +33,21 @@ Authentication and Configuration this will be detected automatically. - After configuring your environment, create a - :class:`~google.cloud.vision.client.Client`. + :class:`~google.cloud.vision_v1.ImageAnnotatorClient`. .. code-block:: python >>> from google.cloud import vision >>> client = vision.ImageAnnotatorClient() -or pass in ``credentials`` and ``project`` explicitly. +or pass in ``credentials`` explicitly. .. code-block:: python >>> from google.cloud import vision - >>> client = vision.Client(project='my-project', credentials=creds) + >>> client = vision.ImageAnnotatorClient( + ... credentials=creds, + ... ) ***************** @@ -127,5 +129,5 @@ API Reference .. toctree:: :maxdepth: 2 - gapic/api - gapic/types + gapic/v1/api + gapic/v1/types diff --git a/language/google/cloud/gapic/language/v1/language_service_client.py b/language/google/cloud/gapic/language/v1/language_service_client.py index 808b69ddeecb..fb55b9568b67 100644 --- a/language/google/cloud/gapic/language/v1/language_service_client.py +++ b/language/google/cloud/gapic/language/v1/language_service_client.py @@ -125,7 +125,7 @@ def __init__(self, # Finally, track the GAPIC package version. metrics_headers['gapic'] = pkg_resources.get_distribution( - 'gapic-google-cloud-language-v1', ).version + 'google-cloud-language', ).version # Load the configuration defaults. default_client_config = json.loads( diff --git a/language/google/cloud/gapic/language/v1beta2/language_service_client.py b/language/google/cloud/gapic/language/v1beta2/language_service_client.py index 69e917f3d749..a990d2a9758a 100644 --- a/language/google/cloud/gapic/language/v1beta2/language_service_client.py +++ b/language/google/cloud/gapic/language/v1beta2/language_service_client.py @@ -125,7 +125,7 @@ def __init__(self, # Finally, track the GAPIC package version. metrics_headers['gapic'] = pkg_resources.get_distribution( - 'gapic-google-cloud-language-v1beta2', ).version + 'google-cloud-language', ).version # Load the configuration defaults. default_client_config = json.loads( diff --git a/language/google/cloud/language/__init__.py b/language/google/cloud/language/__init__.py index 8cc584b17cb8..8bba28ead739 100644 --- a/language/google/cloud/language/__init__.py +++ b/language/google/cloud/language/__init__.py @@ -12,14 +12,49 @@ # See the License for the specific language governing permissions and # limitations under the License. +# ----------------------------------------------------------------------------- +# TRANSITION CODE +# ----------------------------------------------------------------------------- +# The old Language manual layer is now deprecated, but to allow +# users the time to move from the manual layer to the mostly auto-generated +# layer, they are both living side by side for a few months. +# +# Instantiating the old manual layer (`google.cloud.language.Client`) will +# issue a DeprecationWarning. +# +# When it comes time to remove the old layer, everything in this directory +# should go away EXCEPT __init__.py (which can be renamed to language.py and +# put one directory above). +# +# Additionally, the import and export of `Client`, `Document`, and `Encoding` +# should be removed from this file (along with this note), and the rest should +# be left intact. +# ----------------------------------------------------------------------------- + """Client library for Google Cloud Natural Language API.""" +from __future__ import absolute_import from pkg_resources import get_distribution __version__ = get_distribution('google-cloud-language').version +from google.cloud.language_v1 import * # noqa + from google.cloud.language.client import Client from google.cloud.language.document import Document from google.cloud.language.document import Encoding -__all__ = ['Client', 'Document', 'Encoding', '__version__'] +__all__ = ( + # Common + '__version__', + + # Manual Layer + 'Client', + 'Document', + 'Encoding', + + # Auto-gen + 'enums', + 'LanguageServiceClient', + 'types', +) diff --git a/language/google/cloud/language/client.py b/language/google/cloud/language/client.py index da6ea90c156b..58066443c844 100644 --- a/language/google/cloud/language/client.py +++ b/language/google/cloud/language/client.py @@ -52,6 +52,16 @@ class Client(client_module.Client): } def __init__(self, credentials=None, api_version='v1', _http=None): + + # Add a deprecation warning for this class. + warnings.warn( + 'This client class and objects that derive from it have been ' + 'deprecated. Use `google.cloud.language.LanguageServiceClient` ' + '(provided by this package) instead. This client will be removed ' + 'in a future release.', + DeprecationWarning, + ) + super(Client, self).__init__( credentials=credentials, _http=_http) ConnectionClass = self._CONNECTION_CLASSES[api_version] diff --git a/language/google/cloud/language_v1/__init__.py b/language/google/cloud/language_v1/__init__.py new file mode 100644 index 000000000000..a5666eadb5c7 --- /dev/null +++ b/language/google/cloud/language_v1/__init__.py @@ -0,0 +1,30 @@ +# Copyright 2017, Google Inc. All rights reserved. +# +# 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. + +from __future__ import absolute_import + +from google.cloud.gapic.language.v1 import language_service_client as lsc +from google.cloud.gapic.language.v1 import enums + +from google.cloud.language_v1 import types + + +LanguageServiceClient = lsc.LanguageServiceClient + + +__all__ = ( + 'enums', + 'LanguageServiceClient', + 'types', +) diff --git a/language/google/cloud/language_v1/types.py b/language/google/cloud/language_v1/types.py new file mode 100644 index 000000000000..6223f6846e09 --- /dev/null +++ b/language/google/cloud/language_v1/types.py @@ -0,0 +1,30 @@ +# Copyright 2017, Google Inc. All rights reserved. +# +# 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. + +from __future__ import absolute_import +import sys + +from google.cloud.proto.language.v1 import language_service_pb2 + +from google.gax.utils.messages import get_messages + + +names = [] +for name, message in get_messages(language_service_pb2).items(): + message.__module__ = 'google.cloud.language_v1.types' + setattr(sys.modules[__name__], name, message) + names.append(name) + + +__all__ = tuple(sorted(names)) diff --git a/language/google/cloud/language_v1beta2/__init__.py b/language/google/cloud/language_v1beta2/__init__.py new file mode 100644 index 000000000000..e0a3e4cc287a --- /dev/null +++ b/language/google/cloud/language_v1beta2/__init__.py @@ -0,0 +1,30 @@ +# Copyright 2017, Google Inc. All rights reserved. +# +# 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. + +from __future__ import absolute_import + +from google.cloud.gapic.language.v1beta2 import language_service_client as lsc +from google.cloud.gapic.language.v1beta2 import enums + +from google.cloud.language_v1beta2 import types + + +LanguageServiceClient = lsc.LanguageServiceClient + + +__all__ = ( + 'enums', + 'LanguageServiceClient', + 'types', +) diff --git a/language/google/cloud/language_v1beta2/types.py b/language/google/cloud/language_v1beta2/types.py new file mode 100644 index 000000000000..557d05aeb001 --- /dev/null +++ b/language/google/cloud/language_v1beta2/types.py @@ -0,0 +1,30 @@ +# Copyright 2017, Google Inc. All rights reserved. +# +# 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. + +from __future__ import absolute_import +import sys + +from google.cloud.proto.language.v1beta2 import language_service_pb2 + +from google.gax.utils.messages import get_messages + + +names = [] +for name, message in get_messages(language_service_pb2).items(): + message.__module__ = 'google.cloud.language_v1beta2.types' + setattr(sys.modules[__name__], name, message) + names.append(name) + + +__all__ = tuple(sorted(names)) diff --git a/language/setup.py b/language/setup.py index 089d78d6bbb2..16ee4d5603ad 100644 --- a/language/setup.py +++ b/language/setup.py @@ -27,7 +27,7 @@ # consolidate. SETUP_BASE = { 'author': 'Google Cloud Platform', - 'author_email': 'jjg+google-cloud-python@google.com', + 'author_email': 'googleapis-publisher@google.com', 'scripts': [], 'url': 'https://github.com/GoogleCloudPlatform/google-cloud-python', 'license': 'Apache 2.0', @@ -52,7 +52,12 @@ REQUIREMENTS = [ 'google-cloud-core >= 0.25.0, < 0.26dev', + 'google-gax >= 0.15.13, < 0.16dev', + 'googleapis-common-protos[grpc] >= 1.5.2, < 2.0dev', ] +EXTRAS_REQUIRE = { + ':python_version<"3.4"': ['enum34'], +} setup( name='google-cloud-language', @@ -62,8 +67,13 @@ namespace_packages=[ 'google', 'google.cloud', + 'google.cloud.gapic', + 'google.cloud.gapic.language', + 'google.cloud.proto', + 'google.cloud.proto.language', ], packages=find_packages(exclude=('tests*',)), install_requires=REQUIREMENTS, + extras_require=EXTRAS_REQUIRE, **SETUP_BASE ) diff --git a/language/tests/gapic/v1/language_service_smoke_test.py b/language/tests/gapic/v1/language_service_smoke_test.py new file mode 100644 index 000000000000..67839505c670 --- /dev/null +++ b/language/tests/gapic/v1/language_service_smoke_test.py @@ -0,0 +1,30 @@ +# Copyright 2017, Google Inc. All rights reserved. +# +# 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 time +import unittest + +from google.cloud.gapic.language.v1 import enums +from google.cloud.gapic.language.v1 import language_service_client +from google.cloud.proto.language.v1 import language_service_pb2 + + +class LanguageServiceSmokeTest(unittest.TestCase): + def test_analyze_sentiment(self): + + client = language_service_client.LanguageServiceClient() + content = 'Hello, world!' + type_ = enums.Document.Type.PLAIN_TEXT + document = language_service_pb2.Document(content=content, type=type_) + response = client.analyze_sentiment(document) diff --git a/language/tests/gapic/v1/test_language_service_client_v1.py b/language/tests/gapic/v1/test_language_service_client_v1.py new file mode 100644 index 000000000000..a0b1931727ce --- /dev/null +++ b/language/tests/gapic/v1/test_language_service_client_v1.py @@ -0,0 +1,232 @@ +# Copyright 2017, Google Inc. All rights reserved. +# +# 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. +"""Unit tests.""" + +import mock +import unittest + +from google.gax import errors + +from google.cloud.gapic.language.v1 import enums +from google.cloud.gapic.language.v1 import language_service_client +from google.cloud.proto.language.v1 import language_service_pb2 + + +class CustomException(Exception): + pass + + +class TestLanguageServiceClient(unittest.TestCase): + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_sentiment(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + + # Mock response + language = 'language-1613589672' + expected_response = language_service_pb2.AnalyzeSentimentResponse( + language=language) + grpc_stub.AnalyzeSentiment.return_value = expected_response + + response = client.analyze_sentiment(document) + self.assertEqual(expected_response, response) + + grpc_stub.AnalyzeSentiment.assert_called_once() + args, kwargs = grpc_stub.AnalyzeSentiment.call_args + self.assertEqual(len(args), 2) + self.assertEqual(len(kwargs), 1) + self.assertIn('metadata', kwargs) + actual_request = args[0] + + expected_request = language_service_pb2.AnalyzeSentimentRequest( + document=document) + self.assertEqual(expected_request, actual_request) + + @mock.patch('google.gax.config.API_ERRORS', (CustomException, )) + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_sentiment_exception(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + + # Mock exception response + grpc_stub.AnalyzeSentiment.side_effect = CustomException() + + self.assertRaises(errors.GaxError, client.analyze_sentiment, document) + + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_entities(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock response + language = 'language-1613589672' + expected_response = language_service_pb2.AnalyzeEntitiesResponse( + language=language) + grpc_stub.AnalyzeEntities.return_value = expected_response + + response = client.analyze_entities(document, encoding_type) + self.assertEqual(expected_response, response) + + grpc_stub.AnalyzeEntities.assert_called_once() + args, kwargs = grpc_stub.AnalyzeEntities.call_args + self.assertEqual(len(args), 2) + self.assertEqual(len(kwargs), 1) + self.assertIn('metadata', kwargs) + actual_request = args[0] + + expected_request = language_service_pb2.AnalyzeEntitiesRequest( + document=document, encoding_type=encoding_type) + self.assertEqual(expected_request, actual_request) + + @mock.patch('google.gax.config.API_ERRORS', (CustomException, )) + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_entities_exception(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock exception response + grpc_stub.AnalyzeEntities.side_effect = CustomException() + + self.assertRaises(errors.GaxError, client.analyze_entities, document, + encoding_type) + + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_syntax(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock response + language = 'language-1613589672' + expected_response = language_service_pb2.AnalyzeSyntaxResponse( + language=language) + grpc_stub.AnalyzeSyntax.return_value = expected_response + + response = client.analyze_syntax(document, encoding_type) + self.assertEqual(expected_response, response) + + grpc_stub.AnalyzeSyntax.assert_called_once() + args, kwargs = grpc_stub.AnalyzeSyntax.call_args + self.assertEqual(len(args), 2) + self.assertEqual(len(kwargs), 1) + self.assertIn('metadata', kwargs) + actual_request = args[0] + + expected_request = language_service_pb2.AnalyzeSyntaxRequest( + document=document, encoding_type=encoding_type) + self.assertEqual(expected_request, actual_request) + + @mock.patch('google.gax.config.API_ERRORS', (CustomException, )) + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_syntax_exception(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock exception response + grpc_stub.AnalyzeSyntax.side_effect = CustomException() + + self.assertRaises(errors.GaxError, client.analyze_syntax, document, + encoding_type) + + @mock.patch('google.gax.config.create_stub', spec=True) + def test_annotate_text(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + features = language_service_pb2.AnnotateTextRequest.Features() + encoding_type = enums.EncodingType.NONE + + # Mock response + language = 'language-1613589672' + expected_response = language_service_pb2.AnnotateTextResponse( + language=language) + grpc_stub.AnnotateText.return_value = expected_response + + response = client.annotate_text(document, features, encoding_type) + self.assertEqual(expected_response, response) + + grpc_stub.AnnotateText.assert_called_once() + args, kwargs = grpc_stub.AnnotateText.call_args + self.assertEqual(len(args), 2) + self.assertEqual(len(kwargs), 1) + self.assertIn('metadata', kwargs) + actual_request = args[0] + + expected_request = language_service_pb2.AnnotateTextRequest( + document=document, features=features, encoding_type=encoding_type) + self.assertEqual(expected_request, actual_request) + + @mock.patch('google.gax.config.API_ERRORS', (CustomException, )) + @mock.patch('google.gax.config.create_stub', spec=True) + def test_annotate_text_exception(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + features = language_service_pb2.AnnotateTextRequest.Features() + encoding_type = enums.EncodingType.NONE + + # Mock exception response + grpc_stub.AnnotateText.side_effect = CustomException() + + self.assertRaises(errors.GaxError, client.annotate_text, document, + features, encoding_type) diff --git a/language/tests/gapic/v1beta2/language_service_smoke_test.py b/language/tests/gapic/v1beta2/language_service_smoke_test.py new file mode 100644 index 000000000000..d94531f88f75 --- /dev/null +++ b/language/tests/gapic/v1beta2/language_service_smoke_test.py @@ -0,0 +1,30 @@ +# Copyright 2017, Google Inc. All rights reserved. +# +# 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 time +import unittest + +from google.cloud.gapic.language.v1beta2 import enums +from google.cloud.gapic.language.v1beta2 import language_service_client +from google.cloud.proto.language.v1beta2 import language_service_pb2 + + +class LanguageServiceSmokeTest(unittest.TestCase): + def test_analyze_sentiment(self): + + client = language_service_client.LanguageServiceClient() + content = 'Hello, world!' + type_ = enums.Document.Type.PLAIN_TEXT + document = language_service_pb2.Document(content=content, type=type_) + response = client.analyze_sentiment(document) diff --git a/language/tests/gapic/v1beta2/test_language_service_client_v1beta2.py b/language/tests/gapic/v1beta2/test_language_service_client_v1beta2.py new file mode 100644 index 000000000000..fea1c572d4ce --- /dev/null +++ b/language/tests/gapic/v1beta2/test_language_service_client_v1beta2.py @@ -0,0 +1,283 @@ +# Copyright 2017, Google Inc. All rights reserved. +# +# 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. +"""Unit tests.""" + +import mock +import unittest + +from google.gax import errors + +from google.cloud.gapic.language.v1beta2 import enums +from google.cloud.gapic.language.v1beta2 import language_service_client +from google.cloud.proto.language.v1beta2 import language_service_pb2 + + +class CustomException(Exception): + pass + + +class TestLanguageServiceClient(unittest.TestCase): + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_sentiment(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + + # Mock response + language = 'language-1613589672' + expected_response = language_service_pb2.AnalyzeSentimentResponse( + language=language) + grpc_stub.AnalyzeSentiment.return_value = expected_response + + response = client.analyze_sentiment(document) + self.assertEqual(expected_response, response) + + grpc_stub.AnalyzeSentiment.assert_called_once() + args, kwargs = grpc_stub.AnalyzeSentiment.call_args + self.assertEqual(len(args), 2) + self.assertEqual(len(kwargs), 1) + self.assertIn('metadata', kwargs) + actual_request = args[0] + + expected_request = language_service_pb2.AnalyzeSentimentRequest( + document=document) + self.assertEqual(expected_request, actual_request) + + @mock.patch('google.gax.config.API_ERRORS', (CustomException, )) + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_sentiment_exception(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + + # Mock exception response + grpc_stub.AnalyzeSentiment.side_effect = CustomException() + + self.assertRaises(errors.GaxError, client.analyze_sentiment, document) + + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_entities(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock response + language = 'language-1613589672' + expected_response = language_service_pb2.AnalyzeEntitiesResponse( + language=language) + grpc_stub.AnalyzeEntities.return_value = expected_response + + response = client.analyze_entities(document, encoding_type) + self.assertEqual(expected_response, response) + + grpc_stub.AnalyzeEntities.assert_called_once() + args, kwargs = grpc_stub.AnalyzeEntities.call_args + self.assertEqual(len(args), 2) + self.assertEqual(len(kwargs), 1) + self.assertIn('metadata', kwargs) + actual_request = args[0] + + expected_request = language_service_pb2.AnalyzeEntitiesRequest( + document=document, encoding_type=encoding_type) + self.assertEqual(expected_request, actual_request) + + @mock.patch('google.gax.config.API_ERRORS', (CustomException, )) + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_entities_exception(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock exception response + grpc_stub.AnalyzeEntities.side_effect = CustomException() + + self.assertRaises(errors.GaxError, client.analyze_entities, document, + encoding_type) + + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_entity_sentiment(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock response + language = 'language-1613589672' + expected_response = language_service_pb2.AnalyzeEntitySentimentResponse( + language=language) + grpc_stub.AnalyzeEntitySentiment.return_value = expected_response + + response = client.analyze_entity_sentiment(document, encoding_type) + self.assertEqual(expected_response, response) + + grpc_stub.AnalyzeEntitySentiment.assert_called_once() + args, kwargs = grpc_stub.AnalyzeEntitySentiment.call_args + self.assertEqual(len(args), 2) + self.assertEqual(len(kwargs), 1) + self.assertIn('metadata', kwargs) + actual_request = args[0] + + expected_request = language_service_pb2.AnalyzeEntitySentimentRequest( + document=document, encoding_type=encoding_type) + self.assertEqual(expected_request, actual_request) + + @mock.patch('google.gax.config.API_ERRORS', (CustomException, )) + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_entity_sentiment_exception(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock exception response + grpc_stub.AnalyzeEntitySentiment.side_effect = CustomException() + + self.assertRaises(errors.GaxError, client.analyze_entity_sentiment, + document, encoding_type) + + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_syntax(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock response + language = 'language-1613589672' + expected_response = language_service_pb2.AnalyzeSyntaxResponse( + language=language) + grpc_stub.AnalyzeSyntax.return_value = expected_response + + response = client.analyze_syntax(document, encoding_type) + self.assertEqual(expected_response, response) + + grpc_stub.AnalyzeSyntax.assert_called_once() + args, kwargs = grpc_stub.AnalyzeSyntax.call_args + self.assertEqual(len(args), 2) + self.assertEqual(len(kwargs), 1) + self.assertIn('metadata', kwargs) + actual_request = args[0] + + expected_request = language_service_pb2.AnalyzeSyntaxRequest( + document=document, encoding_type=encoding_type) + self.assertEqual(expected_request, actual_request) + + @mock.patch('google.gax.config.API_ERRORS', (CustomException, )) + @mock.patch('google.gax.config.create_stub', spec=True) + def test_analyze_syntax_exception(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + encoding_type = enums.EncodingType.NONE + + # Mock exception response + grpc_stub.AnalyzeSyntax.side_effect = CustomException() + + self.assertRaises(errors.GaxError, client.analyze_syntax, document, + encoding_type) + + @mock.patch('google.gax.config.create_stub', spec=True) + def test_annotate_text(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + features = language_service_pb2.AnnotateTextRequest.Features() + encoding_type = enums.EncodingType.NONE + + # Mock response + language = 'language-1613589672' + expected_response = language_service_pb2.AnnotateTextResponse( + language=language) + grpc_stub.AnnotateText.return_value = expected_response + + response = client.annotate_text(document, features, encoding_type) + self.assertEqual(expected_response, response) + + grpc_stub.AnnotateText.assert_called_once() + args, kwargs = grpc_stub.AnnotateText.call_args + self.assertEqual(len(args), 2) + self.assertEqual(len(kwargs), 1) + self.assertIn('metadata', kwargs) + actual_request = args[0] + + expected_request = language_service_pb2.AnnotateTextRequest( + document=document, features=features, encoding_type=encoding_type) + self.assertEqual(expected_request, actual_request) + + @mock.patch('google.gax.config.API_ERRORS', (CustomException, )) + @mock.patch('google.gax.config.create_stub', spec=True) + def test_annotate_text_exception(self, mock_create_stub): + # Mock gRPC layer + grpc_stub = mock.Mock() + mock_create_stub.return_value = grpc_stub + + client = language_service_client.LanguageServiceClient() + + # Mock request + document = language_service_pb2.Document() + features = language_service_pb2.AnnotateTextRequest.Features() + encoding_type = enums.EncodingType.NONE + + # Mock exception response + grpc_stub.AnnotateText.side_effect = CustomException() + + self.assertRaises(errors.GaxError, client.annotate_text, document, + features, encoding_type) diff --git a/vision/setup.py b/vision/setup.py index 7cc30276fe58..b62e249a9bda 100644 --- a/vision/setup.py +++ b/vision/setup.py @@ -26,7 +26,7 @@ REQUIREMENTS = [ 'google-cloud-core >= 0.25.0, < 0.26dev', - 'google-gax >= 0.15.7, < 0.16dev', + 'google-gax >= 0.15.13, < 0.16dev', 'googleapis-common-protos[grpc] >= 1.5.2, < 2.0dev', ] EXTRAS_REQUIRE = {