File: source_factory.py

package info (click to toggle)
nsscache 0.49-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,664 kB
  • sloc: python: 8,661; xml: 584; sh: 304; makefile: 19
file content (110 lines) | stat: -rw-r--r-- 3,005 bytes parent folder | download
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
# Copyright 2007 Google Inc.
#
# 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; either version 2
# of the License, or (at your option) any later version.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
"""Factory for data source implementations."""

__author__ = (
    "jaq@google.com (Jamie Wilkinson)",
    "vasilios@google.com (Vasilios Hoffman)",
)

_source_implementations = {}


def RegisterImplementation(source):
    """Register a Source implementation with the factory method.

    Sources being registered are expected to have a name attribute,
    unique to themselves.

    Child modules are expected to call this method in the file-level
    scope.

    Args:
      source: A class type that is a subclass of Source

    Returns:
      Nothing

    Raises:
      RuntimeError: no 'name' entry in this source.
    """
    global _source_implementations
    if "name" not in source.__dict__:
        raise RuntimeError("'name' not defined in Source %r" % (source,))

    _source_implementations[source.name] = source


# Discover all the known implementations of sources.
try:
    from nss_cache.sources import httpsource

    httpsource.RegisterImplementation(RegisterImplementation)
except ImportError:
    pass

try:
    from nss_cache.sources import ldapsource

    ldapsource.RegisterImplementation(RegisterImplementation)
except ImportError:
    pass

try:
    from nss_cache.sources import consulsource

    consulsource.RegisterImplementation(RegisterImplementation)
except ImportError:
    pass

try:
    from nss_cache.sources import s3source

    s3source.RegisterImplementation(RegisterImplementation)
except ImportError:
    pass

try:
    from nss_cache.sources import gcssource

    gcssource.RegisterImplementation(RegisterImplementation)
except ImportError:
    pass


def Create(conf):
    """Source creation factory method.

    Args:
     conf: a dictionary of configuration key/value pairs, including one
             required attribute 'name'.

    Returns:
      A Source instance.

    Raises:
      RuntimeError: no sources are registered with RegisterImplementation
    """
    global _source_implementations
    if not _source_implementations:
        raise RuntimeError("no source implementations exist")

    source_name = conf["name"]

    if source_name not in list(_source_implementations.keys()):
        raise RuntimeError("source not implemented: %r" % (source_name,))

    return _source_implementations[source_name](conf)