]>
git.ipfire.org Git - pakfire.git/blob - python/pakfire/config.py
467b36aa96f0a272e2095fe9f6df53b62c9bd01c
2 ###############################################################################
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2011 Pakfire development team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
26 from ConfigParser
import ConfigParser
29 log
= logging
.getLogger("pakfire")
32 from system
import system
34 from constants
import *
37 class _Config(object):
40 global_default_settings
= {
42 "file" : "/var/log/pakfire.log",
45 "rotation_threshold" : 10485760,
49 # A dict with default settings for this config class.
52 def __init__(self
, files
=None):
53 # Configuration settings.
54 self
._config
= self
.global_default_settings
.copy()
55 self
._config
.update(self
.default_settings
)
57 # List of files that were already loaded.
60 # If no files were given, load the default files.
62 # Read default configuration file.
63 self
.read(*self
.files
)
65 repo_path
= self
.get(None, "repo_path", CONFIG_REPOS_DIR
)
67 self
.read_dir(repo_path
, ext
=".repo")
72 for name
, settings
in self
._config
.items():
73 if not name
.startswith("repo:"):
76 # Strip "repo:" from name of the repository.
79 repos
.append((name
, settings
))
83 def read_dir(self
, where
, ext
=".conf"):
84 for file in os
.listdir(where
):
85 if not file.endswith(ext
):
88 file = os
.path
.join(where
, file)
91 def read(self
, *files
):
92 # Do nothing for no files.
97 if not file.startswith("/"):
98 file = os
.path
.join(CONFIG_DIR
, file)
100 if not os
.path
.exists(file):
103 # Normalize filename.
104 file = os
.path
.abspath(file)
106 # Check if file has already been read or
107 # does not exist. Then skip it.
108 if file in self
._files
or not os
.path
.exists(file):
112 with
open(file) as f
:
115 # Save the filename to the list of read files.
116 self
._files
.append(file)
124 config
= ConfigParser()
127 # Read all data from the configuration file in the _config dict.
128 for section
in config
.sections():
129 items
= dict(config
.items(section
))
131 if section
== "DEFAULT":
135 self
._config
[section
].update(items
)
137 self
._config
[section
] = items
139 # Update the logger, because the logging configuration may
141 logger
.setup_logging(self
)
143 def set(self
, section
, key
, value
):
145 self
._config
[section
][key
] = value
147 self
._config
[section
] = { key
: value
}
149 def get_section(self
, section
):
151 return self
._config
[section
]
155 def get(self
, section
, key
, default
=None):
156 s
= self
.get_section(section
)
163 def get_int(self
, section
, key
, default
=None):
164 val
= self
.get(section
=section
, key
=key
, default
=default
)
170 def get_bool(self
, section
, key
, default
=None):
171 val
= self
.get(section
=section
, key
=key
, default
=default
)
173 if val
in (True, "true", "1", "on"):
175 elif val
in (False, "false", "0", "off"):
180 def update(self
, section
, what
):
182 self
._config
[section
].update(what
)
184 self
._config
[section
] = what
188 Dump the configuration that was read.
190 (Only in debugging mode.)
192 log
.debug(_("Configuration:"))
193 for section
, settings
in self
._config
.items():
194 log
.debug(" " + _("Section: %s") % section
)
196 for k
, v
in settings
.items():
197 log
.debug(" %-20s: %s" % (k
, v
))
199 log
.debug(" " + _("No settings in this section."))
201 log
.debug(" " + _("Loaded from files:"))
202 for f
in self
._files
:
206 class Config(_Config
):
207 files
= ["general.conf"]
210 class ConfigBuilder(_Config
):
211 files
= ["general.conf", "builder.conf", "default.conf"]
214 class ConfigClient(_Config
):
215 files
= ["general.conf", "client.conf"]
219 # The default server is the official Pakfire
221 "server" : "https://pakfire.ipfire.org",
226 class ConfigDaemon(_Config
):
227 files
= ["general.conf", "daemon.conf"]
231 # The default server is the official Pakfire
233 "server" : "https://pakfire.ipfire.org",
235 # The default hostname is the host name of this
237 "hostname" : system
.hostname
,