Skip to content

Commit

Permalink
Merge pull request #43 from zjosua/update-template
Browse files Browse the repository at this point in the history
Add template updating code from Image Occlusion Enhanced
  • Loading branch information
zjosua authored Mar 17, 2021
2 parents 0936b95 + ccfaf3e commit 2c2ac73
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/multiple_choice/packaging"]
path = src/multiple_choice/packaging
url = https://github.com/pypa/packaging
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The add-on will automatically style your choices based on whether you answered c
It is licensed under the AGPLv3.
For more information refer to the [LICENSE](https://github.com/zjosua/anki-mc/blob/master/LICENSE) file.

The files `__init__.py` and the template file are based on the Anki add-on [Cloze Overlapper](https://github.com/glutanimate/cloze-overlapper) by Glutanimate.
A bunch of code in this add-on is based on the Anki add-ons [Image Occlusion Enhanced](https://github.com/glutanimate/image-occlusion-enhanced) and [Cloze Overlapper](https://github.com/glutanimate/cloze-overlapper) by Glutanimate.
[Click here to support Glutanimate's work](https://glutanimate.com/support-my-work/).

Persistence is achieved using the code from [Simon Lammer's anki-persistence](https://github.com/SimonLammer/anki-persistence).
Expand Down
2 changes: 1 addition & 1 deletion docs/ankiweb.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
It is licensed under the AGPLv3.
For more information refer to the <a href="https://github.com/zjosua/anki-mc/blob/master/LICENSE">LICENSE</a> file.

The files <code>__init__.py</code> and the template file are based on the Anki add-on <a href="https://github.com/glutanimate/cloze-overlapper">Cloze Overlapper</a> by Glutanimate.
A bunch of code in this add-on is based on the Anki add-ons <a href="https://github.com/glutanimate/image-occlusion-enhanced">Image Occlusion Enhanced</a> and <a href="https://github.com/glutanimate/cloze-overlapper">Cloze Overlapper</a> by Glutanimate.
<a href="https://glutanimate.com/support-my-work/">Click here to support Glutanimate&#39;s work.</a>

Persistence is achieved using the code from <a href="https://github.com/SimonLammer/anki-persistence">Simon Lammer&#39;s anki-persistence</a>.
Expand Down
24 changes: 22 additions & 2 deletions src/multiple_choice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,30 @@

from aqt import gui_hooks, mw

from .template import initializeModel
from .config import *
from .packaging.packaging import version
from .template import *

def getOrCreateModel():
model = mw.col.models.byName(aio_model)
if not model:
# create model
model = addModel(mw.col)
return model
model_version = mw.col.get_config('mc_conf')['version']
if version.parse(model_version) < version.parse(default_conf_syncd['version']):
return updateTemplate(mw.col)
return model

def delayedInit():
initializeModel()
"""Setup add-on config and templates, update if necessary"""
getSyncedConfig()
getLocalConfig()
getOrCreateModel()
if version.parse(mw.col.get_config("mc_conf")['version']) < version.parse(default_conf_syncd['version']):
updateSyncedConfig()
if version.parse(mw.pm.profile['mc_conf'].get('version', 0)) < version.parse(default_conf_syncd['version']):
updateLocalConfig()

gui_hooks.profile_did_open.append(delayedInit)

80 changes: 80 additions & 0 deletions src/multiple_choice/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Multiple Choice for Anki
#
# Copyright (C) 2018-2021 zjosua <https://github.com/zjosua>
#
# This file is based on config.py from Glutanimate's
# Image Occlusion Enhanced Add-on for Anki
#
# Copyright (C) 2016-2020 Aristotelis P. <https://glutanimate.com/>
# Copyright (C) 2012-2015 Tiago Barroso <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version, with the additions
# listed at the end of the license file that accompanied this program.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# NOTE: This program is subject to certain additional terms pursuant to
# Section 7 of the GNU Affero General Public License. You should have
# received a copy of these additional terms immediately following the
# terms and conditions of the GNU Affero General Public License that
# accompanied this program.
#
# If not, please request a copy through one of the means of contact
# listed here: <https://glutanimate.com/contact/>.
#
# Any modifications to this file must keep this entire header intact.

"""
Sets up configuration
"""

from aqt import mw

# Compare semantic version: https://stackoverflow.com/a/11887885
from .packaging.packaging import version

# default configurations
# TODO: update version number before release
default_conf_local = {'version': "2.4.0"}
default_conf_syncd = {'version': "2.4.0"}

def getSyncedConfig():
# Synced preferences
if mw.col.get_config("mc_conf") is None:
# create initial configuration
mw.col.set_config("mc_conf", default_conf_syncd)
mw.col.setMod()

return mw.col.get_config("mc_conf")

def updateSyncedConfig():
print("Updating config DB from earlier MC release")
tmp_conf = mw.col.get_config("mc_conf")
for key in list(default_conf_syncd.keys()):
if key not in mw.col.get_config("mc_conf"):
tmp_conf[key] = default_conf_syncd[key]
tmp_conf['version'] = default_conf_syncd['version']
mw.col.set_config("mc_conf", tmp_conf)
mw.col.setMod()

def getLocalConfig():
# Local preferences
if 'mc_conf' not in mw.pm.profile:
mw.pm.profile["mc_conf"] = default_conf_local

return mw.pm.profile["mc_conf"]

def updateLocalConfig():
for key in list(default_conf_local.keys()):
if key not in mw.col.get_config("mc_conf"):
mw.pm.profile["mc_conf"][key] = default_conf_local[key]
mw.pm.profile['mc_conf']['version'] = default_conf_local['version']
1 change: 1 addition & 0 deletions src/multiple_choice/packaging
Submodule packaging added at 456f94
13 changes: 4 additions & 9 deletions src/multiple_choice/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,17 +510,12 @@ def addModel(col):
return model

def updateTemplate(col):
"""Update add-on card templates"""
print("Updating %s card template".format(kprim_model))
model = col.models.byName(kprim_model)
"""Update add-on note templates"""
print(f"Updating {aio_model} note template")
model = col.models.byName(aio_model)
template = model['tmpls'][0]
template['qfmt'] = card_front
template['afmt'] = card_back
model['css'] = card_css
col.models.save()
col.models.save(model)
return model

def initializeModel():
model = mw.col.models.byName(aio_model)
if not model:
model = addModel(mw.col)

0 comments on commit 2c2ac73

Please sign in to comment.