Use from six.moves.urllib.parse instead of urlparse

In Python 3, urlparse module is gone.
To support both Python 2 and Python 3, "six.moves.urllib.parse"
should be used instead.

Change-Id: I0adddbdea5a80606907a1b78ca1a04d15f61452b
Closes-Bug: #1403433
This commit is contained in:
li,chen 2014-12-25 09:12:01 +08:00
parent 689f020f1f
commit ced7b863e9
3 changed files with 24 additions and 2 deletions

View File

@ -25,4 +25,5 @@ Rally Specific Commandments
* [N330] - Ensure that ``dict.iteritems()`` is not used
* [N331] - Ensure that ``basestring`` is not used
* [N332] - Ensure that ``StringIO.StringIO`` is not used
* [N340] - Ensure that we are importing always ``from rally import objects``
* [N333] - Ensure that ``urlparse`` is not used
* [N340] - Ensure that we are importing always ``from rally import objects``

View File

@ -48,6 +48,7 @@ re_assert_equal_in_start_with_true_or_false = re.compile(
re_iteritems_method = re.compile(r"\.iteritems\(\)")
re_basestring_method = re.compile(r"(^|[\s,(\[=])basestring([\s,)\]]|$)")
re_StringIO_method = re.compile(r"StringIO\.StringIO\(")
re_urlparse_method = re.compile(r"(^|[\s=])urlparse\.")
def _parse_assert_mock_str(line):
@ -267,6 +268,18 @@ def check_StringIO_method(logical_line):
"rather than StringIO.StringIO.")
def check_urlparse_method(logical_line):
"""Check if urlparse is properly called for compatibility with Python 3
The correct form is six.moves.urllib.parse instead of "urlparse".
N333
"""
res = re_urlparse_method.search(logical_line)
if res:
yield (0, "N333: Use six.moves.urllib.parse rather than urlparse.")
def check_no_direct_rally_objects_import(logical_line, filename):
"""Check if rally.objects are properly imported.
@ -297,4 +310,5 @@ def factory(register):
register(check_iteritems_method)
register(check_basestring_method)
register(check_StringIO_method)
register(check_urlparse_method)
register(check_no_direct_rally_objects_import)

View File

@ -139,9 +139,16 @@ class HackingTestCase(test.TestCase):
self.assertEqual(len(list(checks.check_StringIO_method(
"StringIO.StringIO()"))), 1)
self.assertEqual(len(list(checks.check_basestring_method(
self.assertEqual(len(list(checks.check_StringIO_method(
"six.moves.StringIO()"))), 0)
def test_check_urlparse_method(self):
self.assertEqual(len(list(checks.check_urlparse_method(
"urlparse.urlparse(url)"))), 1)
self.assertEqual(len(list(checks.check_urlparse_method(
"six.moves.urllib.parse.urlparse(url)"))), 0)
def test_assert_equal_none(self):
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, None)"))), 1)