File: cache_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 (83 lines) | stat: -rw-r--r-- 2,940 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
# 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.
"""Package level factory implementation for cache implementations.

We use a factory instead of relying on the __init__.py module to
register cache implementations at import time.  This is much more
reliable.
"""

__author__ = "springer@google.com (Matthew Springer)"

import logging

from nss_cache.caches import files

_cache_implementations = {}


def RegisterImplementation(cache_name, map_name, cache):
    """Register a Cache implementation with the CacheFactory.

    Child modules are expected to call this method in the file-level scope
    so that the CacheFactory is aware of them.

    Args:
      cache_name: (string) The name of the NSS backend.
      map_name: (string) The name of the map handled by this Cache.
      cache: A class type that is a subclass of Cache.

    Returns: Nothing
    """
    global _cache_implementations
    if cache_name not in _cache_implementations:
        logging.info("Registering [%s] cache for [%s].", cache_name, map_name)
        _cache_implementations[cache_name] = {}
    _cache_implementations[cache_name][map_name] = cache


def Create(conf, map_name, automount_mountpoint=None):
    """Cache creation factory method.

    Args:
     conf: a dictionary of configuration key/value pairs, including one
       required attribute 'name'
     map_name: a string identifying the map name to handle
     automount_mountpoint: A string containing the automount mountpoint, used only
       by automount maps.

    Returns:
      an instance of a Cache

    Raises:
      RuntimeError: problem instantiating the requested cache
    """
    global _cache_implementations
    if not _cache_implementations:
        raise RuntimeError("no cache implementations exist")
    cache_name = conf["name"]

    if cache_name not in _cache_implementations:
        raise RuntimeError("cache not implemented: %r" % (cache_name,))
    if map_name not in _cache_implementations[cache_name]:
        raise RuntimeError("map %r not supported by cache %r" % (map_name, cache_name))

    return _cache_implementations[cache_name][map_name](
        conf, map_name, automount_mountpoint=automount_mountpoint
    )


files.RegisterAllImplementations(RegisterImplementation)