-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtsv.py
executable file
·157 lines (115 loc) · 3.62 KB
/
tsv.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
#!/usr/bin/env /usr/bin/python3
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title tsv
# @raycast.mode compact
# Optional parameters:
# @raycast.icon ⏰
# @raycast.packageName Timestamp Recorder HTML view
# Documentation:
# @raycast.description Compile your records into a timeline
# @raycast.author Tomasz Sobota
# @raycast.authorURL https://techbranch.net
import csv
import datetime
import os
import webbrowser
class RecordType:
TSR = 1
TSN = 2
class RecordSide:
LEFT = "left"
RIGHT = "right"
# ------------
# PARAMETERS
# ------------
# modify to your preference
home_path = os.path.expanduser('~')
TSR_FILE_PATH = home_path + "/tsr/record.csv"
TSN_FILE_PATH = home_path + "/tsr/notes.csv"
HTML_OUTPUT_PATH = home_path + "/tsr/record.html"
CSS_ASSETS_PATH = "../assets/timeline.css"
DEFAULT_ENTRY_SIDE = RecordSide.LEFT
# ---------
# Helpers
# ---------
# function assigning side to a record type
def get_timeline_side(record_type: RecordType):
if record_type == RecordType.TSR:
return RecordSide.LEFT
if record_type == RecordType.TSN:
return RecordSide.RIGHT
return DEFAULT_ENTRY_SIDE
def csv_to_timeline_entries(trows: list):
containers = []
for row in trows:
ts_side = get_timeline_side(row[2])
pretty_date = row[0].strftime('%H:%M on %d %b %Y')
html_container = f'<div class="container {ts_side}">\n<div class="content">\n'
html_container += f'<h2>{row[1]}</h2>\n'
html_container += f'<p>{pretty_date}</p>\n'
html_container += '</div>\n</div>\n'
containers.append(html_container)
# return containers as a single string with reversed order
return "".join(containers[::-1])
def generate_html_template(embedded_html: str, stylesheet: str):
css = ""
if stylesheet:
css = f"""
<style>
{stylesheet}
</style>
"""
html_template = f"""<!DOCTYPE html>
<html>
<head>
{css}
</head>
<body>
<div class="timeline">
{embedded_html}
</div>
</body>
</html>"""
return html_template
def html_template_to_file(html_template: str, file_path: str):
"""Save html template to file"""
fp = file_path
if file_path is None:
fp = HTML_OUTPUT_PATH
with open(fp, "w") as f:
f.write(html_template)
def read_csv(filepath: str, recordtype: RecordType) -> list:
rows_buffer = []
with open(filepath, "r") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if len(row) == 0:
# omit empty rows
continue
raw_datetime = row[0]
raw_data = row[1]
parsed_datetime = datetime.datetime.fromisoformat(raw_datetime)
rows_buffer.append([parsed_datetime, raw_data, recordtype])
return rows_buffer
def read_file_to_str(filepath):
with open(filepath, 'r') as file:
return file.read()
# ------
# Main
# ------
# make sure directories exist
os.makedirs(os.path.dirname(TSR_FILE_PATH), exist_ok=True)
os.makedirs(os.path.dirname(TSN_FILE_PATH), exist_ok=True)
tsr_rows = []
tsn_rows = []
tsr_rows = read_csv(TSR_FILE_PATH, RecordType.TSR)
tsn_rows = read_csv(TSN_FILE_PATH, RecordType.TSN)
all_rows = tsr_rows + tsn_rows
sorted_rows = sorted(all_rows, key=lambda x: x[0])
css_stylesheet = read_file_to_str(CSS_ASSETS_PATH)
html_entries = csv_to_timeline_entries(sorted_rows)
html_template = generate_html_template(html_entries, css_stylesheet)
_ = html_template_to_file(html_template, HTML_OUTPUT_PATH)
webbrowser.open('file://' + os.path.realpath(HTML_OUTPUT_PATH))
print("Compiled the html report.")