Mercurial > libervia-web
comparison libervia/web/server/utils.py @ 1518:eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 16:49:28 +0200 |
parents | libervia/server/utils.py@106bae41f5c8 |
children |
comparison
equal
deleted
inserted
replaced
1517:b8ed9726525b | 1518:eb00d593801d |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # Libervia: a Salut à Toi frontend | |
5 # Copyright (C) 2011-2021 Jérôme Poisson <goffi@goffi.org> | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 from libervia.backend.core.i18n import _ | |
20 from twisted.internet import reactor | |
21 from twisted.internet import defer | |
22 from libervia.backend.core import exceptions | |
23 from libervia.backend.core.log import getLogger | |
24 import urllib.request, urllib.parse, urllib.error | |
25 | |
26 log = getLogger(__name__) | |
27 | |
28 | |
29 def quote(value, safe="@"): | |
30 """shortcut to quote an unicode value for URL""" | |
31 return urllib.parse.quote(value, safe=safe) | |
32 | |
33 | |
34 class ProgressHandler(object): | |
35 """class to help the management of progressions""" | |
36 | |
37 handlers = {} | |
38 | |
39 def __init__(self, host, progress_id, profile): | |
40 self.host = host | |
41 self.progress_id = progress_id | |
42 self.profile = profile | |
43 | |
44 @classmethod | |
45 def _signal(cls, name, progress_id, data, profile): | |
46 handlers = cls.handlers | |
47 if profile in handlers and progress_id in handlers[profile]: | |
48 handler_data = handlers[profile][progress_id] | |
49 timeout = handler_data["timeout"] | |
50 if timeout.active(): | |
51 timeout.cancel() | |
52 cb = handler_data[name] | |
53 if cb is not None: | |
54 cb(data) | |
55 if name == "started": | |
56 pass | |
57 elif name == "finished": | |
58 handler_data["deferred"].callback(data) | |
59 handler_data["instance"].unregister_handler() | |
60 elif name == "error": | |
61 handler_data["deferred"].errback(Exception(data)) | |
62 handler_data["instance"].unregister_handler() | |
63 else: | |
64 log.error("unexpected signal: {name}".format(name=name)) | |
65 | |
66 def _timeout(self): | |
67 log.warning( | |
68 _( | |
69 "No progress received, cancelling handler: {progress_id} [{profile}]" | |
70 ).format(progress_id=self.progress_id, profile=self.profile) | |
71 ) | |
72 | |
73 def unregister_handler(self): | |
74 """remove a previously registered handler""" | |
75 try: | |
76 del self.handlers[self.profile][self.progress_id] | |
77 except KeyError: | |
78 log.warning( | |
79 _("Trying to remove unknown handler: {progress_id} [{profile}]").format( | |
80 progress_id=self.progress_id, profile=self.profile | |
81 ) | |
82 ) | |
83 else: | |
84 if not self.handlers[self.profile]: | |
85 self.handlers[self.profile] | |
86 | |
87 def register(self, started_cb=None, finished_cb=None, error_cb=None, timeout=30): | |
88 """register the signals to handle progression | |
89 | |
90 @param started_cb(callable, None): method to call when progress_started signal is received | |
91 @param finished_cb(callable, None): method to call when progress_finished signal is received | |
92 @param error_cb(callable, None): method to call when progress_error signal is received | |
93 @param timeout(int): progress time out | |
94 if nothing happen in this progression during this delay, | |
95 an exception is raised | |
96 @return (D(dict[unicode,unicode])): a deferred called when progression is finished | |
97 """ | |
98 handler_data = self.handlers.setdefault(self.profile, {}).setdefault( | |
99 self.progress_id, {} | |
100 ) | |
101 if handler_data: | |
102 raise exceptions.ConflictError( | |
103 "There is already one handler for this progression" | |
104 ) | |
105 handler_data["instance"] = self | |
106 deferred = handler_data["deferred"] = defer.Deferred() | |
107 handler_data["started"] = started_cb | |
108 handler_data["finished"] = finished_cb | |
109 handler_data["error"] = error_cb | |
110 handler_data["timeout"] = reactor.callLater(timeout, self._timeout) | |
111 return deferred | |
112 | |
113 | |
114 class SubPage(str): | |
115 """use to mark subpages when generating a page path""" |