annotate frontends/src/primitivus/progress.py @ 287:2720536b5a22

core: added plugin dependency management
author Goffi <goffi@goffi.org>
date Sun, 06 Feb 2011 22:41:32 +0100
parents b1794cbb88e5
children cf005701624b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
3
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Primitivus: a SAT frontend
228
b1794cbb88e5 2011 copyright upgrade
Goffi <goffi@goffi.org>
parents: 225
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
7
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
12
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
17
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
21
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import urwid
222
3198bfd66daa primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents: 180
diff changeset
23 from urwid_satext import sat_widgets
225
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
24 from sat.tools.jid import JID
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
25
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
26
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
27 class Progress(urwid.WidgetWrap):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
28
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
29 def __init__(self, host):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
30 self.host = host
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
31 self.progress_list = urwid.SimpleListWalker([])
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self.progress_dict = {}
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
33 listbox = urwid.ListBox(self.progress_list)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
34 buttons = []
222
3198bfd66daa primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents: 180
diff changeset
35 buttons.append(sat_widgets.CustomButton(_('Clear progress list'), self.__onClear))
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
36 max_len = max([button.getSize() for button in buttons])
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
37 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
38 main_wid = sat_widgets.FocusFrame(listbox, footer=buttons_wid)
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
39 urwid.WidgetWrap.__init__(self, main_wid)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
40
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def addProgress(self, id, message):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
42 mess_wid = urwid.Text(message)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
43 progr_wid = urwid.ProgressBar('progress_normal', 'progress_complete')
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
44 column = urwid.Columns([mess_wid, progr_wid])
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self.progress_dict[id] = {'full':column,'progress':progr_wid,'state':'init'}
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self.progress_list.append(column)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self.progressCB(self.host.loop, (id, message))
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
48
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def progressCB(self, loop, data):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
50 id, message = data
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
51 data = self.host.bridge.getProgress(id)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
52 pbar = self.progress_dict[id]['progress']
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
53 if data:
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
54 if self.progress_dict[id]['state'] == 'init':
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
55 #first answer, we must construct the bar
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.progress_dict[id]['state'] = 'progress'
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
57 pbar.done = float(data['size'])
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
58
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
59 pbar.set_completion(float(data['position']))
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.updateNotBar()
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
61 else:
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
62 if self.progress_dict[id]['state'] == 'progress':
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
63 self.progress_dict[id]['state'] = 'done'
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
64 pbar.set_completion(pbar.done)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.updateNotBar()
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
66 return
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
67
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
68 loop.set_alarm_in(1,self.progressCB, (id, message))
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
69
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def __removeBar(self, id):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
71 wid = self.progress_dict[id]['full']
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.progress_list.remove(wid)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
73 del(self.progress_dict[id])
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
74
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
75 def __onClear(self, button):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
76 to_remove = []
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
77 for id in self.progress_dict:
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
78 if self.progress_dict[id]['state'] == 'done':
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
79 to_remove.append(id)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
80 for id in to_remove:
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.__removeBar(id)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
82 self.updateNotBar()
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
83
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
84 def updateNotBar(self):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if not self.progress_dict:
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.host.setProgress(None)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
87 return
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
88 progress = 0
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
89 nb_bars = 0
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
90 for id in self.progress_dict:
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
91 pbar = self.progress_dict[id]['progress']
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
92 progress += pbar.current/pbar.done*100
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
93 nb_bars+=1
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
94 av_progress = progress/float(nb_bars)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
95 self.host.setProgress(av_progress)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
96