comparison sat/tools/common/uri.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
22 import urlparse 22 import urlparse
23 import urllib 23 import urllib
24 24
25 # FIXME: basic implementation, need to follow RFC 5122 25 # FIXME: basic implementation, need to follow RFC 5122
26 26
27
27 def parseXMPPUri(uri): 28 def parseXMPPUri(uri):
28 """Parse an XMPP uri and return a dict with various information 29 """Parse an XMPP uri and return a dict with various information
29 30
30 @param uri(unicode): uri to parse 31 @param uri(unicode): uri to parse
31 @return dict(unicode, unicode): data depending of the URI where key can be: 32 @return dict(unicode, unicode): data depending of the URI where key can be:
37 path: XMPP path (jid of the service or entity) 38 path: XMPP path (jid of the service or entity)
38 node: node used 39 node: node used
39 id: id of the element (item for pubsub) 40 id: id of the element (item for pubsub)
40 @raise ValueError: the scheme is not xmpp 41 @raise ValueError: the scheme is not xmpp
41 """ 42 """
42 uri_split = urlparse.urlsplit(uri.encode('utf-8')) 43 uri_split = urlparse.urlsplit(uri.encode("utf-8"))
43 if uri_split.scheme != 'xmpp': 44 if uri_split.scheme != "xmpp":
44 raise ValueError(u'this is not a XMPP URI') 45 raise ValueError(u"this is not a XMPP URI")
45 46
46 # XXX: we don't use jid.JID for path as it can be used both in backend and frontend 47 # XXX: we don't use jid.JID for path as it can be used both in backend and frontend
47 # which may use different JID classes 48 # which may use different JID classes
48 data = {u'path': urllib.unquote(uri_split.path).decode('utf-8')} 49 data = {u"path": urllib.unquote(uri_split.path).decode("utf-8")}
49 50
50 query_end = uri_split.query.find(';') 51 query_end = uri_split.query.find(";")
51 query_type = uri_split.query[:query_end] 52 query_type = uri_split.query[:query_end]
52 if query_end == -1 or '=' in query_type: 53 if query_end == -1 or "=" in query_type:
53 raise ValueError('no query type, invalid XMPP URI') 54 raise ValueError("no query type, invalid XMPP URI")
54 55
55 pairs = urlparse.parse_qs(uri_split.geturl()) 56 pairs = urlparse.parse_qs(uri_split.geturl())
56 for k, v in pairs.items(): 57 for k, v in pairs.items():
57 if len(v) != 1: 58 if len(v) != 1:
58 raise NotImplementedError(u"multiple values not managed") 59 raise NotImplementedError(u"multiple values not managed")
59 if k in ('path', 'type', 'sub_type'): 60 if k in ("path", "type", "sub_type"):
60 raise NotImplementedError(u"reserved key used in URI, this is not supported") 61 raise NotImplementedError(u"reserved key used in URI, this is not supported")
61 data[k.decode('utf-8')] = urllib.unquote(v[0]).decode('utf-8') 62 data[k.decode("utf-8")] = urllib.unquote(v[0]).decode("utf-8")
62 63
63 if query_type: 64 if query_type:
64 data[u'type'] = query_type.decode('utf-8') 65 data[u"type"] = query_type.decode("utf-8")
65 elif u'node' in data: 66 elif u"node" in data:
66 data[u'type'] = u'pubsub' 67 data[u"type"] = u"pubsub"
67 else: 68 else:
68 data[u'type'] = '' 69 data[u"type"] = ""
69 70
70 if u'node' in data: 71 if u"node" in data:
71 if data[u'node'].startswith(u'urn:xmpp:microblog:'): 72 if data[u"node"].startswith(u"urn:xmpp:microblog:"):
72 data[u'sub_type'] = 'microblog' 73 data[u"sub_type"] = "microblog"
73 74
74 return data 75 return data
75 76
77
76 def addPairs(uri, pairs): 78 def addPairs(uri, pairs):
77 for k,v in pairs.iteritems(): 79 for k, v in pairs.iteritems():
78 uri.append(u';' + urllib.quote_plus(k.encode('utf-8')) + u'=' + urllib.quote_plus(v.encode('utf-8'))) 80 uri.append(
81 u";"
82 + urllib.quote_plus(k.encode("utf-8"))
83 + u"="
84 + urllib.quote_plus(v.encode("utf-8"))
85 )
86
79 87
80 def buildXMPPUri(type_, **kwargs): 88 def buildXMPPUri(type_, **kwargs):
81 uri = [u'xmpp:'] 89 uri = [u"xmpp:"]
82 subtype = kwargs.pop('subtype', None) 90 subtype = kwargs.pop("subtype", None)
83 path = kwargs.pop('path') 91 path = kwargs.pop("path")
84 uri.append(urllib.quote_plus(path.encode('utf-8')).replace(u'%40', '@')) 92 uri.append(urllib.quote_plus(path.encode("utf-8")).replace(u"%40", "@"))
85 93
86 if type_ == u'pubsub': 94 if type_ == u"pubsub":
87 if subtype == 'microblog' and not kwargs.get('node'): 95 if subtype == "microblog" and not kwargs.get("node"):
88 kwargs[u'node'] = 'urn:xmpp:microblog:0' 96 kwargs[u"node"] = "urn:xmpp:microblog:0"
89 if kwargs: 97 if kwargs:
90 uri.append(u'?') 98 uri.append(u"?")
91 addPairs(uri, kwargs) 99 addPairs(uri, kwargs)
92 else: 100 else:
93 raise NotImplementedError(u'{type_} URI are not handled yet'.format(type_=type_)) 101 raise NotImplementedError(u"{type_} URI are not handled yet".format(type_=type_))
94 102
95 return u''.join(uri) 103 return u"".join(uri)