config.py 2.6 KB
Newer Older
M
Mark Hymers 已提交
1 2
#!/usr/bin/env python

3 4 5 6 7 8 9
"""
Config access class

@contact: Debian FTPMaster <ftpmaster@debian.org>
@copyright: 2008  Mark Hymers <mhy@debian.org>
@license: GNU General Public License version 2 or later
"""
M
Mark Hymers 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

################################################################################

# <NCommander> mhy, how about "Now with 20% more monty python references"

################################################################################

import apt_pkg
import socket

34
from singleton import Singleton
M
Mark Hymers 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

################################################################################

default_config = "/etc/dak/dak.conf"

def which_conf_file(Cnf):
    res = socket.gethostbyaddr(socket.gethostname())
    if Cnf.get("Config::" + res[0] + "::DakConfig"):
        return Cnf["Config::" + res[0] + "::DakConfig"]
    else:
        return default_config

class Config(Singleton):
    """
    A Config object is a singleton containing
    information about the DAK configuration
    """
    def __init__(self, *args, **kwargs):
        super(Config, self).__init__(*args, **kwargs)

    def _readconf(self):
        apt_pkg.init()

        self.Cnf = apt_pkg.newConfiguration()

        apt_pkg.ReadConfigFileISC(self.Cnf, default_config)

        # Check whether our dak.conf was the real one or
        # just a pointer to our main one
        res = socket.gethostbyaddr(socket.gethostname())
        conffile = self.Cnf.get("Config::" + res[0] + "::DakConfig")
        if conffile:
            apt_pkg.ReadConfigFileISC(self.Cnf, conffile)

        # Rebind some functions
        # TODO: Clean this up
        self.get = self.Cnf.get
        self.SubTree = self.Cnf.SubTree
        self.ValueList = self.Cnf.ValueList
74 75
        self.Find = self.Cnf.Find
        self.FindB = self.Cnf.FindB
M
Mark Hymers 已提交
76 77 78 79

    def _startup(self, *args, **kwargs):
        self._readconf()

80 81 82
    def has_key(self, name):
        return self.Cnf.has_key(name)

M
Mark Hymers 已提交
83 84 85
    def __getitem__(self, name):
        return self.Cnf[name]