gpo: Create base class gp_inf_ext
[nivanova/samba-autobuild/.git] / python / samba / hostconfig.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17
18 """Local host configuration."""
19 from __future__ import absolute_import
20 from .samdb import SamDB
21
22 class Hostconfig(object):
23     """Aggregate object that contains all information about the configuration
24     of a Samba host."""
25
26     def __init__(self, lp):
27         self.lp = lp
28
29     def get_shares(self):
30         return SharesContainer(self.lp)
31
32     def get_samdb(self, session_info, credentials):
33         """Access the SamDB host.
34
35         :param session_info: Session info to use
36         :param credentials: Credentials to access the SamDB with
37         """
38         return SamDB(url=self.lp.samdb_url(),
39                      session_info=session_info, credentials=credentials,
40                      lp=self.lp)
41
42
43 # TODO: Rather than accessing Loadparm directly here, we should really
44 # have bindings to the param/shares.c and use those.
45
46
47 class SharesContainer(object):
48     """A shares container."""
49
50     def __init__(self, lp):
51         self._lp = lp
52
53     def __getitem__(self, name):
54         if name == "global":
55             # [global] is not a share
56             raise KeyError
57         return Share(self._lp[name])
58
59     def __len__(self):
60         if "global" in self._lp.services():
61             return len(self._lp)-1
62         return len(self._lp)
63
64     def keys(self):
65         return [name for name in self._lp.services() if name != "global"]
66
67     def __iter__(self):
68         return iter(self.keys())
69
70
71 class Share(object):
72     """A file share."""
73
74     def __init__(self, service):
75         self._service = service
76
77     def __getitem__(self, name):
78         return self._service[name]
79
80     def __setitem__(self, name, value):
81         self._service[name] = value