Skip to content

Commit 4177a38

Browse files
committed
set license setter and getter for poetry v2 and update styling of output
1 parent 93816f4 commit 4177a38

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/somesy/pyproject/writer.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from somesy.core.models import Entity, Person, ProjectMetadata
1313
from somesy.core.writer import IgnoreKey, ProjectMetadataWriter
1414

15-
from .models import PoetryConfig, SetuptoolsConfig
15+
from .models import License, PoetryConfig, SetuptoolsConfig
1616

1717
logger = logging.getLogger("somesy")
1818

@@ -115,6 +115,7 @@ def __init__(
115115
"homepage": ["urls", "homepage"],
116116
"repository": ["urls", "repository"],
117117
"documentation": ["urls", "documentation"],
118+
"license": ["license", "text"],
118119
}
119120
if version == 1:
120121
super().__init__(
@@ -164,6 +165,31 @@ def _to_person(
164165
logger.warning(f"Cannot convert {person} to Entity.")
165166
return None
166167

168+
@property
169+
def license(self) -> Optional[Union[License, str]]:
170+
"""Get license from pyproject.toml file."""
171+
raw_license = self._get_property(["license"])
172+
if self._poetry_version == 1:
173+
return raw_license
174+
if raw_license is None:
175+
return None
176+
if isinstance(raw_license, str):
177+
return License(text=raw_license)
178+
return raw_license
179+
180+
@license.setter
181+
def license(self, value: Union[License, str]) -> None:
182+
"""Set license in pyproject.toml file."""
183+
# if version is 1, set license as str
184+
if self._poetry_version == 1:
185+
self._set_property(["license"], value)
186+
else:
187+
# if version is 2 and str, set as text
188+
if isinstance(value, str):
189+
self._set_property(["license"], {"text": value})
190+
else:
191+
self._set_property(["license"], value)
192+
167193
def sync(self, metadata: ProjectMetadata) -> None:
168194
"""Sync metadata with pyproject.toml file."""
169195
# Store original _from_person method
@@ -182,6 +208,7 @@ def sync(self, metadata: ProjectMetadata) -> None:
182208

183209
# For Poetry v2, convert authors and maintainers from array of tables to inline tables
184210
if self._poetry_version == 2:
211+
# convert authors and maintainers from array of tables to inline tables
185212
for field in ["authors", "maintainers"]:
186213
field_value = self._get_property([field])
187214
if field_value:
@@ -199,6 +226,27 @@ def sync(self, metadata: ProjectMetadata) -> None:
199226
# Replace the array of tables with the inline array
200227
self._set_property(field, inline_array)
201228

229+
# if license field has text, or file, make it inline table of tomlkit
230+
if self._get_property(["license"]) is not None:
231+
license_value = self._get_property(["license"])
232+
inline_table = tomlkit.inline_table()
233+
if isinstance(license_value, str):
234+
inline_table["text"] = license_value
235+
elif isinstance(license_value, dict):
236+
if "text" in license_value:
237+
inline_table["text"] = license_value["text"]
238+
elif "file" in license_value:
239+
inline_table["file"] = license_value["file"]
240+
elif hasattr(license_value, "text"):
241+
inline_table["text"] = license_value.text
242+
elif hasattr(license_value, "file"):
243+
inline_table["file"] = license_value.file
244+
self._set_property(["license"], inline_table)
245+
# Move urls section to the end if it exists
246+
if "urls" in self._data["project"]:
247+
urls = self._data["project"].pop("urls")
248+
self._data["project"]["urls"] = urls
249+
202250

203251
class SetupTools(PyprojectCommon):
204252
"""Setuptools config file handler parsed from setup.cfg."""

tests/output/test_pyproject_writer.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ def assert_content_match(pyproject_file):
5050
pyproject_file.description
5151
== "This is a test package for demonstration purposes."
5252
)
53-
assert (
54-
pyproject_file.license == "MIT" or pyproject_file.license["text"] == "MIT"
55-
)
53+
license_text = pyproject_file.license
54+
if isinstance(license_text, dict):
55+
license_text = license_text["text"]
56+
if isinstance(license_text, str):
57+
assert license_text == "MIT"
58+
else:
59+
assert license_text.text.value == "MIT"
5660
assert len(pyproject_file.authors) == 1
5761

5862
# assert for all formats

0 commit comments

Comments
 (0)