This commit is contained in:
Alex Yatskov 2018-05-06 18:45:56 -07:00
parent e80bf2e8aa
commit fb37ac9f8a
3 changed files with 101 additions and 55 deletions

View File

@ -414,6 +414,55 @@ class AnkiConnect:
else: else:
raise Exception('cannot create note for unknown reason') raise Exception('cannot create note for unknown reason')
#
# Miscellaneous
#
@api()
def version(self):
return API_VERSION
@api()
def upgrade(self):
response = QMessageBox.question(
self.window(),
'AnkiConnect',
'Upgrade to the latest version?',
QMessageBox.Yes | QMessageBox.No
)
if response == QMessageBox.Yes:
try:
data = download(URL_UPGRADE)
path = os.path.splitext(__file__)[0] + '.py'
with open(path, 'w') as fp:
fp.write(makeStr(data))
QMessageBox.information(
self.window(),
'AnkiConnect',
'Upgraded to the latest version, please restart Anki.'
)
return True
except:
QMessageBox.critical(self.window(), 'AnkiConnect', 'Failed to download latest version.')
return False
@api()
def sync(self):
print self.window().onSync()
@api()
def multi(self, actions):
response = []
for item in actions:
response.append(self.handler(item))
return response
@api() @api()
def storeMediaFile(self, filename, data): def storeMediaFile(self, filename, data):
@ -580,14 +629,6 @@ class AnkiConnect:
return intervals return intervals
@api()
def multi(self, actions):
response = []
for item in actions:
response.append(self.handler(item))
return response
@api() @api()
def modelNames(self): def modelNames(self):
@ -750,7 +791,7 @@ class AnkiConnect:
@api() @api()
def findCards(self, query=None): def findCards(self, query=None):
if query not None: if query is None:
return [] return []
else: else:
return self.collection().findCards(query) return self.collection().findCards(query)
@ -1023,38 +1064,6 @@ class AnkiConnect:
timer.start(1000) # 1s should be enough to allow the response to be sent. timer.start(1000) # 1s should be enough to allow the response to be sent.
@api()
def sync(self):
self.window().onSync()
@api()
def upgrade(self):
response = QMessageBox.question(
self.window(),
'AnkiConnect',
'Upgrade to the latest version?',
QMessageBox.Yes | QMessageBox.No
)
if response == QMessageBox.Yes:
data = download(URL_UPGRADE)
if data is None:
QMessageBox.critical(self.window(), 'AnkiConnect', 'Failed to download latest version.')
else:
path = os.path.splitext(__file__)[0] + '.py'
with open(path, 'w') as fp:
fp.write(makeStr(data))
QMessageBox.information(self.window(), 'AnkiConnect', 'Upgraded to the latest version, please restart Anki.')
return True
return False
@api()
def version(self):
return API_VERSION
@api() @api()
def addNotes(self, notes): def addNotes(self, notes):

41
tests/test_misc.py Normal file → Executable file
View File

@ -1,10 +1,39 @@
# -*- coding: utf-8 -*- #!/usr/bin/env python
import unittest import unittest
from unittest import TestCase import util
from util import callAnkiConnectEndpoint
class TestVersion(TestCase):
class TestVersion(unittest.TestCase):
def test_version(self): def test_version(self):
response = callAnkiConnectEndpoint({'action': 'version'}) result = util.invokeNoError('version')
self.assertEqual(5, response) self.assertEqual(result, 5)
def test_upgrade(self):
util.invokeNoError('upgrade')
def test_sync(self):
util.invokeNoError('sync')
def test_multi(self):
result = util.invokeNoError(
'multi', {
'actions': [
util.request('version'),
util.request('version'),
util.request('version')
]
}
)
self.assertEqual(len(result), 3)
for response in result:
self.assertIsNone(response['error'])
self.assertEqual(response['result'], 5)
if __name__ == '__main__':
unittest.main()

View File

@ -1,11 +1,19 @@
import json import json
import urllib
import urllib2 import urllib2
def callAnkiConnectEndpoint(data):
url = 'http://docker:8888' def request(action, params={}, version=5):
dumpedData = json.dumps(data) return {'action': action, 'params': params, 'version': version}
req = urllib2.Request(url, dumpedData)
response = urllib2.urlopen(req).read()
responseData = json.loads(response) def invoke(action, params={}, version=5, url='http://localhost:8765'):
return responseData requestJson = json.dumps(request(action, params, version))
response = json.load(urllib2.urlopen(urllib2.Request(url, requestJson)))
return response['result'], response['error']
def invokeNoError(action, params={}, version=5, url='http://localhost:8765'):
result, error = invoke(action, params, version, url)
if error is not None:
raise Exception(error)
return result