-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattusage.py
197 lines (159 loc) · 6.45 KB
/
attusage.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
"""
Copyright Russell Ferriday 2010
First release May 2010
This file is part of ATTUsageAnalysis.
ATTUsageAnalysis is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ATTUsageAnalysis is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ATTUsageAnalysis. If not, see <http://www.gnu.org/licenses/>.
This package reads ATT CSV formatted usage files into a neat structure
DocTests
--------
Check the date parser...
>>> print maketime('03/17/2010','10:07PM')
2010-03-17 22:07:00
Create a usage object and read a csv report into it...
>>> usage = AttUsage()
>>> usage.process('testreport.csv')
>>> print usage
[<User> 'JANE SMITH' at '508-235-7829', <User> 'ROGER SMITH' at '508-235-6915']
Show that we have two users on the account...
>>> for user in sorted(usage.users.values(), key=lambda u: u.name):
... print user
<User> 'JANE SMITH' at '508-235-7829'
<User> 'ROGER SMITH' at '508-235-6915'
Look at the calls for the first user...
>>> for call in usage.users.values()[0].calls:
... print call
<Call> 2010-03-18 08:45:00 to 508-235-7829 == VMAIL CL lasting 1 min
<Call> 2010-03-18 08:46:00 to 508-748-9880 == SAN LUS O CA lasting 2 min
<Call> 2010-03-18 12:27:00 to 508-235-6915 == SAN LUS O CA lasting 1 min
<Call> 2010-03-18 12:28:00 to 508-235-6915 == SAN LUS O CA lasting 1 min
<Call> 2010-03-18 16:20:00 to 508-748-9880 == SAN LUS O CA lasting 2 min
<Call> 2010-03-18 16:43:00 to 508-534-0399 == SAN LUS O CA lasting 2 min
<Call> 2010-03-18 21:14:00 to 508-235-6915 == SAN LUS O CA lasting 1 min
<Call> 2010-03-18 21:17:00 to 508-235-6915 == SAN LUS O CA lasting 1 min
<Call> 2010-03-18 21:19:00 to 508-235-6915 == SAN LUS O CA lasting 1 min
<Call> 2010-03-18 21:25:00 to 508-235-6915 == SAN LUS O CA lasting 1 min
And now the texts for the same user...
>>> for text in usage.users.values()[0].texts:
... print text
<Text> 2010-03-17 13:01:00 from 508-235-6915
<Text> 2010-03-18 12:04:00 to 508-235-6915
<Text> 2010-03-18 18:33:00 from 508-704-1151
<Text> 2010-03-18 19:25:00 from 508-235-6915
<Text> 2010-03-18 20:13:00 from 508-235-6915
<Text> 2010-03-18 20:22:00 to 508-235-6915
<Text> 2010-03-18 20:28:00 from 508-235-6915
<Text> 2010-03-19 21:54:00 to 508-704-1151
<Text> 2010-03-19 21:59:00 from 508-704-1151
<Text> 2010-03-19 21:59:00 from 508-704-1151
Look at report.py to see more usage.
"""
import csv
import datetime
class User(object):
def __init__(self, name='', number=''):
self.name = name
self.number = number
self.calls = []
self.texts = []
def __repr__(self):
return "<User> '%s' at '%s'" % (self.name, self.number)
class Call(object):
def __init__(self, time, incoming, number, place, minutes):
self.time = time
self.incoming = incoming
self.number = number
self.place = place
self.minutes = minutes
def __repr__(self):
return '<Call> %s %s %s == %s lasting %s min' % \
(self.time, self.incoming and 'from' or 'to ', \
self.number, self.place, self.minutes)
def is_incoming(self):
return self.incoming
class Text(object):
def __init__(self, time, incoming, number):
self.time = time
self.incoming = incoming
self.number = number
def __repr__(self):
return '<Text> %s %s %s' % \
(self.time, self.incoming and 'from' or 'to ', \
self.number)
def is_incoming(self):
return self.incoming
class AttUsage(object):
def __init__(self):
self.users = {}
self.current_user = None # current user object
self.current_number = ''
self.current_mode = '' # 'call' / 'data'
def process(self, fqpath):
self.fqpath = fqpath
reader = csv.reader(open(self.fqpath, "rb"))
for line in reader:
self._processline(line)
for name, user in self.users.items():
user.calls.sort(key=lambda call: call.time)
user.texts.sort(key=lambda text: text.time)
def _is_empty(self, line):
return [ i for i in line if i ] == []
def _is_int(self, s):
try:
i = int(s)
return True
except:
return False
def _convert_data(self, line):
time = maketime(line[2], line[3])
text = Text(time, line[10]=='In', line[4])
self.current_user.texts.append(text)
def _convert_call(self, line):
time = maketime(line[2], line[3])
call = Call(time, line[5].startswith('INCOMING'), line[4], \
line[5], line[6])
self.current_user.calls.append(call)
def _processline(self, line):
#all empty fields? - forget it
if self._is_empty(line):
return
#what type of block?
if line[0] == 'Call Detail':
self.current_number = line[1]
self.current_mode = 'call'
return
if line[0] == 'Data Detail':
self.current_number = line[1]
self.current_mode = 'data'
return
#update current user
if line[0] == 'User Name:':
username = line[1]
self.current_user = self.users.setdefault(username, \
User(username, self.current_number) )
return
#see if we have a call or data record and process it
if self._is_int(line[0]):
if self.current_mode == 'data':
self._convert_data(line)
else:
self._convert_call(line)
return
def __repr__(self):
return str(sorted(self.users.values(), key=lambda u: u.name))
def maketime(d,t):
dt = ' '.join((d,t))
dt = datetime.datetime.strptime(dt, "%m/%d/%Y %I:%M%p" )
return dt
if __name__ == "__main__":
import doctest
doctest.testmod()