1212from somesy .core .models import Entity , Person , ProjectMetadata
1313from somesy .core .writer import IgnoreKey , ProjectMetadataWriter
1414
15- from .models import PoetryConfig , SetuptoolsConfig
15+ from .models import License , PoetryConfig , SetuptoolsConfig
1616
1717logger = 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
203251class SetupTools (PyprojectCommon ):
204252 """Setuptools config file handler parsed from setup.cfg."""
0 commit comments