-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwd.py
201 lines (170 loc) · 5.16 KB
/
wd.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python
import argparse
import collections
import datetime
import itertools
import re
import time
import sys
import os
import math
import subprocess
parser = argparse.ArgumentParser(description='show diff')
parser.add_argument(
'--cmd',
help='Program output to monitor'
)
parser.add_argument(
'--file',
help='File to monitor'
)
parser.add_argument('-i', '--interval', dest='interval', type=float, default=2.0)
parser.add_argument(
'-u', '--units', default='bin', metavar='U',
help='units, bin or dec'
)
def read_file(file_name):
with file(file_name) as fh:
return fh.read()
def read_cmd(cmd):
proc = subprocess.Popen(
cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
stdout, stderr = proc.communicate()
return stdout
class Node(object):
def __init__(self, match=None, source=None, start=None, end=None, groups=None, tpe=None, text=None):
self.source = source or match.string
self.start = start or match.start()
self.end = end or match.end()
self.groups = groups or match.groupdict()
self.text = (text or self.source[self.start : self.end]).strip()
self.tpe = tpe
def __str__(self):
return 'Node[num:%6s, %r]' % (
self.is_numeric,
self.text,
)
def __repr__(self):
return '%s(%s)' % (
self.__class__.__name__,
','.join(
'%s=%s' % item
for item in self.__dict__.iteritems()
)
)
def __len__(self):
return self.end - self.start
@property
def is_numeric(self):
return self.tpe == 'num'
def get_number(self):
if self.tpe == 'num':
return float(self.text)
def parse_blob(text):
separator = r'[:=|]+'
number = r'-?\d+(?:\.\d+)?'
item_parser = re.compile(
r'(?P<sep>\s*{sep})|(?P<num>\s*{num})(?=\s|$)|(?P<label>\s*[^\s:=|]+)'.format(
num=number,
sep=separator,
)
)
types = ('sep', 'num', 'label')
lines = text.split('\n')
def parse_line(line):
s = item_parser.scanner(line)
m = s.match()
while m:
tpe, val = [(t,g) for g,t in zip(m.groups(), types) if g is not None][0]
yield Node(m, text=val, tpe=tpe)
m = s.match()
for line in lines:
yield list(parse_line(line))
class Formatter(object):
symbols = ['u', 'm', '', 'k', 'M', 'G', 'T', 'E']
def __init__(self, units):
self.units = units
def pretty_num(self, value):
if self.units == 'bin':
power = 1024
elif self.units == 'dec':
power = 1000
else:
raise RuntimeException('invalid unit %r', self.unit)
if value != 0:
e = int(math.log(abs(value))/math.log( power ))
if e != 0:
v = float(value) / power**e
else:
v = value
else:
e = 0
v = value
frac, _ = math.modf(abs(v))
if frac < 0.0001:
v = int(v)
return v, self.symbols[e+2]
def fmt(self, value, width):
v, sym = self.pretty_num(value)
if sym:
if isinstance(v, float):
decimals = 4-len(str(int(v)))
tpl = '%%%s.%sf%s' % (width-1, decimals, sym)
else:
tpl = '%%%ss%s' % (width-1, sym)
else:
if isinstance(v, float):
decimals = 4-len(str(int(v)))
tpl = '%%%s.%sf' % (width, decimals)
else:
tpl = '%%%ss' % width
return tpl % v
def main():
args = parser.parse_args()
formatter = Formatter(
units = args.units
)
if args.cmd:
reader = lambda: read_cmd(args.cmd)
elif args.file:
reader = lambda: read_file(args.file)
else:
parser.print_help()
sys.exit(1)
t0 = time.time()
last = None
while True:
current = list(parse_blob(reader()))
t1 = time.time()
os.system('clear')
print "Every %ss: file: %s" % (args.interval, args.file)
if last:
line_z = itertools.izip(last, current)
for linepair in line_z:
node_z = itertools.izip(*linepair)
for last_n, current_n in node_z:
if last_n.is_numeric and current_n.is_numeric:
l = last_n.get_number()
c = current_n.get_number()
formatted = formatter.fmt(
(c-l)/(t1-t0),
len(current_n)
)
sys.stdout.write( formatted )
else:
pass
sys.stdout.write( current_n.text )
#print current_n
print '\n',
else:
for line in current:
for node in line:
sys.stdout.write( node.text )
print '\n',
last = current
t0 = t1
time.sleep(args.interval)
if __name__ == '__main__':
main()