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:
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()
def storeMediaFile(self, filename, data):
@ -580,14 +629,6 @@ class AnkiConnect:
return intervals
@api()
def multi(self, actions):
response = []
for item in actions:
response.append(self.handler(item))
return response
@api()
def modelNames(self):
@ -750,7 +791,7 @@ class AnkiConnect:
@api()
def findCards(self, query=None):
if query not None:
if query is None:
return []
else:
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.
@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()
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
from unittest import TestCase
from util import callAnkiConnectEndpoint
import util
class TestVersion(TestCase):
class TestVersion(unittest.TestCase):
def test_version(self):
response = callAnkiConnectEndpoint({'action': 'version'})
self.assertEqual(5, response)
result = util.invokeNoError('version')
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 urllib
import urllib2
def callAnkiConnectEndpoint(data):
url = 'http://docker:8888'
dumpedData = json.dumps(data)
req = urllib2.Request(url, dumpedData)
response = urllib2.urlopen(req).read()
responseData = json.loads(response)
return responseData
def request(action, params={}, version=5):
return {'action': action, 'params': params, 'version': version}
def invoke(action, params={}, version=5, url='http://localhost:8765'):
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