utils: Remove unicode-related helpers
The 'unicode_key_value_to_string' and '_encode' helpers were used to handle the difference between unicode and byte strings in Python 2. In a Python 3-only world, they are unnecessary. Remove them. Some references to text encoding in the HACKING file are removed. In theory, we should no longer have to worry about byte strings. Change-Id: I0694b25dc4d72e817cb08ce6cafb39bb8226293d Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
parent
1562ec6fb1
commit
0002625e2b
40
HACKING
40
HACKING
@ -73,43 +73,3 @@ Docstrings
|
||||
:returns: description of the return value
|
||||
|
||||
"""
|
||||
|
||||
Text encoding
|
||||
-------------
|
||||
- All text within python code should be of type 'unicode'.
|
||||
|
||||
WRONG:
|
||||
|
||||
>>> s = 'foo'
|
||||
>>> s
|
||||
'foo'
|
||||
>>> type(s)
|
||||
<type 'str'>
|
||||
|
||||
RIGHT:
|
||||
|
||||
>>> u = u'foo'
|
||||
>>> u
|
||||
u'foo'
|
||||
>>> type(u)
|
||||
<type 'unicode'>
|
||||
|
||||
- Transitions between internal unicode and external strings should always
|
||||
be immediately and explicitly encoded or decoded.
|
||||
|
||||
- All external text that is not explicitly encoded (database storage,
|
||||
commandline arguments, etc.) should be presumed to be encoded as utf-8.
|
||||
|
||||
WRONG:
|
||||
|
||||
mystring = infile.readline()
|
||||
myreturnstring = do_some_magic_with(mystring)
|
||||
outfile.write(myreturnstring)
|
||||
|
||||
RIGHT:
|
||||
|
||||
mystring = infile.readline()
|
||||
mytext = s.decode('utf-8')
|
||||
returntext = do_some_magic_with(mytext)
|
||||
returnstring = returntext.encode('utf-8')
|
||||
outfile.write(returnstring)
|
||||
|
@ -180,7 +180,6 @@ class Manager(utils.HookableMixin):
|
||||
|
||||
def _build_query_string(self, search_opts):
|
||||
search_opts = search_opts or {}
|
||||
search_opts = utils.unicode_key_value_to_string(search_opts)
|
||||
params = sorted(
|
||||
[(k, v) for (k, v) in search_opts.items() if v])
|
||||
query_string = "?%s" % utils.safe_urlencode(params) if params else ''
|
||||
|
@ -48,20 +48,6 @@ TEST_RESPONSE_DICT_V3.set_project_scope()
|
||||
TEST_VERSIONS = fixture.DiscoveryList(href=AUTH_URL)
|
||||
|
||||
|
||||
def to_unicode_dict(catalog_dict):
|
||||
"""Converts dict to unicode dict"""
|
||||
|
||||
if isinstance(catalog_dict, dict):
|
||||
return {to_unicode_dict(key): to_unicode_dict(value)
|
||||
for key, value in catalog_dict.items()}
|
||||
elif isinstance(catalog_dict, list):
|
||||
return [to_unicode_dict(element) for element in catalog_dict]
|
||||
elif isinstance(catalog_dict, str):
|
||||
return catalog_dict + u""
|
||||
else:
|
||||
return catalog_dict
|
||||
|
||||
|
||||
class FakeStdout(object):
|
||||
|
||||
def __init__(self):
|
||||
|
@ -1,24 +0,0 @@
|
||||
# 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 testtools
|
||||
|
||||
from manilaclient import utils
|
||||
|
||||
|
||||
class TestCommonUtils(testtools.TestCase):
|
||||
|
||||
def test_unicode_key_value_to_string(self):
|
||||
src = {u'key': u'\u70fd\u7231\u5a77'}
|
||||
# u'xxxx' in PY3 is str, we will not get extra 'u' from cli
|
||||
# output in PY3
|
||||
self.assertEqual(src, utils.unicode_key_value_to_string(src))
|
@ -47,22 +47,6 @@ def get_function_name(func):
|
||||
return "%s.%s" % (func.__module__, func.__qualname__)
|
||||
|
||||
|
||||
def _encode(src):
|
||||
"""remove extra 'u' in PY2."""
|
||||
return src
|
||||
|
||||
|
||||
def unicode_key_value_to_string(src):
|
||||
"""Recursively converts dictionary keys to strings."""
|
||||
if isinstance(src, dict):
|
||||
return dict((_encode(k),
|
||||
_encode(unicode_key_value_to_string(v)))
|
||||
for k, v in src.items())
|
||||
if isinstance(src, list):
|
||||
return [unicode_key_value_to_string(l) for l in src]
|
||||
return _encode(src)
|
||||
|
||||
|
||||
def safe_urlencode(params_dict):
|
||||
"""Workaround incompatible change to urllib.parse
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user