def test_transfering_sections_and_manipulating_options_with_deepcopy(): existing = """\ [section0] option0 = 0 """ template1 = """\ [section1] option1 = 1 """ template2 = """\ [section2] option2 = 2 # comment """ target = ConfigUpdater() target.read_string(dedent(existing)) source1 = ConfigUpdater() source1.read_string(dedent(template1)) source2 = ConfigUpdater() source2.read_string(dedent(template2)) target["section1"] = copy.deepcopy(source1["section1"]) assert "section1" in target assert "section1" in source1 target["section1"].add_after.section(copy.deepcopy(source2["section2"])) target["section2"]["option2"] = "value" assert "option2 = value" in str(target) assert "option2" not in str(source1) assert "value" not in str(source2)
def test_stdlib_docs_compatibility(): """Make sure optionxform works as documented in the stdlib docs (ConfigParser)""" config = """\ [Section1] Key = Value [Section2] AnotherKey = Value """ typical = ConfigUpdater() typical.read_string(dedent(config)) assert list(typical["Section1"].keys()) == ["key"] assert list(typical["Section2"].keys()) == ["anotherkey"] custom = ConfigUpdater() custom.optionxform = lambda option: option custom.read_string(dedent(config)) assert list(custom["Section1"].keys()) == ["Key"] assert list(custom["Section2"].keys()) == ["AnotherKey"] other = ConfigUpdater() other.optionxform = str other.read_string(dedent(config)) assert list(other["Section1"].keys()) == ["Key"] assert list(other["Section2"].keys()) == ["AnotherKey"]
def test_transfering_sections_and_manipulating_options(): # This test case is similar to `test_transfering_sections_between_elements`, # but now we use the builder API in the options inside the transferred section. # We need to make sure that when a section is copied to a different tree, # the references inside the options are updated, so we don't end up adding blocks in # the original object. existing = """\ [section0] option0 = 0 """ template1 = """\ [section1] option1 = 1 """ target = ConfigUpdater() target.read_string(dedent(existing)) source1 = ConfigUpdater() source1.read_string(dedent(template1)) target["section1"] = source1["section1"].detach() assert "section1" in target target["section1"]["option1"].add_after.option("option2", "value") assert "option2" not in str(source1) assert "option2" in str(target)
def test_section_setitem(): cfg = ConfigUpdater() cfg.optionxform = str.upper cfg.read_string("[section1]\nOTHERKEY = 0") assert "KEY" not in cfg["section1"] cfg["section1"]["key"] = "value" assert "KEY" in cfg["section1"] assert cfg["section1"]["KEY"].value == "value" cfg["section1"]["key"] = "42" assert cfg["section1"]["KEY"].value == "42" assert cfg["section1"]["key"].value == "42" other = ConfigUpdater() other.optionxform = str.lower other.read_string("[section1]\nkEy = value") option = other["section1"]["key"].detach() with pytest.raises(ValueError): # otherkey exists in section1, but option is `key` instead of `otherkey` cfg["section1"]["otherkey"] = option with pytest.raises(ValueError): # anotherkey exists in section1 and option is `key` instead of `anotherkey` cfg["section1"]["anotherkey"] = option assert cfg["section1"]["key"].raw_key == "key" cfg["section1"]["key"] = option assert cfg["section1"]["key"].value == "value" assert cfg["section1"]["key"].key == "KEY" assert cfg["section1"]["key"].raw_key == "kEy"
def test_eq(setup_cfg_path): updater1 = ConfigUpdater() updater1.read(setup_cfg_path) updater2 = ConfigUpdater() updater2.read(setup_cfg_path) assert updater1 == updater2 updater1.remove_section("metadata") assert updater1 != updater2 assert updater1 != updater2["metadata"] assert updater2["metadata"] != updater2["metadata"]["author"] assert not updater1.remove_section("metadata")
def test_eq(setup_cfg_path): updater1 = ConfigUpdater() updater1.read(setup_cfg_path) updater2 = ConfigUpdater() updater2.read(setup_cfg_path) assert updater1 == updater2 updater1.remove_section('metadata') assert updater1 != updater2 assert updater1 != updater2['metadata'] assert updater2['metadata'] != updater2['metadata']['author'] assert not updater1.remove_section('metadata')
def test_empty_lines_in_values_support(): updater = ConfigUpdater() updater.read_string(test21_cfg_in) parser = ConfigParser() parser.read_string(test21_cfg_in) assert updater["main1"]["key1"].value == parser["main1"]["key1"] assert updater["main2"]["key1"].value == parser["main2"]["key1"] assert updater["main4"]["key1"].value == parser["main4"]["key2"] assert test21_cfg_in == str(updater) with pytest.raises(ParsingError): updater = ConfigUpdater(empty_lines_in_values=False) updater.read_string(test21_cfg_in)
def test_comments_in_multiline_values(): indented_case = """\ [header] install_requires = importlib-metadata; python_version<"3.8" #Adding some comments here that are perfectly valid. some-other-dependency """ unindented_case = """\ [header] install_requires = importlib-metadata; python_version<"3.8" #Adding some comments here that are perfectly valid. some-other-dependency """ error_case_if_no_value = """\ [header] install_requires = importlib-metadata; python_version<"3.8" #Adding some comments here that are perfectly valid. some-other-dependency """ parser = ConfigParser(allow_no_value=False) updater = ConfigUpdater(allow_no_value=False) parser.read_string(dedent(indented_case)) updater.read_string(dedent(indented_case)) assert str(updater) == dedent(indented_case) parser.read_string(dedent(unindented_case)) updater.read_string(dedent(unindented_case)) assert str(updater) == dedent(unindented_case) with pytest.raises(configparser.ParsingError): parser.read_string(dedent(error_case_if_no_value)) with pytest.raises(ParsingError): updater.read_string(dedent(error_case_if_no_value)) parser = ConfigParser(allow_no_value=True) updater = ConfigUpdater(allow_no_value=True) parser.read_string(dedent(indented_case)) updater.read_string(dedent(indented_case)) assert str(updater) == dedent(indented_case) parser.read_string(dedent(unindented_case)) updater.read_string(dedent(unindented_case)) assert str(updater) == dedent(unindented_case) parser.read_string(dedent(error_case_if_no_value)) updater.read_string(dedent(error_case_if_no_value)) assert str(updater) == dedent(error_case_if_no_value)
def test_add_before_then_add_after_option(): updater = ConfigUpdater() updater.read_string(test17_cfg_in) updater['section']['key1'].add_before.option('key0', '0') updater['section']['key1'].add_after.option('key2', '2') updater['section']['key2'].add_after.option('key3', '3') assert str(updater) == test17_cfg_out
def test_read_mixed_case_options(): updater = ConfigUpdater() updater.read_string(test15_cfg_in) assert updater.has_option('section', 'OptionA') assert updater.has_option('section', 'optiona') assert updater['section']['OptionA'].value == '2' assert updater['section']['optiona'].value == '2'
def add_new_SFUI(df_final): updater = ConfigUpdater() updater.read('setup.cfg') # Subset into assigned and unassigned df = df_final[df_final['SFUI'] == ''] df_final = df_final[df_final['SFUI'] != ''] if df.empty: return df_final else: # Sort by SF df = df.sort_values(by=['SF']) df = df.reset_index(drop=True) # Assign SFUI assignment = int(updater['metadata']['sfui_last_assignment'].value) + 1 for index, row in df.iterrows(): if index == 0: df['SFUI'].iat[index] = assignment elif df['SF'].at[index] == df['SF'].at[index - 1]: df['SFUI'].iat[index] = assignment else: assignment += 1 df['SFUI'].iat[index] = assignment # Format SFUI df['SFUI'] = 'S' + (df.SFUI.map('{:06}'.format)) # Add back newly assigned df_final = pd.concat([df_final, df]) df_final = df_final.reset_index(drop=True) # Update config file updater['metadata']['sfui_last_assignment'].value = assignment updater.update_file() # Return dataframe return df_final
def test_add_detached_section_option_objects(): updater = ConfigUpdater() updater.read_string(test24_cfg_in) sec1 = updater["sec1"] sec2 = updater["sec2"] assert sec2.container is updater sec2.detach() assert not updater.has_section("sec2") assert not sec2.has_container() with pytest.raises(NotAttachedError): sec2.container new_sec2 = Section("new-sec2") new_opt = Option(key="new-key", value="new-value") new_sec2.add_option(new_opt) with pytest.raises(AlreadyAttachedError): sec1.add_option(new_opt) updater.add_section(new_sec2) assert updater.has_section("new-sec2") assert updater["new-sec2"]["new-key"].value == "new-value" new_sec3 = Section("new-sec3") new_opt2 = Option(key="new-key", value="new-value") updater["new-sec3"] = new_sec3 new_sec3["new-key"] = new_opt2
def read(self): self.main_file.read() if not self.main_file.cleartext: self.main_file.cleartext = NEW_FILE_TEMPLATE self.config = ConfigUpdater() self.config.read_string(self.main_file.cleartext) self.set_members(self.get_members())
def test_updater_to_dict(setup_cfg_path): updater = ConfigUpdater() updater.read(setup_cfg_path) parser = ConfigParser() parser.read(setup_cfg_path) parser_dict = {sect: dict(parser[sect]) for sect in parser.sections()} assert updater.to_dict() == parser_dict
def test_update_no_changes(setup_cfg_path): updater = ConfigUpdater() updater.read(setup_cfg_path) old_mtime = os.path.getmtime(setup_cfg_path) updater.update_file() new_mtime = os.path.getmtime(setup_cfg_path) assert old_mtime != new_mtime
def test_get_option(): updater = ConfigUpdater() updater.read_string(test1_cfg_in) option = updater["default"]["key"] assert option.value == "1" with pytest.raises(KeyError): updater["default"]["wrong_key"]
def test_items(setup_cfg_path): updater = ConfigUpdater() updater.read(setup_cfg_path) sect_names, sects = zip(*updater.items()) exp_sect_namess = [ "metadata", "options", "options.packages.find", "options.extras_require", "test", "tool:pytest", "aliases", "bdist_wheel", "build_sphinx", "devpi:upload", "flake8", "pyscaffold", ] assert list(sect_names) == exp_sect_namess assert all([isinstance(s, Section) for s in sects]) opt_names, opts = zip(*updater.items("devpi:upload")) exp_opt_names = ["no-vcs", "formats"] assert list(opt_names) == exp_opt_names assert all([isinstance(o, Option) for o in opts])
def test_del_section(): updater = ConfigUpdater() updater.read_string(test2_cfg_in) del updater["section2"] assert str(updater) == test2_cfg_out with pytest.raises(KeyError): del updater["section2"]
def test_set_option(): updater = ConfigUpdater(allow_no_value=True) updater.read_string(test1_cfg_in) updater.set("default", "key", "1") assert updater["default"]["key"].value == "1" updater.set("default", "key", 2) assert updater["default"]["key"].value == 2 assert str(updater) == test1_cfg_out updater.set("default", "key") assert updater["default"]["key"].value is None assert str(updater) == test1_cfg_out_none updater.read_string(test1_cfg_in) updater.set("default", "other_key", 3) assert str(updater) == test1_cfg_out_added updater.read_string(test1_cfg_in) values = ["param1", "param2"] updater["default"]["key"].set_values(values) assert str(updater) == test1_cfg_out_values assert values == ["param1", "param2"] # non destructive operation with pytest.raises(NoSectionError): updater.set("wrong_section", "key", "1") new_option = copy.deepcopy(updater["default"]["key"]) updater["default"]["key"] = new_option assert updater["default"]["key"] is new_option new_option = copy.deepcopy(updater["default"]["key"]) new_option.key = "wrong_key" with pytest.raises(ValueError): updater["default"]["key"] = new_option
def test_modify_multiline_value(): ml_value = """\ [metadata] classifiers = Development Status :: 3 - Alpha #Development Status :: 4 - Beta #Development Status :: 5 - Production/Stable Programming Language :: Python :: 3 :: Only Programming Language :: Python :: 3 Programming Language :: Python :: Implementation :: CPython Programming Language :: Python :: Implementation :: PyPy License :: OSI Approved :: MIT License """ ml_value = dedent(ml_value) updater = ConfigUpdater() updater.read_string(ml_value) with pytest.raises(AssignMultilineValueError): updater["metadata"][ "classifiers"].value += "License :: OSI Approved :: BSD" new_classifiers = updater["metadata"]["classifiers"].value.strip().split( "\n") new_classifiers += ["Topic :: Utilities"] updater["metadata"]["classifiers"].set_values(new_classifiers) ml_value += " Topic :: Utilities\n" assert str(updater) == ml_value updater["metadata"]["classifiers"].value = "new_value" updater["metadata"]["classifiers"].value += "_and_more" expected = """\ [metadata] classifiers = new_value_and_more """ assert str(updater) == dedent(expected)
def test_get_option(): updater = ConfigUpdater() updater.read_string(test1_cfg_in) option = updater['default']['key'] assert option.value == '1' with pytest.raises(KeyError): updater['default']['wrong_key']
def test_read_mixed_case_options(): updater = ConfigUpdater() updater.read_string(test15_cfg_in) assert updater.has_option("section", "OptionA") assert updater.has_option("section", "optiona") assert updater["section"]["OptionA"].value == "2" assert updater["section"]["optiona"].value == "2"
def test_del_option(): updater = ConfigUpdater() updater.read_string(test1_cfg_in) del updater['default']['key'] assert str(updater) == "\n[default]\n" with pytest.raises(KeyError): del updater['default']['key']
def test_add_before_then_add_after_option(): updater = ConfigUpdater() updater.read_string(test17_cfg_in) updater["section"]["key1"].add_before.option("key0", "0") updater["section"]["key1"].add_after.option("key2", "2") updater["section"]["key2"].add_after.option("key3", "3") assert str(updater) == test17_cfg_out
def test_validate_format(setup_cfg_path): updater = ConfigUpdater(allow_no_value=False) updater.read(setup_cfg_path) updater.validate_format() updater.set('metadata', 'author') with pytest.raises(ParsingError): updater.validate_format()
def cleartext(self, value): self.config = ConfigUpdater() self.config.read_string(value) self.set_members(self.get_members()) s = io.StringIO() self.config.write(s) self._cleartext = s.getvalue()
def set_load_config(database_path = None, cluster_cookie=None): """ Help to set the mission data loading. Parameters ---------- database_path : str Absolute path where to save all the downloaded files. It not, the default path is ~/heliopy/ cluster_cookie : str Cookie from ESA to download cluster data. """ #Get the path of heliopyrc file config_path = get_config_file() # Read the preexisting config file using configupdater config = ConfigUpdater() config.read(config_path) # Set new data download directory if passed if database_path: config['DEFAULT']['download_dir'].value = database_path # Set new cluster cookie if passed if cluster_cookie: config['DEFAULT']['cluster_cookie'].value = cluster_cookie # Update the config file with new entries config.update_file()
def test_add_before_after_option(): updater = ConfigUpdater() updater.read_string(test4_cfg_in) with pytest.raises(ValueError): updater['section'].add_before.option('key0', 0) updater['section']['key1'].add_before.option('key0', 0) updater['section']['key1'].add_after.option('key2') assert str(updater) == test4_cfg_out
def test_set_optionxform(): updater = ConfigUpdater() updater.read_string(test13_cfg_in) assert updater['section']['capital'].value == '1' assert callable(updater.optionxform) updater.optionxform = lambda x: x updater.read_string(test13_cfg_in) assert updater['section']['CAPITAL'].value == '1'
def test_set_item_section(): updater = ConfigUpdater() sect_updater = ConfigUpdater() updater.read_string(test6_cfg_in) with pytest.raises(ValueError): updater['section'] = 'newsection' sect_updater.read_string(test6_cfg_in) section = sect_updater['section0'] updater['new_section'] = section assert str(updater) == test6_cfg_out_new_sect # test overwriting an existing section updater.read_string(test6_cfg_in) sect_updater.read_string(test6_cfg_in) exist_section = sect_updater['section0'] exist_section['key0'] = 42 updater['section0'] = exist_section assert str(updater) == test6_cfg_out_overwritten