diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2008-12-21 15:55:23 +0100 |
---|---|---|
committer | Jelmer Vernooij <jelmer@samba.org> | 2008-12-21 15:55:23 +0100 |
commit | 676919872df9d108289ba989e55f64eb78e66860 (patch) | |
tree | fe6c6d7378a7b4cead0379081183c61213702157 | |
parent | cf9f2484b70f5b21f0020800ff0f36d49d18b175 (diff) | |
download | samba-676919872df9d108289ba989e55f64eb78e66860.tar.gz samba-676919872df9d108289ba989e55f64eb78e66860.tar.bz2 samba-676919872df9d108289ba989e55f64eb78e66860.zip |
Provide simple Python replacement for ParamFile, which currently exists
with a lot of overhead.
-rw-r--r-- | source4/scripting/python/samba/samba3.py | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/source4/scripting/python/samba/samba3.py b/source4/scripting/python/samba/samba3.py index a3dac27709..083386a0c5 100644 --- a/source4/scripting/python/samba/samba3.py +++ b/source4/scripting/python/samba/samba3.py @@ -660,6 +660,56 @@ class WinsDatabase(object): pass +class ParamFile(object): + """Simple smb.conf-compatible file parser + + Does not use a parameter table, unlike the "normal". + """ + + def __init__(self): + self._sections = {} + + def _sanitize_name(self, name): + return name.strip().lower().replace(" ","") + + def read(self, filename): + """Read a file. + + :param filename: Path to the file + """ + section = None + for i, l in enumerate(open(filename, 'r').xreadlines()): + l = l.strip() + if not l: + continue + if l[0] == "[" and l[-1] == "]": + section = self._sanitize_name(l[1:-2]) + self._sections.setdefault(section, {}) + elif "=" in l: + (k, v) = l.split("=", 1) + self._sections[section][self._sanitize_name(k)] = v + else: + raise Error("Unable to parser line %d: %r" % (i+1,l)) + + def get(self, param, section=None): + """Return the value of a parameter. + + :param param: Parameter name + :param section: Section name, defaults to "global" + :return: parameter value as string if found, None otherwise. + """ + if section is None: + section = "global" + section = self._sanitize_name(section) + if not section in self._sections: + return None + param = self._sanitize_name(param) + return self._sections[section].get(param) + + def __getitem__(self, section): + return self._sections[section] + + class Samba3(object): """Samba 3 configuration and state data reader.""" def __init__(self, libdir, smbconfpath): @@ -670,8 +720,7 @@ class Samba3(object): """ self.smbconfpath = smbconfpath self.libdir = libdir - import param - self.lp = param.ParamFile() + self.lp = ParamFile() self.lp.read(self.smbconfpath) def libdir_path(self, path): |