comparison src/tools/common/uri.py @ 2222:bdc64c487e21

tools/common (uri): added a method to build URI
author Goffi <goffi@goffi.org>
date Sun, 16 Apr 2017 17:55:10 +0200
parents 7e06eafef409
children 761fa220a717
comparison
equal deleted inserted replaced
2221:a6c9bc4d1de0 2222:bdc64c487e21
28 """Parse an XMPP uri and return a dict with various information 28 """Parse an XMPP uri and return a dict with various information
29 29
30 @param uri(unicode): uri to parse 30 @param uri(unicode): uri to parse
31 @return dict(unicode, unicode): data depending of the URI where key can be: 31 @return dict(unicode, unicode): data depending of the URI where key can be:
32 type: one of ("pubsub", TODO) 32 type: one of ("pubsub", TODO)
33 type is always present
33 sub_type: can be: 34 sub_type: can be:
34 - microblog 35 - microblog
35 only used for pubsub for now 36 only used for pubsub for now
36 path: XMPP path (jid of the service or entity) 37 path: XMPP path (jid of the service or entity)
37 node: node used 38 node: node used
67 if u'node' in data: 68 if u'node' in data:
68 if data[u'node'].startswith(u'urn:xmpp:microblog:'): 69 if data[u'node'].startswith(u'urn:xmpp:microblog:'):
69 data[u'sub_type'] = 'microblog' 70 data[u'sub_type'] = 'microblog'
70 71
71 return data 72 return data
73
74 def addPairs(uri, pairs):
75 for k,v in pairs.iteritems():
76 uri.append(u';' + urllib.quote_plus(k) + u'=' + urllib.quote_plus(v))
77
78 def buildXMPPUri(type, **kwargs):
79 uri = [u'xmpp:']
80 subtype = kwargs.pop('subtype', None)
81 path = kwargs.pop('path')
82 uri.append(urllib.quote_plus(path).replace(u'%40', '@'))
83
84 if type == u'pubsub':
85 if subtype == 'microblog' and not 'node' in kwargs:
86 kwargs[u'node'] = 'urn:xmpp:microblog:0'
87 uri.append(u'?')
88 addPairs(uri, kwargs)
89 else:
90 raise NotImplementedError(u'{type} URI are not handled yet'.format(type=type))
91
92 return u''.join(uri)