1

Work on remote API

This commit is contained in:
Alex Yatskov 2016-05-01 19:27:33 -07:00
parent 6e7938a93c
commit 47905e7808
4 changed files with 55 additions and 18 deletions

View File

@ -301,6 +301,9 @@
<property name="text"> <property name="text">
<string>(see the &lt;a href=&quot;https://foosoft.net/projects/yomichan-chrome&quot;&gt;extension page&lt;/a&gt; for details)</string> <string>(see the &lt;a href=&quot;https://foosoft.net/projects/yomichan-chrome&quot;&gt;extension page&lt;/a&gt; for details)</string>
</property> </property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>

View File

@ -185,6 +185,7 @@ class Ui_DialogPreferences(object):
sizePolicy.setVerticalStretch(0) sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth()) sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
self.label_3.setSizePolicy(sizePolicy) self.label_3.setSizePolicy(sizePolicy)
self.label_3.setOpenExternalLinks(True)
self.label_3.setObjectName(_fromUtf8("label_3")) self.label_3.setObjectName(_fromUtf8("label_3"))
self.horizontalLayout_4.addWidget(self.label_3) self.horizontalLayout_4.addWidget(self.label_3)
self.verticalLayout_2.addLayout(self.horizontalLayout_4) self.verticalLayout_2.addLayout(self.horizontalLayout_4)

View File

@ -20,12 +20,14 @@
from PyQt4 import QtCore from PyQt4 import QtCore
from ajax import AjaxServer from ajax import AjaxServer
import constants import constants
import reader_util
class RemoteApi: class RemoteApi:
def __init__(self, anki, enabled, interval=50): def __init__(self, anki, preferences, interval=50):
self.preferences = preferences
self.server = None self.server = None
self.enable(enabled)
self.timer = QtCore.QTimer() self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.advance) self.timer.timeout.connect(self.advance)
self.timer.start(interval) self.timer.start(interval)
@ -36,6 +38,8 @@ class RemoteApi:
'getVersion': self.apiGetVersion, 'getVersion': self.apiGetVersion,
} }
self.enable(self.preferences['enableRemoteApi'])
def enable(self, enabled=True): def enable(self, enabled=True):
if self.server is None and enabled: if self.server is None and enabled:
@ -51,25 +55,53 @@ class RemoteApi:
self.server.advance() self.server.advance()
def prepareNoteParams(self, definition, mode):
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
fields = reader_util.formatFields(
profile['fields'],
markupFunc(definition)
)
return {
'deck': profile['deck'],
'model': profile['model'],
'fields': fields,
'tags': self.preferences['tags']
}
def handler(self, request): def handler(self, request):
self.handlers.get(request.get('action'), self.apiInvalidRequest)(request.get('data')) self.handlers.get(request.get('action'), self.apiInvalidRequest)(request.get('data'))
def apiAddNote(self, data): def apiAddNote(self, data):
deckName = data.get('deckName', unicode()) params = self.prepareNoteParams(data.get('definition'), data.get('mode'))
modelName = data.get('modelName', unicode()) if params is not None:
fields = data.get('fields', {}) return self.anki.addNote(params['deck'], params['model'], params['fields'], params['tags'])
tags = data.get('tags', [])
return self.anki.addNote(deckName, modelName, fields, tags)
def apiCanAddNote(self, data): def apiCanAddNote(self, data):
deckName = data.get('deckName', unicode()) params = self.prepareNoteParams(data.get('definition'), data.get('mode'))
modelName = data.get('modelName', unicode()) if params is not None:
fields = data.get('fields', {}) return self.anki.canAddNote(params['deck'], params['model'], params['fields'])
return self.anki.canAddNote(deckName, modelName, fields)
def apiGetVersion(self, data): def apiGetVersion(self, data):

View File

@ -28,6 +28,7 @@ import sys
class Yomichan: class Yomichan:
def __init__(self): def __init__(self):
self.language = japanese.initLanguage() self.language = japanese.initLanguage()
self.preferences = Preferences() self.preferences = Preferences()
self.preferences.load() self.preferences.load()
@ -40,7 +41,7 @@ class YomichanPlugin(Yomichan):
self.window = None self.window = None
self.anki = anki_bridge.Anki() self.anki = anki_bridge.Anki()
self.parent = self.anki.window() self.parent = self.anki.window()
self.remoteApi = RemoteApi(self.anki, self.preferences['enableRemoteApi']) self.remoteApi = RemoteApi(self.anki, self.preferences)
separator = QtGui.QAction(self.parent) separator = QtGui.QAction(self.parent)
separator.setSeparator(True) separator.setSeparator(True)