/
ProgressBar.py
45 lines (39 loc) · 1.57 KB
/
ProgressBar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import division
from rorn.ResponseWriter import ResponseWriter
from utils import *
inf = float('inf')
class ProgressBar:
# zeroDivZero means the progress should be 0 if amt == total == 0. It will be 100 if False. In either case, it will be infinity if amt > 0
# style is a map of percentages to styles to apply to div.progress-current; the highest percentage <= this bar's percentage will be applied
def __init__(self, label, amt, total, pcnt = None, zeroDivZero = True, style = None):
self.label = label
self.amt = amt
self.total = total
if pcnt:
self.pcnt = pcnt
elif total == 0:
self.pcnt = inf if amt > 0 else 0 if zeroDivZero else 100
else:
self.pcnt = amt / total * 100
if isinstance(style, dict):
if amt == 0 and total == 0 and None in style:
self.cls = style[None]
else:
topPcnt = maxOr(filter(lambda p: p is not None and p <= self.pcnt, style.keys()))
self.cls = style[topPcnt] if topPcnt in style else None
elif isinstance(style, str):
self.cls = style
else:
self.cls = ''
def __str__(self):
w = ResponseWriter()
if self.label:
print "%s " % self.label
print "<div class=\"task-progress-total\" style=\"position: relative; top: 5px\">"
if self.pcnt > 0:
print "<div class=\"progress-current%s\" style=\"width: %d%%;\">" % (" %s" % self.cls, min(self.pcnt, 100))
print "<span class=\"progress-percentage\">%d/%d hours (%s%%)</span>" % (self.amt, self.total, '∞' if self.pcnt == inf else "%d" % self.pcnt)
if self.pcnt > 0:
print "</div>"
print "</div>"
return w.done().replace("\n", "")