1
yomichan-anki/yomi_base/anki_connect.py

122 lines
3.6 KiB
Python
Raw Normal View History

2016-05-02 01:42:17 +00:00
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
# Author: Alex Yatskov <alex@foosoft.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from PyQt4 import QtCore
from ajax import AjaxServer
import constants
2016-05-02 02:27:33 +00:00
import reader_util
2016-05-02 01:42:17 +00:00
2016-05-02 04:27:09 +00:00
class AnkiConnect:
2016-05-04 04:09:10 +00:00
def __init__(self, anki, preferences, interval=25):
2016-05-03 04:30:57 +00:00
self.anki = anki
2016-05-02 02:27:33 +00:00
self.preferences = preferences
self.server = None
2016-05-02 01:42:17 +00:00
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.advance)
self.timer.start(interval)
self.handlers = {
2016-05-03 00:27:46 +00:00
'addNote': self.apiAddNote,
'canAddNotes': self.apiCanAddNotes,
'getVersion': self.apiGetVersion,
2016-05-02 01:42:17 +00:00
}
2016-05-02 02:27:33 +00:00
2016-05-02 03:43:09 +00:00
def advance(self):
2016-05-03 00:27:46 +00:00
enabled = self.preferences['enableAnkiConnect']
2016-05-02 01:42:17 +00:00
if self.server is None and enabled:
self.server = AjaxServer(self.handler)
self.server.listen()
elif self.server is not None and not enabled:
self.server.close()
self.server = None
if self.server is not None:
self.server.advance()
2016-05-06 01:56:00 +00:00
def prepareNoteArgs(self, definition, mode):
2016-05-02 02:27:33 +00:00
if definition is None:
return None
if mode == 'vocabExp':
profile = 'vocab'
markupFunc = reader_util.markupVocabExp
elif mode == 'vocabReading':
profile = 'vocab'
markupFunc = reader_util.markupVocabReading
elif mode == 'kanji':
profile = 'kanji'
markupFunc = reader_util.markupKanji
else:
return None
profile = self.preferences['profiles'].get(profile)
if profile is None:
return None
2016-05-03 04:30:57 +00:00
markup = markupFunc(definition)
if markup is None:
return None
2016-05-02 02:27:33 +00:00
return {
'deck': profile['deck'],
'model': profile['model'],
2016-05-03 04:30:57 +00:00
'fields': reader_util.formatFields(profile['fields'], markup),
2016-05-02 02:27:33 +00:00
'tags': self.preferences['tags']
}
2016-05-02 01:42:17 +00:00
def handler(self, request):
2016-05-02 03:58:55 +00:00
action = request.get('action')
2016-05-06 01:56:00 +00:00
params = request.get('params')
2016-05-02 03:58:55 +00:00
2016-05-06 01:56:00 +00:00
return self.handlers.get(action, self.apiInvalidRequest)(params)
2016-05-02 01:42:17 +00:00
2016-05-06 01:56:00 +00:00
def apiAddNote(self, params):
args = self.prepareNoteArgs(params.get('definition'), params.get('mode'))
if args is not None:
return self.anki.addNote(args['deck'], args['model'], args['fields'], args['tags'])
2016-05-02 01:42:17 +00:00
2016-05-06 01:56:00 +00:00
def apiCanAddNotes(self, params):
2016-05-08 01:24:51 +00:00
states = []
for definition in params.get('definitions', []):
2016-05-06 03:06:36 +00:00
state = {}
2016-05-08 01:24:51 +00:00
for mode in params.get('modes', []):
2016-05-06 01:56:00 +00:00
args = self.prepareNoteArgs(definition, mode)
2016-05-08 01:24:51 +00:00
state[mode] = args is not None and self.anki.canAddNote(args['deck'], args['model'], args['fields'])
states.append(state)
2016-05-02 23:30:03 +00:00
2016-05-08 01:24:51 +00:00
return states
2016-05-02 23:30:03 +00:00
2016-05-06 01:56:00 +00:00
def apiGetVersion(self, params):
2016-05-02 01:42:17 +00:00
return {'version': constants.c['appVersion']}
2016-05-06 01:56:00 +00:00
def apiInvalidRequest(self, params):
2016-05-02 03:58:55 +00:00
return None