From 074177e90be5ad674924fdfe8a558199577d89db Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 27 Aug 2017 15:32:49 -0700 Subject: [PATCH] add decorator for versioning --- AnkiConnect.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/AnkiConnect.py b/AnkiConnect.py index b5f2774..81b7475 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -67,6 +67,16 @@ else: # Helpers # +def webapi(*versions): + def decorator(func): + def method(*args, **kwargs): + return func(*args, **kwargs) + setattr(method, 'versions', versions) + setattr(method, 'api', True) + return method + return decorator + + def webApi(func): func.webApi = True return func @@ -901,6 +911,24 @@ class AnkiConnect: return handler(**params) + def invoke(self, version, name, *args, **kwargs): + for method_name, method_body in inspect.getmembers(self, predicate=inspect.ismethod): + api_version_last = 0 + api_name_last = None + + if getattr(method_body, 'api', False): + for api_version, api_name in getattr(method_body, 'versions', []): + if api_version_last < api_version <= version: + api_version_last = api_version + api_name_last = api_name + + if api_name_last is None and api_version_last == 0: + api_name_last = method_name + + if api_name_last is not None and api_name_last == name: + method_body(*args, **kwargs) + + @webApi def multi(self, actions): return self.anki.multi(actions)