Пример #1
0
def test_comment_preserve_decoder_encoder():
    test_str = """[[products]]
name = "Nail"
sku = 284758393
# This is a comment
color = "gray" # Hello World
# name = { first = 'Tom', last = 'Preston-Werner' }
# arr7 = [
#  1, 2, 3
# ]
# lines  = '''
# The first newline is
# trimmed in raw strings.
#   All other whitespace
#   is preserved.
# '''

[animals]
color = "gray" # col
fruits = "apple" # a = [1,2,3]
a = 3
b-comment = "a is 3"
"""

    s = toml.dumps(toml.loads(test_str,
                              decoder=toml.TomlPreserveCommentDecoder()),
                   encoder=toml.TomlPreserveCommentEncoder())

    assert len(s) == len(test_str) and sorted(test_str) == sorted(s)
Пример #2
0
    def _write_dump(self, data: Any, delete: bool = False) -> None:
        """
        Helper function to write the dump into file.

        :param data: The modified data
        :type data: :class:``hammurabi.rules.mixins.Any``

        :param delete: Indicate if the key should be deleted
        :type delete: bool
        """

        # TOML file cannot handle None as value, hence we need to set
        # something for that field if the user forgot to fill the value.

        self.param.write_text(
            toml.dumps(  # type: ignore
                self.set_by_selector(self.loaded_data, self.split_key, data, delete),
                encoder=toml.TomlPreserveCommentEncoder(),  # type: ignore
            )
        )
Пример #3
0
def format_toml(unformatted: str, _info_str: str) -> str:
    parsed = toml.loads(unformatted, decoder=toml.TomlPreserveCommentDecoder())
    return toml.dumps(parsed, encoder=toml.TomlPreserveCommentEncoder())
Пример #4
0
def dump_toml(path, data):
    with open(path, "w", encoding="utf-8") as fobj:
        toml.dump(data, fobj, encoder=toml.TomlPreserveCommentEncoder())
Пример #5
0
def _dump(data, stream):
    import toml

    return toml.dump(data, stream, encoder=toml.TomlPreserveCommentEncoder())