From 5eeb9aa89db24be756514940a7fff3033766ddf5 Mon Sep 17 00:00:00 2001 From: Jacob Geiger Date: Mon, 21 Mar 2016 12:47:21 -0700 Subject: [PATCH] Updates from GitHub PR * Use {} rather than dict() * Use pkg_resources to load service config file * Simplify format string for tracking header and change separator to ';' instead of '/'. * Fix incorrect comment where "DNS" was used although "domain name" was meant See: https://github.com/GoogleCloudPlatform/gcloud-python/pull/1629 Pre-push hook installed. Change-Id: I02c832dfcd34eed8f79338711d6e2d0e34cf0182 --- .../io/gapi/vgen/py/PythonImportHandler.java | 2 ++ .../main/resources/io/gapi/vgen/py/main.snip | 23 +++++++++--------- .../vgen/testdata/python_library.baseline | 24 +++++++++---------- .../python_no_path_templates.baseline | 24 +++++++++---------- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/vgen/src/main/java/io/gapi/vgen/py/PythonImportHandler.java b/vgen/src/main/java/io/gapi/vgen/py/PythonImportHandler.java index 80af953355..8ca5b5019b 100644 --- a/vgen/src/main/java/io/gapi/vgen/py/PythonImportHandler.java +++ b/vgen/src/main/java/io/gapi/vgen/py/PythonImportHandler.java @@ -33,6 +33,8 @@ public PythonImportHandler(Interface service) { // Add non-service-specific imports. addImport(null, PythonImport.create("os", PythonImport.ImportType.STDLIB)); + addImport(null, + PythonImport.create("pkg_resources", PythonImport.ImportType.STDLIB)); addImport(null, PythonImport.create("platform", PythonImport.ImportType.STDLIB)); addImport(null, diff --git a/vgen/src/main/resources/io/gapi/vgen/py/main.snip b/vgen/src/main/resources/io/gapi/vgen/py/main.snip index cb093fa466..f1e62f1883 100644 --- a/vgen/src/main/resources/io/gapi/vgen/py/main.snip +++ b/vgen/src/main/resources/io/gapi/vgen/py/main.snip @@ -68,7 +68,7 @@ @end @private constantSection(service) - _CODE_GEN_NAME_VERSION = 'gapic-0.1.0' + _CODE_GEN_NAME_VERSION = 'gapic/0.1.0' SERVICE_ADDRESS = '{@context.getServiceConfig.getServiceAddress(service)}' """The default address of the service.""" @@ -110,7 +110,7 @@ """Constructor. Args: - :keyword service_path: The DNS of the API remote host. + :keyword service_path: The domain name of the API remote host. :type service_path: string :keyword port: The port on which to connect to the remote host. :type port: int @@ -145,20 +145,19 @@ app_name = 'gax' if app_version is None: app_version = google.gax.__version__ - bundling_override = bundling_override or dict() - retrying_override = retrying_override or dict() - config_filename = os.path.join( - os.path.dirname(__file__), '{@yamlFilename}.yaml') - with open(config_filename, 'r') as api_yaml: - self._defaults = api_callable.construct_settings( - yaml.load(api_yaml.read()), bundling_override, retrying_override, - config.STATUS_CODE_NAMES, timeout) - google_apis_agent = '{}-{}/{}/gax-{}/{}'.format( + bundling_override = bundling_override or {} + retrying_override = retrying_override or {} + client_config = pkg_resources.resource_string( + __name__, '{@yamlFilename}.yaml') + self._defaults = api_callable.construct_settings( + yaml.load(client_config), bundling_override, retrying_override, + config.STATUS_CODE_NAMES, timeout) + google_apis_agent = '{}/{};{};gax/{};python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, google.gax.__version__, - 'python-{}'.format(platform.python_version()) + platform.python_version() ) self._headers = [('x-google-apis-agent', google_apis_agent)] self.stub = config.create_stub( diff --git a/vgen/src/test/java/io/gapi/vgen/testdata/python_library.baseline b/vgen/src/test/java/io/gapi/vgen/testdata/python_library.baseline index 99198c5f91..95bff0ddf9 100644 --- a/vgen/src/test/java/io/gapi/vgen/testdata/python_library.baseline +++ b/vgen/src/test/java/io/gapi/vgen/testdata/python_library.baseline @@ -23,6 +23,7 @@ # merge preserves those additions if the generated source changes. import os +import pkg_resources import platform from google.gax import api_callable @@ -51,7 +52,7 @@ class LibraryServiceApi(object): Check out `cloud docs! `_ """ - _CODE_GEN_NAME_VERSION = 'gapic-0.1.0' + _CODE_GEN_NAME_VERSION = 'gapic/0.1.0' SERVICE_ADDRESS = 'library-example.googleapis.com' """The default address of the service.""" @@ -101,7 +102,7 @@ class LibraryServiceApi(object): """Constructor. Args: - :keyword service_path: The DNS of the API remote host. + :keyword service_path: The domain name of the API remote host. :type service_path: string :keyword port: The port on which to connect to the remote host. :type port: int @@ -136,20 +137,19 @@ class LibraryServiceApi(object): app_name = 'gax' if app_version is None: app_version = google.gax.__version__ - bundling_override = bundling_override or dict() - retrying_override = retrying_override or dict() - config_filename = os.path.join( - os.path.dirname(__file__), 'library_service_api.yaml') - with open(config_filename, 'r') as api_yaml: - self._defaults = api_callable.construct_settings( - yaml.load(api_yaml.read()), bundling_override, retrying_override, - config.STATUS_CODE_NAMES, timeout) - google_apis_agent = '{}-{}/{}/gax-{}/{}'.format( + bundling_override = bundling_override or {} + retrying_override = retrying_override or {} + client_config = pkg_resources.resource_string( + __name__, 'library_service_api.yaml') + self._defaults = api_callable.construct_settings( + yaml.load(client_config), bundling_override, retrying_override, + config.STATUS_CODE_NAMES, timeout) + google_apis_agent = '{}/{};{};gax/{};python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, google.gax.__version__, - 'python-{}'.format(platform.python_version()) + platform.python_version() ) self._headers = [('x-google-apis-agent', google_apis_agent)] self.stub = config.create_stub( diff --git a/vgen/src/test/java/io/gapi/vgen/testdata/python_no_path_templates.baseline b/vgen/src/test/java/io/gapi/vgen/testdata/python_no_path_templates.baseline index 0342180cb4..93c24bbbe1 100644 --- a/vgen/src/test/java/io/gapi/vgen/testdata/python_no_path_templates.baseline +++ b/vgen/src/test/java/io/gapi/vgen/testdata/python_no_path_templates.baseline @@ -23,6 +23,7 @@ # merge preserves those additions if the generated source changes. import os +import pkg_resources import platform from google.gax import api_callable @@ -35,7 +36,7 @@ from google.example.v1 import no_path_templates_pb2 class NoTemplatesServiceApi(object): - _CODE_GEN_NAME_VERSION = 'gapic-0.1.0' + _CODE_GEN_NAME_VERSION = 'gapic/0.1.0' SERVICE_ADDRESS = 'no-path-templates.googleapis.com' """The default address of the service.""" @@ -63,7 +64,7 @@ class NoTemplatesServiceApi(object): """Constructor. Args: - :keyword service_path: The DNS of the API remote host. + :keyword service_path: The domain name of the API remote host. :type service_path: string :keyword port: The port on which to connect to the remote host. :type port: int @@ -98,20 +99,19 @@ class NoTemplatesServiceApi(object): app_name = 'gax' if app_version is None: app_version = google.gax.__version__ - bundling_override = bundling_override or dict() - retrying_override = retrying_override or dict() - config_filename = os.path.join( - os.path.dirname(__file__), 'no_templates_service_api.yaml') - with open(config_filename, 'r') as api_yaml: - self._defaults = api_callable.construct_settings( - yaml.load(api_yaml.read()), bundling_override, retrying_override, - config.STATUS_CODE_NAMES, timeout) - google_apis_agent = '{}-{}/{}/gax-{}/{}'.format( + bundling_override = bundling_override or {} + retrying_override = retrying_override or {} + client_config = pkg_resources.resource_string( + __name__, 'no_templates_service_api.yaml') + self._defaults = api_callable.construct_settings( + yaml.load(client_config), bundling_override, retrying_override, + config.STATUS_CODE_NAMES, timeout) + google_apis_agent = '{}/{};{};gax/{};python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, google.gax.__version__, - 'python-{}'.format(platform.python_version()) + platform.python_version() ) self._headers = [('x-google-apis-agent', google_apis_agent)] self.stub = config.create_stub(