From 350b12d5804c38aa894babed7df9a13533ff3de7 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 7659bde..36f5c9b 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -68,6 +68,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 @@ -909,6 +919,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)