--- a/py/gadict_srs_anki.py Fri Nov 25 10:42:34 2016 +0200
+++ b/py/gadict_srs_anki.py Fri Nov 25 10:48:21 2016 +0200
@@ -9,6 +9,11 @@
import signal
import regex
+import hashlib
+
+import anki
+from anki.exporting import AnkiPackageExporter
+
import gadict
import gadict_freq
@@ -108,7 +113,7 @@
# Looks like anki libs change working directory to media directory of current deck
# Therefore absolute path should be stored before creating temporary deck
FONAME = os.path.abspath(FONAME)
-TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))
+TMPDIR = tempfile.mkdtemp(dir=os.path.dirname(FONAME))
if not NAME:
NAME, _ = os.path.splitext(os.path.basename(FINAME))
@@ -118,11 +123,7 @@
else:
FDEL = None
-import hashlib
-
-import anki
-from anki.exporting import AnkiPackageExporter
-
+################################################################
MODEL_CSS = """
.card {
@@ -209,7 +210,7 @@
deck_id = collection.decks.id(self.name)
# It is essential to keep model['id'] unchanged between upgrades!!
- MODEL_ID = int(hashlib.sha1(self.name).hexdigest(), 16) % (2**63)
+ model_id = int(hashlib.sha1(self.name).hexdigest(), 16) % (2**63)
################################################################
# Regular card model. SafeBack doesn't include examples to not spoil
@@ -236,7 +237,7 @@
# Equivalent of ``collection.models.add(model)`` without setting
# auto-generated ID.
# Increment +1 is for keeping model['id'] unique from previous v0.9 release.
- model['id'] = MODEL_ID + 1
+ model['id'] = model_id + 1
collection.models.update(model)
collection.models.save(model)
self.model = model
@@ -287,7 +288,7 @@
tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
collection.models.addTemplate(model, tmpl)
- model['id'] = MODEL_ID + 2 # Keep model['id'] unique.
+ model['id'] = model_id + 2 # Keep model['id'] unique.
collection.models.update(model)
collection.models.save(model)
self.model_irr = model
@@ -316,7 +317,7 @@
tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
collection.models.addTemplate(model, tmpl)
- model['id'] = MODEL_ID + 3 # Keep model['id'] unique.
+ model['id'] = model_id + 3 # Keep model['id'] unique.
collection.models.update(model)
collection.models.save(model)
self.model_pl = model
@@ -329,7 +330,7 @@
h = hashlib.md5(":".join((self.name, nodetype, headword)))
return h.hexdigest()
- def add_note(self, headword, front, back, safeback, freq, tags = None):
+ def add_note(self, headword, front, back, safeback, freq, tags=None):
note = anki.notes.Note(self.collection, self.model)
note['Front'] = front
note['Back'] = back
@@ -339,7 +340,7 @@
note.guid = self.guid("front/back", headword)
self.collection.addNote(note)
- def add_note_irr(self, headword, v1, v2, v2alt, v3, v3alt, front, back, freq, tags = None):
+ def add_note_irr(self, headword, v1, v2, v2alt, v3, v3alt, front, back, freq, tags=None):
note = anki.notes.Note(self.collection, self.model_irr)
note['V1'] = v1
note['V2'] = v2
@@ -353,7 +354,7 @@
note.guid = self.guid("irregular verb", headword)
self.collection.addNote(note)
- def add_note_pl(self, headword, singular, plural, front, back, freq, tags = None):
+ def add_note_pl(self, headword, singular, plural, front, back, freq, tags=None):
note = anki.notes.Note(self.collection, self.model_pl)
note['Singular'] = singular
note['Plural'] = plural
@@ -371,7 +372,7 @@
def close(self):
self.collection.close()
-def write_sense(buf, sense, with_examples = True):
+def write_sense(buf, sense, with_examples=True):
buf.append("<div class='sense'>")
if sense.pos:
buf.append("<span class='pos'>")
@@ -439,14 +440,14 @@
buf.append("</div>")
try:
- builder = AnkiDbBuilder(TMPDIR, NAME)
+ BUILDER = AnkiDbBuilder(TMPDIR, NAME)
for identity in FDEL or []:
identity = identity.strip()
warnmsg = "<div class='del'>Please delete this note ({})</div>".format(identity)
- builder.add_note(identity, warnmsg, warnmsg, warnmsg, "del")
- builder.add_note_irr(identity, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, "del")
- builder.add_note_pl(identity, warnmsg, warnmsg, warnmsg, warnmsg, "del")
+ BUILDER.add_note(identity, warnmsg, warnmsg, warnmsg, "del")
+ BUILDER.add_note_irr(identity, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, "del")
+ BUILDER.add_note_pl(identity, warnmsg, warnmsg, warnmsg, warnmsg, "del")
for (headwords, translations) in DOM[1:]:
identity = headwords[0].headword
@@ -497,13 +498,13 @@
direct_from = "".join(buf)
buf = []
for sense in translations:
- write_sense(buf, sense, with_examples = True)
+ write_sense(buf, sense, with_examples=True)
direct_to = "".join(buf)
buf = []
for sense in translations:
- write_sense(buf, sense, with_examples = False)
+ write_sense(buf, sense, with_examples=False)
reverse_from = "".join(buf) # without examples!!
- builder.add_note(identity, direct_from, direct_to, reverse_from, freqmsg, freqtags)
+ BUILDER.add_note(identity, direct_from, direct_to, reverse_from, freqmsg, freqtags)
if v1 and v2 and v3 and RICH_MODE:
riddle1 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v1</span>".format(v1[0], v1[1])
riddle2 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2[0], v2[1])
@@ -511,18 +512,18 @@
if v2alt:
riddle2alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2alt[0], v2alt[1])
else:
- riddle2alt = ""
+ riddle2alt = u""
if v3alt:
riddle3alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v3alt[0], v3alt[1])
else:
- riddle3alt = ""
- builder.add_note_irr(identity, riddle1, riddle2, riddle2alt, riddle3, riddle3alt, direct_from, direct_to, freqmsg)
+ riddle3alt = u""
+ BUILDER.add_note_irr(identity, riddle1, riddle2, riddle2alt, riddle3, riddle3alt, direct_from, direct_to, freqmsg)
if singular and plural and RICH_MODE:
riddle_s = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>s</span>".format(singular[0], singular[1])
riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1])
- builder.add_note_pl(identity, riddle_s, riddle_pl, direct_from, direct_to, freqmsg)
+ BUILDER.add_note_pl(identity, riddle_s, riddle_pl, direct_from, direct_to, freqmsg)
- builder.export(FONAME)
+ BUILDER.export(FONAME)
finally:
- builder.close()
+ BUILDER.close()
shutil.rmtree(TMPDIR, ignore_errors=True)