Mercurial > libervia-backend
annotate sat/tools/common/uri.py @ 2669:bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
class_map was so far used as a global value, but in some cases, several XMLUI managers may be used (it's the case in Cagou's remote controllers plugins, template_xmlui is used to manage remotes).
This patch set class_map as a create argument, this way it can be set with partial, and several XMLUI managers can be instanced at the same time.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 31 Aug 2018 16:03:12 +0200 |
parents | 56f94936df1e |
children | 003b8b4b56a7 |
rev | line source |
---|---|
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT: a jabber client |
2483 | 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ XMPP uri parsing tools """ |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 import urlparse |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 import urllib |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 # FIXME: basic implementation, need to follow RFC 5122 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
27 |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 def parseXMPPUri(uri): |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 """Parse an XMPP uri and return a dict with various information |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 @param uri(unicode): uri to parse |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 @return dict(unicode, unicode): data depending of the URI where key can be: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 type: one of ("pubsub", TODO) |
2222
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
34 type is always present |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 sub_type: can be: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 - microblog |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 only used for pubsub for now |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 path: XMPP path (jid of the service or entity) |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 node: node used |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 id: id of the element (item for pubsub) |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 @raise ValueError: the scheme is not xmpp |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
43 uri_split = urlparse.urlsplit(uri.encode("utf-8")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
44 if uri_split.scheme != "xmpp": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
45 raise ValueError(u"this is not a XMPP URI") |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
2229
761fa220a717
tools (common/uri): minor comment update
Goffi <goffi@goffi.org>
parents:
2222
diff
changeset
|
47 # XXX: we don't use jid.JID for path as it can be used both in backend and frontend |
761fa220a717
tools (common/uri): minor comment update
Goffi <goffi@goffi.org>
parents:
2222
diff
changeset
|
48 # which may use different JID classes |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
49 data = {u"path": urllib.unquote(uri_split.path).decode("utf-8")} |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 query_end = uri_split.query.find(";") |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 query_type = uri_split.query[:query_end] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 if query_end == -1 or "=" in query_type: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 raise ValueError("no query type, invalid XMPP URI") |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 pairs = urlparse.parse_qs(uri_split.geturl()) |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 for k, v in pairs.items(): |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 if len(v) != 1: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 raise NotImplementedError(u"multiple values not managed") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
60 if k in ("path", "type", "sub_type"): |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 raise NotImplementedError(u"reserved key used in URI, this is not supported") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 data[k.decode("utf-8")] = urllib.unquote(v[0]).decode("utf-8") |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 if query_type: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
65 data[u"type"] = query_type.decode("utf-8") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 elif u"node" in data: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 data[u"type"] = u"pubsub" |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
69 data[u"type"] = "" |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 if u"node" in data: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 if data[u"node"].startswith(u"urn:xmpp:microblog:"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 data[u"sub_type"] = "microblog" |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 return data |
2222
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
76 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 |
2222
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
78 def addPairs(uri, pairs): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 for k, v in pairs.iteritems(): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 uri.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
81 u";" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
82 + urllib.quote_plus(k.encode("utf-8")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
83 + u"=" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
84 + urllib.quote_plus(v.encode("utf-8")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
85 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
86 |
2222
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
87 |
2242
e5e54ff0b775
core (tools/common/uri): don't display finale "?" if no extra data is specified
Goffi <goffi@goffi.org>
parents:
2229
diff
changeset
|
88 def buildXMPPUri(type_, **kwargs): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
89 uri = [u"xmpp:"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 subtype = kwargs.pop("subtype", None) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 path = kwargs.pop("path") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
92 uri.append(urllib.quote_plus(path.encode("utf-8")).replace(u"%40", "@")) |
2222
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
93 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 if type_ == u"pubsub": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
95 if subtype == "microblog" and not kwargs.get("node"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 kwargs[u"node"] = "urn:xmpp:microblog:0" |
2242
e5e54ff0b775
core (tools/common/uri): don't display finale "?" if no extra data is specified
Goffi <goffi@goffi.org>
parents:
2229
diff
changeset
|
97 if kwargs: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 uri.append(u"?") |
2242
e5e54ff0b775
core (tools/common/uri): don't display finale "?" if no extra data is specified
Goffi <goffi@goffi.org>
parents:
2229
diff
changeset
|
99 addPairs(uri, kwargs) |
2222
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
100 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 raise NotImplementedError(u"{type_} URI are not handled yet".format(type_=type_)) |
2222
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
102 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 return u"".join(uri) |