#!/usr/bin/python2.5 """ Copyright (C) 2008 Norman Messtorff 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. """ import ConfigParser import string import os from socket import gethostname from Global import Global class Configuration(Global): """Now we have a Configuration Class.""" config = { } def __init__(self, type): """If no configuration available -> generate default""" configpath="/etc/debman/" configsuffix = ".conf" if type == "server": self.configfile = configpath + type + configsuffix if not os.path.exists(self.configfile): self.__gen_server_default() elif type == "node": self.configfile = configpath + type + configsuffix if not os.path.exists(self.configfile): self.__gen_node_default() else: debug(1, " -!- Unknown configuration type! -!- ") self.read_config() self.debug(4, "Class Configuration initialized, configfile: " + self.configfile) def __gen_server_default(self): self.debug(3, "Generating default configuration file") config = ConfigParser.ConfigParser() config.add_section("server") config.set("server", "listen_ip", "localhost") config.set("server", "listen_port", 8080) config.add_section("database") config.set("database", "type", "sqlite") config.set("database", "sqlite_filename", "/etc/debman/server.db") config.set("database", "session_timeout", 5) config.add_section("compare") config.set("compare", "packages_url", "http://ftp.de.debian.org/debian/") config.set("compare", "release", "testing") config.set("compare", "component", "main") config.set("compare", "arch", "i386") fp = open(self.configfile, 'w') config.write(fp) fp.close() del config del fp def __gen_node_default(self): self.debug(3, "Generating default configuration file") config = ConfigParser.ConfigParser() config.add_section("server") config.set("server", "url", "http://localhost:8080") config.add_section("local") config.set("local", "nodeid", "0") config.set("local", "hostname", gethostname()) config.set("local", "password", "0") fp = open(self.configfile, 'w') config.write(fp) fp.close() del config del fp def read_config(self): config = ConfigParser.ConfigParser() config.read(self.configfile) for section in config.sections(): self.config[section] = { } for option in config.options(section): self.config[section][option] = config.get(section, option) self.debug(3, "Configuration loaded") def cfg_write_item(self, section, option, value): config = ConfigParser.ConfigParser() config.read(self.configfile) config.set(section, option, value) self.config[section][option] = value fp = open(self.configfile, 'w') config.write(fp) fp.close() del config del fp