How to use the tomlkit.items.Key function in tomlkit

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_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 / tomlkit / container.py View on Github external
def append(self, key, item):  # type: (Union[Key, str, None], Item) -> Container
        if not isinstance(key, Key) and key is not None:
            key = Key(key)

        if not isinstance(item, Item):
            item = _item(item)

        if isinstance(item, (AoT, Table)) and item.name is None:
            item.name = key.key

        if (
            isinstance(item, Table)
            and self._body
            and not self._parsed
            and not item.trivia.indent
        ):
            item.trivia.indent = "\n"

        if isinstance(item, AoT) and self._body and not self._parsed:
github sdispater / tomlkit / tomlkit / parser.py View on Github external
if c != t.value:
                        raise self.parse_error()

                    in_name = False
                else:
                    in_name = True
                    t = KeyType.Literal if c == "'" else KeyType.Basic

                continue
            elif in_name or c.is_bare_key_char():
                current += c
            else:
                raise self.parse_error()

        if current:
            yield Key(current, t=t, sep="")
github sdispater / tomlkit / tomlkit / container.py View on Github external
def _replace(
        self, key, new_key, value
    ):  # type: (Union[Key, str], Union[Key, str], Item) -> None
        if not isinstance(key, Key):
            key = Key(key)

        if not isinstance(new_key, Key):
            new_key = Key(new_key)

        idx = self._map.get(key, None)
        if idx is None:
            raise NonExistentKey(key)

        self._replace_at(idx, new_key, value)
github sdispater / tomlkit / tomlkit / container.py View on Github external
def item(self, key):  # type: (Union[Key, str]) -> Item
        if not isinstance(key, Key):
            key = Key(key)

        idx = self._map.get(key, None)
        if idx is None:
            raise NonExistentKey(key)

        return self._body[idx][1]
github sdispater / tomlkit / tomlkit / parser.py View on Github external
key_type = None
        dotted = False

        self.mark()
        while self._current.is_bare_key_char() and self.inc():
            pass

        key = self.extract()

        if self._current == ".":
            self.inc()
            dotted = True
            key += "." + self._parse_key().as_string()
            key_type = KeyType.Bare

        return Key(key, key_type, "", dotted)
github sdispater / tomlkit / tomlkit / container.py View on Github external
def _insert_at(
        self, idx, key, item
    ):  # type: (int, Union[str, Key], Union[Item, Any]) -> Container
        if idx > len(self._body) - 1:
            raise ValueError("Unable to insert at position {}".format(idx))

        if not isinstance(key, Key):
            key = Key(key)

        item = _item(item)

        if idx > 0:
            previous_item = self._body[idx - 1][1]
            if (
                not isinstance(previous_item, Whitespace)
                and not isinstance(item, (AoT, Table))
                and "\n" not in previous_item.trivia.trail
            ):
                previous_item.trivia.trail += "\n"

        # Increment indices after the current index
        for k, v in self._map.items():
            if isinstance(v, tuple):
                new_indices = []