def test_set_base_options(self): msf_cfg.theme = msf_cfg.Theme() cp = msf_cfg.ConfigParserWithComments() ask_ori = msf_cfg.ask defaults = MacsyDefaults() resp = [ "Yes", # enter section ? "ordered", # db_type "linear", # replicon_topology "None", # sequence_db ] g = (r for r in resp) def fake_ask(*args, **kwargs): return next(g) try: msf_cfg.ask = fake_ask with self.catch_io(out=True): msf_cfg.set_base_options(cp, defaults) stdout = sys.stdout.getvalue().strip() self.assertEqual( stdout, f"{msf_cfg.theme.SECTION}Configuring base options:{msf_cfg.theme.RESET}" ) self.assertTrue(cp.has_section("base")) self.assertEqual(cp.get("base", "db_type"), "ordered") self.assertEqual(cp.get("base", "replicon_topology"), "linear") self.assertEqual(cp.get("base", "sequence_db"), "None") finally: msf_cfg.ask = ask_ori
def test_set_general_options(self): msf_cfg.theme = msf_cfg.Theme() cp = msf_cfg.ConfigParserWithComments() ask_ori = msf_cfg.ask defaults = MacsyDefaults() resp = [ "Yes", # enter section ? 'warning', # log_level 0, # worker True # mute ] g = (r for r in resp) def fake_ask(*args, **kwargs): return next(g) try: msf_cfg.ask = fake_ask with self.catch_io(out=True): msf_cfg.set_general_options(cp, defaults) stdout = sys.stdout.getvalue().strip() self.assertEqual( stdout, f"{msf_cfg.theme.SECTION}Configuring general options:{msf_cfg.theme.RESET}" ) self.assertTrue(cp.has_section("general")) self.assertEqual(cp.get("general", "log_level"), "warning") self.assertEqual(cp.get("general", "worker"), "0") self.assertEqual(cp.get("general", "mute"), "True") finally: msf_cfg.ask = ask_ori
def test_add_comment(self): cp = msf_cfg.ConfigParserWithComments() cp.add_section('section_1') cp.add_comment('section_1', 'opt_1', 'comment_1') self.assertTrue(cp.has_option('section_1', 'opt_1_1_comment')) cp.add_comment('section_1', 'opt_1', 'comment_1') self.assertTrue(cp.has_option('section_1', 'opt_1_2_comment'))
def test_set_section_use_defaults(self): msf_cfg.theme = msf_cfg.Theme() cp = msf_cfg.ConfigParserWithComments() sec_name = "new_section" options = { 'hmmer': { "question": "that is the question", "validator": msf_cfg.check_str, "default": "Yes", "explanation": "" } } defaults = MacsyDefaults(hmmer="Yes") with self.catch_io(out=True): msf_cfg.set_section(sec_name, options, cp, defaults, use_defaults=True) stdout = sys.stdout.getvalue().strip() self.assertTrue(cp.has_section(sec_name)) self.assertFalse(cp.has_option(sec_name, 'hmmer')) self.assertEqual( stdout, f"{msf_cfg.theme.SECTION}Configuring new_section options:{msf_cfg.theme.RESET}" )
def test_write(self): cp = msf_cfg.ConfigParserWithComments() cp.add_section('section_1') cp.add_comment('section_1', 'opt_1', 'comment_1\ncomment_1 line 2', add_space_before=False, add_space_after=False) cp.add_comment('section_1', 'opt_1', 'comment_1_2', add_space_before=False, add_space_after=False) cp.set('section_1', 'opt_1', 'value_1') cp.add_comment('section_1', 'opt_2', 'comment_2', add_space_before=True, add_space_after=False) cp.set('section_1', 'opt_2', 'value_2') cp.add_comment('section_1', 'opt_3', 'comment_3', add_space_before=True, add_space_after=True) cp.set('section_1', 'opt_3', 'value_3') expected = """[section_1] # comment_1 # comment_1 line 2 # comment_1_2 opt_1 = value_1 # comment_2 opt_2 = value_2 # comment_3 opt_3 = value_3 """ f = io.StringIO() cp.write(f) self.assertEqual(f.getvalue(), expected)
def test_set_path_options_with_system_models_dir(self): msf_cfg.theme = msf_cfg.Theme() cp = msf_cfg.ConfigParserWithComments() ask_ori = msf_cfg.ask defaults = MacsyDefaults() test_dir = os.path.dirname(__file__) parent = os.path.join(os.path.normpath(os.path.join(test_dir, '..'))) resp = [ "Yes", # enter section ? [parent, test_dir], # system_models_dir os.getcwd(), # res_search_dir "res_search_suffix", # res_search_suffix "res_extract_suffix", # res_extract_suffix "profile_suffix" ] # profile_suffix g = (r for r in resp) def fake_ask(*args, **kwargs): return next(g) try: msf_cfg.ask = fake_ask with self.catch_io(out=True): msf_cfg.set_path_options(cp, defaults) stdout = sys.stdout.getvalue().strip() self.assertEqual( stdout, f"{msf_cfg.theme.SECTION}Configuring directories options:{msf_cfg.theme.RESET}" ) self.assertTrue(cp.has_section("directories")) self.maxDiff = None self.assertEqual(cp.get("directories", "system_models_dir"), f"{parent}, {test_dir}") self.assertEqual(cp.get("directories", "res_search_dir"), os.getcwd()) self.assertEqual(cp.get("directories", "res_search_suffix"), "res_search_suffix") self.assertEqual(cp.get("directories", "res_extract_suffix"), "res_extract_suffix") self.assertEqual(cp.get("directories", "profile_suffix"), "profile_suffix") finally: msf_cfg.ask = ask_ori
def test_set_score_options(self): msf_cfg.theme = msf_cfg.Theme() cp = msf_cfg.ConfigParserWithComments() ask_ori = msf_cfg.ask defaults = MacsyDefaults() resp = [ "Yes", # enter section ? 0.1, # mandatory_weight 0.2, # accessory_weight 0.3, # exchangeable_weight 0.4, # redundancy_penalty 0.5 # out_of_cluster_weight ] g = (r for r in resp) def fake_ask(*args, **kwargs): return next(g) try: msf_cfg.ask = fake_ask with self.catch_io(out=True): msf_cfg.set_score_options(cp, defaults) stdout = sys.stdout.getvalue().strip() self.assertEqual( stdout, f"{msf_cfg.theme.SECTION}Configuring score_opt options:{msf_cfg.theme.RESET}" ) self.assertTrue(cp.has_section("score_opt")) self.assertEqual(cp.get("score_opt", "mandatory_weight"), "0.1") self.assertEqual(cp.get("score_opt", "accessory_weight"), "0.2") self.assertEqual(cp.get("score_opt", "exchangeable_weight"), "0.3") self.assertEqual(cp.get("score_opt", "redundancy_penalty"), "0.4") self.assertEqual(cp.get("score_opt", "out_of_cluster_weight"), "0.5") finally: msf_cfg.ask = ask_ori
def test_set_hmmer_options(self): msf_cfg.theme = msf_cfg.Theme() cp = msf_cfg.ConfigParserWithComments() ask_ori = msf_cfg.ask defaults = MacsyDefaults() resp = [ "Yes", # enter section ? defaults.hmmer, # hmmer exe True, # no_cut_ga the fake_ask do not perform casting 0.002, # e_value_search, 0.003, # i_evalue_sel, 0.004 # coverage_profile ] g = (r for r in resp) def fake_ask(*args, **kwargs): return next(g) try: msf_cfg.ask = fake_ask with self.catch_io(out=True): msf_cfg.set_hmmer_options(cp, defaults) stdout = sys.stdout.getvalue().strip() self.assertEqual( stdout, f"{msf_cfg.theme.SECTION}Configuring hmmer options:{msf_cfg.theme.RESET}" ) self.assertTrue(cp.has_section("hmmer")) self.assertFalse(cp.has_option("hmmer", "hmmer")) # all values are casted in str before inserting in ConfigParser self.assertEqual(cp.get("hmmer", "no_cut_ga"), 'True') self.assertEqual(cp.get("hmmer", "e_value_search"), '0.002') self.assertEqual(cp.get("hmmer", "i_evalue_sel"), '0.003') self.assertEqual(cp.get("hmmer", "coverage_profile"), '0.004') finally: msf_cfg.ask = ask_ori