1# Copyright 2007 Google Inc.
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software Foundation,
15# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16"""An implementation of a group map for nsscache.
17
18GroupMap:  An implementation of NSS group maps based on the Map
19class.
20
21GroupMapEntry:  A group map entry based on the MapEntry class.
22"""
23
24__author__ = 'vasilios@google.com (Vasilios Hoffman)'
25
26from nss_cache.maps import maps
27
28
29class GroupMap(maps.Map):
30    """This class represents an NSS group map.
31
32    Map data is stored as a list of MapEntry objects, see the abstract
33    class Map.
34    """
35
36    def __init__(self, iterable=None):
37        """Construct a GroupMap object using optional iterable."""
38        super(GroupMap, self).__init__(iterable)
39
40    def Add(self, entry):
41        """Add a new object, verify it is a GroupMapEntry object."""
42        if not isinstance(entry, GroupMapEntry):
43            raise TypeError
44        return super(GroupMap, self).Add(entry)
45
46
47class GroupMapEntry(maps.MapEntry):
48    """This class represents NSS group map entries."""
49    # Using slots saves us over 2x memory on large maps.
50    __slots__ = ('name', 'passwd', 'gid', 'members', 'groupmembers')
51    _KEY = 'name'
52    _ATTRS = ('name', 'passwd', 'gid', 'members', 'groupmembers')
53
54    def __init__(self, data=None):
55        """Construct a GroupMapEntry, setting reasonable defaults."""
56        self.name = None
57        self.passwd = None
58        self.gid = None
59        self.members = None
60        self.groupmembers = None
61
62        super(GroupMapEntry, self).__init__(data)
63
64        # Seed data with defaults if needed
65        if self.passwd is None:
66            self.passwd = 'x'
67        if self.members is None:
68            self.members = []
69        if self.groupmembers is None:
70            self.groupmembers = []
71