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)
def parse_toml_for_update(text, path): """Parses text into Python structure. NOTE: Python toml package does not currently use ordered dicts, so keys may be re-ordered between load/dump, but this function will at least preserve comments. """ decoder = toml.TomlPreserveCommentDecoder() return parse_toml(text, path, decoder=decoder)
def parse_toml_for_update(text, path): """Parses text into Python structure. NOTE: Python toml package does not currently use ordered dicts, so keys may be re-ordered between load/dump, but this function will at least preserve comments. """ try: return toml.loads(text, decoder=toml.TomlPreserveCommentDecoder()) except toml.TomlDecodeError as exc: raise TOMLFileCorruptedError(path) from exc
def format_toml(unformatted: str, _info_str: str) -> str: parsed = toml.loads(unformatted, decoder=toml.TomlPreserveCommentDecoder()) return toml.dumps(parsed, encoder=toml.TomlPreserveCommentEncoder())
def __loader(toml_str: str) -> MutableMapping[str, Any]: return toml.loads( # type: ignore toml_str, decoder=toml.TomlPreserveCommentDecoder() # type: ignore )