-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlops.py
More file actions
277 lines (230 loc) · 8.94 KB
/
sqlops.py
File metadata and controls
277 lines (230 loc) · 8.94 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import sqlite3
import os
import datetime
import markdown
from bs4 import BeautifulSoup
def create_column(dirpath: str) -> int:
# current date is served as the 'uptime'
# title and abstract of the column should be read from README.md
# create an id for new column
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.execute('select max(id) from columns;')
max_id = next(cursor)[0]
if max_id is None:
max_id = -1
new_id = max_id + 1
cursor = conn.cursor()
cursor.execute('insert into columns(id, uptime, dirpath)\
values (?, DATE(\'now\'), ?);',
(new_id, dirpath))
conn.commit()
conn.close()
return new_id
def fill_column(column_id: int, filepath: str, logger):
"""fill the column information with readme.md
Args:
column_id (int): the id of the column
filepath (str): "column/readme.md", "column/README.md" or other forms of the readme file
"""
# TODO: not tested
logger.info(f"adding readme to column {column_id}")
file = os.path.join(os.getenv("NOTE_REPO_PATH"), filepath)
with open(file) as f:
text = f.read()
text = markdown.markdown(text)
soup = BeautifulSoup(text, 'lxml')
title = soup.h1.string
abstract = soup.p.string
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.cursor()
cursor.execute('update columns set title = ?, abstract = ? \
where id = ?;', (title, abstract, column_id))
conn.commit()
conn.close()
def find_column(dirpath: str):
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.execute('select id from columns where dirpath = ?;', (dirpath,))
try:
id = next(cursor)[0]
conn.close()
return id
except:
# the column is not found
conn.close()
return None
def generate_id():
now = datetime.datetime.now()
date = now.strftime('%Y%m%d')
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.execute('select id from articles;')
max_id = int(date) * 100
for row in cursor:
if row[0] // 100 == int(date) and row[0] > max_id:
max_id = row[0]
max_id = max_id + 1
return max_id
def add_article(column_id: int, filepath: str, logger):
"""add an article to an existing column
Args:
column_id (int): id of the column to be add
filepath (str): "<column_name>/<filename>"
"""
logger.info(f"adding new article {filepath} to column {column_id}")
new_id = generate_id()
logger.info(f"new id {new_id} generated for new article")
# get title and abstract from README.md
file = os.path.join(os.getenv("NOTE_REPO_PATH"), filepath)
logger.info(f"adding file {file} to database")
with open(file, 'r', encoding='utf-8') as f:
text = f.read()
length = len(text)
html_text = markdown.markdown(text)
soup = BeautifulSoup(html_text, "lxml")
title = soup.h1.string
with open(file, 'r', encoding='utf-8') as f:
lines = f.readlines()
contents = lines[1:]
abstract = " ".join(contents).strip()
if len(abstract) > 100:
abstract = abstract[:100]
abstract = abstract.replace("\n", "").replace("\r", "")
# insert new article
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.cursor()
cursor.execute('insert into articles \
values (?, ?, DATE(\'now\'), ?, ?, ?, ?);',
(new_id, title, filepath, abstract, length, column_id))
conn.commit()
conn.close()
def add_log(filepath: str):
# get title number of logs and abstract from README.md
file = os.path.join(os.getenv("NOTE_REPO_PATH"), filepath)
with open(file, 'r', encoding='utf-8') as f:
text = f.read()
html_text = markdown.markdown(text)
soup = BeautifulSoup(html_text, 'lxml')
title = soup.h1.string
num_of_logs = len(soup.find_all('h2'))
with open(file, 'r', encoding='utf-8') as f:
abstract = f.readlines()[1]
# get a new id
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.execute('select max(id) from logs;')
max_id = next(cursor)[0]
if max_id is None:
max_id = -1
new_id = max_id + 1
# insert new log
cursor = conn.cursor()
cursor.execute('insert into logs \
values (?, ?, ?, DATE(\'now\'), DATE(\'now\'), ?, ?);',
(new_id, title, filepath, num_of_logs, abstract))
conn.commit()
conn.close()
def update_log(filepath: str):
# TODO: not tested
# get the id of the log
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.execute('select id from logs where filepath = ?;', (filepath,))
id = next(cursor)[0]
# get title number of logs and abstract from README.md
file = os.path.join(os.getenv("NOTE_REPO_PATH"), filepath)
with open(file, 'r', encoding='utf-8') as f:
text = f.read()
html_text = markdown.markdown(text)
soup = BeautifulSoup(html_text, 'lxml')
title = soup.h1.string
num_of_logs = len(soup.find_all('h2'))
with open(file, 'r', encoding='utf-8') as f:
abstract = f.readlines()[1]
cursor = conn.cursor()
cursor.execute('update logs set \
title = ?, abstract = ?, endtime = DATE(\'now\'), len = ? where id = ?;',
(title, abstract, num_of_logs, id))
conn.commit()
conn.close()
def update_article(filepath: str):
# TODO: not tested
# get the id of the article
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.execute('select id, columnid from articles where filepath = ?;', (filepath,))
article_id, column_id = next(cursor)
# get title and abstract from README.md
file = os.path.join(os.getenv("NOTE_REPO_PATH"), filepath)
with open(file, 'r', encoding='utf-8') as f:
text = f.read()
length = len(text)
html_text = markdown.markdown(text)
soup = BeautifulSoup(html_text, "lxml")
title = soup.h1.string
with open(file, 'r', encoding='utf-8') as f:
lines = f.readlines()
contents = lines[1:]
abstract = " ".join(contents).strip()
if len(abstract) > 100:
abstract = abstract[:100]
abstract = abstract.replace("\n", "").replace("\r", "")
cursor = conn.cursor()
cursor.execute('update articles set \
title = ?, uptime = DATE(\'now\'), abstract = ?, len = ? where id = ?;',
(title, abstract, len(text), article_id))
# also update the update time of the column
cursor.execute('update columns set \
uptime = DATE(\'now\') where id = ?;', (column_id,))
conn.commit()
conn.close()
def update_column(column_id: int, filepath: str):
'''
update the abstract of the column, because the readme is modified
also modify the updated time
'''
# TODO: not tested
with open(filepath) as f:
text = f.read()
text = markdown.markdown(text)
soup = BeautifulSoup(text, 'lxml')
title = soup.h1.string
abstract = soup.p.string
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.cursor()
cursor.execute('update columns set \
title = ?, uptime = DATE(\'now\'), abstract = ? where id = ?;',
(title, abstract, column_id))
conn.commit()
conn.close()
def remove_log(filepath: str):
# TODO: not tested
# get the id of the log
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.execute('select id from logs where filepath = ?;', (filepath,))
id = next(cursor)[0]
cursor = conn.cursor()
cursor.execute('delete from logs where id = ?;', (id,))
conn.commit()
conn.close()
def remove_article(filepath: str):
# TODO: not tested
# get the id of the article
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.execute('select id from articles where filepath = ?;', (filepath,))
id = next(cursor)[0]
cursor = conn.cursor()
cursor.execute('delete from articles where id = ?;', (id,))
conn.commit()
conn.close()
def remove_column(dirpath: str, logger):
# TODO: not tested
# get the id of the column
conn = sqlite3.connect(os.path.join(os.getenv("DB_PATH"), "main.db"))
cursor = conn.execute('select id from columns where dirpath = ?;', (dirpath,))
try:
id = next(cursor)[0]
except:
logger.info(f"column with path {dirpath} is already removed.")
return
cursor = conn.cursor()
cursor.execute('delete from columns where id = ?;', (id,))
conn.commit()
conn.close()
if __name__ == '__main__':
create_column('hello')