SOAdvancedDissector

SOAdvancedDissector Git Source Tree

Root/display.py

1# -*- coding: utf-8 -*-
2#
3# Copyright Grégory Soutadé
4
5# This file is part of SOAdvancedDissector
6
7# SOAdvancedDissector is free software: you can redistribute it and/or modify
8# it under the terms of the GNU 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# SOAdvancedDissector 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 General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with SOAdvancedDissector. If not, see <http://www.gnu.org/licenses/>.
19#
20
21import sys
22
23class ProgressionDisplay():
24 """Class that manage nice display
25 with progression continuous update in percent"""
26
27 def __init__(self):
28 self.reset(False)
29
30 def reset(self, printEmptyLine=False):
31 """Reset display and optionaly print an empty line"""
32 self.message = ''
33 self.target_progression = 0
34 self.cur_progression = 0
35 self.watermark = 0
36 self.cur_watermark = 0
37 if printEmptyLine:
38 print('')
39
40 def setTarget(self, message, target=0, watermark=0):
41 """New progression to display
42
43 Parameters
44 ----------
45 message : str
46 Message to display
47
48 target (Optional) : int
49 Max progression value, if not set, no percent will be displayed
50
51 watermark (Optional) : int
52 Only display message when progression >= watermark
53 """
54 self.reset()
55 self.message = message
56 self.target_progression = target
57 self.watermark = watermark
58
59 def progress(self, increment):
60 """Increment progression and display new one"""
61 self.cur_progression += increment
62 self.cur_watermark += increment
63
64 if self.cur_progression > self.target_progression:
65 self.cur_progression = self.target_progression
66
67 if self.cur_watermark < self.watermark and\
68 self.cur_progression < self.target_progression:
69 return
70
71 display = ''
72 if self.target_progression:
73 cur_percent = int((self.cur_progression*100)/self.target_progression)
74 display = '{} [{}%]'.format(self.message, cur_percent)
75 else:
76 display = '{}'.format(self.message)
77
78 sys.stdout.write('\r{}\r'.format(' ' * (len(display)+5))) # First, clear line
79 sys.stdout.write(display)
80 sys.stdout.flush()
81
82 def finish(self):
83 """End of progression, ensure we display "100%" value"""
84 if self.target_progression:
85 self.progress(self.target_progression) # Print 100%
86 print('')
87 else:
88 pass # Do nothing
89

Archive Download this file

Branches