-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
368 lines (314 loc) · 10.6 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from manager import Manager
import os
import constants
import util
import files
import argparse
parser = argparse.ArgumentParser(
prog="Repeat",
description="Vy learns a subject with spaced-repetition",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-c",
"--command",
choices=[
"add_subject", # subject
"list_subjects",
"list", # subject, states, tags, by, direction
"add", # subject, identifier, name, tags
"remove", # subject, identifier
"get_file", # subject, identifier, postifx
"get_files_by_tag", # subject, tags, postifx
"get_name", # subject, identifier
"get_tags_by_identifier", # subject, identifier
"get_new_tags_to_identifier", # subject, identifier
"list_file", # list all postfix # subject, identifier
"review", # subject, tags, k
"add_tag", # subject, identifier, tags
"remove_tag", # subject, identifier, tags
"start", # subject, identifier
"end", # subject, identifier, passfail
"cancel", # subject, identifier
"list_tag", # subject
"list_state", # subject, tags
"list_state_by_tag", # subject, tags
],
type=str,
required=True,
)
parser.add_argument("--subject", type=str, help="subject identifier")
parser.add_argument(
"-i", "--identifier", type=str, help="item identifier", default=constants.AUTO_ID
)
parser.add_argument("-n", "--name", type=str, help="item name")
parser.add_argument(
"-r",
"--passfail",
choices=[constants.PASS, constants.FAIL],
type=str,
help="result",
)
parser.add_argument(
"-t", "--tags", type=str, help="tags separated by comma", default=None
) # comma separated
parser.add_argument(
"-a",
"--states",
type=str,
# choices=constants.STATES,
help="states separated by comma",
default=None,
) # comma separated
parser.add_argument(
"-b",
"--by",
type=str,
choices=list(constants.BY_TO_PROPERTY.keys()),
default="view",
)
parser.add_argument(
"-d", "--direction", type=str, choices=["inc", "dec"], default="inc"
)
parser.add_argument("-p", "--postfix", type=str, default="untitled.txt")
args = parser.parse_args()
config = vars(args)
command = config["command"]
if command == "add_subject":
# add a new subject, create all necessary directories and files
subject = util.parse_args(config, ["subject"], [True])
files.makedirs_for_subject(subject)
elif command == "list_subjects":
subjects = files.list_all_subjects()
print(subjects)
elif command == "add":
# subject, identifier, name, tags
subject, identifier, name, tags = util.parse_args(
config, ["subject", "identifier", "name", "tags"], [True, False, True, False]
)
manager = Manager(subject)
manager.load()
manager.add(identifier, tags, {"name": name})
manager.save()
elif command == "remove":
# subject, identifier
subject, identifier = util.parse_args(
config, ["subject", "identifier"], [True, True]
)
manager = Manager(subject)
manager.load()
manager.remove_item_by_identifier(identifier)
manager.save()
elif command == "list":
# subject, states, tags, by, direction
subject, states, tags, by, direction = util.parse_args(
config,
["subject", "states", "tags", "by", "direction"],
[True, False, False, False, False],
)
manager = Manager(subject)
manager.load()
identifiers = manager.get_identifiers_by_states_and_tags(tags, states)
info_list = manager.get_item_by_identifiers(identifiers)
info_property = constants.BY_TO_PROPERTY[by]
sorted_info_list = []
for i, info_dict in enumerate(info_list):
sorted_info_list.append((info_dict[info_property], i, info_dict))
sorted_info_list = sorted(sorted_info_list)
if direction == "dec":
sorted_info_list = sorted_info_list[::-1]
for _, _, info_dict in sorted_info_list:
readable_info = util.get_readable_info(
info_dict,
content_fields=["name"],
extra_info=constants.BY_TO_INFO_SHOW[by]["show"],
emphasis=constants.BY_TO_INFO_SHOW[by]["emphasis"],
)
print(readable_info)
elif command == "get_file":
# subject, identifier, postifx
subject, identifier, postfix = util.parse_args(
config, ["subject", "identifier", "postfix"], [True, True, True]
)
manager = Manager(subject)
manager.load()
content = manager.get_content_by_identifier(identifier)
if not content:
print("##_not_exist_##")
exit(1)
name = "unnamed"
if "name" in content:
name = content["name"]
print(files.get_study_file(subject, identifier, postfix, name))
elif command == "get_files_by_tag":
# subject, tag, postifx
subject, tags, postfix = util.parse_args(
config, ["subject", "tags", "postfix"], [True, True, True]
)
manager = Manager(subject)
manager.load()
identifiers = manager.get_identifiers_by_tags(tags)
for identifier in identifiers:
content = manager.get_content_by_identifier(identifier)
if not content or "name" not in content:
continue
filepath = files.get_study_file(subject, identifier, postfix, content["name"])
if os.path.isfile(filepath):
print(filepath)
elif command == "get_name":
# subject, identifier
subject, identifier = util.parse_args(
config, ["subject", "identifier"], [True, True, True]
)
manager = Manager(subject)
manager.load()
content = manager.get_content_by_identifier(identifier)
if not content:
print("##_not_exist_##")
exit(1)
name = "unnamed"
if "name" in content:
name = content["name"]
print(name)
elif command == "get_tags_by_identifier":
subject, identifier = util.parse_args(
config, ["subject", "identifier"], [True, True]
)
manager = Manager(subject)
manager.load()
tags = manager.get_tags_by_identifier(identifier)
if not tags:
print(f"Cannot find any tag from {identifier}")
exit(1)
for tag in sorted(tags):
print(tag)
elif command == "get_new_tags_to_identifier": # subject, identifier
subject, identifier = util.parse_args(
config, ["subject", "identifier"], [True, True]
)
manager = Manager(subject)
manager.load()
tags = manager.get_tags_by_identifier(identifier)
new_tags = list(manager.tags.difference(tags))
if not new_tags:
print(f"Cannot find any tag from {identifier}")
exit(1)
for tag in sorted(new_tags):
print(tag)
elif command == "list_file":
# list all postfix # subject, identifier
subject, identifier = util.parse_args(
config, ["subject", "identifier"], [True, True]
)
for file in files.list_all_study_files(subject, identifier):
print(file)
elif command == "review":
# subject, tags, k
subject, tags, by = util.parse_args(
config, ["subject", "tags", "by"], [True, False, True]
)
manager = Manager(subject)
manager.load()
identifiers = manager.suggest(
constants.DEFAULT_NUMBER_OF_SUGGESTION[by], tags, by=by
)
info_list = manager.get_item_by_identifiers(identifiers)
for info_dict in info_list:
readable_info = util.get_readable_info(
info_dict,
content_fields=["name"],
extra_info=[
constants.LABEL_CONTENT,
constants.LABEL_TAG,
constants.LABEL_STATE,
constants.LABEL_COMPETENCY,
constants.LABEL_PASS_PCT,
],
)
print(readable_info)
elif command == "add_tag":
# subject, identifier, tags
subject, identifier, tags = util.parse_args(
config, ["subject", "identifier", "tags"], [True, True, True]
)
if len(tags) == 0:
print("No tag provided!")
exit(1)
manager = Manager(subject)
manager.load()
manager.add_tags_to_identifier(identifier, tags)
manager.save()
elif command == "remove_tag":
# subject, identifier, tags
subject, identifier, tags = util.parse_args(
config, ["subject", "identifier", "tags"], [True, True, True]
)
if len(tags) == 0:
print("No tag provided!")
exit(1)
manager = Manager(subject)
manager.load()
manager.remove_tags_from_identifier(identifier, tags)
manager.save()
elif command == "start":
# subject, identifier
subject, identifier = util.parse_args(
config, ["subject", "identifier"], [True, True]
)
manager = Manager(subject)
manager.load()
manager.start_study_item(identifier)
manager.save()
elif command == "end":
# subject, identifier, passfail
subject, identifier, passfail = util.parse_args(
config, ["subject", "identifier", "passfail"], [True, True, True]
)
manager = Manager(subject)
manager.load()
manager.end_study_item(identifier, passfail)
manager.save()
elif command == "cancel":
subject, identifier = util.parse_args(
config, ["subject", "identifier"], [True, True]
)
manager = Manager(subject)
manager.load()
manager.cancel_study_item(identifier)
manager.save()
elif command == "list_tag":
# subject
subject, states = util.parse_args(config, ["subject", "states"], [True, False])
manager = Manager(subject)
manager.load()
tag_count = manager.list_tags(states)
for tag, count in tag_count:
print(f"{tag:20s}: {count:>4d}")
elif command == "list_state":
# subject, tags
subject, tags = util.parse_args(config, ["subject", "tags"], [True, False])
manager = Manager(subject)
manager.load()
state_count = manager.list_states(tags)
for state, count in state_count:
print(f"{state}: {count}")
elif command == "list_state_by_tag":
# subject, states
subject, states = util.parse_args(config, ["subject", "states"], [True])
manager = Manager(subject)
manager.load()
tag_count = manager.list_tags(states)
for tag, count in tag_count:
print(f"{tag:20s} : {count:>4d}", end="")
state_count = manager.list_states([tag], states)
display_state = False
for state, scount in state_count:
if state != constants.STATE_STUDIED:
styled_state = constants.GET_STYLED_STATE(state)
if not display_state:
print(":", end="")
print(f"{styled_state}({scount})", end=" ")
display_state = True
print("")
else:
raise Exception(f"Unknown command: {command}")