annotate libervia/tui/progress.py @ 4326:5fd6a4dc2122

cli (output/std): use `rich` to output JSON.
author Goffi <goffi@goffi.org>
date Wed, 20 Nov 2024 11:38:44 +0100
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
3
4076
b620a8e882e1 refactoring: rename `libervia.frontends.primitivus` to `libervia.tui`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
4 # Libervia TUI
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
10 # (at your option) any later version.
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
15 # GNU Affero General Public License for more details.
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
19
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
20 from libervia.backend.core.i18n import _
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import urwid
222
3198bfd66daa primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents: 180
diff changeset
22 from urwid_satext import sat_widgets
4074
26b7ed2817da refactoring: rename `sat_frontends` to `libervia.frontends`
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
23 from libervia.frontends.quick_frontend import quick_widgets
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
24
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
25
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 811
diff changeset
26 class Progress(urwid.WidgetWrap, quick_widgets.QuickWidget):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 811
diff changeset
27 PROFILES_ALLOW_NONE = True
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
28
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 811
diff changeset
29 def __init__(self, host, target, profiles):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 811
diff changeset
30 assert target is None and profiles is None
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 811
diff changeset
31 quick_widgets.QuickWidget.__init__(self, host, target)
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self.host = host
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
33 self.progress_list = urwid.SimpleListWalker([])
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
34 self.progress_dict = {}
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
35 listbox = urwid.ListBox(self.progress_list)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
36 buttons = []
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
37 buttons.append(sat_widgets.CustomButton(_("Clear progress list"), self._on_clear))
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
38 max_len = max([button.get_size() for button in buttons])
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39 buttons_wid = urwid.GridFlow(buttons, max_len, 1, 0, "center")
222
3198bfd66daa primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents: 180
diff changeset
40 main_wid = sat_widgets.FocusFrame(listbox, footer=buttons_wid)
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
41 urwid.WidgetWrap.__init__(self, main_wid)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
42
1636
e00f450c25fc primitivus: fixed progress panel
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
43 def add(self, progress_id, message, profile):
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
44 mess_wid = urwid.Text(message)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 progr_wid = urwid.ProgressBar("progress_normal", "progress_complete")
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
46 column = urwid.Columns([mess_wid, progr_wid])
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 self.progress_dict[(progress_id, profile)] = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 "full": column,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 "progress": progr_wid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 "state": "init",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 }
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.progress_list.append(column)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
53 self.progress_cb(self.host.loop, (progress_id, message, profile))
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
54
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
55 def progress_cb(self, loop, data):
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
56 progress_id, message, profile = data
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
57 data = self.host.bridge.progress_get(progress_id, profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 pbar = self.progress_dict[(progress_id, profile)]["progress"]
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
59 if data:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 if self.progress_dict[(progress_id, profile)]["state"] == "init":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 # first answer, we must construct the bar
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 self.progress_dict[(progress_id, profile)]["state"] = "progress"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 pbar.done = float(data["size"])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
64
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 pbar.set_completion(float(data["position"]))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
66 self.update_not_bar()
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
67 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 if self.progress_dict[(progress_id, profile)]["state"] == "progress":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 self.progress_dict[(progress_id, profile)]["state"] = "done"
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
70 pbar.set_completion(pbar.done)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
71 self.update_not_bar()
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
73
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
74 loop.set_alarm_in(0.2, self.progress_cb, (progress_id, message, profile))
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
75
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
76 def _remove_bar(self, progress_id, profile):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
77 wid = self.progress_dict[(progress_id, profile)]["full"]
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
78 self.progress_list.remove(wid)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4076
diff changeset
79 del self.progress_dict[(progress_id, profile)]
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
80
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
81 def _on_clear(self, button):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 to_remove = []
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 for progress_id, profile in self.progress_dict:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 if self.progress_dict[(progress_id, profile)]["state"] == "done":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 to_remove.append((progress_id, profile))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 for progress_id, profile in to_remove:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
87 self._remove_bar(progress_id, profile)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
88 self.update_not_bar()
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
89
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
90 def update_not_bar(self):
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if not self.progress_dict:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
92 self.host.set_progress(None)
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
94 progress = 0
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
95 nb_bars = 0
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
96 for progress_id, profile in self.progress_dict:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
97 pbar = self.progress_dict[(progress_id, profile)]["progress"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98 progress += pbar.current / pbar.done * 100
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
99 nb_bars += 1
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 av_progress = progress / float(nb_bars)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
101 self.host.set_progress(av_progress)