class SettingsManager(object): ''' SettingsManager class manage configuration file, reading and writing settings ''' def __init__(self, fileName): "Initialize all the variables use to manage settings of the configuration file" self.settings = Settings(fileName) self.fileName = fileName def load(self): ''' Settings are read of the configuration file given a file_name ''' reader = SettingsReader() try: self.settings = reader.read(self.fileName) except FileNotFoundException: self.settings.createDefaultSettings() self.save() def save(self): ''' Settings are save\wrote in the configuration file ''' writer = SettingsWriter() writer.write(self.settings, self.fileName) def getSettings(self): ''' return settings of the configuration file ''' return self.settings
def test_the_create_default_settings_should_set_default_value_for_output_type(self): settings = Settings("") settings.createDefaultSettings() self.assertEqual(settings.DEFAULT_OUTPUT_TYPE, settings.getOutputType()) self.assertEqual(settings.DEFAULT_ALGORITHM_NAME, settings.getAlgorithmName()) self.assertEqual(settings.DEFAULT_LEVEL_NAME, settings.getDefaultLevel()) self.assertEqual(3, len(settings.getLevels())) self.assertEqual(self.defaultLevel, settings.getLevels()[0])
def setUp(self): self.level1 = Level("Level 1", 5, 10) self.level2 = Level("Level 2", 11, 20) self.level3 = Level("Level 3", 21, 30) self.settings = Settings("") self.settings.addLevel(self.level1) self.settings.addLevel(self.level2) self.settings.addLevel(self.level3) self.defaultLevel = Level(self.settings.DEFAULT_LEVEL_NAME, self.settings.DEFAULT_MIN, self.settings.DEFAULT_MAX)
class TestSettings(unittest.TestCase): def setUp(self): self.level1 = Level("Level 1", 5, 10) self.level2 = Level("Level 2", 11, 20) self.level3 = Level("Level 3", 21, 30) self.settings = Settings("") self.settings.addLevel(self.level1) self.settings.addLevel(self.level2) self.settings.addLevel(self.level3) self.defaultLevel = Level(self.settings.DEFAULT_LEVEL_NAME, self.settings.DEFAULT_MIN, self.settings.DEFAULT_MAX) def tearDown(self): pass def test_getLevelNone(self): level = self.settings.getLevel("Level 4") self.assertEqual(None, level) def test_getLevel(self): level = self.settings.getLevel("Level 2") self.assertEqual(self.level2, level) def test_the_create_default_settings_should_set_default_value_for_output_type(self): settings = Settings("") settings.createDefaultSettings() self.assertEqual(settings.DEFAULT_OUTPUT_TYPE, settings.getOutputType()) self.assertEqual(settings.DEFAULT_ALGORITHM_NAME, settings.getAlgorithmName()) self.assertEqual(settings.DEFAULT_LEVEL_NAME, settings.getDefaultLevel()) self.assertEqual(3, len(settings.getLevels())) self.assertEqual(self.defaultLevel, settings.getLevels()[0])
def setUp(self): self.fileName = "settings.xml" self.outputType = "file" self.algorithmName = "Peter Norving" self.defaultLevel = "Level 2" self.levels = [Level("Level 1", 5, 10), Level("Level 2", 11, 20), Level("Level 3", 21, 30)] self.path = "c:\\test" self.settings = Settings(self.fileName) self.settings.setOutputType(self.outputType) self.settings.setAlgorithmName(self.algorithmName) self.settings.setPath(self.path) self.settings.setDefaultLevel(self.defaultLevel) for level in self.levels: self.settings.addLevel(level)
def read(self, fileName): settings = Settings(fileName) try: settingsdoc = minidom.parse(fileName) settings.outputType = self.readAttributeConfiguration('outputtype', settings.DEFAULT_OUTPUT_TYPE, settingsdoc) if(settings.outputType == "file"): settings.path = self.readAttributeConfiguration('path', "", settingsdoc) settings.algorithmName = self.readAttributeConfiguration('algorithmname', settings.DEFAULT_ALGORITHM_NAME, settingsdoc) settings.defaultLevel = self.readAttributeConfiguration('defaultlevel', settings.DEFAULT_LEVEL_NAME, settingsdoc) settings.levels = self.readlevels(settingsdoc, settings) except FileNotFoundError: raise FileNotFoundException(fileName) except ExpatError: raise InvalidXMLSettingsException(fileName) return settings
class TestSettingsWriter(unittest.TestCase): def setUp(self): self.fileName = "settings.xml" self.outputType = "file" self.algorithmName = "Peter Norving" self.defaultLevel = "Level 2" self.levels = [Level("Level 1", 5, 10), Level("Level 2", 11, 20), Level("Level 3", 21, 30)] self.path = "c:\\test" self.settings = Settings(self.fileName) self.settings.setOutputType(self.outputType) self.settings.setAlgorithmName(self.algorithmName) self.settings.setPath(self.path) self.settings.setDefaultLevel(self.defaultLevel) for level in self.levels: self.settings.addLevel(level) def tearDown(self): pass def readFile(self, fileName): file = open(self.fileName) result = "" line = file.readline() while line != "": result = result + line line = file.readline() return result def test_given_the_ouputtype_parameter_then_should_be_set_the_ouputtype_parameter_in_the_file_when_it_is_saved(self): outputType = "console" writer = SettingsWriter() self.settings.setOutputType(outputType) writer.write(self.settings, self.fileName) actualValue = self.readFile(self.fileName) excpectedValue = "<data><outputtype>console</outputtype><algorithmname>Peter Norving</algorithmname><defaultlevel>Level 2</defaultlevel><path>c:\\test</path><levels><level maxLevel=\"10\" minLevel=\"5\" name=\"Level 1\" /><level maxLevel=\"20\" minLevel=\"11\" name=\"Level 2\" /><level maxLevel=\"30\" minLevel=\"21\" name=\"Level 3\" /></levels></data>" self.maxDiff = None self.assertEqual(excpectedValue, actualValue) def test_given_the_algorithmName_parameter_then_should_be_set_the_algorithmName_parameter_in_the_file_when_it_is_saved(self): algorithmName = "Recursive" writer = SettingsWriter() self.settings.setAlgorithmName(algorithmName) writer.write(self.settings, self.fileName) actualValue = self.readFile(self.fileName) excpectedValue = "<data><outputtype>file</outputtype><algorithmname>Recursive</algorithmname><defaultlevel>Level 2</defaultlevel><path>c:\\test</path><levels><level maxLevel=\"10\" minLevel=\"5\" name=\"Level 1\" /><level maxLevel=\"20\" minLevel=\"11\" name=\"Level 2\" /><level maxLevel=\"30\" minLevel=\"21\" name=\"Level 3\" /></levels></data>" self.maxDiff = None self.assertEqual(excpectedValue, actualValue) def test_given_the_defaultLevel_parameter_then_should_be_set_the_defaultLevel_parameter_in_the_file_when_it_is_saved(self): defaultLevel = "Level 1" writer = SettingsWriter() self.settings.setDefaultLevel(defaultLevel) writer.write(self.settings, self.fileName) actualValue = self.readFile(self.fileName) excpectedValue = "<data><outputtype>file</outputtype><algorithmname>Peter Norving</algorithmname><defaultlevel>Level 1</defaultlevel><path>c:\\test</path><levels><level maxLevel=\"10\" minLevel=\"5\" name=\"Level 1\" /><level maxLevel=\"20\" minLevel=\"11\" name=\"Level 2\" /><level maxLevel=\"30\" minLevel=\"21\" name=\"Level 3\" /></levels></data>" self.maxDiff = None self.assertEqual(excpectedValue, actualValue) def test_given_the_path_parameter_then_should_be_set_the_path_parameter_in_the_file_when_it_is_saved(self): path = "h:\\test\\test2" writer = SettingsWriter() self.settings.setPath(path) writer.write(self.settings, self.fileName) actualValue = self.readFile(self.fileName) excpectedValue = "<data><outputtype>file</outputtype><algorithmname>Peter Norving</algorithmname><defaultlevel>Level 2</defaultlevel><path>h:\\test\\test2</path><levels><level maxLevel=\"10\" minLevel=\"5\" name=\"Level 1\" /><level maxLevel=\"20\" minLevel=\"11\" name=\"Level 2\" /><level maxLevel=\"30\" minLevel=\"21\" name=\"Level 3\" /></levels></data>" self.maxDiff = None self.assertEqual(excpectedValue, actualValue) def test_given_the_levels_parameter_then_should_be_added_the_levels_parameter_in_the_file_when_it_is_saved(self): level = Level("Level 4", 7, 40) self.settings.addLevel(level) writer = SettingsWriter() writer.write(self.settings, self.fileName) actualValue = self.readFile(self.fileName) excpectedValue = "<data><outputtype>file</outputtype><algorithmname>Peter Norving</algorithmname><defaultlevel>Level 2</defaultlevel><path>c:\\test</path><levels><level maxLevel=\"10\" minLevel=\"5\" name=\"Level 1\" /><level maxLevel=\"20\" minLevel=\"11\" name=\"Level 2\" /><level maxLevel=\"30\" minLevel=\"21\" name=\"Level 3\" /><level maxLevel=\"40\" minLevel=\"7\" name=\"Level 4\" /></levels></data>" self.maxDiff = None self.assertEqual(excpectedValue, actualValue)
def __init__(self, fileName): "Initialize all the variables use to manage settings of the configuration file" self.settings = Settings(fileName) self.fileName = fileName