5
5
from typing import Any
6
6
7
7
from packaging .utils import canonicalize_name
8
+ from poetry .core .packages .package import AUTHOR_REGEX
8
9
from poetry .core .utils .helpers import module_name
9
10
from tomlkit import inline_table
10
11
from tomlkit import loads
11
12
from tomlkit import table
12
13
from tomlkit .toml_document import TOMLDocument
13
14
15
+ from poetry .factory import Factory
14
16
from poetry .pyproject .toml import PyProjectTOML
15
17
16
18
21
23
22
24
23
25
POETRY_DEFAULT = """\
24
- [tool.poetry ]
26
+ [project ]
25
27
name = ""
26
28
version = ""
27
29
description = ""
28
- authors = []
29
- license = ""
30
+ authors = [
31
+ ]
32
+ license = {}
30
33
readme = ""
31
- packages = []
34
+ requires-python = ""
35
+ dependencies = [
36
+ ]
32
37
33
- [tool.poetry.dependencies]
38
+ [tool.poetry]
39
+ packages = []
34
40
35
41
[tool.poetry.group.dev.dependencies]
36
42
"""
@@ -48,7 +54,7 @@ def __init__(
48
54
readme_format : str = "md" ,
49
55
author : str | None = None ,
50
56
license : str | None = None ,
51
- python : str = "*" ,
57
+ python : str | None = None ,
52
58
dependencies : Mapping [str , str | Mapping [str , Any ]] | None = None ,
53
59
dev_dependencies : Mapping [str , str | Mapping [str , Any ]] | None = None ,
54
60
) -> None :
@@ -117,34 +123,49 @@ def create(
117
123
if with_pyproject :
118
124
self ._write_poetry (path )
119
125
120
- def generate_poetry_content (self ) -> TOMLDocument :
126
+ def generate_project_content (self ) -> TOMLDocument :
121
127
template = POETRY_DEFAULT
122
128
123
129
content : dict [str , Any ] = loads (template )
124
130
125
- poetry_content = content ["tool" ]["poetry" ]
126
- poetry_content ["name" ] = self ._project
127
- poetry_content ["version" ] = self ._version
128
- poetry_content ["description" ] = self ._description
129
- poetry_content ["authors" ].append (self ._author )
131
+ project_content = content ["project" ]
132
+ project_content ["name" ] = self ._project
133
+ project_content ["version" ] = self ._version
134
+ project_content ["description" ] = self ._description
135
+ m = AUTHOR_REGEX .match (self ._author )
136
+ if m is None :
137
+ # This should not happen because author has been validated before.
138
+ raise ValueError (f"Invalid author: { self ._author } " )
139
+ else :
140
+ author = {"name" : m .group ("name" )}
141
+ if email := m .group ("email" ):
142
+ author ["email" ] = email
143
+ project_content ["authors" ].append (author )
130
144
131
145
if self ._license :
132
- poetry_content ["license" ] = self ._license
146
+ project_content ["license" ]["text" ] = self ._license
147
+ else :
148
+ project_content .remove ("license" )
149
+
150
+ project_content ["readme" ] = f"README.{ self ._readme_format } "
151
+
152
+ if self ._python :
153
+ project_content ["requires-python" ] = self ._python
133
154
else :
134
- poetry_content .remove ("license" )
155
+ project_content .remove ("requires-python" )
156
+
157
+ for dep_name , dep_constraint in self ._dependencies .items ():
158
+ dependency = Factory .create_dependency (dep_name , dep_constraint )
159
+ project_content ["dependencies" ].append (dependency .to_pep_508 ())
160
+
161
+ poetry_content = content ["tool" ]["poetry" ]
135
162
136
- poetry_content ["readme" ] = f"README.{ self ._readme_format } "
137
163
packages = self .get_package_include ()
138
164
if packages :
139
165
poetry_content ["packages" ].append (packages )
140
166
else :
141
167
poetry_content .remove ("packages" )
142
168
143
- poetry_content ["dependencies" ]["python" ] = self ._python
144
-
145
- for dep_name , dep_constraint in self ._dependencies .items ():
146
- poetry_content ["dependencies" ][dep_name ] = dep_constraint
147
-
148
169
if self ._dev_dependencies :
149
170
for dep_name , dep_constraint in self ._dev_dependencies .items ():
150
171
poetry_content ["group" ]["dev" ]["dependencies" ][dep_name ] = (
@@ -153,6 +174,9 @@ def generate_poetry_content(self) -> TOMLDocument:
153
174
else :
154
175
del poetry_content ["group" ]
155
176
177
+ if not poetry_content :
178
+ del content ["tool" ]["poetry" ]
179
+
156
180
# Add build system
157
181
build_system = table ()
158
182
build_system_version = ""
@@ -194,7 +218,7 @@ def _create_tests(path: Path) -> None:
194
218
195
219
def _write_poetry (self , path : Path ) -> None :
196
220
pyproject = PyProjectTOML (path / "pyproject.toml" )
197
- content = self .generate_poetry_content ()
221
+ content = self .generate_project_content ()
198
222
for section , item in content .items ():
199
223
pyproject .data .append (section , item )
200
224
pyproject .save ()
0 commit comments