diff 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
line wrap: on
line diff
--- a/src/tools/common/uri.py	Mon Apr 03 00:23:01 2017 +0200
+++ b/src/tools/common/uri.py	Sun Apr 16 17:55:10 2017 +0200
@@ -30,6 +30,7 @@
     @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 @@
             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)