-
Notifications
You must be signed in to change notification settings - Fork 18
/
__init__.py
236 lines (214 loc) · 6.89 KB
/
__init__.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
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
"""Utility functions to work with other datasets."""
# Core Library modules
import logging
import sys
from typing import Dict
# Third party modules
import pymysql
import pymysql.cursors
# Local modules
from .. import utils
logging.basicConfig(
format="%(asctime)s %(levelname)s %(message)s",
level=logging.DEBUG,
stream=sys.stdout,
)
__formula_to_dbid_cache = None
username2id: Dict[str, str] = {}
def formula_to_dbid(formula_str, backslash_fix=False):
"""
Convert a LaTeX formula to the database index.
Parameters
----------
formula_str : string
The formula as LaTeX code.
backslash_fix : boolean
If this is set to true, then it will be checked if the same formula
exists with a preceeding backslash.
Returns
-------
int :
The database index.
"""
global __formula_to_dbid_cache
if __formula_to_dbid_cache is None:
mysql = utils.get_mysql_cfg()
connection = pymysql.connect(
host=mysql["host"],
user=mysql["user"],
passwd=mysql["passwd"],
db=mysql["db"],
charset="utf8mb4",
cursorclass=pymysql.cursors.DictCursor,
)
cursor = connection.cursor()
# Get all formulas that should get examined
sql = "SELECT `id`, `formula_in_latex` FROM `wm_formula` "
cursor.execute(sql)
formulas = cursor.fetchall()
__formula_to_dbid_cache = {}
for fm in formulas:
__formula_to_dbid_cache[fm["formula_in_latex"]] = fm["id"]
if formula_str in __formula_to_dbid_cache:
return __formula_to_dbid_cache[formula_str]
elif backslash_fix and ("\\%s" % formula_str) in __formula_to_dbid_cache:
return __formula_to_dbid_cache["\\%s" % formula_str]
else:
logging.info(
"Symbol '%s' was not found. Add it to write-math.com.", formula_str
)
mysql = utils.get_mysql_cfg()
connection = pymysql.connect(
host=mysql["host"],
user=mysql["user"],
passwd=mysql["passwd"],
db=mysql["db"],
charset="utf8mb4",
cursorclass=pymysql.cursors.DictCursor,
)
cursor = connection.cursor()
sql = (
"INSERT INTO `wm_formula` (`user_id`, `formula_name`, "
"`formula_in_latex`, "
"`mode`, `package`) VALUES ("
"'10', %s, %s, 'bothmodes', NULL);"
)
if len(formula_str) < 20:
logging.info("Insert formula %s.", formula_str)
cursor.execute(sql, (formula_str, formula_str))
connection.commit()
__formula_to_dbid_cache[formula_str] = connection.insert_id()
return __formula_to_dbid_cache[formula_str]
def getuserid(username, copyright_str):
"""Get the ID of the user with `username` from write-math.com. If he
doesn't exist by now, create it. Add `copyright_str` as a description.
Parameters
----------
username : string
Name of a user.
copyright_str : string
Description text of a user in Markdown format.
Returns
-------
int :
ID on write-math.com of the user.
"""
global username2id
if username not in username2id:
mysql = utils.get_mysql_cfg()
connection = pymysql.connect(
host=mysql["host"],
user=mysql["user"],
passwd=mysql["passwd"],
db=mysql["db"],
charset="utf8mb4",
cursorclass=pymysql.cursors.DictCursor,
)
cursor = connection.cursor()
sql = (
"INSERT IGNORE INTO `wm_users` ("
"`display_name` , "
"`password` ,"
"`account_type` ,"
"`confirmation_code` ,"
"`status` ,"
"`description`"
") "
"VALUES ("
"%s, '', 'Regular User', '', 'activated', %s"
");"
)
cursor.execute(sql, (username, copyright_str))
connection.commit()
# Get the id
try:
sql = "SELECT `id` FROM `wm_users` " "WHERE `display_name` = %s LIMIT 1"
cursor.execute(sql, username)
uid = cursor.fetchone()["id"]
except Exception as inst:
logging.debug("username not found: %s", username)
print(inst)
# logging.info("%s: %s", username, uid)
username2id[username] = uid
return username2id[username]
def insert_recording(hw):
"""Insert recording `hw` into database."""
mysql = utils.get_mysql_cfg()
connection = pymysql.connect(
host=mysql["host"],
user=mysql["user"],
passwd=mysql["passwd"],
db=mysql["db"],
charset="utf8mb4",
cursorclass=pymysql.cursors.DictCursor,
)
try:
cursor = connection.cursor()
sql = (
"INSERT INTO `wm_raw_draw_data` ("
"`user_id`, "
"`data`, "
"`md5data`, "
"`creation_date`, "
"`device_type`, "
"`accepted_formula_id`, "
"`secret`, "
"`ip`, "
"`segmentation`, "
"`internal_id`, "
"`description` "
") VALUES (%s, %s, MD5(data), "
"%s, %s, %s, %s, %s, %s, %s, %s);"
)
data = (
hw.user_id,
hw.raw_data_json,
getattr(hw, "creation_date", None),
getattr(hw, "device_type", ""),
getattr(hw, "formula_id", None),
getattr(hw, "secret", ""),
getattr(hw, "ip", None),
str(getattr(hw, "segmentation", "")),
getattr(hw, "internal_id", ""),
getattr(hw, "description", ""),
)
cursor.execute(sql, data)
connection.commit()
for symbol_id, strokes in zip(hw.symbol_stream, hw.segmentation):
insert_symbol_mapping(cursor.lastrowid, symbol_id, hw.user_id, strokes)
logging.info("Insert raw data.")
except pymysql.err.IntegrityError as e:
print(f"Error: {e} (can probably be ignored)")
def insert_symbol_mapping(raw_data_id, symbol_id, user_id, strokes):
"""
Insert data into `wm_strokes_to_symbol`.
Parameters
----------
raw_data_id : int
user_id : int
strokes: list of int
"""
mysql = utils.get_mysql_cfg()
connection = pymysql.connect(
host=mysql["host"],
user=mysql["user"],
passwd=mysql["passwd"],
db=mysql["db"],
charset="utf8mb4",
cursorclass=pymysql.cursors.DictCursor,
)
cursor = connection.cursor()
sql = (
"INSERT INTO `wm_partial_answer` "
"(`recording_id`, `symbol_id`, `strokes`, `user_id`, "
"`is_accepted`) "
"VALUES (%s, %s, %s, %s, 1);"
)
data = (
raw_data_id,
symbol_id,
",".join([str(stroke) for stroke in strokes]),
user_id,
)
cursor.execute(sql, data)
connection.commit()