-
Notifications
You must be signed in to change notification settings - Fork 141
/
pyozw_progressbar.py
159 lines (128 loc) · 4.14 KB
/
pyozw_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sys
import os
import time
import datetime
import io
import threading
try:
PY3 = not unicode
except NameError:
PY3 = True
TEMPLATE = (
'\r'
'{prefix} '
'{percent}% '
'{count} '
'Elapsed: 0{elapsed} '
'Remaining: {remaining} '
'{file_name}'
)
def remap(value, old_min=0, old_max=0, new_min=0, new_max=0):
old_range = old_max - old_min
new_range = new_max - new_min
return (
int((((value - old_min) * new_range) / old_range)) + new_min
)
print_lock = threading.Lock()
class ProgressBar(object):
def __init__(self):
self.stdout = sys.stdout
sys.stdout = self
self.files = []
self.count = 0
self.start = None
self.prefix = ''
self.last_line_len = 0
self.file_position = 0
def flush(self):
pass
def isatty(self):
return True
def close(self):
if self.start is not None:
print_lock.acquire()
if self.files:
self.update(50, 100, file_name=self.files[-1])
else:
self.update(50, 100, file_name='')
self.stdout.write('\n\n')
self.stdout.flush()
print_lock.release()
sys.stdout = self.stdout
def write(self, line):
if PY3:
try:
line = line.decode("utf-8")
except AttributeError:
pass
if str('Build openzwave') in line:
print_lock.acquire()
self.stdout.write('\n')
self.stdout.flush()
# try:
# self.file_position = self.stdout.tell()
# except (io.UnsupportedOperation, WindowsError):
# pass
self.prefix = 'Build OpenZWave'
self.start = time.time()
self.update(0, 0, file_name='')
print_lock.release()
elif self.start is not None:
if str('/errorReport:queue') in line:
print_lock.acquire()
files = line.split('/errorReport:queue ')[1]
self.files = [
os.path.split(f)[1] for f in files.split(' ')
]
self.count = len(self.files)
print_lock.release()
elif line.strip() in self.files:
print_lock.acquire()
self.files.remove(line.strip())
count = remap(
self.count - len(self.files),
old_max=self.count,
new_max=50
)
percent = remap(
self.count - len(self.files),
old_max=self.count,
new_max=100
)
self.update(count, percent, line.strip())
if not self.files:
self.update(50, 100, file_name='')
print_lock.release()
def update(self, count, percent, file_name):
current = time.time()
elapsed = current - self.start
percent = str(percent)
percent = ' ' * (3 - len(percent)) + percent
if count:
time_per_file = elapsed / (self.count - len(self.files))
remaining = time_per_file * len(self.files)
remaining = '0' + str(datetime.timedelta(seconds=int(remaining)))
else:
remaining = '99:99:99'
line = TEMPLATE.format(
prefix=self.prefix,
percent=percent,
count='#' * count + ' ' * (50 - count),
elapsed=str(datetime.timedelta(seconds=int(elapsed))),
remaining=remaining,
file_name=file_name
)
# try:
# self.stdout.seek(self.file_position)
# self.stdout.truncate()
# except (io.UnsupportedOperation, WindowsError):
# pass
if self.last_line_len:
self.stdout.write('\r' + ' ' * self.last_line_len)
self.stdout.write(line)
self.stdout.flush()
self.last_line_len = len(line)
def __getattr__(self, item):
if item in self.__dict__:
return self.__dict__[item]
return getattr(self.stdout, item)