annotate libervia/server/utils.py @ 1169:97e850e6fae9

pages (i18n): if locale is not specified, "accept-language" header is used to try to determine the best one
author Goffi <goffi@goffi.org>
date Fri, 12 Apr 2019 14:08:02 +0200
parents 2af117bfe6cc
children b2d067339de3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
3
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Libervia: a Salut à Toi frontend
1144
2af117bfe6cc dates update
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
5 # Copyright (C) 2011-2019 Jérôme Poisson <goffi@goffi.org>
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
6
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
11
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
16
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
19 from sat.core.i18n import _
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
20 from twisted.internet import reactor
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
21 from twisted.internet import defer
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
22 from sat.core import exceptions
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
23 from sat.core.log import getLogger
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import urllib
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
25
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
26 log = getLogger(__name__)
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
27
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
28
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
29 def quote(value, safe="@"):
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff changeset
30 """shortcut to quote an unicode value for URL"""
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
31 return urllib.quote(value.encode("utf-8"), safe=safe)
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
32
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
33
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
34 class ProgressHandler(object):
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
35 """class to help the management of progressions"""
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
36
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
37 handlers = {}
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
38
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
39 def __init__(self, host, progress_id, profile):
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
40 self.host = host
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
41 self.progress_id = progress_id
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
42 self.profile = profile
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
43
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
44 @classmethod
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
45 def _signal(cls, name, progress_id, data, profile):
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
46 handlers = cls.handlers
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
47 if profile in handlers and progress_id in handlers[profile]:
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
48 handler_data = handlers[profile][progress_id]
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
49 timeout = handler_data[u"timeout"]
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
50 if timeout.active():
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
51 timeout.cancel()
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
52 cb = handler_data[name]
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
53 if cb is not None:
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
54 cb(data)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
55 if name == u"started":
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
56 pass
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
57 elif name == u"finished":
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
58 handler_data[u"deferred"].callback(data)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
59 handler_data[u"instance"].unregister_handler()
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
60 elif name == u"error":
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
61 handler_data[u"deferred"].errback(Exception(data))
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
62 handler_data[u"instance"].unregister_handler()
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1063
diff changeset
63 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
64 log.error(u"unexpected signal: {name}".format(name=name))
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
65
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
66 def _timeout(self):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
67 log.warning(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
68 _(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
69 u"No progress received, cancelling handler: {progress_id} [{profile}]"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
70 ).format(progress_id=self.progress_id, profile=self.profile)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
71 )
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
72
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
73 def unregister_handler(self):
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
74 """remove a previously registered handler"""
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
75 try:
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
76 del self.handlers[self.profile][self.progress_id]
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
77 except KeyError:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
78 log.warning(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
79 _(u"Trying to remove unknown handler: {progress_id} [{profile}]").format(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
80 progress_id=self.progress_id, profile=self.profile
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
81 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
82 )
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
83 else:
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
84 if not self.handlers[self.profile]:
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
85 self.handlers[self.profile]
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
86
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
87 def register(self, started_cb=None, finished_cb=None, error_cb=None, timeout=30):
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
88 """register the signals to handle progression
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
89
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
90 @param started_cb(callable, None): method to call when progressStarted signal is received
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
91 @param finished_cb(callable, None): method to call when progressFinished signal is received
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
92 @param error_cb(callable, None): method to call when progressError signal is received
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
93 @param timeout(int): progress time out
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
94 if nothing happen in this progression during this delay,
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
95 an exception is raised
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
96 @return (D(dict[unicode,unicode])): a deferred called when progression is finished
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
97 """
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
98 handler_data = self.handlers.setdefault(self.profile, {}).setdefault(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
99 self.progress_id, {}
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
100 )
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
101 if handler_data:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
102 raise exceptions.ConflictError(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
103 u"There is already one handler for this progression"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
104 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
105 handler_data[u"instance"] = self
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
106 deferred = handler_data[u"deferred"] = defer.Deferred()
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
107 handler_data[u"started"] = started_cb
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
108 handler_data[u"finished"] = finished_cb
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
109 handler_data[u"error"] = error_cb
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1092
diff changeset
110 handler_data[u"timeout"] = reactor.callLater(timeout, self._timeout)
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
111 return deferred
1092
63ed5f6bd4eb pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
112
63ed5f6bd4eb pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
113
63ed5f6bd4eb pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
114 class SubPage(unicode):
63ed5f6bd4eb pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
115 """use to mark subpages when generating a page path"""