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">
<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 name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
</layout>

View File

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

View File

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

View File

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