51
51
d = datetime .datetime .today ()
52
52
today = f"{ d .day :02d} _{ d .month :02d} _{ d .year :04d} "
53
53
54
+ whi ("Loading api keys" )
54
55
load_api_keys ()
55
56
56
57
@@ -184,7 +185,7 @@ def handle_exception(exc_type, exc_value, exc_traceback):
184
185
[print (line ) for line in traceback .format_tb (exc_traceback )]
185
186
print (str (exc_value ))
186
187
print (str (exc_type ))
187
- print ("\n --verbose was used so opening debug console at the "
188
+ print ("\n --debug was used so opening debug console at the "
188
189
"appropriate frame. Press 'c' to continue to the frame "
189
190
"of this print." )
190
191
pdb .post_mortem (exc_traceback )
@@ -203,13 +204,14 @@ def handle_exception(exc_type, exc_value, exc_traceback):
203
204
print (json .dumps (db_content , ensure_ascii = False , indent = 4 ))
204
205
return
205
206
else :
206
- sync_anki ()
207
+ # sync_anki()
207
208
assert query is not None , "Must specify --query"
208
209
assert dataset_path is not None , "Must specify --dataset_path"
209
210
litellm .set_verbose = verbose
210
211
211
212
# arg sanity check and storing
212
- assert "note:" in query , "You have to specify a notetype in the query"
213
+ # TODO: Is this needed? The example in the readme doesn't set it
214
+ # assert "note:" in query, f"You have to specify a notetype in the query ({query})"
213
215
assert mode in ["reformulate" , "reset" ], "Invalid value for 'mode'"
214
216
assert isinstance (exclude_done , bool ), "exclude_done must be a boolean"
215
217
assert isinstance (exclude_version , bool ), "exclude_version must be a boolean"
@@ -225,7 +227,7 @@ def handle_exception(exc_type, exc_value, exc_traceback):
225
227
main_field_index = int (main_field_index )
226
228
assert main_field_index >= 0 , "invalid field_index"
227
229
self .mode = mode
228
- if string_formatting is not None :
230
+ if string_formatting :
229
231
red (f"Loading specific string formatting from { string_formatting } " )
230
232
cloze_input_parser , cloze_output_parser = load_formatting_funcs (
231
233
path = string_formatting ,
@@ -264,14 +266,14 @@ def handle_exception(exc_type, exc_value, exc_traceback):
264
266
query += f" -AnkiReformulator:\" *version*=*'{ self .VERSION } '*\" "
265
267
266
268
# load db just in case
267
- self . db_content = self . load_db ()
268
- if not self . db_content :
269
- red (
270
- "Empty database. If you have already ran anki_reformulator "
271
- "before then something went wrong! "
272
- )
273
- else :
274
- self .compute_cost (self .db_content )
269
+
270
+ # TODO: How is the user supposed to create the database in the first place?
271
+ # self.db_content = self.load_db()
272
+ # if not self.db_content:
273
+ # red("Empty database. If you have already ran anki_reformulator "
274
+ # "before then something went wrong!" )
275
+ # else:
276
+ # self.compute_cost(self.db_content)
275
277
276
278
# load dataset
277
279
dataset = load_dataset (dataset_path )
@@ -286,9 +288,7 @@ def handle_exception(exc_type, exc_value, exc_traceback):
286
288
nids = anki (action = "findNotes" ,
287
289
query = "tag:AnkiReformulator::RESETTING" )
288
290
if nids :
289
- red (
290
- f"Found { len (nids )} notes with tag AnkiReformulator::RESETTING : { nids } "
291
- )
291
+ red (f"Found { len (nids )} notes with tag AnkiReformulator::RESETTING : { nids } " )
292
292
nids = anki (action = "findNotes" , query = "tag:AnkiReformulator::DOING" )
293
293
if nids :
294
294
red (f"Found { len (nids )} notes with tag AnkiReformulator::DOING : { nids } " )
@@ -298,13 +298,10 @@ def handle_exception(exc_type, exc_value, exc_traceback):
298
298
assert nids , f"No notes found for the query '{ query } '"
299
299
300
300
# find the model field names
301
- fields = anki (
302
- action = "notesInfo" ,
303
- notes = [int (nids [0 ])]
304
- )[0 ]["fields" ]
305
- assert (
306
- "AnkiReformulator" in fields .keys ()
307
- ), "The notetype to edit must have a field called 'AnkiReformulator'"
301
+ fields = anki (action = "notesInfo" ,
302
+ notes = [int (nids [0 ])])[0 ]["fields" ]
303
+ # assert "AnkiReformulator" in fields.keys(), \
304
+ # "The notetype to edit must have a field called 'AnkiReformulator'"
308
305
self .field_name = list (fields .keys ())[0 ]
309
306
310
307
if self .exclude_media :
@@ -328,9 +325,8 @@ def handle_exception(exc_type, exc_value, exc_traceback):
328
325
self .notes = self .notes .loc [nids ]
329
326
assert not self .notes .empty , "Empty notes df"
330
327
331
- assert (
332
- len (set (self .notes ["modelName" ].tolist ())) == 1
333
- ), "Contains more than 1 note type"
328
+ assert len (set (self .notes ["modelName" ].tolist ())) == 1 , \
329
+ "Contains more than 1 note type"
334
330
335
331
# check absence of image and sounds in the main field
336
332
# as well incorrect tags
@@ -358,11 +354,9 @@ def handle_exception(exc_type, exc_value, exc_traceback):
358
354
else :
359
355
assert not tag .lower ().startswith ("ankireformulator" )
360
356
361
-
362
357
# check if too many tokens
363
358
tkn_sum = sum ([tkn_len (d ["content" ]) for d in self .dataset ])
364
359
tkn_sum += sum (
365
- [
366
360
tkn_len (
367
361
replace_media (
368
362
content = note ["fields" ][self .field_name ]["value" ],
@@ -371,7 +365,7 @@ def handle_exception(exc_type, exc_value, exc_traceback):
371
365
)[0 ]
372
366
)
373
367
for _ , note in self .notes .iterrows ()
374
- ] )
368
+ )
375
369
if tkn_sum > tkn_warn_limit :
376
370
raise Exception (
377
371
f"Found { tkn_sum } tokens to process, which is "
@@ -983,7 +977,7 @@ def load_db(self) -> Dict:
983
977
All log dictionaries from the database, or False if database not found
984
978
"""
985
979
if not (REFORMULATOR_DIR / "reformulator.db" ).exists ():
986
- red ("db not found: '$ REFORMULATOR_DIR/reformulator.db'" )
980
+ red (f "db not found: '{ REFORMULATOR_DIR } /reformulator.db'" )
987
981
return False
988
982
conn = sqlite3 .connect (str ((REFORMULATOR_DIR / "reformulator.db" ).absolute ()))
989
983
cursor = conn .cursor ()
@@ -1000,10 +994,10 @@ def load_db(self) -> Dict:
1000
994
try :
1001
995
args , kwargs = fire .Fire (lambda * args , ** kwargs : [args , kwargs ])
1002
996
if "help" in kwargs :
1003
- print (help (AnkiReformulator ))
997
+ print (help (AnkiReformulator ), file = sys . stderr )
1004
998
else :
1005
999
whi (f"Launching reformulator.py with args '{ args } ' and kwargs '{ kwargs } '" )
1006
1000
AnkiReformulator (* args , ** kwargs )
1001
+ sync_anki ()
1007
1002
except Exception :
1008
- sync_anki ()
1009
1003
raise
0 commit comments