예제 #1
0
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 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_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_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
예제 #6
0
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"
예제 #7
0
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_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_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']
예제 #10
0
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)
예제 #11
0
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)
예제 #12
0
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_no_duplicate_blocks_with_blockbuilder():
    updater = ConfigUpdater()
    updater.read_string(test19_cfg_in)
    with pytest.raises(DuplicateOptionError):
        updater["section"]["Key0"].add_after.option("key0", "1")
    with pytest.raises(DuplicateSectionError):
        updater["section"].add_after.section("section")
    assert str(updater) == test19_cfg_in
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_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
예제 #16
0
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'
예제 #17
0
def test_iter_consistency_with_configparser():
    parser = ConfigParser()
    parser.read_string(test24_cfg_in)
    updater = ConfigUpdater()
    updater.read_string(test24_cfg_in)
    # [1:] to drop 'DEFAULT' section
    assert list(parser)[1:] == list(updater)
    assert list(parser["sec1"]) == list(updater["sec1"])
예제 #18
0
def test_no_duplicate_blocks_with_blockbuilder():
    updater = ConfigUpdater()
    updater.read_string(test19_cfg_in)
    with pytest.raises(DuplicateOptionError):
        updater['section']['Key0'].add_after.option('key0', '1')
    with pytest.raises(DuplicateSectionError):
        updater['section'].add_after.section('section')
    assert str(updater) == test19_cfg_in
예제 #19
0
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
예제 #20
0
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']
    with pytest.raises(ValueError):
        updater['section1']._get_option_idx('wrong key')
예제 #21
0
파일: config.py 프로젝트: a8/AWattPrice
def bootstrap_config(path: Optional[Path] = None) -> ConfigUpdater:
    """Create the Config file and populate it."""
    if path is None:
        path = Path(os.path.expanduser("~")) / ".config" / "awattprice" / "config.ini"
    if not path.parent.is_dir():
        os.makedirs(path.parent.as_posix())
    config_updater = ConfigUpdater()
    config_updater.read_string(DEFAULT_CONFIG)
    write_config_updater(path, config_updater)
    return config_updater
예제 #22
0
def test_add_section():
    updater = ConfigUpdater()
    updater.read_string(test7_cfg_in)
    with pytest.raises(DuplicateSectionError):
        updater.add_section('section1')
    updater.add_section('section2')
    updater['section2']['key1'] = 1
    assert str(updater) == test7_cfg_out
    with pytest.raises(ValueError):
        updater.add_section(updater['section2']['key1'])
예제 #23
0
def test_as_list_comma_separated():
    cfg = """\
    [metadata]
    classifiers = terminal, developers
    """
    cfg = dedent(cfg)
    updater = ConfigUpdater()
    updater.read_string(cfg)
    option = updater["metadata"]["classifiers"]
    assert option.as_list(",") == ["terminal", "developers"]
예제 #24
0
def test_value_change():
    updater = ConfigUpdater()
    updater.read_string(test1_cfg_in)
    assert updater['default']['key'].value == '1'
    updater['default']['key'].value = '2'
    assert str(updater) == test1_cfg_out
    # try another way
    section = updater['default']
    section['key'] = '1'
    assert str(updater) == test1_cfg_in
def test_value_change():
    updater = ConfigUpdater()
    updater.read_string(test1_cfg_in)
    assert updater["default"]["key"].value == "1"
    updater["default"]["key"].value = "2"
    assert str(updater) == test1_cfg_out
    # try another way
    section = updater["default"]
    section["key"] = "1"
    assert str(updater) == test1_cfg_in
def test_add_section():
    updater = ConfigUpdater()
    updater.read_string(test7_cfg_in)
    with pytest.raises(DuplicateSectionError):
        updater.add_section("section1")
    updater.add_section("section2")
    updater["section2"]["key1"] = 1
    assert str(updater) == test7_cfg_out
    with pytest.raises(ValueError):
        updater.add_section(updater["section2"]["key1"])
예제 #27
0
def test_remove_option():
    updater = ConfigUpdater()
    updater.read_string(test1_cfg_in)
    updater.remove_option('default', 'key')
    assert str(updater) == test1_cfg_out_removed
    updater.remove_option('default', 'non_existent_key')
    updater.read_string(test1_cfg_in)
    del updater['default']['key']
    assert str(updater) == test1_cfg_out_removed
    with pytest.raises(NoSectionError):
        updater.remove_option('wrong_section', 'key')
def test_remove_option():
    updater = ConfigUpdater()
    updater.read_string(test1_cfg_in)
    updater.remove_option("default", "key")
    assert str(updater) == test1_cfg_out_removed
    updater.remove_option("default", "non_existent_key")
    updater.read_string(test1_cfg_in)
    del updater["default"]["key"]
    assert str(updater) == test1_cfg_out_removed
    with pytest.raises(NoSectionError):
        updater.remove_option("wrong_section", "key")
def test_entry_point(tmpfolder):
    args = ["--no-config", "--custom-extension", "pyscaffoldext-some_extension"]
    # --no-config: avoid extra config from dev's machine interference
    cli.main(args)
    assert Path("pyscaffoldext-some_extension/setup.cfg").exists()

    setup_cfg = ConfigUpdater()
    setup_cfg.read_string(Path("pyscaffoldext-some_extension/setup.cfg").read_text())
    entry_point = setup_cfg.get("options.entry_points", "pyscaffold.cli").value
    expected = "\nsome_extension = pyscaffoldext.some_extension.extension:SomeExtension"
    assert entry_point == expected
예제 #30
0
def test_custom():
    config = """\
    [section1]
    k_e_y = value
    """

    custom = ConfigUpdater()
    custom.optionxform = lambda option: option.replace("_", "")
    custom.read_string(dedent(config))
    assert list(custom["section1"].keys()) == ["key"]
    assert custom["section1"]["key"].value == "value"
    assert custom["section1"]["key"].raw_key == "k_e_y"