Skip to content

Commit 9d20023

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
sambacc: add smbconf_api module for abstract smbconf operations
The smbconf library provided by Samba allows python code to interact with Samba's configuration backends. Before adding a module that uses those libraries directly add a module that provides general abstractions and interfaces in that vein. Signed-off-by: John Mulligan <[email protected]>
1 parent 7c09f66 commit 9d20023

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

sambacc/smbconf_api.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#
2+
# sambacc: a samba container configuration tool
3+
# Copyright (C) 2023 John Mulligan
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>
17+
#
18+
19+
import typing
20+
21+
22+
class ConfigStore(typing.Protocol):
23+
def __getitem__(self, name: str) -> list[tuple[str, str]]:
24+
... # pragma: no cover
25+
26+
def __setitem__(self, name: str, value: list[tuple[str, str]]) -> None:
27+
... # pragma: no cover
28+
29+
def __iter__(self) -> typing.Iterator[str]:
30+
... # pragma: no cover
31+
32+
33+
class SimpleConfigStore:
34+
def __init__(self) -> None:
35+
self._data: dict[str, list[tuple[str, str]]] = {}
36+
37+
@property
38+
def writeable(self) -> bool:
39+
"""True if using a read-write backend."""
40+
return True
41+
42+
def __getitem__(self, name: str) -> list[tuple[str, str]]:
43+
return self._data[name]
44+
45+
def __setitem__(self, name: str, value: list[tuple[str, str]]) -> None:
46+
self._data[name] = value
47+
48+
def __iter__(self) -> typing.Iterator[str]:
49+
return iter(self._data.keys())
50+
51+
def import_smbconf(
52+
self, src: ConfigStore, batch_size: typing.Optional[int] = None
53+
) -> None:
54+
"""Import content from one SMBConf configuration object into the
55+
current SMBConf configuration object.
56+
57+
batch_size is ignored.
58+
"""
59+
for sname in src:
60+
self[sname] = src[sname]
61+
62+
63+
def write_store_as_smb_conf(out: typing.IO, conf: ConfigStore) -> None:
64+
"""Write the configuration store in smb.conf format to `out`."""
65+
# unfortunately, AFAIK, there's no way for an smbconf to write
66+
# into a an smb.conf/ini style file. We have to do it on our own.
67+
# ---
68+
# Make sure global section comes first.
69+
sections = sorted(conf, key=lambda v: 0 if v == "global" else 1)
70+
for sname in sections:
71+
out.write(str("\n[{}]\n".format(sname)))
72+
for skey, sval in conf[sname]:
73+
out.write(str(f"\t{skey} = {sval}\n"))

0 commit comments

Comments
 (0)