Skip to content

Commit

Permalink
#19 - Looping Animation
Browse files Browse the repository at this point in the history
- Seperate hook and quiz into different files
  • Loading branch information
jcklie committed Sep 28, 2019
1 parent ed7ea19 commit 057de52
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 48 deletions.
2 changes: 1 addition & 1 deletion maobi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def patch_quiz():
from .quiz import maobi_hook
from .hook import maobi_hook

addHook("prepareQA", maobi_hook)

Expand Down
12 changes: 7 additions & 5 deletions maobi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,17 @@ def __init__(self, parent, card):
self.setWindowTitle("Maobi Configuration")

self._enabled = self._build_enabled_checkbox()
self._field = self._build_field_combo_box()
self._writing_field = self._build_field_combo_box()
self._animation_field = self._build_field_combo_box()
self._grid = self._build_grid_combo_box()
self._size = self._build_size_spin_box()
self._leniency = self._build_leniency_slider()

formGroupBox = QGroupBox("Edit Maobi configuration")
layout = QFormLayout()
layout.addRow(QLabel("Enabled:"), self._enabled)
layout.addRow(QLabel("Field:"), self._field)
layout.addRow(QLabel("Writing Field:"), self._writing_field)
layout.addRow(QLabel("Animation Field:"), self._animation_field)
layout.addRow(QLabel("Grid:"), self._grid)
layout.addRow(QLabel("Size:"), self._size)
layout.addRow(QLabel("Leniency:"), self._leniency)
Expand Down Expand Up @@ -213,8 +215,8 @@ def _load_config(self):
return

# Find the right field
idx = self._field.findText(deck_config.field)
self._field.setCurrentIndex(idx)
idx = self._writing_field.findText(deck_config.field)
self._writing_field.setCurrentIndex(idx)

self._enabled.setChecked(deck_config.enabled)

Expand Down Expand Up @@ -259,7 +261,7 @@ def _template_name(self) -> str:
return self._card.template()["name"]

def _field_name(self) -> str:
return self._field.currentText()
return self._writing_field.currentText()

def _is_enabled(self) -> bool:
return self._enabled.isChecked()
Expand Down
39 changes: 39 additions & 0 deletions maobi/hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from anki.cards import Card

from aqt import mw

from .config import MaobiConfig
from .quiz import draw_quiz
from .util import debug, _build_error_message


def maobi_hook(html: str, card: Card, context: str) -> str:
# Only show the quiz on the front side, else it can lead to rendering issues
if context not in {"reviewQuestion", "clayoutQuestion", "previewQuestion"}:
return html

# This reads from the config.json in the addon folder
maobi_config = MaobiConfig.load()

# Search the active deck configuration
deck_name = mw.col.decks.current()["name"]
template_name = card.template()["name"]
config = maobi_config.search_active_deck_config(deck_name, template_name)

# Return if we did not find it
if not config:
debug(maobi_config, f"Config not found")
return html

if not config.enabled:
debug(maobi_config, f"Config disabled")
return html

# Get the character to write and the corresponding character data
try:
html = draw_quiz(html, card, config)
except Exception as e:
debug(maobi_config, str(e))
return _build_error_message(html, str(e))

return html
47 changes: 5 additions & 42 deletions maobi/quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

from anki.cards import Card
from anki.utils import stripHTML
from aqt import mw

from .config import GridType, DeckConfig, MaobiConfig
from .util import debug, error
from .util import debug, error, MaobiException

PATH_MAOBI = os.path.dirname(os.path.realpath(__file__))
PATH_HANZI_WRITER = os.path.join(PATH_MAOBI, "hanzi-writer.min.js")
Expand Down Expand Up @@ -63,40 +62,11 @@
"""
)

class MaobiException(Exception):
def __init__(self, message):
super(MaobiException, self).__init__(message)


def maobi_hook(html: str, card: Card, context: str) -> str:
# Only show the quiz on the front side, else it can lead to rendering issues
if context not in {"reviewQuestion", "clayoutQuestion", "previewQuestion"}:
return html

# This reads from the config.json in the addon folder
maobi_config = MaobiConfig.load()

# Search the active deck configuration
deck_name = mw.col.decks.current()["name"]
template_name = card.template()["name"]
config = maobi_config.search_active_deck_config(deck_name, template_name)

# Return if we did not find it
if not config:
debug(maobi_config, f"Config not found")
return html

if not config.enabled:
debug(maobi_config, f"Config disabled")
return html

def draw_quiz(html: str, card: Card, config: MaobiConfig) -> str:
# Get the character to write and the corresponding character data
try:
character = _get_character(card, config)
character_data = _load_character_data(character)
except MaobiException as e:
debug(maobi_config, str(e))
return _build_error_message(html, str(e))
character = _get_character(card, config)
character_data = _load_character_data(character)

# Style the character div depending on the configuration
styles = []
Expand Down Expand Up @@ -214,11 +184,4 @@ def _build_hanzi_grid_style(grid_type: GridType) -> str:
return style.substitute(target_div=TARGET_DIV, svg_data=quote(svg_data))


def _build_error_message(html: str, message: str) -> str:
""" Constructs the HTML for an error text with message `message` over the original html. """
return f"""<p style="text-align: center; color: red; font-size: large;">
Maobi encountered an error: <br />
{message}
</p>
{html}
"""

15 changes: 15 additions & 0 deletions maobi/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import sys


class MaobiException(Exception):
def __init__(self, message):
super(MaobiException, self).__init__(message)


def error(msg: str):
""" Raises an error with message `msg`. This will cause a popup in Anki. """
sys.stderr.write(msg)
Expand All @@ -12,3 +17,13 @@ def debug(config, msg: str):
if config.debug:
sys.stderr.write(msg)
sys.stderr.write("\n")


def _build_error_message(html: str, message: str) -> str:
""" Constructs the HTML for an error text with message `message` over the original html. """
return f"""<p style="text-align: center; color: red; font-size: large;">
Maobi encountered an error: <br />
{message}
</p>
{html}
"""

0 comments on commit 057de52

Please sign in to comment.