-
Notifications
You must be signed in to change notification settings - Fork 0
/
review.py
147 lines (118 loc) · 5.07 KB
/
review.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
import card_repository
from card import Card
from edit import edit
from iterators import review_iterator, browsing_iterator
from review_card import review_card
from user_colors import print_info, print_instruction, print_error, colored_prompt
from timestamps import add_half_interval
def include_flipped_card():
print_info('This is a two-way card.')
print_info('Should your changes also affect the flipped version of this card?')
while True:
answer = colored_prompt('[y/n]: ')
if answer in ['y', 'n']:
break
if answer == 'y':
return True
else:
return False
def bump_flipped_card_timestamp(conn, cursor, card):
flipped_card = card_repository.select_flipped_card(cursor, card)
if flipped_card == None:
return
bumped_last_viewed = add_half_interval(flipped_card.last_viewed)
bumped_flipped_card = Card(
flipped_card.front,
flipped_card.back,
flipped_card.score,
bumped_last_viewed
)
card_repository.insert(conn, cursor, bumped_flipped_card)
card_repository.delete(conn, cursor, flipped_card)
def iterate(conn, cursor, iterator, count):
if count == 0:
print_error('No results.')
elif count == 1:
print_info('[1 CARD.]')
else:
print_info('[{} CARDS.]'.format(count))
try:
for card in iterator:
print_instruction('[Ctrl+C to quit.]')
updated_card, contents_changed = review_card(card)
is_two_way_card = card_repository.is_two_way_card(cursor, card)
if is_two_way_card and contents_changed:
flipped_card_too = include_flipped_card()
else:
flipped_card_too = False
if updated_card == None:
card_repository.delete(conn, cursor, card)
if flipped_card_too:
card_repository.delete_flipped_card(conn, cursor, card)
count -= 1
# Not an error, but I feel this should be red.
print_error('DELETED.')
table_is_empty = card_repository.check_if_empty(cursor)
if table_is_empty:
print_info('You have no more cards!')
break
else:
if contents_changed:
while True:
card_repository.insert(conn, cursor, updated_card)
if flipped_card_too:
card_repository.insert_flipped_card(conn, cursor, updated_card)
else:
bump_flipped_card_timestamp(conn, cursor, card)
print_instruction('Create another version based on this same card?')
print_info('Protip: This is how you split one card into several cards.')
while True:
answer = colored_prompt('[y/n]: ')
if answer in ['y', 'n']:
break
if answer == 'n':
break
else:
updated_card = edit(card, flipped_card_too)
card_repository.delete(conn, cursor, card)
if flipped_card_too:
card_repository.delete_flipped_card(conn, cursor, card)
else:
card_repository.insert(conn, cursor, updated_card)
card_repository.delete(conn, cursor, card)
bump_flipped_card_timestamp(conn, cursor, card)
count -= 1
if count > 0:
print_info('[{} MORE:]'.format(count))
except KeyboardInterrupt:
print()
def review(conn, cursor):
count = card_repository.count(cursor)
iterator = review_iterator(conn, cursor)
iterate(conn, cursor, iterator, count)
def browse(conn, cursor, results):
count = len(results)
iterator = browsing_iterator(results)
iterate(conn, cursor, iterator, count)
def browse_by_regex(conn, cursor):
regex = colored_prompt('Enter regular expression: ')
results = card_repository.select_by_regex(cursor, regex)
browse(conn, cursor, results)
def browse_by_regex_front(conn, cursor):
regex = colored_prompt('Enter regular expression: ')
results = card_repository.select_by_regex_front(cursor, regex)
browse(conn, cursor, results)
def browse_by_regex_back(conn, cursor):
regex = colored_prompt('Enter regular expression: ')
results = card_repository.select_by_regex_back(cursor, regex)
browse(conn, cursor, results)
def browse_by_score(conn, cursor):
score = colored_prompt('Show cards with the following score: ')
results = card_repository.select_by_score(cursor, score)
browse(conn, cursor, results)
def browse_by_last_viewed(conn, cursor):
results = card_repository.select_by_last_viewed(cursor)
browse(conn, cursor, results)
def browse_by_last_viewed_reverse(conn, cursor):
results = card_repository.select_by_last_viewed_reverse(cursor)
browse(conn, cursor, results)