-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspin.py
176 lines (156 loc) · 4.52 KB
/
spin.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#! /usr/bin/env python
'''
Simple module to provide feedback to the user that work is being done.
'''
import sys
import time
import itertools
import unittest
def _output(msg, delta=None, ctime=None, term='\n'):
'''
Send output to stderr.
'''
output = ''
if ctime is not None:
output += time.ctime(ctime)
if delta is not None:
output += '(%.2f)' % delta
if output:
output += ' '
if msg:
output += msg
start = ''
if term:
if '\r' in term:
start = ' '
output += term
sys.stderr.write(start + output)
sys.stderr.flush()
class IdleSpin(object):
'''
Simple class to show that things are happening, i.e. we're not stuck
'''
def __init__(self, spinchars='|/-\\', mincycle=.1, showstats=True):
'''
Initialize the spinner
'''
self._chars = itertools.cycle(spinchars)
self._lastspin = 0
self._prefix = ''
self._lastout = time.time()
self._mincycle = mincycle
self._showstats = showstats
self._stats = dict(msgs=0, shifts=0, spins=0, skips=0)
self._mlen = 0
def __enter__(self):
'''
Handler for context manager
'''
return self
def __exit__(self, type, value, traceback): # pylint:disable=redefined-builtin
'''
Handler for context manager
'''
self.close()
def spin(self):
'''
Display the indicator
'''
ctime = time.time()
if self._lastspin + self._mincycle > ctime:
self._stats['skips'] += 1
return
msg = self._prefix + next(self._chars)
mlen = len(msg)
if mlen < self._mlen:
msg += ' ' * (self._mlen - mlen)
_output(msg, delta=ctime-self._lastout, term='\r')
self._mlen = mlen
self._lastspin = ctime
self._stats['spins'] += 1
def shift(self, shiftchar='.', groupings=(('X', 10), ('L', 50), ('C', 100), ('D', 500), ('M', 1000))):
'''
Shift the spinner over a character. Useful for indicating a change of processing unit,
i.e. a new file or directory...
'''
self._prefix += shiftchar
mult = 1
for groupchar, groupat in groupings:
groupat /= mult
self._prefix = self._prefix.replace(shiftchar*groupat, groupchar)
shiftchar = groupchar
mult *= groupat
self._stats['shifts'] += 1
def output(self, msg, *args, **kwds):
'''
Not really sure how this is different than spin() aside from the rate limiter.
'''
ctime = time.time()
if self._lastout + self._mincycle > ctime:
self._stats['skips'] += 1
return
_output(msg, ctime=ctime, delta=ctime - self._lastout, *args, **kwds)
self._lastout = ctime
self._stats['msgs'] += 1
def close(self):
'''
Show statistics at the end
'''
sys.stderr.write('\n')
if self._showstats:
sys.stderr.write('%s\n' % self._stats)
def get_stats(self):
'''
Return the stats to the caller
'''
return self._stats
class TestSpin(unittest.TestCase):
'''
Tests for IdleSpin class
'''
def test_no_spin(self):
'''
Test of no spinning
'''
spinner = IdleSpin()
self.assertTrue(spinner.get_stats(), dict(msgs=0, shifts=0, spins=0, skips=0))
def test_spin_collapse(self):
'''
Test collapsing
'''
spinner = IdleSpin()
loops = 3
for _ in range(0, loops):
spinner.spin()
self.assertTrue(spinner.get_stats(), dict(msgs=0, shifts=0, spins=1, skips=(loops-1)))
def test_spin_normal(self):
'''
Test normal spin
'''
spinner = IdleSpin()
loops = 3
for _ in range(0, loops):
spinner.spin()
time.sleep(.15)
self.assertTrue(spinner.get_stats(), dict(msgs=0, shifts=0, spins=loops, skips=0))
def test_group(self):
'''
Test grouping
'''
spinner = IdleSpin()
for _ in range(0, 30):
spinner.spin()
spinner.shift()
self.assertTrue(spinner.get_stats(), dict(msgs=0, shifts=30, spins=0, skips=0))
def main():
'''
Main program
'''
#unittest.main()
spinner = IdleSpin()
for _ in range(0, 101):
spinner.spin()
time.sleep(.15)
spinner.shift()
if __name__ == '__main__':
main()