Mercurial > libervia-web
comparison libervia/server/utils.py @ 1124:28e3eb3bb217
files reorganisation and installation rework:
- files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory)
- VERSION file is now used, as for other SàT projects
- replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly
- removed check for data_dir if it's empty
- installation tested working in virtual env
- libervia launching script is now in bin/libervia
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 25 Aug 2018 17:59:48 +0200 |
parents | src/server/utils.py@cdd389ef97bc |
children | 2af117bfe6cc |
comparison
equal
deleted
inserted
replaced
1123:63a4b8fe9782 | 1124:28e3eb3bb217 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Libervia: a Salut à Toi frontend | |
5 # Copyright (C) 2011-2018 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 sat.core.i18n import _ | |
20 from twisted.internet import reactor | |
21 from twisted.internet import defer | |
22 from sat.core import exceptions | |
23 from sat.core.log import getLogger | |
24 import urllib | |
25 | |
26 log = getLogger(__name__) | |
27 | |
28 | |
29 def quote(value, safe="@"): | |
30 """shortcut to quote an unicode value for URL""" | |
31 return urllib.quote(value.encode("utf-8"), 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[u"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 == u"started": | |
56 pass | |
57 elif name == u"finished": | |
58 handler_data[u"deferred"].callback(data) | |
59 handler_data[u"instance"].unregister_handler() | |
60 elif name == u"error": | |
61 handler_data[u"deferred"].errback(Exception(data)) | |
62 handler_data[u"instance"].unregister_handler() | |
63 else: | |
64 log.error(u"unexpected signal: {name}".format(name=name)) | |
65 | |
66 def _timeout(self): | |
67 log.warning( | |
68 _( | |
69 u"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 _(u"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 progressStarted signal is received | |
91 @param finished_cb(callable, None): method to call when progressFinished signal is received | |
92 @param error_cb(callable, None): method to call when progressError 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 u"There is already one handler for this progression" | |
104 ) | |
105 handler_data[u"instance"] = self | |
106 deferred = handler_data[u"deferred"] = defer.Deferred() | |
107 handler_data[u"started"] = started_cb | |
108 handler_data[u"finished"] = finished_cb | |
109 handler_data[u"error"] = error_cb | |
110 handler_data[u"timeout"] = reactor.callLater(timeout, self._timeout) | |
111 return deferred | |
112 | |
113 | |
114 class SubPage(unicode): | |
115 """use to mark subpages when generating a page path""" |