-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkomstat.py
executable file
·243 lines (201 loc) · 6.16 KB
/
komstat.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/sw/kom++/bin/python
# -*- coding: iso-8859-1 -*-
# LysKOM statistics gatherer. Requires kom++
# Written by Peter Liljenberg 1998
# Public Domain, no guarantees
import getopt
from string import *
import komxx
from time import *
import sys
class Data:
def __init__(self, author):
self.author = author
self.texts = 0
self.chars = 0
self.lines = 0
def addtext(self, text):
str = text.body()
self.texts = self.texts + 1
self.chars = self.chars + text.num_chars()
self.lines = self.lines + text.num_lines()
def format(self):
formatstr = '%-30.30s %6d %6d %7d %7.2f %8.2f\n'
if self.texts != 0:
return formatstr % (self.author, self.texts, self.lines, self.chars,
float(self.lines) / self.texts,
float(self.chars) / self.texts)
else:
return formatstr % (self.author, self.texts, self.lines, self.chars, 0, 0)
def __cmp__(self, obj):
if self.texts == obj.texts:
return cmp(self.lines, obj.lines)
else:
return cmp(self.texts, obj.texts)
class Statistik:
def __init__(self, session, conf, post, start, end):
self.s = session
self.conf = conf
self.post = post
self.total = Data('TOTALT')
self.start = start
self.end = end
self.authors = {}
def addtext(self, text):
try:
self.total.addtext(text)
# print text.text_no(), text.author(), text.subject()
if self.authors.has_key(text.author()):
d = self.authors[text.author()]
else:
try:
d = Data(s.pers(text.author()).name())
except komxx.err_undef_pers:
d = Data('Person %d (finns inte)' % text.author())
self.authors[text.author()] = d
d.addtext(text)
except komxx.err_text_zero:
pass
except komxx.errno_error, msg:
print 'Kan inte läsa inlägg:', msg
def printdata(self):
subject = 'Statistik för ' + self.conf.name()
body = strftime('Statistikperioden är %Y-%m-%d till ',
self.start)
body = body + strftime('%Y-%m-%d\n\n', self.end)
body = body + ' Inlägg Rader Tecken R/I T/I\n'
body = body + '---------------------------------------------------------------------\n'
body = body + self.total.format() + '\n'
as = self.authors.values()
as.sort()
as.reverse()
for a in as:
body = body + a.format()
if self.post:
tno = self.s.create_text(subject, body, [self.conf.conf_no()], [], [], [])
print 'Skapade text', tno, 'i', self.conf.name()
print body
def dostat(s, confs, start, end, post):
# Add 24 hs to end, to make it the day after end-day.
tot = mktime(end)
tot = tot + 86400
nto = localtime(tot)
for c in confs:
ts = s.last_local_before(c, start) + 1
te = s.last_local_before(c, nto) + 1
tmap = s.text_mapping(c)
conf = s.conf(c)
stat = Statistik(s, conf, post, start, end)
for n in range(ts, te):
try:
tno = tmap[n]
except komxx.err_no_such_local_text:
print 'Missing local text', n, 'in', conf.name()
else:
text = s.text(tno)
stat.addtext(text)
stat.printdata()
def getconflist(s, okconfs, exclude):
confs = []
for c, min, max in s.confs_with_unread():
conf = s.conf(c)
try:
okconfs.index(c)
ok = 1
except ValueError:
ok = 0
if (ok ^ exclude) == 0:
print 'Hoppar över', conf.name()
else:
print 'Gräver igenom', conf.name()
confs.append(c)
return confs
def parse_date(date):
d = split(date, '-')
today = localtime(time())
year = today[0]
month = today[1]
if len(d) == 1:
day = atoi(d[0])
elif len(d) == 2:
month = atoi(d[0])
day = atoi(d[1])
elif len(d) == 3:
year = atoi(d[0])
month = atoi(d[1])
day = atoi(d[2])
else:
raise ValueError, 'Malformed date'
return (year, month, day, 0, 0, 0, 0, 0, -1)
def getpass(prompt = "Password: "):
sys.stdout.flush()
passwd = raw_input(prompt)
sys.stdout.write('\x1b[1A\x1b[2K')
sys.stdout.flush()
return passwd
usage = """Usage: komstat [options] persno start [end]
komstat -l persno
start and end are dates on the form YEAR-MONTH-DAY, where YEAR and MONTH
defaults to the current year and month, respectively.
end default is today. All the texts in the inclusive interval
[start, end] will be counted.
Note: 'All confs' below refer to all the confs persno is a member of and
has unread texts in.
Options:
-l --list List all confs with unread texts
-c <conflist> --confs=<conflist>
Only collect statistic for these confs.
conflist is a commaseparated list of confnos.
This option can be given more than once.
-x --exclude Process all confs except those given by -c
-p --post Send statistics data to the processed conf
"""
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], "c:xlp",
["confs=", "exclude", "list", "post"])
exclude = 0
okconfs = []
listconfs = 0
post = 0
persno = atoi(args[0])
for op, val in opts:
if op == '-x' or op == '--exclude':
exclude = 1
elif op == '-c' or op == '--confs':
val = split(val, ',')
for v in val:
okconfs.append(atoi(v, 10))
elif op == '-l' or op == '--list':
listconfs = 1
elif op == '-p' or op == '--post':
post = 1
if not listconfs:
if len(args) < 2:
raise 'Ledsen Error', 'Missing arguments'
start = parse_date(args[1])
try:
end = parse_date(args[2])
except IndexError:
end = localtime(time())
except 'foo': pass
# except (getopt.error, 'Ledsen Error', ValueError), val:
# print usage
else:
s = komxx.new_session("kom.lysator.liu.se", "4894",
"Ctrl-Cs statistisksork", "*pip*")
if s.login(persno, getpass(), 1) == komxx.st_ok:
if listconfs:
for c, min, max in s.confs_with_unread():
print c, s.conf(c).name()
else:
if len(okconfs) == 0:
confs = getconflist(s, [], 1)
elif exclude:
confs = getconflist(s, okconfs, exclude)
else:
confs = okconfs
dostat(s, confs, start, end, post)
s.logout()
else:
print '*smakk*'