pyldb: avoid segfault when adding an element with no name
[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
23 class Hostconfig(object):
24     """Aggregate object that contains all information about the configuration
25     of a Samba host."""
26
27     def __init__(self, lp):
28         self.lp = lp
29
30     def get_shares(self):
31         return SharesContainer(self.lp)
32
33     def get_samdb(self, session_info, credentials):
34         """Access the SamDB host.
35
36         :param session_info: Session info to use
37         :param credentials: Credentials to access the SamDB with
38         """
39         return SamDB(url=self.lp.samdb_url(),
40                      session_info=session_info, credentials=credentials,
41                      lp=self.lp)
42
43
44 # TODO: Rather than accessing Loadparm directly here, we should really
45 # have bindings to the param/shares.c and use those.
46
47
48 class SharesContainer(object):
49     """A shares container."""
50
51     def __init__(self, lp):
52         self._lp = lp
53
54     def __getitem__(self, name):
55         if name == "global":
56             # [global] is not a share
57             raise KeyError
58         return Share(self._lp[name])
59
60     def __len__(self):
61         if "global" in self._lp.services():
62             return len(self._lp) - 1
63         return len(self._lp)
64
65     def keys(self):
66         return [name for name in self._lp.services() if name != "global"]
67
68     def __iter__(self):
69         return iter(self.keys())
70
71
72 class Share(object):
73     """A file share."""
74
75     def __init__(self, service):
76         self._service = service
77
78     def __getitem__(self, name):
79         return self._service[name]
80
81     def __setitem__(self, name, value):
82         self._service[name] = value