def export_config_file(self): filename = asksaveasfilename( initialdir=self.get_state()['home_directory'], title='select .cfg file', initialfile='exported_config.cfg', defaultextension='.cfg') if not filename: return None save_config_to_filename(self.get_state(), filename)
def test_ConfigLoader_set_state_from_file_bad_directory_value_raises_ConfigFileError( self): filename = os.path.join(TESTS_FILES, 'tst.cfg') loader = ConfigLoader() save_config_to_filename( {'home_directory': os.path.join(TESTS_FILES, 'not', 'there')}, filename) self.assertRaises(ConfigFileError, loader.set_state_from_file, filename) # cleanup os.remove(filename)
def test_init_button_assignment_load_config(self, mock_filename): filename = os.path.join(TESTS_FILES, 'tst.cfg') mock_filename.return_value = filename save_config_to_filename({'file_prefix': 'silly'}, filename) main = MainFrame() action_frame = get_action_frame(main) default = main.get_state() main.load_config_from_file() self.assertNotEqual(default, main.get_state()) action_frame.reload_config.invoke() self.assertEqual(default, main.get_state()) os.remove(filename)
def test_load_config_from_file_loads_from_file(self, mock_filename): filename = os.path.join(TESTS_FILES, 'tst.cfg') mock_filename.return_value = filename save_config_to_filename({'probability_plural_noun': 0.0}, filename) main = MainFrame() default_state = main.get_state() main.load_config_from_file() self.assertNotEqual(main.get_state(), default_state) loader = ConfigLoader() loader.set_state_from_file(filename) self.assertEqual(loader.state, main.get_state()) os.remove(filename)
def test_ConfigLoader_set_state_from_file_no_errors(self): filename = os.path.join(TESTS_FILES, 'test.cfg') save_config_to_filename({'error_probability': 1.0}, filename) # default 0.20 loader = ConfigLoader() loader.set_state_from_file(filename) default_loader = ConfigLoader() loader_state = loader.state default_state = default_loader.state self.assertNotEqual(loader_state, default_state) self.assertEqual(loader_state['error_probability'], 1.0) loader_state['error_probability'] = 0.20 self.assertEqual(loader_state, default_state) # cleanup os.remove(filename)
def test_save_config_to_filename(self): filename = os.path.join(TESTS_FILES, 'test.cfg') with open(DEFAULT_CONFIG, 'r') as f: default_text = f.read() save_config_to_filename( { 'paragraph_type': 'bobo', 'paragraph_size': 10 }, filename) with open(filename, 'r') as f: config_text = f.read() # cleanup os.remove(filename) answer = default_text.replace('paragraph_size = 15', 'paragraph_size = 10') answer = answer.replace('paragraph_type = chain', 'paragraph_type = bobo') self.assertEqual(answer, config_text)
def test_load_config_from_file_bad_file_without_reset( self, mock_filename, mock_error): filename = os.path.join(TESTS_FILES, 'tst.cfg') bad_path = os.path.join(TESTS_FILES, 'nope', 'really_nope') mock_filename.return_value = filename save_config_to_filename({'save_directory': bad_path}, filename) main = MainFrame() save_config({'probability_plural_noun': 0.0}) main.load_config() current_state = main.get_state() main.load_config_from_file() self.assertEqual(main.get_state(), current_state) self.assertEqual(mock_error.call_args[0][0], 'bad config file') msg = "ConfigFileError: Config Loader failed to create the following directory:\n" self.assertIn(msg, mock_error.call_args[0][1]) os.remove(filename)
def test_load_config_from_file_bad_file_with_reset(self, mock_filename, mock_error): filename = os.path.join(TESTS_FILES, 'tst.cfg') mock_filename.return_value = filename save_config_to_filename({'probability_plural_noun': 'oops'}, filename) main = MainFrame() default_state = main.get_state() save_config({'probability_plural_noun': 0.0}) main.load_config() main.load_config_from_file() self.assertEqual(main.get_state(), default_state) mock_error.assert_called_with( 'bad config file', "ConfigFileError: Tried to set key: 'probability_plural_noun' to incompatible value: 'oops'." ) os.remove(filename)