How to use tomlkit - 10 common examples

To help you get started, we’ve selected a few tomlkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sdispater / tomlkit / tests / test_toml_document.py View on Github external
def test_toml_document_set_super_table_element():
    content = """[site.user]
name = "John"
"""

    doc = parse(content)
    doc["site"]["user"] = "Tom"

    assert (
        doc.as_string()
        == """[site]
user = "Tom"
github wemake-services / wemake-python-package / tests / test_project_generation.py View on Github external
def test_pyproject_toml(cookies, context):
    """Ensures that all variables are replaced inside project files."""
    baked_project = cookies.bake(extra_context=context)
    path = os.path.join(str(baked_project.project), 'pyproject.toml')

    with open(path) as pyproject:
        poetry = tomlkit.parse(pyproject.read())['tool']['poetry']

    assert poetry['name'] == context['project_name']
    assert poetry['description'] == context['project_description']
    assert poetry['repository'] == 'https://github.com/{0}/{1}'.format(
        context['organization'],
        context['project_name'],
    )
github dephell / dephell / tests / test_converters / test_poetrylock.py View on Github external
def test_dump(requirements_path):
    converter = PoetryLockConverter()
    resolver = converter.load_resolver(requirements_path / 'poetry.lock.toml')
    reqs = Requirement.from_graph(graph=resolver.graph, lock=False)
    assert len(reqs) > 2
    content = converter.dumps(reqs=reqs, project=resolver.graph.metainfo)
    assert 'name = "enum34"' in content
    assert 'Python 3.4 Enum backported' in content

    parsed = tomlkit.parse(content)['package']
    parsed = {dep['name']: dep for dep in parsed}
github dephell / dephell / tests / test_converters / test_poetry.py View on Github external
content = dedent("""
        [tool.poetry]
        name = "test"
        version = "1.2.3"

        [tool.poetry.dependencies]
        python = "*"

        [[tool.poetry.source]]
        name = "pypi"
        url = "https://pypi.org/pypi"
    """)
    converter = PoetryConverter()
    root = converter.loads(content)
    new_content = converter.dumps(reqs=[], project=root)
    parsed = tomlkit.parse(content)['tool']['poetry']
    new_parsed = tomlkit.parse(new_content)['tool']['poetry']
    assert parsed['source'] == new_parsed['source']
    assert parsed == new_parsed
github sdispater / tomlkit / tests / test_build.py View on Github external
clients.add(nl())
    clients.add(comment("Line breaks are OK when inside arrays"))
    clients["hosts"] = array(
        """[
  "alpha",
  "omega"
]"""
    )

    doc.add(nl())
    doc.add(comment("Products"))

    products = aot()
    doc["products"] = products

    hammer = table().indent(2)
    hammer["name"] = "Hammer"
    hammer["sku"] = 738594937

    nail = table().indent(2)
    nail["name"] = "Nail"
    nail["sku"] = 284758393
    nail["color"] = "gray"

    products.append(hammer)
    products.append(nail)

    assert content == doc.as_string()
github dry-python / dependencies / tests / test_configuration.py View on Github external
def test_poetry_deps_not_pinned():
    """Dependencies of pyproject.toml files should not have versions."""
    for pyproject_toml in ["pyproject.toml", "tests/helpers/pyproject.toml"]:
        pyproject_toml = tomlkit.loads(open(pyproject_toml).read())
        versions = [
            d.get("version")
            for d in pyproject_toml["tool"]["poetry"].get("dependencies", {}).values()
            if isinstance(d, dict)
        ]
        assert all(v == "*" for v in versions)
github dry-python / stories / tests / test_configuration.py View on Github external
def test_packages_are_ordered():
    """Packages of pyproject.toml files should be in order."""
    for pyproject_toml in ["pyproject.toml", "tests/helpers/pyproject.toml"]:
        pyproject_toml = tomlkit.loads(open(pyproject_toml).read())
        packages = [
            re.sub(r"\.py$", "", p["include"])
            for p in pyproject_toml["tool"]["poetry"]["packages"]
        ]
        assert packages == sorted(packages)
github sdispater / tomlkit / tests / test_items.py View on Github external
assert isinstance(table, Table)
    assert "" == table.as_string()

    table.append(Key("foo"), String(StringType.SLB, "bar", "bar", Trivia(trail="\n")))

    assert 'foo = "bar"\n' == table.as_string()

    table.append(
        Key("baz"),
        Integer(34, Trivia(comment_ws="   ", comment="# Integer", trail=""), "34"),
    )

    assert 'foo = "bar"\nbaz = 34   # Integer' == table.as_string()

    table.remove(Key("baz"))

    assert 'foo = "bar"\n' == table.as_string()

    table.remove(Key("foo"))

    assert "" == table.as_string()

    with pytest.raises(NonExistentKey):
        table.remove(Key("foo"))
github sdispater / tomlkit / tests / test_items.py View on Github external
def test_items_can_be_appended_to_and_removed_from_an_inline_table():
    string = """table = {}
"""

    parser = Parser(string)
    _, table = parser._parse_item()

    assert isinstance(table, InlineTable)
    assert "{}" == table.as_string()

    table.append(Key("foo"), String(StringType.SLB, "bar", "bar", Trivia(trail="")))

    assert '{foo = "bar"}' == table.as_string()

    table.append(Key("baz"), Integer(34, Trivia(trail=""), "34"))

    assert '{foo = "bar", baz = 34}' == table.as_string()

    table.remove(Key("baz"))

    assert '{foo = "bar"}' == table.as_string()

    table.remove(Key("foo"))

    assert "{}" == table.as_string()

    with pytest.raises(NonExistentKey):
        table.remove(Key("foo"))
github sdispater / tomlkit / tests / test_items.py View on Github external
def test_dicts_are_converted_to_tables():
    t = item({"foo": {"bar": "baz"}})

    assert (
        t.as_string()
        == """[foo]
bar = "baz"