-
Notifications
You must be signed in to change notification settings - Fork 2
/
batteryusageparser.py
62 lines (45 loc) · 1.39 KB
/
batteryusageparser.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
import re
from utils import get_paths_from_dir
def parse_battery_report(fname):
with open(fname, 'r') as html_file:
html_lines = html_file.readlines()
creationtime = int(fname.split('.')[0].split('report')[-1])
reportLineFound = False
percentageFound = False
percentage = 0
for i, line in enumerate(html_lines):
if reportLineFound == False:
matchObj = re.match(r'.*Report generated.*', line)
if matchObj:
reportLineFound = True
continue
if reportLineFound:
matchObj = re.match(r'.*"percent">(\d+).*', line)
if matchObj:
percentageFound = True
percentage = matchObj.group(1)
continue
if percentageFound:
matchObj = re.match(r'.*"mw">(.*) mWh.*', line)
if matchObj:
capacity = matchObj.group(1)
capacity = re.sub(r',','', capacity)
html_file.close()
return {
"creationtime": creationtime,
"battery" : int(percentage),
"capacity" : int(capacity)
}
print("Something is wrong with the battery report named %s" % fname)
return None
def get_batteryreport_files(datadir):
files = get_paths_from_dir(datadir, file_matchers=['batteryreport'])
return files
def open_battery_reports(datadir):
files = get_batteryreport_files(datadir)
currdata = {}
for file in files:
# Data is in mWh by default
data = parse_battery_report(file)
currdata[str(data['creationtime'])] = (data['battery'], data['capacity'])
return currdata