Anki/pylib/anki/importing/pauker.py
RumovZ 9dc3cf216a
PEP8 for rest of pylib (#1451)
* PEP8 dbproxy.py

* PEP8 errors.py

* PEP8 httpclient.py

* PEP8 lang.py

* PEP8 latex.py

* Add decorator to deprectate key words

* Make replacement for deprecated attribute optional

* Use new helper `_print_replacement_warning()`

* PEP8 media.py

* PEP8 rsbackend.py

* PEP8 sound.py

* PEP8 stdmodels.py

* PEP8 storage.py

* PEP8 sync.py

* PEP8 tags.py

* PEP8 template.py

* PEP8 types.py

* Fix DeprecatedNamesMixinForModule

The class methods need to be overridden with instance methods, so every
module has its own dicts.

* Use `# pylint: disable=invalid-name` instead of id

* PEP8 utils.py

* Only decorate `__getattr__` with `@no_type_check`

* Fix mypy issue with snakecase

Importing it from `anki._vendor` raises attribute errors.

* Format

* Remove inheritance of DeprecatedNamesMixin

There's almost no shared code now and overriding classmethods with
instance methods raises mypy issues.

* Fix traceback frames of deprecation warnings

* remove fn/TimedLog (dae)

Neither Anki nor add-ons appear to have been using it

* fix some issues with stringcase use (dae)

- the wheel was depending on the PyPI version instead of our vendored
version
- _vendor:stringcase should not have been listed in the anki py_library.
We already include the sources in py_srcs, and need to refer to them
directly. By listing _vendor:stringcase as well, we were making a
top-level stringcase library available, which would have only worked for
distributing because the wheel definition was also incorrect.
- mypy errors are what caused me to mistakenly add the above - they
were because the type: ignore at the top of stringcase.py was causing
mypy to completely ignore the file, so it was not aware of any attributes
it contained.
2021-10-25 14:50:13 +10:00

92 lines
2.7 KiB
Python

# Copyright: Andreas Klauer <Andreas.Klauer@metamorpher.de>
# License: BSD-3
import gzip
import html
import math
import random
import time
import xml.etree.ElementTree as ET
from anki.importing.noteimp import ForeignCard, ForeignNote, NoteImporter
from anki.stdmodels import _legacy_add_forward_reverse
ONE_DAY = 60 * 60 * 24
class PaukerImporter(NoteImporter):
"""Import Pauker 1.8 Lesson (*.pau.gz)"""
needMapper = False
allowHTML = True
def run(self):
model = _legacy_add_forward_reverse(self.col)
model["name"] = "Pauker"
self.col.models.save(model, updateReqs=False)
self.col.models.set_current(model)
self.model = model
self.initMapping()
NoteImporter.run(self)
def fields(self):
"""Pauker is Front/Back"""
return 2
def foreignNotes(self):
"""Build and return a list of notes."""
notes = []
try:
f = gzip.open(self.file)
tree = ET.parse(f) # type: ignore
lesson = tree.getroot()
assert lesson.tag == "Lesson"
finally:
f.close()
index = -4
for batch in lesson.findall("./Batch"):
index += 1
for card in batch.findall("./Card"):
# Create a note for this card.
front = card.findtext("./FrontSide/Text")
back = card.findtext("./ReverseSide/Text")
note = ForeignNote()
assert front and back
note.fields = [
html.escape(x.strip())
.replace("\n", "<br>")
.replace(" ", " &nbsp;")
for x in [front, back]
]
notes.append(note)
# Determine due date for cards.
frontdue = card.find("./FrontSide[@LearnedTimestamp]")
backdue = card.find("./ReverseSide[@Batch][@LearnedTimestamp]")
if frontdue is not None:
note.cards[0] = self._learnedCard(
index, int(frontdue.attrib["LearnedTimestamp"])
)
if backdue is not None:
note.cards[1] = self._learnedCard(
int(backdue.attrib["Batch"]),
int(backdue.attrib["LearnedTimestamp"]),
)
return notes
def _learnedCard(self, batch, timestamp):
ivl = math.exp(batch)
now = time.time()
due = ivl - (now - timestamp / 1000.0) / ONE_DAY
fc = ForeignCard()
fc.due = self.col.sched.today + int(due + 0.5)
fc.ivl = random.randint(int(ivl * 0.90), int(ivl + 0.5))
fc.factor = random.randint(1500, 2500)
return fc