From 1c0014c54724b047851be19b5ef3d218fa84cc8e Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Mon, 29 Jul 2019 17:03:51 -0400 Subject: [PATCH 01/15] SG-12945: Added localized param to Shotgun object --- shotgun_api3/shotgun.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 151394ed..d04ccfee 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -427,6 +427,7 @@ def __init__(self, sg): self.session_token = None self.authorization = None self.no_ssl_validation = False + self.localized = True @property def records_per_page(self): @@ -469,7 +470,8 @@ def __init__(self, password=None, sudo_as_login=None, session_token=None, - auth_token=None): + auth_token=None, + localized=True): """ Initializes a new instance of the Shotgun client. @@ -538,6 +540,9 @@ def __init__(self, ``auth_token`` is invalid. .. todo: Add this info to the Authentication section of the docs + :param bool localized: A boolean to asks for some fields to be localized. When ``True``, a + header ``locale`` with value ``auto`` is added to HTTP requests. Default is ``True``. + .. note:: A note about proxy connections: If you are using Python <= v2.6.2, HTTPS connections through a proxy server will not work due to a bug in the :mod:`urllib2` library (see http://bugs.python.org/issue1424152). This will affect upload and @@ -598,6 +603,7 @@ def __init__(self, self.config.convert_datetimes_to_utc = convert_datetimes_to_utc self.config.no_ssl_validation = NO_SSL_VALIDATION self.config.raw_http_proxy = http_proxy + self.config.localized = localized try: self.config.rpc_attempt_interval = int(os.environ.get("SHOTGUN_API_RETRY_INTERVAL", 3000)) @@ -3205,6 +3211,10 @@ def _call_rpc(self, method, params, include_auth_params=True, first=False): "content-type": "application/json; charset=utf-8", "connection": "keep-alive" } + + if self.config.localized is True: + req_headers["locale"] = "auto" + http_status, resp_headers, body = self._make_call("POST", self.config.api_path, encoded_payload, req_headers) LOG.debug("Completed rpc call to %s" % (method)) From 151afde2cae4bdc21c30085248af221246415c8d Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Thu, 1 Aug 2019 14:58:40 -0400 Subject: [PATCH 02/15] set default localization param to False --- shotgun_api3/shotgun.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index d04ccfee..66b8ba7e 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -427,7 +427,7 @@ def __init__(self, sg): self.session_token = None self.authorization = None self.no_ssl_validation = False - self.localized = True + self.localized = False @property def records_per_page(self): @@ -471,7 +471,7 @@ def __init__(self, sudo_as_login=None, session_token=None, auth_token=None, - localized=True): + localized=False): """ Initializes a new instance of the Shotgun client. From 1cfd074d11e0d0d20de433eadce9ce1511c6e2b8 Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Mon, 2 Sep 2019 17:12:59 -0400 Subject: [PATCH 03/15] added test for locale header --- tests/test_client.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_client.py b/tests/test_client.py index 74ddb4d1..076bc84c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -184,6 +184,32 @@ def test_authorization(self): expected = "Basic " + b64encode(urllib.parse.unquote(login_password)).strip() self.assertEqual(expected, headers.get("Authorization")) + def test_localization_header(self): + """Localization header passed to server""" + self.sg = api.Shotgun( + self.config.server_url, + self.config.script_name, + self.config.api_key, + http_proxy=self.config.http_proxy, + ensure_ascii=True, + connect=False, + ca_certs=None, + login=None, + password=None, + sudo_as_login=None, + session_token=None, + auth_token=None, + localized=True + ) + self._setup_mock() + + self.sg.info() + + args, _ = self.sg._http_request.call_args + (_, _, _, headers) = args + expected_header_value = "auto" + self.assertEqual("auto", headers.get("locale")) + def test_user_agent(self): """User-Agent passed to server""" # test default user agent From 16744ca3809b80ca2569788aaecbb0f2ad75489f Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Tue, 3 Sep 2019 16:44:40 -0400 Subject: [PATCH 04/15] added instruction for good syntax on example config file --- tests/example_config | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/example_config b/tests/example_config index 3ced7dc7..451b10dd 100644 --- a/tests/example_config +++ b/tests/example_config @@ -15,6 +15,7 @@ # Full url to the Shotgun server server # e.g. http://my-company.shotgunstudio.com +# be careful to not end server_url with a "/", or some tests may fail server_url : http://0.0.0.0:3000 # script name as key as listed in admin panel for your server script_name : test script name From f550358c306804f3cfd08913d97e2231422f6ffa Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Wed, 4 Sep 2019 12:05:09 -0400 Subject: [PATCH 05/15] removed localized param from constructor --- shotgun_api3/shotgun.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 66b8ba7e..3105c5e0 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -470,8 +470,7 @@ def __init__(self, password=None, sudo_as_login=None, session_token=None, - auth_token=None, - localized=False): + auth_token=None): """ Initializes a new instance of the Shotgun client. @@ -540,9 +539,6 @@ def __init__(self, ``auth_token`` is invalid. .. todo: Add this info to the Authentication section of the docs - :param bool localized: A boolean to asks for some fields to be localized. When ``True``, a - header ``locale`` with value ``auto`` is added to HTTP requests. Default is ``True``. - .. note:: A note about proxy connections: If you are using Python <= v2.6.2, HTTPS connections through a proxy server will not work due to a bug in the :mod:`urllib2` library (see http://bugs.python.org/issue1424152). This will affect upload and From 756cfcb329d52cce747b582bf078c2bbe5e2bf67 Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Wed, 4 Sep 2019 12:05:33 -0400 Subject: [PATCH 06/15] added test for localized header when not passing localized param (default) --- tests/test_client.py | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 076bc84c..ae74b575 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -184,30 +184,26 @@ def test_authorization(self): expected = "Basic " + b64encode(urllib.parse.unquote(login_password)).strip() self.assertEqual(expected, headers.get("Authorization")) - def test_localization_header(self): - """Localization header passed to server""" - self.sg = api.Shotgun( - self.config.server_url, - self.config.script_name, - self.config.api_key, - http_proxy=self.config.http_proxy, - ensure_ascii=True, - connect=False, - ca_certs=None, - login=None, - password=None, - sudo_as_login=None, - session_token=None, - auth_token=None, - localized=True - ) - self._setup_mock() + def test_localization_header_default(self): + """Localization header not passed to server without explicitly setting SG localization config to True""" + self.sg.info() + + args, _ = self.sg._http_request.call_args + (_, _, _, headers) = args + expected_header_value = "auto" + + self.assertEqual(None, headers.get("locale")) + + def test_localization_header_when_localized(self): + """Localization header passed to server when setting SG localization config to True""" + self.sg.config.localized = True self.sg.info() args, _ = self.sg._http_request.call_args (_, _, _, headers) = args expected_header_value = "auto" + self.assertEqual("auto", headers.get("locale")) def test_user_agent(self): From c695d7fc684007309118f9d783e577f2221961e7 Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Thu, 5 Sep 2019 13:40:13 -0400 Subject: [PATCH 07/15] added documentation for localization --- docs/reference.rst | 42 +++++++++++++++++++++++++++++++++++++++++ shotgun_api3/shotgun.py | 6 ++++++ 2 files changed, 48 insertions(+) diff --git a/docs/reference.rst b/docs/reference.rst index c39facc0..37f4bdfe 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -891,3 +891,45 @@ Stores the number of milliseconds to wait between request retries. By default, In the case that both this environment variable and the config's ``rpc_attempt_interval`` property are set, the value in ``rpc_attempt_interal`` will be used. +************ +Localization +************ + +Shotgun API offers the ability to return display namnes in user's language. +The server currently returns localized display names for those methods: + + * Shotgun.schema_entity_read + * Shotgun.schema_field_read + +Localization is disabled by default. To enable localization, set the ``localized`` property to ``True``. +The property cannot be set at Shotgun class instantiation. When the class is instantiated, the ``localized`` config property is set to ``False``. + +Example for a user whose Language preference is set to Japanese: + +.. code-block:: python + :emphasize-lines: 8,19 + + >>> sg = Shotgun(site_name, script_name, script_key) + >>> sg.config.localized # checking that localization is disabled + False + >>> sg.schema_field_read('Shot') + { + 'sg_vendor_groups': { + 'mandatory': {'editable': False, 'value': False}, + # the value field (display name) is not localized + 'name': {'editable': True, 'value': 'Vendor Groups'}, + ... + }, + ... + } + >>> sg.config.localized = True # enabling the localization + >>> sg.schema_field_read('Shot') + { + 'sg_vendor_groups': { + 'mandatory': {'editable': False, 'value': False}, + # the value field (display name) is localized + 'name': {'editable': True, 'value': '\xe3\x83\x99\xe3\x83\xb3\xe3\x83\x80\xe3\x83\xbc \xe3\x82\xb0\xe3\x83\xab\xe3\x83\xbc\xe3\x83\x97'}, + ... + }, + ... + } \ No newline at end of file diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 3105c5e0..5983a108 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -1820,6 +1820,9 @@ def schema_entity_read(self, project_entity=None): ``{'type': 'Project', 'id': 3}`` :returns: dict of Entity Type to dict containing the display name. :rtype: dict + + .. note:: + The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more informations. """ params = {} @@ -1919,6 +1922,9 @@ def schema_field_read(self, entity_type, field_name=None, project_entity=None): .. note:: If you don't specify a ``project_entity``, everything is reported as visible. + .. note:: + The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more informations. + >>> sg.schema_field_read('Asset', 'shots') {'shots': {'data_type': {'editable': False, 'value': 'multi_entity'}, 'description': {'editable': True, 'value': ''}, From e44943ff071bf71dd20623810a8505d591d26b2e Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Thu, 5 Sep 2019 16:02:08 -0400 Subject: [PATCH 08/15] replaced default property after removing param from constructor --- shotgun_api3/shotgun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 5983a108..5c1c64df 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -599,7 +599,7 @@ def __init__(self, self.config.convert_datetimes_to_utc = convert_datetimes_to_utc self.config.no_ssl_validation = NO_SSL_VALIDATION self.config.raw_http_proxy = http_proxy - self.config.localized = localized + self.config.localized = False try: self.config.rpc_attempt_interval = int(os.environ.get("SHOTGUN_API_RETRY_INTERVAL", 3000)) From f51d2422aafa486185da87bf090a8f83c3f96826 Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Fri, 6 Sep 2019 10:26:15 -0400 Subject: [PATCH 09/15] updated documentation to fix various typos/mistakes and add more information --- docs/reference.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/reference.rst b/docs/reference.rst index 37f4bdfe..34cb2c41 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -895,7 +895,9 @@ In the case that both this environment variable and the config's ``rpc_attempt_i Localization ************ -Shotgun API offers the ability to return display namnes in user's language. +The Shotgun API offers the ability to return localized display names in the current user's language. +Requests made from script/API users are localized in the site settings. + The server currently returns localized display names for those methods: * Shotgun.schema_entity_read From c5b364710779036214c6b4334857429227ffe7ff Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Fri, 6 Sep 2019 10:34:07 -0400 Subject: [PATCH 10/15] fixed more doc and removed double property assignment --- docs/reference.rst | 7 ++----- shotgun_api3/shotgun.py | 5 ++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/docs/reference.rst b/docs/reference.rst index 34cb2c41..ff1f3f54 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -898,15 +898,12 @@ Localization The Shotgun API offers the ability to return localized display names in the current user's language. Requests made from script/API users are localized in the site settings. -The server currently returns localized display names for those methods: - - * Shotgun.schema_entity_read - * Shotgun.schema_field_read +This functionality is currently supported by the methods ``Shotgun.schema_entity_read`` and ``Shotgun.schema_field_read``. Localization is disabled by default. To enable localization, set the ``localized`` property to ``True``. The property cannot be set at Shotgun class instantiation. When the class is instantiated, the ``localized`` config property is set to ``False``. -Example for a user whose Language preference is set to Japanese: +Example for a user whose language preference is set to Japanese: .. code-block:: python :emphasize-lines: 8,19 diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 5c1c64df..3e09e659 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -599,7 +599,6 @@ def __init__(self, self.config.convert_datetimes_to_utc = convert_datetimes_to_utc self.config.no_ssl_validation = NO_SSL_VALIDATION self.config.raw_http_proxy = http_proxy - self.config.localized = False try: self.config.rpc_attempt_interval = int(os.environ.get("SHOTGUN_API_RETRY_INTERVAL", 3000)) @@ -1822,7 +1821,7 @@ def schema_entity_read(self, project_entity=None): :rtype: dict .. note:: - The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more informations. + The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more information. """ params = {} @@ -1923,7 +1922,7 @@ def schema_field_read(self, entity_type, field_name=None, project_entity=None): If you don't specify a ``project_entity``, everything is reported as visible. .. note:: - The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more informations. + The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more information. >>> sg.schema_field_read('Asset', 'shots') {'shots': {'data_type': {'editable': False, 'value': 'multi_entity'}, From d8f34431711d85b464962f104ff88edd42a7f583 Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Fri, 6 Sep 2019 10:49:16 -0400 Subject: [PATCH 11/15] Removed redundant information from documentation --- docs/reference.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/reference.rst b/docs/reference.rst index ff1f3f54..6c6bdd59 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -901,7 +901,6 @@ Requests made from script/API users are localized in the site settings. This functionality is currently supported by the methods ``Shotgun.schema_entity_read`` and ``Shotgun.schema_field_read``. Localization is disabled by default. To enable localization, set the ``localized`` property to ``True``. -The property cannot be set at Shotgun class instantiation. When the class is instantiated, the ``localized`` config property is set to ``False``. Example for a user whose language preference is set to Japanese: From 7f371657c89d16b926e3c0982f041df50a47e178 Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Mon, 9 Sep 2019 16:09:23 -0400 Subject: [PATCH 12/15] updated highlighted lines in doc localization code example --- docs/reference.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference.rst b/docs/reference.rst index 6c6bdd59..3f79ac95 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -905,7 +905,7 @@ Localization is disabled by default. To enable localization, set the ``localized Example for a user whose language preference is set to Japanese: .. code-block:: python - :emphasize-lines: 8,19 + :emphasize-lines: 9,20 >>> sg = Shotgun(site_name, script_name, script_key) >>> sg.config.localized # checking that localization is disabled From a688c7aa8ec28ccef96ed0bd079bdc4dce99cbc4 Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Mon, 9 Sep 2019 16:32:40 -0400 Subject: [PATCH 13/15] added note on localized string decoding in the doc --- docs/reference.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/reference.rst b/docs/reference.rst index 3f79ac95..6ad73218 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -930,4 +930,7 @@ Example for a user whose language preference is set to Japanese: ... }, ... - } \ No newline at end of file + } + +.. note:: + If needed, the encoding of the returned localized string can be ensured regardless the Python version using shotgun_api3.lib.six.ensure_text(). From 053ccb2a6f8d940d6941496d4558582f49c5b7fc Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Wed, 11 Sep 2019 18:09:27 -0400 Subject: [PATCH 14/15] added Shotgun.schema_read() to the list of methods supported by localization --- docs/reference.rst | 2 +- shotgun_api3/shotgun.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/reference.rst b/docs/reference.rst index 6ad73218..d5f68d99 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -898,7 +898,7 @@ Localization The Shotgun API offers the ability to return localized display names in the current user's language. Requests made from script/API users are localized in the site settings. -This functionality is currently supported by the methods ``Shotgun.schema_entity_read`` and ``Shotgun.schema_field_read``. +This functionality is currently supported by the methods ``Shotgun.schema_entity_read``, ``Shotgun.schema_field_read``, and ``Shotgun.schema_read``. Localization is disabled by default. To enable localization, set the ``localized`` property to ``True``. diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 3e09e659..183687f1 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -1891,6 +1891,9 @@ def schema_read(self, project_entity=None): types. Properties that are ``'editable': True``, can be updated using the :meth:`~shotgun_api3.Shotgun.schema_field_update` method. :rtype: dict + + .. note:: + The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more information. """ params = {} From 53f98ca3b0da13a6e677aedb6283fdc966872806 Mon Sep 17 00:00:00 2001 From: Paul ALISIR Date: Tue, 17 Sep 2019 09:54:19 -0400 Subject: [PATCH 15/15] packaging for the v3.1.2 release --- HISTORY.rst | 5 +++++ setup.py | 2 +- shotgun_api3/shotgun.py | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index a437a47d..e9f2988b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,6 +4,11 @@ Shotgun Python API Changelog Here you can see the full list of changes between each Python API release. +v3.1.2 (2019 Sept 17) +===================== +- Adds an optional `localized` property on the Shotgun object which allows to retrieve localized display names on + methods ``schema_entity_read()``, ``schema_field_read()``, and ``schema_read()``. + v3.1.1 (2019 August 29) ===================== - Fixes a regression on Python 2.7.0-2.7.9 on Windows with the mimetypes module. diff --git a/setup.py b/setup.py index e925bdd4..8837466c 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ setup( name='shotgun_api3', - version='3.1.1', + version='3.1.2', description='Shotgun Python API ', long_description=readme, author='Shotgun Software', diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 183687f1..5395696c 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -117,7 +117,7 @@ def _is_mimetypes_broken(): # ---------------------------------------------------------------------------- # Version -__version__ = "3.1.1" +__version__ = "3.1.2" # ---------------------------------------------------------------------------- # Errors