comparison libervia/tui/progress.py @ 4076:b620a8e882e1

refactoring: rename `libervia.frontends.primitivus` to `libervia.tui`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:25:25 +0200
parents libervia/frontends/primitivus/progress.py@26b7ed2817da
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4075:47401850dec6 4076:b620a8e882e1
1 #!/usr/bin/env python3
2
3
4 # Libervia TUI
5 # Copyright (C) 2009-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
20 from libervia.backend.core.i18n import _
21 import urwid
22 from urwid_satext import sat_widgets
23 from libervia.frontends.quick_frontend import quick_widgets
24
25
26 class Progress(urwid.WidgetWrap, quick_widgets.QuickWidget):
27 PROFILES_ALLOW_NONE = True
28
29 def __init__(self, host, target, profiles):
30 assert target is None and profiles is None
31 quick_widgets.QuickWidget.__init__(self, host, target)
32 self.host = host
33 self.progress_list = urwid.SimpleListWalker([])
34 self.progress_dict = {}
35 listbox = urwid.ListBox(self.progress_list)
36 buttons = []
37 buttons.append(sat_widgets.CustomButton(_("Clear progress list"), self._on_clear))
38 max_len = max([button.get_size() for button in buttons])
39 buttons_wid = urwid.GridFlow(buttons, max_len, 1, 0, "center")
40 main_wid = sat_widgets.FocusFrame(listbox, footer=buttons_wid)
41 urwid.WidgetWrap.__init__(self, main_wid)
42
43 def add(self, progress_id, message, profile):
44 mess_wid = urwid.Text(message)
45 progr_wid = urwid.ProgressBar("progress_normal", "progress_complete")
46 column = urwid.Columns([mess_wid, progr_wid])
47 self.progress_dict[(progress_id, profile)] = {
48 "full": column,
49 "progress": progr_wid,
50 "state": "init",
51 }
52 self.progress_list.append(column)
53 self.progress_cb(self.host.loop, (progress_id, message, profile))
54
55 def progress_cb(self, loop, data):
56 progress_id, message, profile = data
57 data = self.host.bridge.progress_get(progress_id, profile)
58 pbar = self.progress_dict[(progress_id, profile)]["progress"]
59 if data:
60 if self.progress_dict[(progress_id, profile)]["state"] == "init":
61 # first answer, we must construct the bar
62 self.progress_dict[(progress_id, profile)]["state"] = "progress"
63 pbar.done = float(data["size"])
64
65 pbar.set_completion(float(data["position"]))
66 self.update_not_bar()
67 else:
68 if self.progress_dict[(progress_id, profile)]["state"] == "progress":
69 self.progress_dict[(progress_id, profile)]["state"] = "done"
70 pbar.set_completion(pbar.done)
71 self.update_not_bar()
72 return
73
74 loop.set_alarm_in(0.2, self.progress_cb, (progress_id, message, profile))
75
76 def _remove_bar(self, progress_id, profile):
77 wid = self.progress_dict[(progress_id, profile)]["full"]
78 self.progress_list.remove(wid)
79 del (self.progress_dict[(progress_id, profile)])
80
81 def _on_clear(self, button):
82 to_remove = []
83 for progress_id, profile in self.progress_dict:
84 if self.progress_dict[(progress_id, profile)]["state"] == "done":
85 to_remove.append((progress_id, profile))
86 for progress_id, profile in to_remove:
87 self._remove_bar(progress_id, profile)
88 self.update_not_bar()
89
90 def update_not_bar(self):
91 if not self.progress_dict:
92 self.host.set_progress(None)
93 return
94 progress = 0
95 nb_bars = 0
96 for progress_id, profile in self.progress_dict:
97 pbar = self.progress_dict[(progress_id, profile)]["progress"]
98 progress += pbar.current / pbar.done * 100
99 nb_bars += 1
100 av_progress = progress / float(nb_bars)
101 self.host.set_progress(av_progress)