improvements to python interop

This commit is contained in:
Alex Yatskov 2017-07-04 11:29:09 -07:00
parent 0507add4c6
commit cdba2c47ee

View File

@ -567,12 +567,25 @@ class AnkiConnect:
def handler(self, request): def handler(self, request):
action = 'api_' + request.get('action', '') action = 'api_' + request.get('action', '')
params = request.get('params', {})
if hasattr(self, action): if hasattr(self, action):
try: handler = getattr(self, action)
return getattr(self, action)(**params) if callable(handler):
except TypeError: argsAll = handler.__code__.co_varnames[1:]
return None argsReq = argsAll
argsDef = handler.__defaults__
if argsDef is not None:
argsReq = argsAll[:-len(argsDef)]
params = request.get('params', {})
for argReq in argsReq:
if argReq not in params:
return
for param in params:
if param not in argsAll:
return
handler(**params)
def api_deckNames(self): def api_deckNames(self):