mirror of https://github.com/tLDP/LDP
Name the logfile in the configuration file, not hard coded
This commit is contained in:
parent
c210f9f996
commit
4736ef346a
|
@ -9,6 +9,11 @@ This module provides configuration information from lampadas.conf.
|
|||
# Modules ##################################################################
|
||||
|
||||
|
||||
# Globals
|
||||
|
||||
CONF_FILE = '../conf/lampadas.conf'
|
||||
|
||||
|
||||
# BaseConfig ###############################################################
|
||||
|
||||
class ConfigFileReadErrorException(Exception) :
|
||||
|
@ -22,24 +27,37 @@ class Config:
|
|||
|
||||
DBType = ""
|
||||
DBName = ""
|
||||
Logfile = ""
|
||||
|
||||
def __init__(self) :
|
||||
import ConfigParser
|
||||
|
||||
self.config = ConfigParser.ConfigParser()
|
||||
self.config.readfp(open('../conf/lampadas.conf'))
|
||||
if not self.config.has_section('DB') :
|
||||
raise ConfigFileReadErrorException("File 'lampadas.conf' is missing or does not contain a '[DB]' section")
|
||||
|
||||
if not self.config.has_option('DB', 'dbtype') :
|
||||
raise ConfigFileReadErrorException("Can't read option 'dbtype' from lampadas.conf")
|
||||
self.DBType = self.config.get('DB', 'dbtype')
|
||||
|
||||
if not self.config.has_option('DB', 'dbname') :
|
||||
raise ConfigFileReadErrorException("Can't read option 'dbname' from lampadas.conf")
|
||||
self.DBName = self.config.get('DB', 'dbname')
|
||||
|
||||
self.config.readfp(open(CONF_FILE))
|
||||
|
||||
self.DBType = self.ReadVar('DB', 'dbtype')
|
||||
self.DBName = self.ReadVar('DB', 'dbname')
|
||||
self.Logfile = self.ReadVar('LOG', 'logfile')
|
||||
|
||||
|
||||
def ReadVar(self, section, name):
|
||||
if not self.config.has_section(section) :
|
||||
raise ConfigFileReadErrorException("File '" + CONF_FILE + "' is missing or does not contain a '" + section + "' section")
|
||||
|
||||
if not self.config.has_option(section, name):
|
||||
raise ConfigFileReadErrorException("Can't read option '" + name + "' from " + CONF_FILE)
|
||||
|
||||
return self.config.get(section, name)
|
||||
|
||||
|
||||
# main
|
||||
if __name__ == '__main__' :
|
||||
print "This module cannot be run from the command line"
|
||||
print "Running unit tests..."
|
||||
Config = Config()
|
||||
assert Config.DBType > ''
|
||||
print "DBType= " + Config.DBType
|
||||
assert Config.DBName > ''
|
||||
print "DBName= " + Config.DBName
|
||||
assert Config.Logfile > ''
|
||||
print "Logfile=" + Config.Logfile
|
||||
print "Unit tests complete."
|
||||
|
|
|
@ -8,11 +8,10 @@ This module generates the system log for Lampadas.
|
|||
|
||||
# Modules ##################################################################
|
||||
|
||||
import Config
|
||||
|
||||
Config = Config.Config()
|
||||
|
||||
# Constants
|
||||
|
||||
LOG_FILE = 'lampadas.log'
|
||||
|
||||
class Log:
|
||||
"""
|
||||
|
@ -20,7 +19,7 @@ class Log:
|
|||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.log = open(LOG_FILE, 'a+')
|
||||
self.log = open(Config.Logfile, 'a+')
|
||||
|
||||
def __del__(self):
|
||||
self.log.close
|
||||
|
@ -33,4 +32,4 @@ class Log:
|
|||
|
||||
def Truncate(self):
|
||||
self.log.close
|
||||
self.log = open(LOG_FILE, 'w+')
|
||||
self.log = open(Config.Logfile, 'w+')
|
||||
|
|
Loading…
Reference in New Issue