File: source.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 (143 lines) | stat: -rw-r--r-- 4,829 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
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
# 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.
"""Base class of data source object for nss_cache."""

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

import logging

from nss_cache import config
from nss_cache import error


class Source(object):
    """Abstract base class for map data sources."""

    UPDATER = None

    def __init__(self, conf):
        """Initialise the Source object.

        Args:
          conf: A dictionary of key/value pairs.

        Raises:
          RuntimeError: object wasn't initialised with a dict
        """
        if not isinstance(conf, dict):
            raise RuntimeError("Source constructor not passed a dictionary")

        self.conf = conf

        # create a logger for our children
        self.log = logging.getLogger(__name__)

    def GetMap(self, map_name, since=None, location=None):
        """Get a specific map from this source.

        Args:
          map_name: A string representation of the map you want
          since: optional timestamp for incremental query
          location: optional field used by automounts to indicate a specific map

        Returns:
          A Map child class for the map requested.

        Raises:
          UnsupportedMap: for unknown source maps
        """
        if map_name == config.MAP_PASSWORD:
            return self.GetPasswdMap(since)
        elif map_name == config.MAP_SSHKEY:
            return self.GetSshkeyMap(since)
        elif map_name == config.MAP_GROUP:
            return self.GetGroupMap(since)
        elif map_name == config.MAP_SHADOW:
            return self.GetShadowMap(since)
        elif map_name == config.MAP_NETGROUP:
            return self.GetNetgroupMap(since)
        elif map_name == config.MAP_AUTOMOUNT:
            return self.GetAutomountMap(since, location=location)

        raise error.UnsupportedMap("Source can not fetch %s" % map_name)

    def GetAutomountMap(self, since=None, location=None):
        """Get an automount map from this source."""
        raise NotImplementedError

    def GetAutomountMasterMap(self):
        """Get an automount map from this source."""
        raise NotImplementedError

    def Verify(self):
        """Perform verification of the source availability.

        Attempt to open/connect or otherwise use the data source, and
        report if there are any problems.
        """
        raise NotImplementedError


class FileSource(object):
    """Abstract base class for file data sources."""

    def __init__(self, conf):
        """Initialise the Source object.

        Args:
          conf: A dictionary of key/value pairs.

        Raises:
          RuntimeError: object wasn't initialised with a dict
        """
        if not isinstance(conf, dict):
            raise RuntimeError("Source constructor not passed a dictionary")

        self.conf = conf

        # create a logger for our children
        self.log = logging.getLogger(__name__)

    def GetFile(self, map_name, dst_file, current_file, location=None):
        """Retrieve a file from this source.

        Args:
          map_name: A string representation of the map whose file you want
          dst_file: Temporary filename to write to.
          current_file: Path to the current cache.
          location: optional field used by automounts to indicate a specific map

        Returns:
          path to new file

        Raises:
          UnsupportedMap: for unknown source maps
        """
        if map_name == config.MAP_PASSWORD:
            return self.GetPasswdFile(dst_file, current_file)
        elif map_name == config.MAP_GROUP:
            return self.GetGroupFile(dst_file, current_file)
        elif map_name == config.MAP_SHADOW:
            return self.GetShadowFile(dst_file, current_file)
        elif map_name == config.MAP_NETGROUP:
            return self.GetNetgroupFile(dst_file, current_file)
        elif map_name == config.MAP_AUTOMOUNT:
            return self.GetAutomountFile(dst_file, current_file, location=location)

        raise error.UnsupportedMap("Source can not fetch %s" % map_name)