bitbake: fetch2.URI: add support for query parameters

This change introduces the .query property of the URI class. It is a
read/write dict of the parameters supplied in the query string of the
URI. E.g.:

  http://example.com/?foo=bar => .query = {'foo': 'bar'}

(Bitbake rev: 1cb2b3c458c8c5521591d2c8f2e0058143fc77bb)

Signed-off-by: Olof Johansson <olof.johansson@axis.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Olof Johansson
2014-01-20 12:03:22 +01:00
committed by Richard Purdie
parent 64fdd3abbb
commit aca2d14e93
2 changed files with 53 additions and 15 deletions

View File

@@ -162,6 +162,7 @@ class URI(object):
* path_quoted (read/write) * path_quoted (read/write)
A URI quoted version of path A URI quoted version of path
* params (dict) (read/write) * params (dict) (read/write)
* query (dict) (read/write)
* relative (bool) (read only) * relative (bool) (read only)
True if this is a "relative URI", (e.g. file:foo.diff) True if this is a "relative URI", (e.g. file:foo.diff)
@@ -201,6 +202,7 @@ class URI(object):
self.port = None self.port = None
self._path = '' self._path = ''
self.params = {} self.params = {}
self.query = {}
self.relative = False self.relative = False
if not uri: if not uri:
@@ -253,36 +255,42 @@ class URI(object):
self.path = urllib.unquote(path) self.path = urllib.unquote(path)
if param_str: if param_str:
self.params = self._param_dict(param_str) self.params = self._param_str_split(param_str, ";")
if urlp.query:
self.query = self._param_str_split(urlp.query, "&")
def __str__(self): def __str__(self):
userinfo = self.userinfo userinfo = self.userinfo
if userinfo: if userinfo:
userinfo += '@' userinfo += '@'
return "%s:%s%s%s%s%s" % ( return "%s:%s%s%s%s%s%s" % (
self.scheme, self.scheme,
'' if self.relative else '//', '' if self.relative else '//',
userinfo, userinfo,
self.hostport, self.hostport,
self.path_quoted, self.path_quoted,
self._param_str) self._query_str(),
self._param_str())
@property
def _param_str(self): def _param_str(self):
ret = '' return (
for key, val in self.params.items(): ''.join([';', self._param_str_join(self.params, ";")])
ret += ";%s=%s" % (key, val) if self.params else '')
def _query_str(self):
return (
''.join(['?', self._param_str_join(self.query, "&")])
if self.query else '')
def _param_str_split(self, string, elmdelim, kvdelim="="):
ret = {}
for k, v in [x.split(kvdelim, 1) for x in string.split(elmdelim)]:
ret[k] = v
return ret return ret
def _param_dict(self, param_str): def _param_str_join(self, dict_, elmdelim, kvdelim="="):
parm = {} return elmdelim.join([kvdelim.join([k, v]) for k, v in dict_.items()])
for keyval in param_str.split(";"):
key, val = keyval.split("=", 1)
parm[key] = val
return parm
@property @property
def hostport(self): def hostport(self):

View File

@@ -39,6 +39,7 @@ class URITest(unittest.TestCase):
'username': '', 'username': '',
'password': '', 'password': '',
'params': {}, 'params': {},
'query': {},
'relative': False 'relative': False
}, },
"http://www.google.com/index.html;param1=value1" : { "http://www.google.com/index.html;param1=value1" : {
@@ -54,6 +55,23 @@ class URITest(unittest.TestCase):
'params': { 'params': {
'param1': 'value1' 'param1': 'value1'
}, },
'query': {},
'relative': False
},
"http://www.example.org/index.html?param1=value1" : {
'uri': 'http://www.example.org/index.html?param1=value1',
'scheme': 'http',
'hostname': 'www.example.org',
'port': None,
'hostport': 'www.example.org',
'path': '/index.html',
'userinfo': '',
'username': '',
'password': '',
'params': {},
'query': {
'param1': 'value1'
},
'relative': False 'relative': False
}, },
"http://www.example.com:8080/index.html" : { "http://www.example.com:8080/index.html" : {
@@ -67,6 +85,7 @@ class URITest(unittest.TestCase):
'username': '', 'username': '',
'password': '', 'password': '',
'params': {}, 'params': {},
'query': {},
'relative': False 'relative': False
}, },
"cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : { "cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : {
@@ -82,6 +101,7 @@ class URITest(unittest.TestCase):
'params': { 'params': {
'module': 'familiar/dist/ipkg' 'module': 'familiar/dist/ipkg'
}, },
'query': {},
'relative': False 'relative': False
}, },
"cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg": { "cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg": {
@@ -98,6 +118,7 @@ class URITest(unittest.TestCase):
'tag': 'V0-99-81', 'tag': 'V0-99-81',
'module': 'familiar/dist/ipkg' 'module': 'familiar/dist/ipkg'
}, },
'query': {},
'relative': False 'relative': False
}, },
"file://example.diff": { # NOTE: Not RFC compliant! "file://example.diff": { # NOTE: Not RFC compliant!
@@ -111,6 +132,7 @@ class URITest(unittest.TestCase):
'username': '', 'username': '',
'password': '', 'password': '',
'params': {}, 'params': {},
'query': {},
'relative': True 'relative': True
}, },
"file:example.diff": { # NOTE: RFC compliant version of the former "file:example.diff": { # NOTE: RFC compliant version of the former
@@ -125,6 +147,7 @@ class URITest(unittest.TestCase):
'username': '', 'username': '',
'password': '', 'password': '',
'params': {}, 'params': {},
'query': {},
'relative': True 'relative': True
}, },
"file:///tmp/example.diff": { "file:///tmp/example.diff": {
@@ -139,6 +162,7 @@ class URITest(unittest.TestCase):
'username': '', 'username': '',
'password': '', 'password': '',
'params': {}, 'params': {},
'query': {},
'relative': False 'relative': False
}, },
"git:///path/example.git": { "git:///path/example.git": {
@@ -153,6 +177,7 @@ class URITest(unittest.TestCase):
'username': '', 'username': '',
'password': '', 'password': '',
'params': {}, 'params': {},
'query': {},
'relative': False 'relative': False
}, },
"git:path/example.git": { "git:path/example.git": {
@@ -167,6 +192,7 @@ class URITest(unittest.TestCase):
'username': '', 'username': '',
'password': '', 'password': '',
'params': {}, 'params': {},
'query': {},
'relative': True 'relative': True
}, },
"git://example.net/path/example.git": { "git://example.net/path/example.git": {
@@ -181,6 +207,7 @@ class URITest(unittest.TestCase):
'username': '', 'username': '',
'password': '', 'password': '',
'params': {}, 'params': {},
'query': {},
'relative': False 'relative': False
} }
} }
@@ -243,6 +270,9 @@ class URITest(unittest.TestCase):
uri.params = test['params'] uri.params = test['params']
self.assertEqual(uri.params, test['params']) self.assertEqual(uri.params, test['params'])
uri.query = test['query']
self.assertEqual(uri.query, test['query'])
self.assertEqual(str(uri), test['uri']) self.assertEqual(str(uri), test['uri'])
uri.params = {} uri.params = {}