]> git.decadent.org.uk Git - dak.git/blob - daklib/Config.py
Merge branch 'psycopg2' into content_generation
[dak.git] / daklib / Config.py
1 #!/usr/bin/env python
2
3 # Config access class
4 # Copyright (C) 2008  Mark Hymers <mhy@debian.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 # <NCommander> mhy, how about "Now with 20% more monty python references"
23
24 ################################################################################
25
26 import apt_pkg
27 import socket
28
29 from Singleton import Singleton
30
31 ################################################################################
32
33 default_config = "/etc/dak/dak.conf"
34
35 def which_conf_file(Cnf):
36     res = socket.gethostbyaddr(socket.gethostname())
37     if Cnf.get("Config::" + res[0] + "::DakConfig"):
38         return Cnf["Config::" + res[0] + "::DakConfig"]
39     else:
40         return default_config
41
42 class Config(Singleton):
43     """
44     A Config object is a singleton containing
45     information about the DAK configuration
46     """
47     def __init__(self, *args, **kwargs):
48         super(Config, self).__init__(*args, **kwargs)
49
50     def _readconf(self):
51         apt_pkg.init()
52
53         self.Cnf = apt_pkg.newConfiguration()
54
55         apt_pkg.ReadConfigFileISC(self.Cnf, default_config)
56
57         # Check whether our dak.conf was the real one or
58         # just a pointer to our main one
59         res = socket.gethostbyaddr(socket.gethostname())
60         conffile = self.Cnf.get("Config::" + res[0] + "::DakConfig")
61         if conffile:
62             apt_pkg.ReadConfigFileISC(self.Cnf, conffile)
63
64         # Rebind some functions
65         # TODO: Clean this up
66         self.get = self.Cnf.get
67         self.SubTree = self.Cnf.SubTree
68         self.ValueList = self.Cnf.ValueList
69
70     def _startup(self, *args, **kwargs):
71         self._readconf()
72
73     def __getitem__(self, name):
74         return self.Cnf[name]
75
76     def GetDBConnString(self):
77         s = "dbname=%s" % self.Cnf["DB::Name"]
78         if self.Cnf["DB::Host"]:
79             s += " host=%s" % self.Cnf["DB::Host"]
80         if self.Cnf["DB::Port"] and self.Cnf["DB::Port"] != "-1":
81             s += " port=%s" % self.Cnf["DB::Port"]
82
83         return s