tools/common (uri): added a method to build URI

This commit is contained in:
Goffi 2017-04-16 17:55:10 +02:00
commit 9d60e145c0

View file

@ -30,6 +30,7 @@ def parseXMPPUri(uri):
@param uri(unicode): uri to parse
@return dict(unicode, unicode): data depending of the URI where key can be:
type: one of ("pubsub", TODO)
type is always present
sub_type: can be:
- microblog
only used for pubsub for now
@ -69,3 +70,23 @@ def parseXMPPUri(uri):
data[u'sub_type'] = 'microblog'
return data
def addPairs(uri, pairs):
for k,v in pairs.iteritems():
uri.append(u';' + urllib.quote_plus(k) + u'=' + urllib.quote_plus(v))
def buildXMPPUri(type, **kwargs):
uri = [u'xmpp:']
subtype = kwargs.pop('subtype', None)
path = kwargs.pop('path')
uri.append(urllib.quote_plus(path).replace(u'%40', '@'))
if type == u'pubsub':
if subtype == 'microblog' and not 'node' in kwargs:
kwargs[u'node'] = 'urn:xmpp:microblog:0'
uri.append(u'?')
addPairs(uri, kwargs)
else:
raise NotImplementedError(u'{type} URI are not handled yet'.format(type=type))
return u''.join(uri)