Skip to content

Commit b51f04e

Browse files
committed
better toml inline table formatting (missing whitespaces after a comma)
1 parent 44c8602 commit b51f04e

5 files changed

Lines changed: 71 additions & 5 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ dependencies = [
3333
]
3434

3535
authors = [
36-
{name = "Mustafa Soylu",email = "m.soylu@fz-juelich.de"},
37-
{name = "Anton Pirogov",email = "a.pirogov@fz-juelich.de"},
36+
{name = "Mustafa Soylu", email = "m.soylu@fz-juelich.de"},
37+
{name = "Anton Pirogov", email = "a.pirogov@fz-juelich.de"},
3838
]
3939
maintainers = [
40-
{name = "Mustafa Soylu",email = "m.soylu@fz-juelich.de"},
40+
{name = "Mustafa Soylu", email = "m.soylu@fz-juelich.de"},
4141
]
4242

4343
[project.license]

src/somesy/fortran/writer.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ def save(self, path: Optional[Path] = None) -> None:
9696
self._data["description"] = tomlkit.string(
9797
self._data["description"], multiline=True
9898
)
99+
100+
# Handle arrays with proper formatting
101+
for key, value in self._data.items():
102+
if isinstance(value, list):
103+
array = tomlkit.array()
104+
array.extend(value)
105+
array.multiline(True)
106+
# Ensure whitespace after commas in inline tables
107+
for item in array:
108+
if isinstance(item, tomlkit.items.InlineTable):
109+
# Rebuild the inline table with desired formatting
110+
formatted_item = tomlkit.inline_table()
111+
for k, v in item.value.items():
112+
formatted_item[k] = v
113+
formatted_item.trivia.trail = " " # Add space after each comma
114+
array[array.index(item)] = formatted_item
115+
self._data[key] = array
116+
else:
117+
self._data[key] = value
118+
99119
with open(path, "w") as f:
100120
tomlkit.dump(self._data, f)
101121

src/somesy/julia/writer.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ def save(self, path: Optional[Path] = None) -> None:
6060
self._data["description"] = tomlkit.string(
6161
self._data["description"], multiline=True
6262
)
63+
64+
# Handle arrays with proper formatting
65+
for key, value in self._data.items():
66+
if isinstance(value, list):
67+
array = tomlkit.array()
68+
array.extend(value)
69+
array.multiline(True)
70+
# Ensure whitespace after commas in inline tables
71+
for item in array:
72+
if isinstance(item, tomlkit.items.InlineTable):
73+
# Rebuild the inline table with desired formatting
74+
formatted_item = tomlkit.inline_table()
75+
for k, v in item.value.items():
76+
formatted_item[k] = v
77+
formatted_item.trivia.trail = " " # Add space after each comma
78+
array[array.index(item)] = formatted_item
79+
self._data[key] = array
80+
else:
81+
self._data[key] = value
82+
6383
with open(path, "w") as f:
6484
tomlkit.dump(self._data, f)
6585

src/somesy/pyproject/writer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ def _set_property(self, key: Union[str, List[str], IgnoreKey], value: Any) -> No
101101
array = tomlkit.array()
102102
array.extend(value)
103103
array.multiline(True)
104+
# Ensure whitespace after commas in inline tables
105+
for item in array:
106+
if isinstance(item, tomlkit.items.InlineTable):
107+
# Rebuild the inline table with desired formatting
108+
formatted_item = tomlkit.inline_table()
109+
for k, v in item.value.items():
110+
formatted_item[k] = v
111+
formatted_item.trivia.trail = " " # Add space after each comma
112+
array[array.index(item)] = formatted_item
104113
curr[key_path[-1]] = array
105114
else:
106115
curr[key_path[-1]] = value

src/somesy/rust/writer.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Any, List, Optional, Union
66

77
from rich.pretty import pretty_repr
8-
from tomlkit import dump, load, string, table
8+
from tomlkit import array, dump, inline_table, items, load, string, table
99

1010
from somesy.core.models import Entity, Person, ProjectMetadata
1111
from somesy.core.writer import FieldKeyMapping, IgnoreKey, ProjectMetadataWriter
@@ -99,7 +99,24 @@ def _set_property(self, key: Union[str, List[str], IgnoreKey], value: Any) -> No
9999
if key not in curr:
100100
curr.add(key, table())
101101
curr = curr[key]
102-
curr[key_path[-1]] = value
102+
103+
# Handle arrays with proper formatting
104+
if isinstance(value, list):
105+
arr = array()
106+
arr.extend(value)
107+
arr.multiline(True)
108+
# Ensure whitespace after commas in inline tables
109+
for item in arr:
110+
if isinstance(item, items.InlineTable):
111+
# Rebuild the inline table with desired formatting
112+
formatted_item = inline_table()
113+
for k, v in item.value.items():
114+
formatted_item[k] = v
115+
formatted_item.trivia.trail = " " # Add space after each comma
116+
arr[arr.index(item)] = formatted_item
117+
curr[key_path[-1]] = arr
118+
else:
119+
curr[key_path[-1]] = value
103120

104121
@staticmethod
105122
def _from_person(person: Union[Person, Entity]):

0 commit comments

Comments
 (0)