forked from yihong0618/iBeats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (62 loc) · 2.11 KB
/
main.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
import re
import argparse
import os
from heart import Heart
GITHUB_README_COMMENTS = (
"(<!--START_SECTION:{name}-->\n)(.*)(<!--END_SECTION:{name}-->\n)"
)
HEART_RATE_HEAD = "| Time | Rate | \n | ---- | ---- | \n"
HEART_RATE_STAT_TEMPLATE = "| {time} | {value} |\n"
OUT_FOLDER = os.path.join(os.getcwd(), "files")
def replace_readme_comments(file_name, comment_str, comments_name):
with open(file_name, "r+") as f:
text = f.read()
# regrex sub from github readme comments
text = re.sub(
GITHUB_README_COMMENTS.format(name=comments_name),
r"\1{}\n\3".format(comment_str),
text,
flags=re.DOTALL,
)
f.seek(0)
f.write(text)
f.truncate()
def parse_ios_str_to_list(list_str):
l = list_str.splitlines()
print(l)
# filter the empty value
return [i for i in l if i]
def make_summary_str(time_list, value_list):
s = HEART_RATE_HEAD
for t, v in zip(time_list, value_list):
s += HEART_RATE_STAT_TEMPLATE.format(time=t, value=v)
return s
def main(time_list_str, value_list_str):
time_list = parse_ios_str_to_list(time_list_str)
value_list = parse_ios_str_to_list(value_list_str)
print(value_list)
print(time_list)
try:
value_int_list = [int(float(i)) for i in value_list]
except IOError:
print("Error: values is invalid")
value_int_list = [int(float(i)) for i in value_list.split('\\n')]
s = make_summary_str(time_list, value_int_list)
replace_readme_comments("README.md", s, "my_heart_rate")
print(s)
# generate heart rate svg and save
h = Heart(os.path.join(OUT_FOLDER, "heart.svg"))
print(value_int_list)
h.set_values(value_int_list)
h.make_heart_svg()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("time_list_str", help="time_list_str")
parser.add_argument("value_list_str", help="value_list_str")
options = parser.parse_args()
print(options.time_list_str)
print(options.value_list_str)
main(
options.time_list_str,
options.value_list_str,
)