use decorators instead of api_ prefix

This commit is contained in:
Alex Yatskov 2017-07-04 11:42:08 -07:00
parent cdba2c47ee
commit f57b767fba

View File

@ -61,6 +61,11 @@ except:
unicode = str unicode = str
def webApi(func):
func.webApi = True
return func
# #
# Helpers # Helpers
# #
@ -561,15 +566,16 @@ class AnkiConnect:
'Failed to listen on port {}.\nMake sure it is available and is not in use.'.format(NET_PORT) 'Failed to listen on port {}.\nMake sure it is available and is not in use.'.format(NET_PORT)
) )
def advance(self): def advance(self):
self.server.advance() self.server.advance()
def handler(self, request): def handler(self, request):
action = 'api_' + request.get('action', '') action = request.get('action', '')
if hasattr(self, action): if hasattr(self, action):
handler = getattr(self, action) handler = getattr(self, action)
if callable(handler): if hasattr(handler, 'webApi') and getattr(handler, 'webApi'):
argsAll = handler.__code__.co_varnames[1:] argsAll = handler.__code__.co_varnames[1:]
argsReq = argsAll argsReq = argsAll
@ -588,25 +594,30 @@ class AnkiConnect:
handler(**params) handler(**params)
def api_deckNames(self): @webApi
def deckNames(self):
return self.anki.deckNames() return self.anki.deckNames()
def api_modelNames(self): @webApi
def modelNames(self):
return self.anki.modelNames() return self.anki.modelNames()
def api_modelFieldNames(self, modelName): @webApi
def modelFieldNames(self, modelName):
return self.anki.modelFieldNames(modelName) return self.anki.modelFieldNames(modelName)
def api_addNote(self, note): @webApi
def addNote(self, note):
params = AnkiNoteParams(note) params = AnkiNoteParams(note)
if params.validate(): if params.validate():
return self.anki.addNote(params) return self.anki.addNote(params)
def api_addNotes(self, notes): @webApi
def addNotes(self, notes):
results = [] results = []
for note in notes: for note in notes:
params = AnkiNoteParams(note) params = AnkiNoteParams(note)
@ -618,7 +629,8 @@ class AnkiConnect:
return results return results
def api_canAddNotes(self, notes): @webApi
def canAddNotes(self, notes):
results = [] results = []
for note in notes: for note in notes:
params = AnkiNoteParams(note) params = AnkiNoteParams(note)
@ -627,7 +639,8 @@ class AnkiConnect:
return results return results
def api_upgrade(self): @webApi
def upgrade(self):
response = QMessageBox.question( response = QMessageBox.question(
self.anki.window(), self.anki.window(),
'AnkiConnect', 'AnkiConnect',
@ -648,43 +661,54 @@ class AnkiConnect:
return False return False
def api_version(self):
@webApi
def version(self):
return API_VERSION return API_VERSION
def api_guiBrowse(self, query): @webApi
def guiBrowse(self, query):
return self.anki.guiBrowse(query) return self.anki.guiBrowse(query)
def api_guiAddCards(self): @webApi
def guiAddCards(self):
return self.anki.guiAddCards() return self.anki.guiAddCards()
def api_guiCurrentCard(self): @webApi
def guiCurrentCard(self):
return self.anki.guiCurrentCard() return self.anki.guiCurrentCard()
def api_guiAnswerCard(self, ease): @webApi
def guiAnswerCard(self, ease):
return self.anki.guiAnswerCard(ease) return self.anki.guiAnswerCard(ease)
def api_guiShowQuestion(self): @webApi
def guiShowQuestion(self):
return self.anki.guiShowQuestion() return self.anki.guiShowQuestion()
def api_guiShowAnswer(self): @webApi
def guiShowAnswer(self):
return self.anki.guiShowAnswer() return self.anki.guiShowAnswer()
def api_guiDeckOverview(self, name): @webApi
def guiDeckOverview(self, name):
return self.anki.guiDeckOverview(name) return self.anki.guiDeckOverview(name)
def api_guiDeckBrowser(self): @webApi
def guiDeckBrowser(self):
return self.anki.guiDeckBrowser() return self.anki.guiDeckBrowser()
def api_guiDeckReview(self, name): @webApi
def guiDeckReview(self, name):
self.anki.guiDeckReview(name) self.anki.guiDeckReview(name)