Add urlopen/URLError/pathname2url in urlutils

They are used by heatclent and other projects.

Close-Bug #1242453

Change-Id: I0ff1afbafa78b2e91b7c0cf833f449c31debbe16
This commit is contained in:
Kui Shi
2013-10-21 02:54:00 +08:00
parent 5c55c5c933
commit 71743d9c2e
2 changed files with 28 additions and 0 deletions

View File

@@ -24,7 +24,9 @@ import six
if six.PY3:
# python3
import urllib.error
import urllib.parse
import urllib.request
urlencode = urllib.parse.urlencode
urljoin = urllib.parse.urljoin
@@ -34,9 +36,14 @@ if six.PY3:
urlparse = urllib.parse.urlparse
urlsplit = urllib.parse.urlsplit
urlunsplit = urllib.parse.urlunsplit
urlopen = urllib.request.urlopen
URLError = urllib.error.URLError
pathname2url = urllib.request.pathname2url
else:
# python2
import urllib
import urllib2
import urlparse
urlencode = urllib.urlencode
@@ -49,3 +56,7 @@ else:
urlparse = parse.urlparse
urlsplit = parse.urlsplit
urlunsplit = parse.urlunsplit
urlopen = urllib2.urlopen
URLError = urllib2.URLError
pathname2url = urllib.pathname2url

View File

@@ -16,6 +16,8 @@
# under the License.
#
import tempfile
from openstack.common.py3kcompat import urlutils
from openstack.common import test
@@ -56,3 +58,18 @@ class CompatTestCase(test.BaseTestCase):
url = "http://www.yahoo.com"
result = urlutils.urlunsplit(urlutils.urlsplit(url))
self.assertEqual(result, 'http://www.yahoo.com')
def test_urlopen(self):
tmp = tempfile.NamedTemporaryFile()
url = 'file://' + tmp.name
result = urlutils.urlopen(url)
self.assertEqual(result.url, url)
tmp.close()
def test_URLError(self):
self.assertRaises(urlutils.URLError, urlutils.urlopen, 'wrong://url')
def test_pathname2url(self):
pathname = '~fake'
result = urlutils.pathname2url(pathname)
self.assertEqual(result, '%7Efake')