annotate frontends/src/primitivus/progress.py @ 640:8211b462af6b

Added Soul to contributors
author Goffi <goffi@goffi.org>
date Sun, 08 Sep 2013 19:18:46 +0200
parents 84a6e83157c2
children bfabeedbf32e
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
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
4 # Primitivus: a SAT frontend
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 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
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
20 import urwid
222
3198bfd66daa primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents: 180
diff changeset
21 from urwid_satext import sat_widgets
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
22
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
23
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
24 class Progress(urwid.WidgetWrap):
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 def __init__(self, host):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
27 self.host = host
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
28 self.progress_list = urwid.SimpleListWalker([])
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
29 self.progress_dict = {}
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
30 listbox = urwid.ListBox(self.progress_list)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
31 buttons = []
222
3198bfd66daa primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents: 180
diff changeset
32 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
33 max_len = max([button.getSize() for button in buttons])
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
34 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
35 main_wid = sat_widgets.FocusFrame(listbox, footer=buttons_wid)
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
36 urwid.WidgetWrap.__init__(self, main_wid)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
37
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
38 def addProgress(self, progress_id, message):
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
39 profile = self.host.profile # TODO: manage multiple profiles
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
40 mess_wid = urwid.Text(message)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
41 progr_wid = urwid.ProgressBar('progress_normal', 'progress_complete')
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
42 column = urwid.Columns([mess_wid, progr_wid])
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
43 self.progress_dict[(progress_id, profile)] = {'full':column,'progress':progr_wid,'state':'init'}
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.progress_list.append(column)
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
45 self.progressCB(self.host.loop, (progress_id, message, profile))
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
46
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def progressCB(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
48 progress_id, message, profile = data
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
49 data = self.host.bridge.getProgress(progress_id, profile)
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
50 pbar = self.progress_dict[(progress_id, profile)]['progress'] #FIXME: must manage profiles
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
51 if data:
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
52 if self.progress_dict[(progress_id, profile)]['state'] == 'init':
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
53 #first answer, we must construct the bar
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
54 self.progress_dict[(progress_id, profile)]['state'] = 'progress'
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
55 pbar.done = float(data['size'])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
56
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
57 pbar.set_completion(float(data['position']))
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.updateNotBar()
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
59 else:
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
60 if self.progress_dict[(progress_id, profile)]['state'] == 'progress':
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
61 self.progress_dict[(progress_id, profile)]['state'] = 'done'
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
62 pbar.set_completion(pbar.done)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
63 self.updateNotBar()
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
64 return
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
65
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
66 loop.set_alarm_in(1,self.progressCB, (progress_id, message, profile))
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
67
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
68 def __removeBar(self, progress_id, profile):
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
69 wid = self.progress_dict[(progress_id, profile)]['full']
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.progress_list.remove(wid)
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
71 del(self.progress_dict[(progress_id, profile)])
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
72
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def __onClear(self, button):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
74 to_remove = []
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
75 for progress_id, profile in self.progress_dict:
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
76 if self.progress_dict[(progress_id, profile)]['state'] == 'done':
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
77 to_remove.append((progress_id, profile))
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
78 for progress_id, profile in to_remove:
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
79 self.__removeBar(progress_id, profile)
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.updateNotBar()
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
81
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def updateNotBar(self):
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
83 if not self.progress_dict:
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
84 self.host.setProgress(None)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
85 return
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
86 progress = 0
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
87 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
88 for progress_id, profile in self.progress_dict:
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
89 pbar = self.progress_dict[(progress_id, profile)]['progress']
180
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
90 progress += pbar.current/pbar.done*100
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
91 nb_bars+=1
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
92 av_progress = progress/float(nb_bars)
fdb961f27ae9 Primitivus: file sending and progress management
Goffi <goffi@goffi.org>
parents:
diff changeset
93 self.host.setProgress(av_progress)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
94