This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvocabulary.py
269 lines (236 loc) · 9.74 KB
/
vocabulary.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
#!/usr/bin/env python3
"""This module contains the Vocbulary class.
It's used as a data source for the main table.
"""
from os.path import getsize
import json
import random
import ui
class Vocabulary:
"""The core class to process all of the word data.
Data is stored as a list of dictionaries. In the future, this may change
to something more robust like a SQL database.
"""
def __init__(self, data_file: str):
"""Load the vocabulary from a given data file."""
# _words[0] is words with notes. _words[1] is history
self._words = [{}, {}]
self.query = '' # used for searching the list
self.fulltext_toggle = False
self.data_file = data_file
self.data_id = None # Identifies sync conflicts
self.load_json_file()
def load_json_file(self, filename=''):
"""Load vocabulary data from the JSON file."""
if not filename:
filename = self.data_file
try:
with open(filename, 'r') as infile:
self._words = json.load(infile)
self.data_id = getsize(filename)
except FileNotFoundError:
with open('default-' + filename, 'r') as infile:
self._words = json.load(infile)
def save_json_file(self, indent=1):
"""Save vocabulary data to the JSON file."""
with open(self.data_file, 'w') as outfile:
json.dump(self._words, outfile, indent=indent)
self.data_id = getsize(self.data_file)
def verify_data(self):
"""Merge new data.
This compares the current data file to the original. If they don't
match, assumes that it's because new iCloud data synced from another
device. It replaces the old data with fresh data.
TODO: this method is very crude and will not account for all types of
sync conflicts. It should probably be revisited.
"""
if self.data_id != getsize(self.data_file):
print('data not in sync! merging data...')
with open(self.data_file, 'r') as infile:
self._words = json.load(infile)
self.data_id = getsize(self.data_file)
#self._words = [{**self._words[0], **new_words[0]},
# {**self._words[1], **new_words[1]}]
def set_word(self, word: str, notes=''):
"""Add a word or updates the word if it already exists.
After this, call `del_dup_word()`.
Return either a tuple that can be passed to Table.insert_rows(), or
return `None` if there is no row to insert.
"""
word = word.strip()
new_word = True
if word and notes: # adds to notes
section = 0
elif word: # adds to history
section = 1
if word in self._words[section]:
new_word = False
self.verify_data()
self._words[section][word] = notes
self.save_json_file()
word_list = self.list_words(section)
if word in word_list: # a word could be filtered out by a query
row = word_list.index(word)
else:
return None
if self.query:
section += 1
if new_word:
return row, section
else:
return None
def del_dup_word(self, word: str, notes=''):
"""Delete any duplicate entries of a word in the wrong section.
Call this after `set_word()`.
Return a tuple that can be passed to TableView.delete_rows(), or
return `None` if there is no row to delete.
"""
word = word.strip()
section = None
row = None
if notes and word in self._words[1]:
# checks if the word has notes and is in history
section = 1
row = self.list_words(1).index(word)
elif not notes and word in self._words[0]:
# checks if a word without notes is in the notes section
section = 0
row = self.list_words(0).index(word)
if section is not None:
self.delete_word(section, word)
if self.query:
section += 1
return row, section
else:
return None
def get_notes(self, word: str):
"""Look up a word and return its notes.
Words without notes return empty strings. This can be used to check if
a word is in the personal dictionary or not.
"""
word = word.strip()
if word in self._words[0]:
return self._words[0][word]
else:
return ''
def set_query(self, query: str):
"""Set a string that filters `list_words()` output.
To "unset" the query, just set an empty string.
"""
self.query = query.strip()
def _filter_query(self, word):
hasdef = False
if self.fulltext_toggle and word in self._words[0]:
w = self._words[0][word].casefold()
hasdef = w.find(self.query.casefold()) != -1
wordbegins = word.casefold().startswith(self.query.casefold())
return hasdef or wordbegins
def count_words(self, section: int):
"""Return the number of words in a section."""
return len(self.list_words(section))
def list_words(self, section: int):
"""Return a list of words in a section."""
if self.query:
words = filter(self._filter_query, self._words[section].keys())
else:
words = self._words[section].keys()
return sorted(words, key=lambda s: s.casefold())
def delete_word(self, section: int, word: str):
"""Delete a word."""
word = word.strip()
del self._words[section][word]
self.save_json_file()
def delete_multiple(self, rows: list):
"""Call to delete several words at once.
Returns an iterator of the words deleted.
"""
# first we extract the words from the rows
wordlist = []
for row in rows:
s = row[0]
if self.query:
s -= 1
word = self.list_words(section=s)[row[1]]
wordlist.append((s, word))
# then we deleted them
# it has to be a two-step process or else the indexes will be off
for section, word in wordlist:
del self._words[section][word]
self.save_json_file()
# Then we return the words
return (x[1] for x in wordlist)
def random_word(self):
"""Return a random word with notes."""
return random.choice(list(self._words[0].keys()))
# ---- Tableview methods
# the `_query` variable activates a hack that inserts a section with search
# suggestions. I might add actual suggestions from the WordNik API sometime
def tableview_number_of_sections(self, tableview):
"""Return the number of sections."""
if self.query:
# when there's a query, we add a section for it.
return 3
else:
return 2
def tableview_title_for_header(self, tableview, section):
"""Return a title for the given section."""
headers = ['Words with notes', 'Words from history']
if self.query:
# returns an extra section for search suggestions
headers.insert(0, '')
return headers[section]
def tableview_number_of_rows(self, tableview, section):
"""Return the number of rows in the section."""
if self.query and section == 0:
# The search suggestions always have 1 row.
return 1
elif self.query:
# The extra section doesn't exist in the data, so we delete it
# before calling other methods
section -= 1
return self.count_words(section)
def tableview_cell_for_row(self, tableview, section, row):
"""Create and return a cell for the given section/row."""
if self.query and section == 0 and row == 0:
# Adds a special table cell for search suggestions
cell = ui.TableViewCell('value1')
cell.text_label.text = self.query
detail = 'Look up “' + self.query + '”'
cell.detail_text_label.text = detail
cell.image_view.image = ui.Image.named('iob:ios7_search_24')
cell.accessory_type = 'disclosure_indicator'
return cell
elif self.query:
# The extra section doesn't exist in the data, so we delete it
# before calling other methods
section -= 1
cell = ui.TableViewCell()
cell.text_label.text = self.list_words(section=section)[row]
if section == 0:
img = 'iob:document_text_24'
elif section == 1:
img = 'iob:ios7_clock_outline_24'
cell.image_view.image = ui.Image.named(img)
cell.accessory_type = 'disclosure_indicator'
return cell
def tableview_can_delete(self, tableview, section, row):
"""Return True if the user should be able to delete the given row."""
if self.query and section == 0: # Can't delete suggestions
return False
else:
return True
def tableview_delete(self, tableview, section, row):
"""Call when the user confirms deletion of the given row."""
s = section
if self.query:
# The extra section doesn't exist in the data, so we delete it
# before calling other methods
s -= 1
word = self.list_words(section=s)[row]
self.delete_word(s, word)
tableview.delete_rows([(row, section)])
# This is a slightly hacky way to make sure that when the selected word
# is deleted, it also gets cleared from the WordView
wordview = tableview.superview.navigation_view.superview.content_column
if wordview['word'].text == word:
wordview.clear()