From 2ea561c9cc70311555b6ce65a11130f53b51f439 Mon Sep 17 00:00:00 2001 From: David Rousselie Date: Thu, 15 May 2008 08:45:39 +0200 Subject: [PATCH] Accept uncomplete configuration file, fall back to default value darcs-hash:20080515064539-86b55-23b6f5dfb124f10c8f5eebdfdcdcc10b83e08564.gz --- src/jcl/runner.py | 20 ++++++++++++-------- src/jcl/tests/runner.py | 26 +++++++++++++++++++------- src/jcl/tests/uncomplete_jcl.conf | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 src/jcl/tests/uncomplete_jcl.conf diff --git a/src/jcl/runner.py b/src/jcl/runner.py index b417b03..588bd08 100644 --- a/src/jcl/runner.py +++ b/src/jcl/runner.py @@ -130,12 +130,14 @@ class JCLRunner(object): (section, set_func) = cleanopts[opt] if section is not None: attr = opt.replace("-", "_") - config_property = self.config.get(section, attr) - self.logger.debug("Setting " + attr + " = " + - config_property + - " from configuration file " + - self.config_file) - set_func(config_property) + if self.config.has_section(section) \ + and self.config.has_option(section, attr): + config_property = self.config.get(section, attr) + self.logger.debug("Setting " + attr + " = " + + config_property + + " from configuration file " + + self.config_file) + set_func(config_property) def configure(self): """ @@ -154,7 +156,9 @@ class JCLRunner(object): else: 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"): self.debug = True self.logger.debug("Debug activated") @@ -245,7 +249,7 @@ class JCLRunner(object): model.db_connection_str = self.db_url model.db_connect() self.setup_db() - ipshell = IPShellEmbed(["-pi1", self.component_short_name + "[\\#]: "], + ipshell = IPShellEmbed(["-pi1", self.component_short_name + "[\\#]: "], "Starting " + self.component_name + " v" \ + self.component_version + " console.", "Ending " + self.component_name + " v" \ diff --git a/src/jcl/tests/runner.py b/src/jcl/tests/runner.py index 18c8375..2afc47c 100644 --- a/src/jcl/tests/runner.py +++ b/src/jcl/tests/runner.py @@ -3,18 +3,18 @@ ## Login : David Rousselie ## Started on Fri May 18 13:43:37 2007 David Rousselie ## $Id$ -## +## ## Copyright (C) 2007 David Rousselie ## 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA @@ -45,7 +45,7 @@ class JCLRunner_TestCase(unittest.TestCase): def tearDown(self): self.runner = None sys.argv = [""] - + def test_configure_default(self): self.runner.configure() 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.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): sys.argv = ["", "-c", "src/jcl/tests/jcl.conf", \ "-S", "test2_localhost", \ @@ -161,10 +174,10 @@ class JCLRunner_TestCase(unittest.TestCase): os.unlink(db_path) self.assertFalse(os.access("/tmp/jcl.pid", os.F_OK)) self.assertEquals(self.i, 2) - + def test__get_help(self): self.assertNotEquals(self.runner._get_help(), None) - + def suite(): test_suite = unittest.TestSuite() test_suite.addTest(unittest.makeSuite(JCLRunner_TestCase, 'test')) @@ -172,4 +185,3 @@ def suite(): if __name__ == '__main__': unittest.main(defaultTest='suite') - diff --git a/src/jcl/tests/uncomplete_jcl.conf b/src/jcl/tests/uncomplete_jcl.conf new file mode 100644 index 0000000..b5d6a42 --- /dev/null +++ b/src/jcl/tests/uncomplete_jcl.conf @@ -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