-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
194 lines (149 loc) · 4.85 KB
/
logger.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
"""Weight Loss
Usage:
logger.py <category> javascript
logger.py <category> <weight>
logger.py <category> <weight> open
logger.py <category> <weight> <time>
Options:
-h --help Show this screen.
"""
from docopt import docopt
from datetime import datetime
import sys
import time
import os.path
import webbrowser
PATH_NAME = os.path.dirname(os.path.realpath(sys.argv[0]))
WEIGHT_FILE = PATH_NAME + "/weight.txt"
BENCH_FILE = PATH_NAME + "/bench.txt"
SQUAT_FILE = PATH_NAME + "/squat.txt"
DEADLIFT_FILE = PATH_NAME + "/deadlift.txt"
category_to_file_name = {
"bench": BENCH_FILE,
"squat": SQUAT_FILE,
"deadlift": DEADLIFT_FILE,
"weight": WEIGHT_FILE
}
JAVASCRIPT_FILE = PATH_NAME + "/weight.js"
TIME_FORMAT = "%Y-%m-%d-%H:%M" # e.g. 2015-07-15-13:34
def usage():
return __doc__
def die(msg):
sys.stderr.write(msg + "\n")
sys.exit(-1)
def err(s):
die("Error: %s" % s)
def datetime_to_epoch(time):
# http://stackoverflow.com/a/11743262/3187068
return (time - datetime(1970, 1, 1)).total_seconds()
def epoch_to_datetime(seconds):
return datetime.fromtimestamp(seconds)
def parse_float(s):
try:
return float(s)
except ValueError:
err("{} is not a float.".format(s))
def parse_time(s):
try:
return datetime.strptime(s, TIME_FORMAT)
except ValueError:
err("{} is not a time formatted like 2015-07-15-13:34.".format(s))
def parse_weights(filename):
if not os.path.isfile(filename):
return []
with open(filename, "r") as f:
weights = []
for line in f:
try:
parts = line.split()
time = int(parts[0])
weight = float(parts[1])
weights.append((time, weight))
except:
continue
return weights
def record_weight(time, weight, filename):
weights = parse_weights(filename)
with open(filename, "w+") as f:
weights.append((time, weight))
weights.sort()
for (time, weight) in weights:
f.write("{} {}\n".format(time, weight))
def generate_javascript(filename):
template = """
$(function () {{
$('#container').highcharts({{
chart: {{
type: 'line'
}},
title: {{
text: 'Progress'
}},
xAxis: {{
type: 'datetime',
dateTimeLabelFormats: {{ // don't display the dummy year
month: '%e. %b',
year: '%b'
}},
title: {{
text: 'Date'
}}
}},
yAxis: {{
title: {{
text: 'Weight (lb)'
}}
}},
plotOptions: {{
line: {{
dataLabels: {{
enabled: true
}},
enableMouseTracking: true,
marker: {{
enabled: true
}}
}}
}},
series: [{{
name: 'Fabien Bessez',
data: [{}]
}}]
}});
}});
"""
timeweights = parse_weights(filename)
times = [timeweight[0] for timeweight in timeweights]
times = [epoch_to_datetime(time) for time in times]
times = ["Date.UTC({}, {}, {}, {}, {})".format(time.year, time.month - 1, time.day, time.hour, time.minute) for time in times]
weights = [timeweight[1] for timeweight in timeweights]
weights = ["{}".format(weight) for weight in weights]
with open(JAVASCRIPT_FILE, "w+") as f:
f.write(template.format(", ".join("[{}, {}]".format(time, weight) for (time, weight) in zip (times, weights))))
def main(args):
arg_category = args["<category>"]
arg_weight = args["<weight>"]
arg_time = args["<time>"]
arg_open = args["open"]
arg_javascript = args["javascript"]
try:
filename = category_to_file_name[arg_category]
local_url = 'file://' + PATH_NAME + '/index.html'
except:
die("Need to specify category")
if arg_category and not arg_javascript:
if not arg_time:
record_weight(int(time.time()), parse_float(arg_weight), filename)
else:
t = int(datetime_to_epoch(parse_time(arg_time)))
record_weight(t, parse_float(arg_weight), filename)
generate_javascript(filename)
if arg_open:
webbrowser.open(local_url)
elif arg_javascript or arg_open:
generate_javascript(filename)
webbrowser.open(local_url)
else:
die(usage())
if __name__ == '__main__':
main(docopt(usage()))