Permalink
Browse files

using YadtConfigParser from yadtcommons

  • Loading branch information...
1 parent 226f888 commit 3538f62cd5b62ed0bbfe593c714059cfaf8167d0 Marcel Wolf committed Feb 25, 2013
Showing with 3 additions and 236 deletions.
  1. +1 −0 build.py
  2. +1 −95 src/main/python/yadtreceiver/configuration.py
  3. +1 −141 src/unittest/python/configuration_tests.py
View
@@ -43,6 +43,7 @@ def set_properties(project):
project.depends_on('PyYAML')
project.depends_on('Twisted')
project.depends_on('yadtbroadcast-client')
+ project.depends_on('yadtcommons')
project.build_depends_on('mock')
project.build_depends_on('coverage')
@@ -40,6 +40,7 @@
import os
import socket
import sys
+from yadtcommons.configuration import YadtConfigParser
from ConfigParser import SafeConfigParser
@@ -62,101 +63,6 @@
SECTION_GRAPHITE = 'graphite'
-class ConfigurationException (Exception):
- """
- to be raised when an configuration error occurs.
- """
-
-
-class YadtConfigParser (object):
- def __init__(self):
- """
- Creates instance of SafeConfigParser which will be used to parse
- the configuration file.
- """
- self._parser = SafeConfigParser()
-
-
- def get_option(self, section, option, default_value):
- """
- @return: the option from the section if it exists,
- otherwise the given default value.
- """
- if self._parser.has_option(section, option):
- return self._parser.get(section, option)
-
- return default_value
-
-
- def get_option_as_yes_or_no_boolean(self, section, option, default_value):
- """
- @return: the boolean option from the section if it exists,
- otherwise the given default value.
- """
- option_value = self.get_option(section, option, default_value)
-
- if option_value != 'yes' and option_value != 'no':
- raise ConfigurationException('Option %s in section %s expected "yes" or "no", but got %s'
- % (option, section, option_value))
-
- return option_value == 'yes'
-
-
- def get_option_as_int(self, section, option, default_value):
- """
- @return: the integer option from the section if it exists,
- otherwise the given default value.
- """
- option_value = self.get_option(section, option, default_value)
-
- if not option_value.isdigit():
- raise ConfigurationException('Option %s in section %s expected a integer value, but got %s'
- % (option, section, option_value))
- return int(option_value)
-
-
- def get_option_as_list(self, section, option, default_value):
- """
- @return: the option (list of strings) from the section if it exists
- otherwise the given default value.
- """
- option_value = self.get_option(section, option, '')
-
- if option_value == '':
- return default_value
-
- list_of_unstripped_options = option_value.split(',')
-
- result = []
- for unstripped_option in list_of_unstripped_options:
- result.append(unstripped_option.strip())
-
- return result
-
-
- def get_option_as_set(self, section, option, default_value):
- """
- @return: the option (set of strings) from the section if it exists,
- otherwise the given default value.
- """
- option_values = self.get_option_as_list(section, option, default_value)
- return set(option_values)
-
-
- def read_configuration_file(self, filename):
- """
- reads the file into the parser. Will exit with error code 1 if
- the configuration file does not exist.
- """
- if not os.path.exists(filename):
- sys.stderr.write('Configuration file "%s" does not exist.\n' % filename)
- exit(1)
- return
-
- sys.stdout.write('Loading configuration file "%s"' % filename)
- self._parser.read([filename])
-
-
class ReceiverConfigLoader (object):
"""
uses a SafeConfigParser to offer some convenience methods.
@@ -34,149 +34,9 @@
SECTION_BROADCASTER,
SECTION_GRAPHITE,
SECTION_RECEIVER,
- ConfigurationException,
ReceiverConfigLoader,
- YadtConfigParser,
load)
-
-
-class YadtConfigParserTests (unittest.TestCase):
- def test_should_create_instance_of_YadtConfigParser (self):
- parser = YadtConfigParser()
-
- name_of_type = parser._parser.__class__.__name__
- self.assertEqual('SafeConfigParser', name_of_type)
-
-
- @patch('yadtreceiver.configuration.sys')
- @patch('yadtreceiver.configuration.os.path.exists')
- @patch('__builtin__.exit')
- def test_should_exit_when_configuration_file_does_not_exist (self, mock_exit, mock_exists, mock_log):
- mock_parser = Mock(YadtConfigParser)
- mock_exists.return_value = False
-
- YadtConfigParser.read_configuration_file(mock_parser, 'some.cfg')
-
- self.assertEqual(call('some.cfg'), mock_exists.call_args)
- self.assertEqual(call(1), mock_exit.call_args)
-
-
- @patch('yadtreceiver.configuration.sys')
- @patch('yadtreceiver.configuration.os.path.exists')
- def test_should_read_configuration_file (self, mock_exists, mock_log):
- mock_parser = Mock(YadtConfigParser)
- mock_wrapped_parser = Mock()
- mock_parser._parser = mock_wrapped_parser
- mock_exists.return_value = True
-
- YadtConfigParser.read_configuration_file(mock_parser, 'some.cfg')
-
- self.assertEqual(call(['some.cfg']), mock_wrapped_parser.read.call_args)
-
-
- def test_should_raise_exception_when_given_option_is_not_digit (self):
- mock_parser = Mock(YadtConfigParser)
- mock_parser.get_option.return_value = 'abcdef'
-
- self.assertRaises(ConfigurationException, YadtConfigParser.get_option_as_int, mock_parser, 'section', 'option', 'default_value')
-
-
- def test_should_return_default_when_option_not_available (self):
- mock_parser = Mock(YadtConfigParser)
- mock_parser.get_option.return_value = ''
-
- actual_option = YadtConfigParser.get_option_as_list(mock_parser, 'section', 'option', 'default_value')
-
- self.assertEqual('default_value', actual_option)
- self.assertEqual(call('section', 'option', ''), mock_parser.get_option.call_args)
-
-
- def test_should_return_list_separated_by_comma (self):
- mock_parser = Mock(YadtConfigParser)
- mock_parser.get_option.return_value = ' abc, def,ghi,jkl '
-
- actual_option = YadtConfigParser.get_option_as_list(mock_parser, 'section', 'option', 'default_value')
-
- self.assertEqual(['abc', 'def', 'ghi', 'jkl'], actual_option)
- self.assertEqual(call('section', 'option', ''), mock_parser.get_option.call_args)
-
-
- def test_should_return_a_set (self):
- mock_parser = Mock(YadtConfigParser)
- mock_parser.get_option_as_list.return_value = ['abc', 'def', 'ghi', 'jkl']
-
- actual_option = YadtConfigParser.get_option_as_set(mock_parser, 'section', 'option', 'default_value')
-
- self.assertEqual(set(['abc', 'def', 'ghi', 'jkl']), actual_option)
- self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option_as_list.call_args)
-
-
- def test_should_return_yes_as_boolean_value_true (self):
- mock_parser = Mock(YadtConfigParser)
- mock_parser.get_option.return_value = 'yes'
-
- actual_option = YadtConfigParser.get_option_as_yes_or_no_boolean(mock_parser, 'section', 'option', 'default_value')
-
- self.assertEqual(True, actual_option)
- self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option.call_args)
-
-
- def test_should_return_no_as_boolean_value_false (self):
- mock_parser = Mock(YadtConfigParser)
- mock_parser.get_option.return_value = 'no'
-
- actual_option = YadtConfigParser.get_option_as_yes_or_no_boolean(mock_parser, 'section', 'option', 'default_value')
-
- self.assertEqual(False, actual_option)
- self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option.call_args)
-
-
- def test_should_raise_exception_when_given_value_is_not_yes_or_no (self):
- mock_parser = Mock(YadtConfigParser)
- mock_wrapped_parser = Mock()
- mock_wrapped_parser.get_option.return_value = 'tralala'
- mock_parser._parser = mock_wrapped_parser
-
- self.assertRaises(ConfigurationException, YadtConfigParser.get_option_as_yes_or_no_boolean, mock_parser, 'section', 'option', 'default_value')
-
-
- def test_should_return_option_from_section (self):
- mock_parser = Mock(YadtConfigParser)
- mock_wrapped_parser = Mock()
- mock_wrapped_parser.has_section.return_value = True
- mock_wrapped_parser.has_option.return_value = True
- mock_wrapped_parser.get.return_value = 'the option'
- mock_parser._parser = mock_wrapped_parser
-
- actual_option = YadtConfigParser.get_option(mock_parser, 'section', 'option', 'default_value')
-
- self.assertEqual('the option', actual_option)
- self.assertEqual(call('section', 'option'), mock_wrapped_parser.has_option.call_args)
- self.assertEqual(call('section', 'option'), mock_wrapped_parser.get.call_args)
-
-
- def test_should_return_default_value_when_option_not_in_section (self):
- mock_parser = Mock(YadtConfigParser)
- mock_wrapped_parser = Mock()
- mock_wrapped_parser.has_section.return_value = True
- mock_wrapped_parser.has_option.return_value = False
- mock_parser._parser = mock_wrapped_parser
-
- actual_option = YadtConfigParser.get_option(mock_parser, 'section', 'option', 'default_value')
-
- self.assertEqual('default_value', actual_option)
- self.assertEqual(call('section', 'option'), mock_wrapped_parser.has_option.call_args)
-
-
- def test_should_return_option_as_int (self):
- mock_parser = Mock(YadtConfigParser)
- mock_parser.get_option.return_value = '123456'
-
- actual_option = YadtConfigParser.get_option_as_int(mock_parser, 'section', 'option', 'default_value')
-
- self.assertEqual(123456, actual_option)
- self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option.call_args)
-
+from yadtcommons.configuration import YadtConfigParser
class ReceiverConfigLoaderTests (unittest.TestCase):
def test_should_create_instance_of_SafeConfigParser (self):

0 comments on commit 3538f62

Please sign in to comment.