summaryrefslogtreecommitdiffstats
path: root/ipalib/plugable.py
blob: 0a6a0caaf30a8e0fea899d459834ad81fdba025e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# Authors:
#   Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2008  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Base classes for plug-in architecture and generative API.
"""

import re
import inspect
import errors


def to_cli(name):
    """
    Takes a Python identifier and transforms it into form suitable for the
    Command Line Interface.
    """
    assert isinstance(name, str)
    return name.replace('_', '-')


def from_cli(cli_name):
    """
    Takes a string from the Command Line Interface and transforms it into a
    Python identifier.
    """
    assert isinstance(cli_name, basestring)
    return cli_name.replace('-', '_')


def check_identifier(name):
    """
    Raises errors.NameSpaceError if `name` is not a valid Python identifier
    suitable for use in a NameSpace.
    """
    regex = r'^[a-z][_a-z0-9]*[a-z0-9]$'
    if re.match(regex, name) is None:
        raise errors.NameSpaceError(name, regex)


class Abstract(object):
    __public__ = frozenset()

    @classmethod
    def implements(cls, arg):
        assert type(cls.__public__) is frozenset
        if isinstance(arg, str):
            return arg in cls.__public__
        if type(getattr(arg, '__public__', None)) is frozenset:
            return cls.__public__.issuperset(arg.__public__)
        if type(arg) is frozenset:
            return cls.__public__.issuperset(arg)
        raise TypeError(
            "must be str, frozenset, or have frozenset '__public__' attribute"
        )


class Plugin(object):
    """
    Base class for all plugins.
    """

    __api = None

    def __get_api(self):
        """
        Returns the plugable.API instance passed to Plugin.finalize(), or
        or returns None if finalize() has not yet been called.
        """
        return self.__api
    api = property(__get_api)

    def finalize(self, api):
        """
        After all the plugins are instantiated, the plugable.API calls this
        method, passing itself as the only argument. This is where plugins
        should check that other plugins they depend upon have actually be
        loaded.
        """
        assert self.__api is None, 'finalize() can only be called once'
        assert api is not None, 'finalize() argument cannot be None'
        self.__api = api

    def __get_name(self):
        """
        Returns the class name of this instance.
        """
        return self.__class__.__name__
    name = property(__get_name)

    def __repr__(self):
        """
        Returns a fully qualified <module><name> representation of the class.
        """
        return '%s.%s()' % (
            self.__class__.__module__,
            self.__class__.__name__
        )


class ReadOnly(object):
    """
    Base class for classes with read-only attributes.
    """
    __locked = False

    def _lock(self):
        assert self.__locked is False
        self.__locked = True

    def __setattr__(self, name, value):
        """
        Raises an AttributeError if ReadOnly._lock() has already been called;
        otherwise calls object.__setattr__()
        """
        if self.__locked:
            raise AttributeError('read-only: cannot set %s.%s' %
                (self.__class__.__name__, name)
            )
        return object.__setattr__(self, name, value)

    def __delattr__(self, name):
        """
        Raises an AttributeError if ReadOnly._lock() has already been called;
        otherwise calls object.__delattr__()
        """
        if self.__locked:
            raise AttributeError('read-only: cannot del %s.%s' %
                (self.__class__.__name__, name)
            )
        return object.__delattr__(self, name)


class Proxy(ReadOnly):
    __slots__ = (
        '__base',
        '__target',
        '__name_attr',
        'name',
        '__public__',
    )

    def __init__(self, base, target, name_attr='name'):
        if not inspect.isclass(base):
            raise TypeError('arg1 must be a class, got %r' % base)
        if not isinstance(target, base):
            raise ValueError('arg2 must be instance of arg1, got %r' % target)
        self.__base = base
        self.__target = target
        self.__name_attr = name_attr
        self.name = getattr(target, name_attr)
        self.__public__ = base.__public__
        assert type(self.__public__) is frozenset
        check_identifier(self.name)
        self._lock()


    def __iter__(self):
        for name in sorted(self.__public__):
            yield name

    def __getitem__(self, key):
        if key in self.__public__:
            return getattr(self.__target, key)
        raise KeyError('no proxy attribute %r' % key)

    def __getattr__(self, name):
        if name in self.__public__:
            return getattr(self.__target, name)
        raise AttributeError('no proxy attribute %r' % name)

    def __call__(self, *args, **kw):
        return self['__call__'](*args, **kw)

    def _clone(self, name_attr):
        return self.__class__(self.__base, self.__target, name_attr)

    def __repr__(self):
        return '%s(%s, %r, %r)' % (
            self.__class__.__name__,
            self.__base.__name__,
            self.__target,
            self.__name_attr,
        )


class NameSpace(ReadOnly):
    """
    A read-only namespace of (key, value) pairs that can be accessed
    both as instance attributes and as dictionary items.
    """

    def __init__(self, proxies):
        """
        NameSpace
        """
        self.__proxies = tuple(proxies)
        self.__d = dict()
        for proxy in self.__proxies:
            assert isinstance(proxy, Proxy)
            assert proxy.name not in self.__d
            self.__d[proxy.name] = proxy
            assert not hasattr(self, proxy.name)
            setattr(self, proxy.name, proxy)
        self._lock()

    def __iter__(self):
        """
        Iterates through the proxies in this NameSpace in the same order they
        were passed in the contructor.
        """
        for proxy in self.__proxies:
            yield proxy

    def __len__(self):
        """
        Returns number of proxies in this NameSpace.
        """
        return len(self.__proxies)

    def __contains__(self, key):
        """
        Returns True if a proxy named `key` is in this NameSpace.
        """
        return key in self.__d

    def __getitem__(self, key):
        """
        Returns proxy named `key`; otherwise raises KeyError.
        """
        if key in self.__d:
            return self.__d[key]
        raise KeyError('NameSpace has no item for key %r' % key)

    def __repr__(self):
        return '%s(<%d proxies>)' % (self.__class__.__name__, len(self))


class Registrar(object):
    def __init__(self, *allowed):
        """
        `*allowed` is a list of the base classes plugins can be subclassed
        from.
        """
        self.__allowed = frozenset(allowed)
        self.__d = {}
        self.__registered = set()
        assert len(self.__allowed) == len(allowed)
        for base in self.__allowed:
            assert inspect.isclass(base)
            assert base.__name__ not in self.__d
            self.__d[base.__name__] = {}

    def __findbase(self, cls):
        """
        If `cls` is a subclass of a base in self.__allowed, returns that
        base; otherwise raises SubclassError.
        """
        assert inspect.isclass(cls)
        found = False
        for base in self.__allowed:
            if issubclass(cls, base):
                found = True
                yield base
        if not found:
            raise errors.SubclassError(cls, self.__allowed)

    def __call__(self, cls, override=False):
        """
        Register the plugin `cls`.
        """
        if not inspect.isclass(cls):
            raise TypeError('plugin must be a class: %r'  % cls)

        # Raise DuplicateError if this exact class was already registered:
        if cls in self.__registered:
            raise errors.DuplicateError(cls)

        # Find the base class or raise SubclassError:
        for base in self.__findbase(cls):
            sub_d = self.__d[base.__name__]

            # Check override:
            if cls.__name__ in sub_d:
                # Must use override=True to override:
                if not override:
                    raise errors.OverrideError(base, cls)
            else:
                # There was nothing already registered to override:
                if override:
                    raise errors.MissingOverrideError(base, cls)

            # The plugin is okay, add to sub_d:
            sub_d[cls.__name__] = cls

        # The plugin is okay, add to __registered:
        self.__registered.add(cls)

    def __getitem__(self, item):
        """
        Returns a copy of the namespace dict of the base class named `name`.
        """
        if inspect.isclass(item):
            if item not in self.__allowed:
                raise KeyError(repr(item))
            key = item.__name__
        else:
            key = item
        return dict(self.__d[key])

    def __contains__(self, item):
        """
        Returns True if a base class named `name` is in this Registrar.
        """
        if inspect.isclass(item):
            return item in self.__allowed
        return item in self.__d

    def __iter__(self):
        """
        Iterates through a (base, registered_plugins) tuple for each allowed
        base.
        """
        for base in self.__allowed:
            sub_d = self.__d[base.__name__]
            yield (base, tuple(sub_d[k] for k in sorted(sub_d)))


class API(ReadOnly):
    def __init__(self, *allowed):
        keys = tuple(b.__name__ for b in allowed)
        self.register = Registrar(*allowed)
        self._lock()

    def __call__(self):
        """
        Finalize the registration, instantiate the plugins.
        """
        d = {}
        def plugin_iter(base, classes):
            for cls in classes:
                if cls not in d:
                    d[cls] = cls()
                plugin = d[cls]
                yield Proxy(base, plugin)

        for (base, classes) in self.register:
            ns = NameSpace(plugin_iter(base, classes))
            assert not hasattr(self, base.__name__)
            object.__setattr__(self, base.__name__, ns)
        for plugin in d.values():
            plugin.finalize(self)
            assert plugin.api is self

    def __iter__(self):
        for key in self.__keys:
            yield key