From 7804a74826eebd1ed07bc9eaa28c1eb9bff5a3f7 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Thu, 1 Aug 2013 14:55:10 +0200 Subject: Allow API plugin registration via a decorator This makes plugin registration easier to read, less error-prone, and, for many Plugins in a single module, faster to write. Functionally, the decorator is equivalent to current plugin registration. However, in the future this style will allow cleaner semantics. As an example, and to exercise the new syntax to prevent regressions, the ping plugin is converted to this style. --- ipalib/plugable.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'ipalib/plugable.py') diff --git a/ipalib/plugable.py b/ipalib/plugable.py index 25698d8f..3be8bb11 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -59,6 +59,33 @@ def is_production_mode(obj): return obj.env.mode == 'production' +class Registry(object): + """A decorator that makes plugins available to the API + + Usage:: + + register = Registry() + + @register() + class obj_mod(...): + ... + + For forward compatibility, make sure that the module-level instance of + this object is named "register". + """ + # TODO: Instead of auto-loading when plugin modules are imported, + # plugins should be stored in this object. + # The API should examine it and load plugins explicitly. + def __call__(self): + from ipalib import api + + def decorator(cls): + api.register(cls) + return cls + + return decorator + + class SetProxy(ReadOnly): """ A read-only container with set/sequence behaviour. -- cgit