Skip to content

Commit af607ff

Browse files
wimglennhukkin
andauthored
Add indent control knob (#49)
Co-authored-by: Taneli Hukkinen <[email protected]>
1 parent 36354bd commit af607ff

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/tomli_w/_writer.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
ILLEGAL_BASIC_STR_CHARS = frozenset('"\\') | ASCII_CTRL - frozenset("\t")
1212
BARE_KEY_CHARS = frozenset(string.ascii_letters + string.digits + "-_")
1313
ARRAY_TYPES = (list, tuple)
14-
ARRAY_INDENT = " " * 4
1514
MAX_LINE_LENGTH = 100
1615

1716
COMPACT_ESCAPES = MappingProxyType(
@@ -27,22 +26,29 @@
2726

2827

2928
def dump(
30-
__obj: Mapping[str, Any], __fp: IO[bytes], *, multiline_strings: bool = False
29+
__obj: Mapping[str, Any],
30+
__fp: IO[bytes],
31+
*,
32+
multiline_strings: bool = False,
33+
indent: int = 4,
3134
) -> None:
32-
ctx = Context(multiline_strings, {})
35+
ctx = Context(multiline_strings, {}, " " * indent)
3336
for chunk in gen_table_chunks(__obj, ctx, name=""):
3437
__fp.write(chunk.encode())
3538

3639

37-
def dumps(__obj: Mapping[str, Any], *, multiline_strings: bool = False) -> str:
38-
ctx = Context(multiline_strings, {})
40+
def dumps(
41+
__obj: Mapping[str, Any], *, multiline_strings: bool = False, indent: int = 4
42+
) -> str:
43+
ctx = Context(multiline_strings, {}, " " * indent)
3944
return "".join(gen_table_chunks(__obj, ctx, name=""))
4045

4146

4247
class Context(NamedTuple):
4348
allow_multiline: bool
4449
# cache rendered inline tables (mapping from object id to rendered inline table)
4550
inline_table_cache: dict[int, str]
51+
indent_str: str
4652

4753

4854
def gen_table_chunks(
@@ -136,8 +142,8 @@ def format_inline_table(obj: Mapping, ctx: Context) -> str:
136142
def format_inline_array(obj: tuple | list, ctx: Context, nest_level: int) -> str:
137143
if not obj:
138144
return "[]"
139-
item_indent = ARRAY_INDENT * (1 + nest_level)
140-
closing_bracket_indent = ARRAY_INDENT * nest_level
145+
item_indent = ctx.indent_str * (1 + nest_level)
146+
closing_bracket_indent = ctx.indent_str * nest_level
141147
return (
142148
"[\n"
143149
+ ",\n".join(
@@ -197,5 +203,5 @@ def is_aot(obj: Any) -> bool:
197203
def is_suitable_inline_table(obj: Mapping, ctx: Context) -> bool:
198204
"""Use heuristics to decide if the inline-style representation is a good
199205
choice for a given table."""
200-
rendered_inline = f"{ARRAY_INDENT}{format_inline_table(obj, ctx)},"
206+
rendered_inline = f"{ctx.indent_str}{format_inline_table(obj, ctx)},"
201207
return len(rendered_inline) <= MAX_LINE_LENGTH and "\n" not in rendered_inline

tests/test_style.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,16 @@ def test_multiline_in_aot():
272272
]
273273
"""
274274
)
275+
276+
277+
def test_array_indent_override():
278+
data = {"k0": ["v1", "v2"]}
279+
assert (
280+
tomli_w.dumps(data, indent=2)
281+
== """\
282+
k0 = [
283+
"v1",
284+
"v2",
285+
]
286+
"""
287+
)

0 commit comments

Comments
 (0)