yaml-sort 0.2.1__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {yaml_sort-0.2.1/src/yaml_sort.egg-info → yaml_sort-0.2.2}/PKG-INFO +6 -1
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/README.md +5 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/pyproject.toml +1 -1
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/src/yaml_sort/core.py +89 -10
- {yaml_sort-0.2.1 → yaml_sort-0.2.2/src/yaml_sort.egg-info}/PKG-INFO +6 -1
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/tests/test_yaml_sort.py +31 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/LICENSE +0 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/setup.cfg +0 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/src/yaml_sort/__init__.py +0 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/src/yaml_sort/cli.py +0 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/src/yaml_sort.egg-info/SOURCES.txt +0 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/src/yaml_sort.egg-info/dependency_links.txt +0 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/src/yaml_sort.egg-info/entry_points.txt +0 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/src/yaml_sort.egg-info/top_level.txt +0 -0
- {yaml_sort-0.2.1 → yaml_sort-0.2.2}/tests/test_packaging.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: yaml-sort
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Deterministic YAML key sorter — sorts mapping keys alphabetically while preserving comments and byte-for-byte formatting.
|
|
5
5
|
Author: Soren L. Hansen
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -34,6 +34,11 @@ re-emits it verbatim, moving only what it needs to (mapping keys).
|
|
|
34
34
|
- Sorts mapping keys alphabetically, bottom-up — including inside list items
|
|
35
35
|
and nested maps.
|
|
36
36
|
- Never reorders list items (sequence order is meaningful).
|
|
37
|
+
- Literal (`|`) and folded (`>`) block-scalar bodies are opaque: everything
|
|
38
|
+
indented deeper than the key is kept verbatim, including blank lines and lines
|
|
39
|
+
that start with `#` or look like mapping keys. (Without this, a `prompt: |`
|
|
40
|
+
body containing such a line would be parsed as structure and the rest of the
|
|
41
|
+
document silently dropped.)
|
|
37
42
|
- A comment that is the *last* thing in a file (a footer) stays pinned at the
|
|
38
43
|
very bottom — it is not dragged into the middle when the neighbouring key
|
|
39
44
|
re-sorts.
|
|
@@ -18,6 +18,11 @@ re-emits it verbatim, moving only what it needs to (mapping keys).
|
|
|
18
18
|
- Sorts mapping keys alphabetically, bottom-up — including inside list items
|
|
19
19
|
and nested maps.
|
|
20
20
|
- Never reorders list items (sequence order is meaningful).
|
|
21
|
+
- Literal (`|`) and folded (`>`) block-scalar bodies are opaque: everything
|
|
22
|
+
indented deeper than the key is kept verbatim, including blank lines and lines
|
|
23
|
+
that start with `#` or look like mapping keys. (Without this, a `prompt: |`
|
|
24
|
+
body containing such a line would be parsed as structure and the rest of the
|
|
25
|
+
document silently dropped.)
|
|
21
26
|
- A comment that is the *last* thing in a file (a footer) stays pinned at the
|
|
22
27
|
very bottom — it is not dragged into the middle when the neighbouring key
|
|
23
28
|
re-sorts.
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "yaml-sort"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.2"
|
|
8
8
|
description = "Deterministic YAML key sorter — sorts mapping keys alphabetically while preserving comments and byte-for-byte formatting."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
@@ -19,6 +19,9 @@ Notable behaviour:
|
|
|
19
19
|
items and nested maps.
|
|
20
20
|
* List items are *ordered* — a sequence's order is meaningful and is never
|
|
21
21
|
changed.
|
|
22
|
+
* Literal (``|``) and folded (``>``) block-scalar bodies are opaque: every line
|
|
23
|
+
indented deeper than the key is preserved verbatim, including blank lines and
|
|
24
|
+
lines that begin with ``#`` or look like mapping keys.
|
|
22
25
|
* For a list item that opens a mapping (e.g. ``- api_key: none`` followed by
|
|
23
26
|
``base_url:``…), the "anchor" key fused to the ``- `` is sorted along with
|
|
24
27
|
its siblings, and the ``- `` re-fuses to whichever key sorts first, keeping
|
|
@@ -52,6 +55,25 @@ def _is_gap(line: str) -> bool:
|
|
|
52
55
|
|
|
53
56
|
_KEY_RE = re.compile(r"^(?:['\"]([^'\"]+)['\"]|([^#:\s][^:]*?))\s*:")
|
|
54
57
|
|
|
58
|
+
# `|`, `|-`, `|+`, `|2`, `|2-`, `>`, `>-`, `>2+`, optionally followed by a
|
|
59
|
+
# comment (`| # keep this`).
|
|
60
|
+
_BLOCK_SCALAR_RE = re.compile(r"^[|>](?:[1-9][+-]?|[+-][1-9]?)?\s*(?:#.*)?$")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _is_block_scalar_header(line: str) -> bool:
|
|
64
|
+
"""True if *line* opens a literal (``|``) or folded (``>``) block scalar.
|
|
65
|
+
|
|
66
|
+
Either ``key: |`` or a sequence item ``- |``.
|
|
67
|
+
"""
|
|
68
|
+
h = line.lstrip()
|
|
69
|
+
if h.startswith("- "):
|
|
70
|
+
h = h[2:].lstrip()
|
|
71
|
+
return bool(_BLOCK_SCALAR_RE.match(h))
|
|
72
|
+
m = _KEY_RE.match(h)
|
|
73
|
+
if not m:
|
|
74
|
+
return False
|
|
75
|
+
return bool(_BLOCK_SCALAR_RE.match(h[m.end():].strip()))
|
|
76
|
+
|
|
55
77
|
|
|
56
78
|
def _is_key_line(line: str) -> bool:
|
|
57
79
|
"""True if *line* is a mapping key line (not a list item, comment, etc.)."""
|
|
@@ -153,6 +175,8 @@ class KeyBlock:
|
|
|
153
175
|
self.value = None # Container | None
|
|
154
176
|
self.dash_anchor = False # True if emitted with a `- ` prefix
|
|
155
177
|
self.anchor_indent = 0 # indent of the enclosing seq when anchor
|
|
178
|
+
self.block_scalar = False # cont holds RAW lines of a |/> body
|
|
179
|
+
self.block_indent = 0 # indent of the header line when block_scalar
|
|
156
180
|
|
|
157
181
|
|
|
158
182
|
class ItemBlock:
|
|
@@ -160,6 +184,8 @@ class ItemBlock:
|
|
|
160
184
|
self.header = header # header line text minus leading indent
|
|
161
185
|
self.cont = cont or [] # folded plain-scalar continuation lines
|
|
162
186
|
self.value = None # Container | None
|
|
187
|
+
self.block_scalar = False # cont holds RAW lines of a |/> body
|
|
188
|
+
self.block_indent = 0 # indent of the header line when block_scalar
|
|
163
189
|
|
|
164
190
|
|
|
165
191
|
# ── parser ───────────────────────────────────────────────────────────────────
|
|
@@ -230,6 +256,32 @@ def _gather_cont(lines, idx: int, indent: int, quoted: bool = False):
|
|
|
230
256
|
return cont, i
|
|
231
257
|
|
|
232
258
|
|
|
259
|
+
def _gather_block_scalar(lines, idx: int, indent: int):
|
|
260
|
+
"""Consume a literal/folded block-scalar body, verbatim.
|
|
261
|
+
|
|
262
|
+
Every line indented deeper than *indent* belongs to the scalar — including
|
|
263
|
+
blank lines and lines beginning with ``#``, which are content here, not
|
|
264
|
+
gaps. Trailing blank lines are only kept when another body line follows
|
|
265
|
+
them, so a blank separator before the next key stays a gap.
|
|
266
|
+
|
|
267
|
+
Returns (raw_body_lines, next_idx). The body lines are kept in full
|
|
268
|
+
(indent included); emitters re-offset them relative to the header.
|
|
269
|
+
"""
|
|
270
|
+
body: list[str] = []
|
|
271
|
+
i = idx
|
|
272
|
+
while i < len(lines):
|
|
273
|
+
j = i
|
|
274
|
+
while j < len(lines) and not lines[j].strip():
|
|
275
|
+
j += 1
|
|
276
|
+
if j < len(lines) and _indent(lines[j]) > indent:
|
|
277
|
+
body.extend(lines[i:j])
|
|
278
|
+
body.append(lines[j])
|
|
279
|
+
i = j + 1
|
|
280
|
+
else:
|
|
281
|
+
break
|
|
282
|
+
return body, i
|
|
283
|
+
|
|
284
|
+
|
|
233
285
|
def parse_block(lines, idx: int, indent: int, kind: str):
|
|
234
286
|
"""Parse a single mapping key or sequence item at *indent*.
|
|
235
287
|
|
|
@@ -250,6 +302,16 @@ def parse_block(lines, idx: int, indent: int, kind: str):
|
|
|
250
302
|
has_inline = item_key[1] if item_key else False
|
|
251
303
|
quoted = _value_quoted(raw)
|
|
252
304
|
|
|
305
|
+
if _is_block_scalar_header(raw):
|
|
306
|
+
# A `|`/`>` body is opaque: consume it verbatim so that blank lines,
|
|
307
|
+
# comments and key-shaped lines inside it are not parsed as structure.
|
|
308
|
+
body, i = _gather_block_scalar(lines, i, indent)
|
|
309
|
+
blk = KeyBlock(name or "", header) if kind == "map" else ItemBlock(header)
|
|
310
|
+
blk.block_scalar = True
|
|
311
|
+
blk.block_indent = indent
|
|
312
|
+
blk.cont = body
|
|
313
|
+
return blk, i
|
|
314
|
+
|
|
253
315
|
cont: list[str] = []
|
|
254
316
|
value = None
|
|
255
317
|
anchor_block = None
|
|
@@ -391,6 +453,16 @@ def _eol(s: str) -> str:
|
|
|
391
453
|
return s if s.endswith("\n") else s + "\n"
|
|
392
454
|
|
|
393
455
|
|
|
456
|
+
def _shift_line(raw: str, delta: int) -> str:
|
|
457
|
+
"""Re-offset a raw block-scalar body line by *delta* columns."""
|
|
458
|
+
if delta == 0 or not raw.strip():
|
|
459
|
+
return _eol(raw)
|
|
460
|
+
if delta > 0:
|
|
461
|
+
return _eol(" " * delta + raw)
|
|
462
|
+
strip = min(-delta, len(raw) - len(raw.lstrip(" ")))
|
|
463
|
+
return _eol(raw[strip:])
|
|
464
|
+
|
|
465
|
+
|
|
394
466
|
# ── emitter ──────────────────────────────────────────────────────────────────
|
|
395
467
|
|
|
396
468
|
def _emit_keep(node: Container, out: list[str]) -> None:
|
|
@@ -403,17 +475,20 @@ def _emit_keep(node: Container, out: list[str]) -> None:
|
|
|
403
475
|
_emit_keep(b, out)
|
|
404
476
|
continue
|
|
405
477
|
if node.dash_first and first:
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
out.append(_eol(" " * node.seq_indent + c))
|
|
478
|
+
key_indent = node.seq_indent
|
|
479
|
+
out.append(_eol(" " * key_indent + "- " + b.header))
|
|
409
480
|
elif isinstance(b, KeyBlock) and b.dash_anchor:
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
481
|
+
key_indent = b.anchor_indent
|
|
482
|
+
out.append(_eol(" " * key_indent + "- " + b.header))
|
|
483
|
+
else:
|
|
484
|
+
key_indent = node.indent
|
|
485
|
+
out.append(_eol(" " * key_indent + b.header))
|
|
486
|
+
if b.block_scalar:
|
|
487
|
+
delta = key_indent - b.block_indent
|
|
488
|
+
out.extend(_shift_line(line, delta) for line in b.cont)
|
|
413
489
|
else:
|
|
414
|
-
out.append(_eol(" " * node.indent + b.header))
|
|
415
490
|
for c in b.cont:
|
|
416
|
-
out.append(_eol(" " *
|
|
491
|
+
out.append(_eol(" " * key_indent + c))
|
|
417
492
|
first = False
|
|
418
493
|
if isinstance(b.value, Container):
|
|
419
494
|
_emit_keep(b.value, out)
|
|
@@ -445,8 +520,12 @@ def _emit_reindent(node: Container, indent: int, list_indent: int, out: list[str
|
|
|
445
520
|
key_indent = indent + (2 if node.dash_first else 0)
|
|
446
521
|
prefix = ""
|
|
447
522
|
out.append(_eol(" " * key_indent + prefix + b.header))
|
|
448
|
-
|
|
449
|
-
|
|
523
|
+
if b.block_scalar:
|
|
524
|
+
delta = key_indent - b.block_indent
|
|
525
|
+
out.extend(_shift_line(line, delta) for line in b.cont)
|
|
526
|
+
else:
|
|
527
|
+
for c in b.cont:
|
|
528
|
+
out.append(_eol(" " * key_indent + c))
|
|
450
529
|
first = False
|
|
451
530
|
if isinstance(b.value, Container):
|
|
452
531
|
child = key_indent + (2 if b.value.kind == "map" else list_indent)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: yaml-sort
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Deterministic YAML key sorter — sorts mapping keys alphabetically while preserving comments and byte-for-byte formatting.
|
|
5
5
|
Author: Soren L. Hansen
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -34,6 +34,11 @@ re-emits it verbatim, moving only what it needs to (mapping keys).
|
|
|
34
34
|
- Sorts mapping keys alphabetically, bottom-up — including inside list items
|
|
35
35
|
and nested maps.
|
|
36
36
|
- Never reorders list items (sequence order is meaningful).
|
|
37
|
+
- Literal (`|`) and folded (`>`) block-scalar bodies are opaque: everything
|
|
38
|
+
indented deeper than the key is kept verbatim, including blank lines and lines
|
|
39
|
+
that start with `#` or look like mapping keys. (Without this, a `prompt: |`
|
|
40
|
+
body containing such a line would be parsed as structure and the rest of the
|
|
41
|
+
document silently dropped.)
|
|
37
42
|
- A comment that is the *last* thing in a file (a footer) stays pinned at the
|
|
38
43
|
very bottom — it is not dragged into the middle when the neighbouring key
|
|
39
44
|
re-sorts.
|
|
@@ -66,6 +66,12 @@ CASES = [
|
|
|
66
66
|
("folded_plain_scalar", None),
|
|
67
67
|
("quoted_scalar_with_colon", None),
|
|
68
68
|
("quoted_empty_scalar", None),
|
|
69
|
+
# block scalars — bodies are opaque: blank lines, '#' and key-shaped
|
|
70
|
+
# lines inside them are content, and must survive sorting (and the rest
|
|
71
|
+
# of the document must not be dropped)
|
|
72
|
+
("block_scalar_literal", None),
|
|
73
|
+
("block_scalar_folded", None),
|
|
74
|
+
("block_scalar_list_item", None),
|
|
69
75
|
("comment_only", None),
|
|
70
76
|
("trailing_newline", None),
|
|
71
77
|
("unicode", None),
|
|
@@ -94,6 +100,31 @@ def test_case_is_idempotent(name, list_indent):
|
|
|
94
100
|
assert sort_text(once, **kwargs) == once
|
|
95
101
|
|
|
96
102
|
|
|
103
|
+
def _norm(line: str) -> str:
|
|
104
|
+
"""Strip indent and a `- ` item prefix.
|
|
105
|
+
|
|
106
|
+
A list item's dash re-fuses to whichever key sorts first, so the dash moves
|
|
107
|
+
between lines; indentation of the fused line shifts by two columns with it.
|
|
108
|
+
Neither is a content change, so both are normalised away.
|
|
109
|
+
"""
|
|
110
|
+
body = line.strip()
|
|
111
|
+
return body[2:] if body.startswith("- ") else body
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@pytest.mark.parametrize("name,list_indent", [c for c in CASES if c[1] is None])
|
|
115
|
+
def test_sorting_preserves_every_line(name, list_indent):
|
|
116
|
+
"""Sorting may only reorder lines — never drop, merge, or invent one.
|
|
117
|
+
|
|
118
|
+
This is what a block-scalar body being parsed as structure violates: the
|
|
119
|
+
tail of the document silently disappears.
|
|
120
|
+
"""
|
|
121
|
+
src = _read(name, "in.yaml")
|
|
122
|
+
out = sort_text(src)
|
|
123
|
+
assert sorted(map(_norm, out.splitlines())) == sorted(
|
|
124
|
+
map(_norm, src.splitlines())
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
|
|
97
128
|
# ── fixture integrity ─────────────────────────────────────────────────────────
|
|
98
129
|
|
|
99
130
|
def test_fixture_pairing():
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|