Accept uncomplete configuration file, fall back to default value

darcs-hash:20080515064539-86b55-23b6f5dfb124f10c8f5eebdfdcdcc10b83e08564.gz
This commit is contained in:
David Rousselie
2008-05-15 08:45:39 +02:00
parent b1099cc903
commit 2ea561c9cc
3 changed files with 50 additions and 15 deletions

View File

@@ -130,12 +130,14 @@ class JCLRunner(object):
(section, set_func) = cleanopts[opt] (section, set_func) = cleanopts[opt]
if section is not None: if section is not None:
attr = opt.replace("-", "_") attr = opt.replace("-", "_")
config_property = self.config.get(section, attr) if self.config.has_section(section) \
self.logger.debug("Setting " + attr + " = " + and self.config.has_option(section, attr):
config_property + config_property = self.config.get(section, attr)
" from configuration file " + self.logger.debug("Setting " + attr + " = " +
self.config_file) config_property +
set_func(config_property) " from configuration file " +
self.config_file)
set_func(config_property)
def configure(self): def configure(self):
""" """
@@ -154,7 +156,9 @@ class JCLRunner(object):
else: else:
cleanopts[option[1]] = (option[2], option[4]) cleanopts[option[1]] = (option[2], option[4])
commandline_args = self.__configure_commandline_args(shortopts, longopts, cleanopts) commandline_args = self.__configure_commandline_args(shortopts,
longopts,
cleanopts)
if commandline_args.has_key("debug") or commandline_args.has_key("d"): if commandline_args.has_key("debug") or commandline_args.has_key("d"):
self.debug = True self.debug = True
self.logger.debug("Debug activated") self.logger.debug("Debug activated")
@@ -245,7 +249,7 @@ class JCLRunner(object):
model.db_connection_str = self.db_url model.db_connection_str = self.db_url
model.db_connect() model.db_connect()
self.setup_db() self.setup_db()
ipshell = IPShellEmbed(["-pi1", self.component_short_name + "[\\#]: "], ipshell = IPShellEmbed(["-pi1", self.component_short_name + "[\\#]: "],
"Starting " + self.component_name + " v" \ "Starting " + self.component_name + " v" \
+ self.component_version + " console.", + self.component_version + " console.",
"Ending " + self.component_name + " v" \ "Ending " + self.component_name + " v" \

View File

@@ -3,18 +3,18 @@
## Login : David Rousselie <dax@happycoders.org> ## Login : David Rousselie <dax@happycoders.org>
## Started on Fri May 18 13:43:37 2007 David Rousselie ## Started on Fri May 18 13:43:37 2007 David Rousselie
## $Id$ ## $Id$
## ##
## Copyright (C) 2007 David Rousselie ## Copyright (C) 2007 David Rousselie
## This program is free software; you can redistribute it and/or modify ## 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 ## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or ## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version. ## (at your option) any later version.
## ##
## This program is distributed in the hope that it will be useful, ## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of ## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details. ## GNU General Public License for more details.
## ##
## You should have received a copy of the GNU General Public License ## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software ## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -45,7 +45,7 @@ class JCLRunner_TestCase(unittest.TestCase):
def tearDown(self): def tearDown(self):
self.runner = None self.runner = None
sys.argv = [""] sys.argv = [""]
def test_configure_default(self): def test_configure_default(self):
self.runner.configure() self.runner.configure()
self.assertEquals(self.runner.config_file, "jcl.conf") self.assertEquals(self.runner.config_file, "jcl.conf")
@@ -70,6 +70,19 @@ class JCLRunner_TestCase(unittest.TestCase):
self.assertEquals(self.runner.pid_file, "/var/run/jabber/test_jcl.pid") self.assertEquals(self.runner.pid_file, "/var/run/jabber/test_jcl.pid")
self.assertFalse(self.runner.debug) self.assertFalse(self.runner.debug)
def test_configure_uncomplete_configfile(self):
self.runner.config_file = "src/jcl/tests/uncomplete_jcl.conf"
self.runner.configure()
self.assertEquals(self.runner.server, "test_localhost")
self.assertEquals(self.runner.port, 42)
self.assertEquals(self.runner.secret, "test_secret")
self.assertEquals(self.runner.service_jid, "test_jcl.localhost")
self.assertEquals(self.runner.language, "test_en")
self.assertEquals(self.runner.db_url, "test_sqlite://root@localhost/var/spool/jabber/test_jcl.db")
# pid_file is not in uncmplete_jcl.conf, must be default value
self.assertEquals(self.runner.pid_file, "/var/run/jabber/jcl.pid")
self.assertFalse(self.runner.debug)
def test_configure_commandline_shortopt(self): def test_configure_commandline_shortopt(self):
sys.argv = ["", "-c", "src/jcl/tests/jcl.conf", \ sys.argv = ["", "-c", "src/jcl/tests/jcl.conf", \
"-S", "test2_localhost", \ "-S", "test2_localhost", \
@@ -161,10 +174,10 @@ class JCLRunner_TestCase(unittest.TestCase):
os.unlink(db_path) os.unlink(db_path)
self.assertFalse(os.access("/tmp/jcl.pid", os.F_OK)) self.assertFalse(os.access("/tmp/jcl.pid", os.F_OK))
self.assertEquals(self.i, 2) self.assertEquals(self.i, 2)
def test__get_help(self): def test__get_help(self):
self.assertNotEquals(self.runner._get_help(), None) self.assertNotEquals(self.runner._get_help(), None)
def suite(): def suite():
test_suite = unittest.TestSuite() test_suite = unittest.TestSuite()
test_suite.addTest(unittest.makeSuite(JCLRunner_TestCase, 'test')) test_suite.addTest(unittest.makeSuite(JCLRunner_TestCase, 'test'))
@@ -172,4 +185,3 @@ def suite():
if __name__ == '__main__': if __name__ == '__main__':
unittest.main(defaultTest='suite') unittest.main(defaultTest='suite')

View File

@@ -0,0 +1,19 @@
[jabber]
server: test_localhost
port: 42
secret: test_secret
service_jid: test_jcl.localhost
#supported language: en, fr (See src/jmc/lang.py to add more)
language: test_en
[db]
#type: mysql
type: test_sqlite
#host: root@localhost
host: root@localhost
name: /var/spool/jabber/test_jcl.db
#url: %(type)%(host)%(name)?debug=1&debugThreading=1
db_url: %(type)s://%(host)s%(name)s
[component]
log_file: /tmp/jcl.log