From 030de924d2fbaff9faa6034b9697a109abe20495 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 14 Mar 2019 16:35:11 -0700 Subject: [PATCH 1/9] Add in progress beta snippets Change-Id: I2cd8ddc2307a8e40d56ce7e493749dc05c34d164 --- translate/cloud-client/README.rst | 37 ++ translate/cloud-client/README.rst.in | 3 + translate/cloud-client/beta_snippets.py | 350 +++++++++++++++++++ translate/cloud-client/beta_snippets_test.py | 94 +++++ 4 files changed, 484 insertions(+) create mode 100644 translate/cloud-client/beta_snippets.py create mode 100644 translate/cloud-client/beta_snippets_test.py diff --git a/translate/cloud-client/README.rst b/translate/cloud-client/README.rst index 4935e3c8b067..f5065ed4be20 100644 --- a/translate/cloud-client/README.rst +++ b/translate/cloud-client/README.rst @@ -121,6 +121,43 @@ To run this sample: +Beta Snippets ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=translate/cloud-client/beta_snippets.py,translate/cloud-client/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python beta_snippets.py + + usage: beta_snippets.py [-h] + {translate-text,batch-translate-text,detect-language,list-languages,list-languages-with-target,create-glossary,get-glossary,list-glossaries,delete-glossary,translate-with-glossary} + ... + + positional arguments: + {translate-text,batch-translate-text,detect-language,list-languages,list-languages-with-target,create-glossary,get-glossary,list-glossaries,delete-glossary,translate-with-glossary} + translate-text + batch-translate-text + detect-language + list-languages + list-languages-with-target + create-glossary + get-glossary + list-glossaries + delete-glossary + translate-with-glossary + + optional arguments: + -h, --help show this help message and exit + + + The client library diff --git a/translate/cloud-client/README.rst.in b/translate/cloud-client/README.rst.in index a2483218cc40..ba804e74de3d 100644 --- a/translate/cloud-client/README.rst.in +++ b/translate/cloud-client/README.rst.in @@ -18,6 +18,9 @@ samples: - name: Snippets file: snippets.py show_help: true +- name: Beta Snippets + file: beta_snippets.py + show_help: true cloud_client_library: true diff --git a/translate/cloud-client/beta_snippets.py b/translate/cloud-client/beta_snippets.py new file mode 100644 index 000000000000..623a4042af6e --- /dev/null +++ b/translate/cloud-client/beta_snippets.py @@ -0,0 +1,350 @@ +# Copyright 2019 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 argparse + + +def translate_text(project_id, text): + # [START translate_translate_text_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = YOUR_PROJECT_ID + # text = 'Text you wish to translate' + location = 'global' + + parent = client.location_path(project_id, location) + + response = client.translate_text( + parent=parent, + contents=[text], + mime_type='text/plain', # mime types: text/plain, text/html + source_language_code='en-US', + target_language_code='sr-Latn') + + for translation in response.translations: + print('Translated Text: {}'.format(translation)) + # [END translate_translate_text_beta] + + +def batch_translate_text(project_id, input_uri, output_uri): + # [START translate_batch_translate_text_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = YOUR_PROJECT_ID + # input_uri = 'gs://cloud-samples-data/translation/text.txt' + # output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/' + location = 'global' + + parent = client.location_path(project_id, location) + + gcs_source = translate.types.GcsSource(input_uri=input_uri) + + input_config = translate.types.InputConfig( + mime_type='text/plain', # mime types: text/plain, text/html + gcs_source=gcs_source) + + gcs_destination = translate.types.GcsDestination( + output_uri_prefix=output_uri) + + output_config = translate.types.OutputConfig( + gcs_destination=gcs_destination) + + operation = client.batch_translate_text( + parent=parent, + source_language_code='en-US', + target_language_codes=['sr-Latn'], + input_configs=[input_config], + output_config=output_config) + + result = operation.result(90) + + print('Total Characters: {}'.format(result.total_characters)) + print('Translated Characters: {}'.format(result.translated_characters)) + # [END translate_batch_translate_text_beta] + + +def detect_language(project_id, text): + # [START translate_detect_language_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = YOUR_PROJECT_ID + # text = 'Text you wish to translate' + location = 'global' + + parent = client.location_path(project_id, location) + + response = client.detect_language( + parent=parent, + content=text, + mime_type='text/plain') # mime types: text/plain, text/html + + for language in response.languages: + print('Language Code: {} (Confidence: {})'.format( + language.language_code, + language.confidence)) + # [END translate_detect_language_beta] + + +def list_languages(project_id): + # [START translate_list_codes_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = YOUR_PROJECT_ID + location = 'global' + + parent = client.location_path(project_id, location) + + response = client.get_supported_languages(parent) + + print('Supported Languages:') + for language in response.languages: + print('Language Code: {}'.format(language.language_code)) + # [END translate_list_codes_beta] + + +def list_languages_with_target(project_id, display_language_code): + # [START translate_list_language_names_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = YOUR_PROJECT_ID + # display_language_code = 'is' + location = 'global' + + parent = client.location_path(project_id, location) + + response = client.get_supported_languages( + parent=parent, + display_language_code=display_language_code) + + print('Supported Languages:') + for language in response.languages: + print('Language Code: {}'.format(language.language_code)) + print('Display Name: {}\n'.format(language.display_name)) + # [END translate_list_language_names_beta] + + +def create_glossary(project_id): + # [START translate_create_glossary_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = 'YOUR_PROJECT_ID' + location = 'us-central1' # The location of the glossary + + name = client.glossary_path( + project_id, + location, + 'glossary') # The name of your glossary + + language_codes_set = translate.types.Glossary.LanguageCodesSet( + language_codes=['en', 'es']) + + gcs_source = translate.types.GcsSource( + input_uri='gs://cloud-samples-data/translation/glossary.csv') + + input_config = translate.types.GlossaryInputConfig( + gcs_source=gcs_source) + + glossary = translate.types.Glossary( + name=name, + language_codes_set=language_codes_set, + input_config=input_config) + + parent = client.location_path(project_id, location) + + operation = client.create_glossary(parent=parent, glossary=glossary) + + result = operation.result(timeout=90) + print('Created: {}'.format(result.name)) + print('Input Uri: {}'.format(result.input_config.gcs_source.input_uri)) + # [END translate_create_glossary_beta] + + +def list_glossaries(project_id): + # [START translate_list_glossary_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = 'YOUR_PROJECT_ID' + location = 'us-central1' # The location of the glossary + + parent = client.location_path(project_id, location) + + for glossary in client.list_glossaries(parent): + print('Name: {}'.format(glossary.name)) + print('Language Pair:') + print('\tSource Language Code: {}'.format( + glossary.language_pair.source_language_code)) + print('\tTarget Language Code: {}'.format( + glossary.language_pair.target_language_code)) + print('Input Uri: {}'.format( + glossary.input_config.gcs_source.input_uri)) + # [END translate_list_glossary_beta] + + +def get_glossary(project_id): + # [START translate_get_glossary_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = 'YOUR_PROJECT_ID' + + parent = client.glossary_path( + project_id, + 'us-central1', # The location of the glossary + 'glossary') # The name of your glossary + + response = client.get_glossary(parent) + print('Name: {}'.format(response.name)) + print('Language Pair:') + print('\tSource Language Code: {}'.format( + response.language_pair.source_language_code)) + print('\tTarget Language Code: {}'.format( + response.language_pair.target_language_code)) + print('Input Uri: {}'.format( + response.input_config.gcs_source.input_uri)) + # [END translate_get_glossary_beta] + + +def delete_glossary(project_id): + # [START translate_delete_glossary_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = 'YOUR_PROJECT_ID' + + parent = client.glossary_path( + project_id, + 'us-central1', # The location of the glossary + 'glossary') # The name of your glossary + + operation = client.delete_glossary(parent) + result = operation.result(timeout=90) + print('Deleted: {}'.format(result.name)) + # [END translate_delete_glossary_beta] + + +def translate_text_with_glossary(project_id, text): + # [START translate_translate_text_with_glossary_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = 'YOUR_PROJECT_ID' + # text = 'Text you wish to translate' + location = 'us-central1' # The location of the glossary + + glossary = client.glossary_path( + project_id, + location, + 'glossary') # The name of your glossary + + glossary_config = translate.types.TranslateTextGlossaryConfig( + glossary=glossary) + + parent = client.location_path(project_id, location) + + result = client.translate_text( + parent=parent, + contents=[text], + mime_type='text/plain', # mime types: text/plain, text/html + source_language_code='en', + target_language_code='es', + glossary_config=glossary_config) + + for translation in result.translations: + print(translation) + # [END translate_translate_text_with_glossary_beta] + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + + subparsers = parser.add_subparsers(dest='command') + + translate_text_parser = subparsers.add_parser( + 'translate-text', help=translate_text.__doc__) + translate_text_parser.add_argument('project_id') + translate_text_parser.add_argument('text') + + batch_translate_text_parser = subparsers.add_parser( + 'batch-translate-text', help=translate_text.__doc__) + batch_translate_text_parser.add_argument('project_id') + batch_translate_text_parser.add_argument('gcs_source') + batch_translate_text_parser.add_argument('gcs_destination') + + detect_langage_parser = subparsers.add_parser( + 'detect-language', help=detect_language.__doc__) + detect_langage_parser.add_argument('project_id') + detect_langage_parser.add_argument('text') + + list_languages_parser = subparsers.add_parser( + 'list-languages', help=list_languages.__doc__) + list_languages_parser.add_argument('project_id') + + list_languages_with_target_parser = subparsers.add_parser( + 'list-languages-with-target', help=list_languages_with_target.__doc__) + list_languages_with_target_parser.add_argument('project_id') + list_languages_with_target_parser.add_argument('display_language_code') + + create_glossary_parser = subparsers.add_parser( + 'create-glossary', help=create_glossary.__doc__) + create_glossary_parser.add_argument('project_id') + + get_glossary_parser = subparsers.add_parser( + 'get-glossary', help=get_glossary.__doc__) + get_glossary_parser.add_argument('project_id') + + list_glossary_parser = subparsers.add_parser( + 'list-glossaries', help=list_glossaries.__doc__) + list_glossary_parser.add_argument('project_id') + + delete_glossary_parser = subparsers.add_parser( + 'delete-glossary', help=delete_glossary.__doc__) + delete_glossary_parser.add_argument('project_id') + + translate_with_glossary_parser = subparsers.add_parser( + 'translate-with-glossary', help=translate_text_with_glossary.__doc__) + translate_with_glossary_parser.add_argument('project_id') + translate_with_glossary_parser.add_argument('text') + + args = parser.parse_args() + + if args.command == 'translate-text': + translate_text(args.project_id, args.text) + elif args.command == 'batch-translate-text': + batch_translate_text( + args.project_id, args.gcs_source, args.gcs_destination) + elif args.command == 'detect-language': + detect_language(args.project_id, args.text) + elif args.command == 'list-languages': + list_languages(args.project_id) + elif args.command == 'list-languages-with-target': + list_languages_with_target(args.project_id, args.display_language_code) + elif args.command == 'create-glossary': + create_glossary(args.project_id) + elif args.command == 'get-glossary': + get_glossary(args.project_id) + elif args.command == 'list-glossaries': + list_glossaries(args.project_id) + elif args.command == 'delete-glossary': + delete_glossary(args.project_id) + elif args.command == 'translate-with-glossary': + translate_text_with_glossary(args.project_id, args.text) diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py new file mode 100644 index 000000000000..0efb2299077e --- /dev/null +++ b/translate/cloud-client/beta_snippets_test.py @@ -0,0 +1,94 @@ +# Copyright 2019 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 os +import beta_snippets +from google.cloud import storage + +PROJECT_ID = os.environ['GCLOUD_PROJECT'] + + +def test_translate_text(capsys): + beta_snippets.translate_text(PROJECT_ID, 'Hello world') + out, _ = capsys.readouterr() + assert 'Zdravo svet' in out + + +def test_batch_translate_text(capsys): + beta_snippets.batch_translate_text( + PROJECT_ID, + 'gs://cloud-samples-data/translation/text.txt', + 'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(PROJECT_ID)) + out, _ = capsys.readouterr() + assert 'Total Characters: 13' in out + assert 'Translated Characters: 13' in out + + storage_client = storage.Client() + bucket = storage_client.get_bucket(PROJECT_ID) + + blobs = bucket.list_blobs(prefix='translation/BATCH_TRANSLATION_OUTPUT') + + for blob in blobs: + blob.delete() + + +def test_detect_language(capsys): + beta_snippets.detect_language(PROJECT_ID, 'Hæ sæta') + out, _ = capsys.readouterr() + assert 'is' in out + + +def test_list_languages(capsys): + beta_snippets.list_languages(PROJECT_ID) + out, _ = capsys.readouterr() + assert 'zh-CN' in out + + +def test_list_languages_with_target(capsys): + beta_snippets.list_languages_with_target(PROJECT_ID, 'is') + out, _ = capsys.readouterr() + assert u'Language Code: sq' in out + assert u'Display Name: albanska' in out + + +def test_create_glossary(capsys): + beta_snippets.create_glossary(PROJECT_ID) + out, _ = capsys.readouterr() + assert 'gs://cloud-samples-data/translation/glossary.csv' in out + + +def test_get_glossary(capsys): + beta_snippets.get_glossary(PROJECT_ID) + out, _ = capsys.readouterr() + assert 'gs://cloud-samples-data/translation/glossary.csv' in out + + +def test_list_glossary(capsys): + beta_snippets.list_glossaries(PROJECT_ID) + out, _ = capsys.readouterr() + assert 'gs://cloud-samples-data/translation/glossary.csv' in out + + +def test_translate_text_with_glossary(capsys): + beta_snippets.translate_text_with_glossary(PROJECT_ID, 'directions') + out, _ = capsys.readouterr() + assert 'direcciones' in out + + +def test_delete_glossary(capsys): + beta_snippets.delete_glossary(PROJECT_ID) + out, _ = capsys.readouterr() + assert PROJECT_ID in out + assert 'us-central1' in out + assert 'glossary' in out From d513efed3981f3fb9a9150ae703bc0e8d615dec9 Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Wed, 3 Apr 2019 11:22:42 -0700 Subject: [PATCH 2/9] Add google-cloud-storage dependency Change-Id: Iff7bc9b2c82b1e829580a3d4ad628087dbeee8be --- translate/cloud-client/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/translate/cloud-client/requirements.txt b/translate/cloud-client/requirements.txt index 62b23909bf67..381e047944f1 100644 --- a/translate/cloud-client/requirements.txt +++ b/translate/cloud-client/requirements.txt @@ -1 +1,2 @@ google-cloud-translate==1.3.3 +google-cloud-storage==1.14.0 From aabd1acddf063e418b37656827a761afccf403ba Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Wed, 3 Apr 2019 11:24:10 -0700 Subject: [PATCH 3/9] Non-'global' location required for BatchTranslateText Change-Id: I5198aa6368a088e8f5ee295dc55a5e9e4ca8f494 --- translate/cloud-client/beta_snippets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translate/cloud-client/beta_snippets.py b/translate/cloud-client/beta_snippets.py index 623a4042af6e..6a381287dc7a 100644 --- a/translate/cloud-client/beta_snippets.py +++ b/translate/cloud-client/beta_snippets.py @@ -46,7 +46,7 @@ def batch_translate_text(project_id, input_uri, output_uri): # project_id = YOUR_PROJECT_ID # input_uri = 'gs://cloud-samples-data/translation/text.txt' # output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/' - location = 'global' + location = 'us-central1' parent = client.location_path(project_id, location) From 316efe182bda2748e7a1303aa74f8e06f12b2c38 Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Wed, 3 Apr 2019 11:55:27 -0700 Subject: [PATCH 4/9] Upgrade google-cloud-translate to 1.4.0 1.4.0 includes the new v3beta1 alongside V2 Change-Id: I5adfe78ea7e78d84678db343cd84516e3d05491f --- translate/cloud-client/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translate/cloud-client/requirements.txt b/translate/cloud-client/requirements.txt index 381e047944f1..318e3485aa01 100644 --- a/translate/cloud-client/requirements.txt +++ b/translate/cloud-client/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-translate==1.3.3 +google-cloud-translate==1.4.0 google-cloud-storage==1.14.0 From d1b2d7ab5a4d5b36e8ae470a35b13e435b588dab Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Wed, 3 Apr 2019 15:51:28 -0700 Subject: [PATCH 5/9] Update Translate samples You can now provide your own glossary ID The tests now run within a randomly created bucket (deleted after each test) Change-Id: I5cb2680cd0e9e43c85932a6a0dc19e6fab5008c5 --- translate/cloud-client/beta_snippets.py | 5 ++-- translate/cloud-client/beta_snippets_test.py | 29 +++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/translate/cloud-client/beta_snippets.py b/translate/cloud-client/beta_snippets.py index 6a381287dc7a..235aa41c1acc 100644 --- a/translate/cloud-client/beta_snippets.py +++ b/translate/cloud-client/beta_snippets.py @@ -139,18 +139,19 @@ def list_languages_with_target(project_id, display_language_code): # [END translate_list_language_names_beta] -def create_glossary(project_id): +def create_glossary(project_id, glossary_id): # [START translate_create_glossary_beta] from google.cloud import translate_v3beta1 as translate client = translate.TranslationServiceClient() # project_id = 'YOUR_PROJECT_ID' + # glossary_id = 'glossary-id' location = 'us-central1' # The location of the glossary name = client.glossary_path( project_id, location, - 'glossary') # The name of your glossary + glossary_id) language_codes_set = translate.types.Glossary.LanguageCodesSet( language_codes=['en', 'es']) diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py index 0efb2299077e..dad69aca2399 100644 --- a/translate/cloud-client/beta_snippets_test.py +++ b/translate/cloud-client/beta_snippets_test.py @@ -13,36 +13,45 @@ # limitations under the License. import os +import pytest +import uuid import beta_snippets from google.cloud import storage PROJECT_ID = os.environ['GCLOUD_PROJECT'] +storage_client = storage.Client() +bucket = None + + +@pytest.fixture +def setup(): + # Create a bucket for the tests to use + bucket = storage_client.create_bucket(uuid.uuid4()) + + yield None + + # Delete the bucket (must delete files in it first) + for blob in bucket.list_blobs(): + blob.delete() + bucket.delete() def test_translate_text(capsys): beta_snippets.translate_text(PROJECT_ID, 'Hello world') out, _ = capsys.readouterr() assert 'Zdravo svet' in out - def test_batch_translate_text(capsys): beta_snippets.batch_translate_text( PROJECT_ID, 'gs://cloud-samples-data/translation/text.txt', - 'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(PROJECT_ID)) + 'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(bucket.name)) out, _ = capsys.readouterr() assert 'Total Characters: 13' in out assert 'Translated Characters: 13' in out - storage_client = storage.Client() - bucket = storage_client.get_bucket(PROJECT_ID) - blobs = bucket.list_blobs(prefix='translation/BATCH_TRANSLATION_OUTPUT') - for blob in blobs: - blob.delete() - - def test_detect_language(capsys): beta_snippets.detect_language(PROJECT_ID, 'Hæ sæta') out, _ = capsys.readouterr() @@ -63,7 +72,7 @@ def test_list_languages_with_target(capsys): def test_create_glossary(capsys): - beta_snippets.create_glossary(PROJECT_ID) + beta_snippets.create_glossary(PROJECT_ID, 'glossary') out, _ = capsys.readouterr() assert 'gs://cloud-samples-data/translation/glossary.csv' in out From bdcc31e9b035820af0d54c0725dd39a42fb29c15 Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Wed, 3 Apr 2019 16:22:02 -0700 Subject: [PATCH 6/9] pytest.fixture for random test bucket Change-Id: I8e816ed4c95a6235347a29849044b4cab02d40a8 --- translate/cloud-client/beta_snippets_test.py | 24 ++++++++------------ 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py index dad69aca2399..49809d4f8f9a 100644 --- a/translate/cloud-client/beta_snippets_test.py +++ b/translate/cloud-client/beta_snippets_test.py @@ -20,28 +20,24 @@ PROJECT_ID = os.environ['GCLOUD_PROJECT'] -storage_client = storage.Client() -bucket = None +@pytest.fixture(scope='function') +def bucket(): + # Create a temporaty bucket to store annotation output. + bucket_name = str(uuid.uuid1()) + storage_client = storage.Client() + bucket = storage_client.create_bucket(bucket_name) + yield bucket -@pytest.fixture -def setup(): - # Create a bucket for the tests to use - bucket = storage_client.create_bucket(uuid.uuid4()) - - yield None - - # Delete the bucket (must delete files in it first) - for blob in bucket.list_blobs(): - blob.delete() - bucket.delete() + # Teardown. + bucket.delete(force=True) def test_translate_text(capsys): beta_snippets.translate_text(PROJECT_ID, 'Hello world') out, _ = capsys.readouterr() assert 'Zdravo svet' in out -def test_batch_translate_text(capsys): +def test_batch_translate_text(capsys, bucket): beta_snippets.batch_translate_text( PROJECT_ID, 'gs://cloud-samples-data/translation/text.txt', From d0dc2dddba0998825f47bfbb2de323ea199b83c1 Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Wed, 3 Apr 2019 16:36:34 -0700 Subject: [PATCH 7/9] flake8 spec fixes Change-Id: I4932bcf856a9498b01d9661c90c6b45ee2958ee1 --- translate/cloud-client/beta_snippets_test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py index 49809d4f8f9a..ab38ed5d786c 100644 --- a/translate/cloud-client/beta_snippets_test.py +++ b/translate/cloud-client/beta_snippets_test.py @@ -20,6 +20,7 @@ PROJECT_ID = os.environ['GCLOUD_PROJECT'] + @pytest.fixture(scope='function') def bucket(): # Create a temporaty bucket to store annotation output. @@ -32,11 +33,13 @@ def bucket(): # Teardown. bucket.delete(force=True) + def test_translate_text(capsys): beta_snippets.translate_text(PROJECT_ID, 'Hello world') out, _ = capsys.readouterr() assert 'Zdravo svet' in out + def test_batch_translate_text(capsys, bucket): beta_snippets.batch_translate_text( PROJECT_ID, @@ -46,7 +49,6 @@ def test_batch_translate_text(capsys, bucket): assert 'Total Characters: 13' in out assert 'Translated Characters: 13' in out - blobs = bucket.list_blobs(prefix='translation/BATCH_TRANSLATION_OUTPUT') def test_detect_language(capsys): beta_snippets.detect_language(PROJECT_ID, 'Hæ sæta') From 1294bfc0af849662e07ce687c780b8268a2fc23b Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Fri, 5 Apr 2019 00:27:20 -0700 Subject: [PATCH 8/9] Added pytest fixture for creating glossary (WIP) Change-Id: Iddb5ecbf0eefb9efd2243dc4bc56b585102e9351 --- translate/cloud-client/beta_snippets.py | 40 ++++++----- translate/cloud-client/beta_snippets_test.py | 74 ++++++++++++++------ 2 files changed, 77 insertions(+), 37 deletions(-) diff --git a/translate/cloud-client/beta_snippets.py b/translate/cloud-client/beta_snippets.py index 235aa41c1acc..7cd94aed59a0 100644 --- a/translate/cloud-client/beta_snippets.py +++ b/translate/cloud-client/beta_snippets.py @@ -189,27 +189,26 @@ def list_glossaries(project_id): for glossary in client.list_glossaries(parent): print('Name: {}'.format(glossary.name)) - print('Language Pair:') - print('\tSource Language Code: {}'.format( - glossary.language_pair.source_language_code)) - print('\tTarget Language Code: {}'.format( - glossary.language_pair.target_language_code)) - print('Input Uri: {}'.format( + print('Entry count: {}'.format(glossary.entry_count)) + print('Input uri: {}'.format( glossary.input_config.gcs_source.input_uri)) + for language_code in glossary.language_codes_set.language_codes: + print('Language code: {}'.format(language_code)) # [END translate_list_glossary_beta] -def get_glossary(project_id): +def get_glossary(project_id, glossary_id): # [START translate_get_glossary_beta] from google.cloud import translate_v3beta1 as translate client = translate.TranslationServiceClient() # project_id = 'YOUR_PROJECT_ID' + # glossary_id = 'GLOSSARY_ID' parent = client.glossary_path( project_id, 'us-central1', # The location of the glossary - 'glossary') # The name of your glossary + glossary_id) response = client.get_glossary(parent) print('Name: {}'.format(response.name)) @@ -223,17 +222,18 @@ def get_glossary(project_id): # [END translate_get_glossary_beta] -def delete_glossary(project_id): +def delete_glossary(project_id, glossary_id): # [START translate_delete_glossary_beta] from google.cloud import translate_v3beta1 as translate client = translate.TranslationServiceClient() # project_id = 'YOUR_PROJECT_ID' + # glossary_id = 'GLOSSARY_ID' parent = client.glossary_path( project_id, 'us-central1', # The location of the glossary - 'glossary') # The name of your glossary + glossary_id) operation = client.delete_glossary(parent) result = operation.result(timeout=90) @@ -241,19 +241,20 @@ def delete_glossary(project_id): # [END translate_delete_glossary_beta] -def translate_text_with_glossary(project_id, text): +def translate_text_with_glossary(project_id, glossary_id, text): # [START translate_translate_text_with_glossary_beta] from google.cloud import translate_v3beta1 as translate client = translate.TranslationServiceClient() # project_id = 'YOUR_PROJECT_ID' + # glossary_id = 'GLOSSARY_ID' # text = 'Text you wish to translate' location = 'us-central1' # The location of the glossary glossary = client.glossary_path( project_id, - location, - 'glossary') # The name of your glossary + 'us-central1', # The location of the glossary + glossary_id) glossary_config = translate.types.TranslateTextGlossaryConfig( glossary=glossary) @@ -308,10 +309,12 @@ def translate_text_with_glossary(project_id, text): create_glossary_parser = subparsers.add_parser( 'create-glossary', help=create_glossary.__doc__) create_glossary_parser.add_argument('project_id') + create_glossary_parser.add_argument('glossary_id') get_glossary_parser = subparsers.add_parser( 'get-glossary', help=get_glossary.__doc__) get_glossary_parser.add_argument('project_id') + get_glossary_parser.add_argument('glossary_id') list_glossary_parser = subparsers.add_parser( 'list-glossaries', help=list_glossaries.__doc__) @@ -320,10 +323,12 @@ def translate_text_with_glossary(project_id, text): delete_glossary_parser = subparsers.add_parser( 'delete-glossary', help=delete_glossary.__doc__) delete_glossary_parser.add_argument('project_id') + delete_glossary_parser.add_argument('glossary_id') translate_with_glossary_parser = subparsers.add_parser( 'translate-with-glossary', help=translate_text_with_glossary.__doc__) translate_with_glossary_parser.add_argument('project_id') + translate_with_glossary_parser.add_argument('glossary_id') translate_with_glossary_parser.add_argument('text') args = parser.parse_args() @@ -340,12 +345,13 @@ def translate_text_with_glossary(project_id, text): elif args.command == 'list-languages-with-target': list_languages_with_target(args.project_id, args.display_language_code) elif args.command == 'create-glossary': - create_glossary(args.project_id) + create_glossary(args.project_id, args.glossary_id) elif args.command == 'get-glossary': - get_glossary(args.project_id) + get_glossary(args.project_id, args.glossary_id) elif args.command == 'list-glossaries': list_glossaries(args.project_id) elif args.command == 'delete-glossary': - delete_glossary(args.project_id) + delete_glossary(args.project_id, args.glossary_id) elif args.command == 'translate-with-glossary': - translate_text_with_glossary(args.project_id, args.text) + translate_text_with_glossary( + args.project_id, args.glossary_id, args.text) diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py index ab38ed5d786c..ce6daab72f06 100644 --- a/translate/cloud-client/beta_snippets_test.py +++ b/translate/cloud-client/beta_snippets_test.py @@ -23,31 +23,58 @@ @pytest.fixture(scope='function') def bucket(): - # Create a temporaty bucket to store annotation output. + """Create a temporary bucket to store annotation output.""" bucket_name = str(uuid.uuid1()) storage_client = storage.Client() bucket = storage_client.create_bucket(bucket_name) yield bucket - # Teardown. bucket.delete(force=True) +# Can this DI a unique_glossary_id into it? +@pytest.fixture(scope='session') +def glossary(): + """Get the ID of a glossary available to session (do not mutate/delete).""" + glossary_id = 'must-start-with-letters-' + str(uuid.uuid1()) + beta_snippets.create_glossary(PROJECT_ID, glossary_id) + + yield glossary_id + + try: + beta_snippets.delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass + + +@pytest.fixture(scope='function') +def unique_glossary_id(): + """Get a unique ID. Attempts to delete glossary with this ID after test.""" + glossary_id = 'must-start-with-letters-' + str(uuid.uuid1()) + + yield glossary_id + + try: + beta_snippets.delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass + + def test_translate_text(capsys): beta_snippets.translate_text(PROJECT_ID, 'Hello world') out, _ = capsys.readouterr() assert 'Zdravo svet' in out -def test_batch_translate_text(capsys, bucket): - beta_snippets.batch_translate_text( - PROJECT_ID, - 'gs://cloud-samples-data/translation/text.txt', - 'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(bucket.name)) - out, _ = capsys.readouterr() - assert 'Total Characters: 13' in out - assert 'Translated Characters: 13' in out +# def test_batch_translate_text(capsys, bucket): +# beta_snippets.batch_translate_text( +# PROJECT_ID, +# 'gs://cloud-samples-data/translation/text.txt', +# 'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(bucket.name)) +# out, _ = capsys.readouterr() +# assert 'Total Characters: 13' in +# assert 'Translated Characters: 13' in out def test_detect_language(capsys): @@ -69,33 +96,40 @@ def test_list_languages_with_target(capsys): assert u'Display Name: albanska' in out -def test_create_glossary(capsys): - beta_snippets.create_glossary(PROJECT_ID, 'glossary') +def test_create_glossary(capsys, unique_glossary_id): + beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) out, _ = capsys.readouterr() + assert 'Created' in out + assert PROJECT_ID in out + assert unique_glossary_id in out assert 'gs://cloud-samples-data/translation/glossary.csv' in out -def test_get_glossary(capsys): - beta_snippets.get_glossary(PROJECT_ID) +def test_get_glossary(capsys, glossary): + beta_snippets.get_glossary(PROJECT_ID, glossary) out, _ = capsys.readouterr() + # ASSERT STUFF ABOUT GLOSSARY assert 'gs://cloud-samples-data/translation/glossary.csv' in out -def test_list_glossary(capsys): +def test_list_glossary(capsys, glossary): beta_snippets.list_glossaries(PROJECT_ID) out, _ = capsys.readouterr() + # ASSERT STUFF ABOUT GLOSSARY assert 'gs://cloud-samples-data/translation/glossary.csv' in out -def test_translate_text_with_glossary(capsys): - beta_snippets.translate_text_with_glossary(PROJECT_ID, 'directions') +def test_translate_text_with_glossary(capsys, glossary): + beta_snippets.translate_text_with_glossary( + PROJECT_ID, glossary, 'directions') out, _ = capsys.readouterr() assert 'direcciones' in out -def test_delete_glossary(capsys): - beta_snippets.delete_glossary(PROJECT_ID) +def test_delete_glossary(capsys, unique_glossary_id): + beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) + beta_snippets.delete_glossary(PROJECT_ID, unique_glossary_id) out, _ = capsys.readouterr() assert PROJECT_ID in out assert 'us-central1' in out - assert 'glossary' in out + assert unique_glossary_id in out From b217cf8962f76f9ffc09135db4a32c3965765287 Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Fri, 5 Apr 2019 18:22:11 -0700 Subject: [PATCH 9/9] Add assertions, remove placeholder TODOs Change-Id: If1eb20bca5bfcc87dd0652d5488b2188afa626af --- translate/cloud-client/beta_snippets_test.py | 21 ++++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py index ce6daab72f06..f7099a27cae1 100644 --- a/translate/cloud-client/beta_snippets_test.py +++ b/translate/cloud-client/beta_snippets_test.py @@ -33,7 +33,6 @@ def bucket(): bucket.delete(force=True) -# Can this DI a unique_glossary_id into it? @pytest.fixture(scope='session') def glossary(): """Get the ID of a glossary available to session (do not mutate/delete).""" @@ -67,14 +66,14 @@ def test_translate_text(capsys): assert 'Zdravo svet' in out -# def test_batch_translate_text(capsys, bucket): -# beta_snippets.batch_translate_text( -# PROJECT_ID, -# 'gs://cloud-samples-data/translation/text.txt', -# 'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(bucket.name)) -# out, _ = capsys.readouterr() -# assert 'Total Characters: 13' in -# assert 'Translated Characters: 13' in out +def test_batch_translate_text(capsys, bucket): + beta_snippets.batch_translate_text( + PROJECT_ID, + 'gs://cloud-samples-data/translation/text.txt', + 'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(bucket.name)) + out, _ = capsys.readouterr() + assert 'Total Characters: 13' in out + assert 'Translated Characters: 13' in out def test_detect_language(capsys): @@ -108,14 +107,14 @@ def test_create_glossary(capsys, unique_glossary_id): def test_get_glossary(capsys, glossary): beta_snippets.get_glossary(PROJECT_ID, glossary) out, _ = capsys.readouterr() - # ASSERT STUFF ABOUT GLOSSARY + assert glossary in out assert 'gs://cloud-samples-data/translation/glossary.csv' in out def test_list_glossary(capsys, glossary): beta_snippets.list_glossaries(PROJECT_ID) out, _ = capsys.readouterr() - # ASSERT STUFF ABOUT GLOSSARY + assert glossary in out assert 'gs://cloud-samples-data/translation/glossary.csv' in out