minimindslab-toml-inline-table-sort 0.1.0__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.
- minimindslab_toml_inline_table_sort-0.1.0/LICENSE +21 -0
- minimindslab_toml_inline_table_sort-0.1.0/PKG-INFO +32 -0
- minimindslab_toml_inline_table_sort-0.1.0/README.md +21 -0
- minimindslab_toml_inline_table_sort-0.1.0/pyproject.toml +19 -0
- minimindslab_toml_inline_table_sort-0.1.0/setup.cfg +4 -0
- minimindslab_toml_inline_table_sort-0.1.0/src/minimindslab_toml_inline_table_sort/__init__.py +167 -0
- minimindslab_toml_inline_table_sort-0.1.0/src/minimindslab_toml_inline_table_sort.egg-info/PKG-INFO +32 -0
- minimindslab_toml_inline_table_sort-0.1.0/src/minimindslab_toml_inline_table_sort.egg-info/SOURCES.txt +9 -0
- minimindslab_toml_inline_table_sort-0.1.0/src/minimindslab_toml_inline_table_sort.egg-info/dependency_links.txt +1 -0
- minimindslab_toml_inline_table_sort-0.1.0/src/minimindslab_toml_inline_table_sort.egg-info/top_level.txt +1 -0
- minimindslab_toml_inline_table_sort-0.1.0/tests/test_sort_inline_tables.py +34 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MiniMindsLab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: minimindslab-toml-inline-table-sort
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Deterministically reorder keys inside TOML inline tables and inline array-of-tables fragments while preserving all scalar values and whitespace outside the targeted structures.
|
|
5
|
+
Author: MiniMindsLab
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# MiniMindsLab TOML Inline Table Sort
|
|
13
|
+
|
|
14
|
+
Deterministically reorder keys inside TOML inline tables while keeping document text outside those inline tables unchanged.
|
|
15
|
+
|
|
16
|
+
## What it does
|
|
17
|
+
|
|
18
|
+
- Sorts keys within TOML inline tables, e.g. `{b = 2, a = 1}` -> `{a = 1, b = 2}`.
|
|
19
|
+
- Recursively sorts nested inline tables.
|
|
20
|
+
- Handles inline array-of-tables fragments, e.g. `[{b=2,a=1}, {d=4,c=3}]`.
|
|
21
|
+
- Leaves non-targeted text and whitespace outside targeted inline structures unchanged.
|
|
22
|
+
- Raises `ValueError` for malformed inline-table syntax.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from minimindslab_toml_inline_table_sort import sort_toml_inline_tables
|
|
28
|
+
|
|
29
|
+
text = "a = {b = 2, a = 1}\n"
|
|
30
|
+
print(sort_toml_inline_tables(text))
|
|
31
|
+
# a = {a = 1, b = 2}
|
|
32
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MiniMindsLab TOML Inline Table Sort
|
|
2
|
+
|
|
3
|
+
Deterministically reorder keys inside TOML inline tables while keeping document text outside those inline tables unchanged.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
- Sorts keys within TOML inline tables, e.g. `{b = 2, a = 1}` -> `{a = 1, b = 2}`.
|
|
8
|
+
- Recursively sorts nested inline tables.
|
|
9
|
+
- Handles inline array-of-tables fragments, e.g. `[{b=2,a=1}, {d=4,c=3}]`.
|
|
10
|
+
- Leaves non-targeted text and whitespace outside targeted inline structures unchanged.
|
|
11
|
+
- Raises `ValueError` for malformed inline-table syntax.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from minimindslab_toml_inline_table_sort import sort_toml_inline_tables
|
|
17
|
+
|
|
18
|
+
text = "a = {b = 2, a = 1}\n"
|
|
19
|
+
print(sort_toml_inline_tables(text))
|
|
20
|
+
# a = {a = 1, b = 2}
|
|
21
|
+
```
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0.3"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "minimindslab-toml-inline-table-sort"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Deterministically reorder keys inside TOML inline tables and inline array-of-tables fragments while preserving all scalar values and whitespace outside the targeted structures."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "MiniMindsLab" }]
|
|
13
|
+
dependencies = []
|
|
14
|
+
|
|
15
|
+
[tool.setuptools]
|
|
16
|
+
package-dir = {"" = "src"}
|
|
17
|
+
|
|
18
|
+
[tool.setuptools.packages.find]
|
|
19
|
+
where = ["src"]
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"""MiniMindsLab TOML Inline Table Sort."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = ["sort_toml_inline_tables"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def sort_toml_inline_tables(text: str) -> str:
|
|
9
|
+
"""Return *text* with keys sorted in each TOML inline table.
|
|
10
|
+
|
|
11
|
+
The function preserves all bytes outside inline-table/inline-array structures.
|
|
12
|
+
Inside each inline table, keys are sorted ascending by key name.
|
|
13
|
+
"""
|
|
14
|
+
if not isinstance(text, str):
|
|
15
|
+
raise TypeError("text must be a str")
|
|
16
|
+
parser = _Parser(text)
|
|
17
|
+
return parser.transform()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class _Parser:
|
|
21
|
+
def __init__(self, s: str) -> None:
|
|
22
|
+
self.s = s
|
|
23
|
+
self.n = len(s)
|
|
24
|
+
|
|
25
|
+
def transform(self) -> str:
|
|
26
|
+
out: list[str] = []
|
|
27
|
+
i = 0
|
|
28
|
+
while i < self.n:
|
|
29
|
+
c = self.s[i]
|
|
30
|
+
if c in ('"', "'"):
|
|
31
|
+
j = self._read_string(i)
|
|
32
|
+
out.append(self.s[i:j])
|
|
33
|
+
i = j
|
|
34
|
+
continue
|
|
35
|
+
if c == "{":
|
|
36
|
+
table, j = self._parse_inline_table(i)
|
|
37
|
+
out.append(self._render_table(table))
|
|
38
|
+
i = j
|
|
39
|
+
continue
|
|
40
|
+
if c == "[":
|
|
41
|
+
arr, j = self._parse_array(i)
|
|
42
|
+
out.append(self._render_array(arr))
|
|
43
|
+
i = j
|
|
44
|
+
continue
|
|
45
|
+
out.append(c)
|
|
46
|
+
i += 1
|
|
47
|
+
return "".join(out)
|
|
48
|
+
|
|
49
|
+
def _skip_ws(self, i: int) -> int:
|
|
50
|
+
while i < self.n and self.s[i] in " \t\r\n":
|
|
51
|
+
i += 1
|
|
52
|
+
return i
|
|
53
|
+
|
|
54
|
+
def _read_string(self, i: int) -> int:
|
|
55
|
+
q = self.s[i]
|
|
56
|
+
i += 1
|
|
57
|
+
while i < self.n:
|
|
58
|
+
c = self.s[i]
|
|
59
|
+
if q == '"' and c == "\\":
|
|
60
|
+
i += 2
|
|
61
|
+
continue
|
|
62
|
+
if c == q:
|
|
63
|
+
return i + 1
|
|
64
|
+
i += 1
|
|
65
|
+
raise ValueError("Unterminated string")
|
|
66
|
+
|
|
67
|
+
def _parse_array(self, i: int):
|
|
68
|
+
assert self.s[i] == "["
|
|
69
|
+
i += 1
|
|
70
|
+
items = []
|
|
71
|
+
while True:
|
|
72
|
+
i = self._skip_ws(i)
|
|
73
|
+
if i >= self.n:
|
|
74
|
+
raise ValueError("Malformed array")
|
|
75
|
+
if self.s[i] == "]":
|
|
76
|
+
return items, i + 1
|
|
77
|
+
val, i = self._parse_value(i)
|
|
78
|
+
items.append(val)
|
|
79
|
+
i = self._skip_ws(i)
|
|
80
|
+
if i >= self.n:
|
|
81
|
+
raise ValueError("Malformed array")
|
|
82
|
+
if self.s[i] == ",":
|
|
83
|
+
i += 1
|
|
84
|
+
continue
|
|
85
|
+
if self.s[i] == "]":
|
|
86
|
+
return items, i + 1
|
|
87
|
+
raise ValueError("Malformed array")
|
|
88
|
+
|
|
89
|
+
def _parse_inline_table(self, i: int):
|
|
90
|
+
assert self.s[i] == "{"
|
|
91
|
+
i += 1
|
|
92
|
+
pairs = []
|
|
93
|
+
while True:
|
|
94
|
+
i = self._skip_ws(i)
|
|
95
|
+
if i >= self.n:
|
|
96
|
+
raise ValueError("Malformed inline table")
|
|
97
|
+
if self.s[i] == "}":
|
|
98
|
+
return pairs, i + 1
|
|
99
|
+
key, i = self._parse_key(i)
|
|
100
|
+
i = self._skip_ws(i)
|
|
101
|
+
if i >= self.n or self.s[i] != "=":
|
|
102
|
+
raise ValueError("Malformed inline table")
|
|
103
|
+
i += 1
|
|
104
|
+
i = self._skip_ws(i)
|
|
105
|
+
val, i = self._parse_value(i)
|
|
106
|
+
pairs.append((key, val))
|
|
107
|
+
i = self._skip_ws(i)
|
|
108
|
+
if i >= self.n:
|
|
109
|
+
raise ValueError("Malformed inline table")
|
|
110
|
+
if self.s[i] == ",":
|
|
111
|
+
i += 1
|
|
112
|
+
continue
|
|
113
|
+
if self.s[i] == "}":
|
|
114
|
+
return pairs, i + 1
|
|
115
|
+
raise ValueError("Malformed inline table")
|
|
116
|
+
|
|
117
|
+
def _parse_key(self, i: int):
|
|
118
|
+
if i >= self.n:
|
|
119
|
+
raise ValueError("Malformed key")
|
|
120
|
+
c = self.s[i]
|
|
121
|
+
if c in ('"', "'"):
|
|
122
|
+
j = self._read_string(i)
|
|
123
|
+
return self.s[i:j], j
|
|
124
|
+
start = i
|
|
125
|
+
while i < self.n and (self.s[i].isalnum() or self.s[i] in "_-"):
|
|
126
|
+
i += 1
|
|
127
|
+
if i == start:
|
|
128
|
+
raise ValueError("Malformed key")
|
|
129
|
+
return self.s[start:i], i
|
|
130
|
+
|
|
131
|
+
def _parse_value(self, i: int):
|
|
132
|
+
if i >= self.n:
|
|
133
|
+
raise ValueError("Malformed value")
|
|
134
|
+
c = self.s[i]
|
|
135
|
+
if c == "{":
|
|
136
|
+
return self._parse_inline_table(i)
|
|
137
|
+
if c == "[":
|
|
138
|
+
return self._parse_array(i)
|
|
139
|
+
if c in ('"', "'"):
|
|
140
|
+
j = self._read_string(i)
|
|
141
|
+
return self.s[i:j], j
|
|
142
|
+
start = i
|
|
143
|
+
while i < self.n and self.s[i] not in ",}]":
|
|
144
|
+
if self.s[i] in ('"', "'"):
|
|
145
|
+
j = self._read_string(i)
|
|
146
|
+
i = j
|
|
147
|
+
continue
|
|
148
|
+
i += 1
|
|
149
|
+
raw = self.s[start:i].strip()
|
|
150
|
+
if raw == "":
|
|
151
|
+
raise ValueError("Malformed value")
|
|
152
|
+
return raw, i
|
|
153
|
+
|
|
154
|
+
def _render_value(self, v):
|
|
155
|
+
if isinstance(v, list):
|
|
156
|
+
if v and isinstance(v[0], tuple):
|
|
157
|
+
return self._render_table(v)
|
|
158
|
+
return self._render_array(v)
|
|
159
|
+
return v
|
|
160
|
+
|
|
161
|
+
def _render_table(self, pairs):
|
|
162
|
+
sorted_pairs = sorted(pairs, key=lambda kv: kv[0])
|
|
163
|
+
inner = ", ".join(f"{k} = {self._render_value(v)}" for k, v in sorted_pairs)
|
|
164
|
+
return "{" + inner + "}"
|
|
165
|
+
|
|
166
|
+
def _render_array(self, items):
|
|
167
|
+
return "[" + ", ".join(self._render_value(v) for v in items) + "]"
|
minimindslab_toml_inline_table_sort-0.1.0/src/minimindslab_toml_inline_table_sort.egg-info/PKG-INFO
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: minimindslab-toml-inline-table-sort
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Deterministically reorder keys inside TOML inline tables and inline array-of-tables fragments while preserving all scalar values and whitespace outside the targeted structures.
|
|
5
|
+
Author: MiniMindsLab
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# MiniMindsLab TOML Inline Table Sort
|
|
13
|
+
|
|
14
|
+
Deterministically reorder keys inside TOML inline tables while keeping document text outside those inline tables unchanged.
|
|
15
|
+
|
|
16
|
+
## What it does
|
|
17
|
+
|
|
18
|
+
- Sorts keys within TOML inline tables, e.g. `{b = 2, a = 1}` -> `{a = 1, b = 2}`.
|
|
19
|
+
- Recursively sorts nested inline tables.
|
|
20
|
+
- Handles inline array-of-tables fragments, e.g. `[{b=2,a=1}, {d=4,c=3}]`.
|
|
21
|
+
- Leaves non-targeted text and whitespace outside targeted inline structures unchanged.
|
|
22
|
+
- Raises `ValueError` for malformed inline-table syntax.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from minimindslab_toml_inline_table_sort import sort_toml_inline_tables
|
|
28
|
+
|
|
29
|
+
text = "a = {b = 2, a = 1}\n"
|
|
30
|
+
print(sort_toml_inline_tables(text))
|
|
31
|
+
# a = {a = 1, b = 2}
|
|
32
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/minimindslab_toml_inline_table_sort/__init__.py
|
|
5
|
+
src/minimindslab_toml_inline_table_sort.egg-info/PKG-INFO
|
|
6
|
+
src/minimindslab_toml_inline_table_sort.egg-info/SOURCES.txt
|
|
7
|
+
src/minimindslab_toml_inline_table_sort.egg-info/dependency_links.txt
|
|
8
|
+
src/minimindslab_toml_inline_table_sort.egg-info/top_level.txt
|
|
9
|
+
tests/test_sort_inline_tables.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
minimindslab_toml_inline_table_sort
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
from minimindslab_toml_inline_table_sort import sort_toml_inline_tables
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class SortInlineTablesTests(unittest.TestCase):
|
|
7
|
+
def test_acceptance_basic_sort(self):
|
|
8
|
+
src = "a = {b = 2, a = 1}"
|
|
9
|
+
self.assertEqual(sort_toml_inline_tables(src), "a = {a = 1, b = 2}")
|
|
10
|
+
|
|
11
|
+
def test_acceptance_nested_only_inside(self):
|
|
12
|
+
src = "title = 'x'\na = {z = 0, n = {b = 2, a = 1}}\nend=true"
|
|
13
|
+
out = sort_toml_inline_tables(src)
|
|
14
|
+
self.assertEqual(out, "title = 'x'\na = {n = {a = 1, b = 2}, z = 0}\nend=true")
|
|
15
|
+
|
|
16
|
+
def test_acceptance_already_sorted_byte_identical(self):
|
|
17
|
+
src = "a = {a = 1, b = 2}\n"
|
|
18
|
+
self.assertEqual(sort_toml_inline_tables(src), src)
|
|
19
|
+
|
|
20
|
+
def test_acceptance_malformed_raises(self):
|
|
21
|
+
with self.assertRaises(ValueError):
|
|
22
|
+
sort_toml_inline_tables("a = {b = 2, a = }")
|
|
23
|
+
|
|
24
|
+
def test_inline_array_of_tables(self):
|
|
25
|
+
src = "x = [{b=2,a=1}, {d=4,c=3}]"
|
|
26
|
+
self.assertEqual(sort_toml_inline_tables(src), "x = [{a = 1, b = 2}, {c = 3, d = 4}]")
|
|
27
|
+
|
|
28
|
+
def test_negative_type_error(self):
|
|
29
|
+
with self.assertRaises(TypeError):
|
|
30
|
+
sort_toml_inline_tables(None)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
unittest.main()
|