Skip to content

Commit 7fc0736

Browse files
committed
feat: format will be defaulted from changelog file name
1 parent 20c9bcd commit 7fc0736

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.. A new scriv changelog fragment.
2+
..
3+
.. Uncomment the section that is right (remove the leading dots).
4+
.. For top level release notes, leave all the headers commented out.
5+
..
6+
.. Removed
7+
.. .......
8+
..
9+
.. - A bullet item for the Removed category.
10+
..
11+
Added
12+
.....
13+
14+
- The ``format`` setting is now set based on the ``changelog`` setting.
15+
Previously, ``changelog=README.md`` would still use .rst formatting. Now it
16+
will use Markdown.
17+
18+
..
19+
.. Changed
20+
.. .......
21+
..
22+
.. - A bullet item for the Changed category.
23+
..
24+
.. Deprecated
25+
.. ..........
26+
..
27+
.. - A bullet item for the Deprecated category.
28+
..
29+
.. Fixed
30+
.. .....
31+
..
32+
.. - A bullet item for the Fixed category.
33+
..
34+
.. Security
35+
.. ........
36+
..
37+
.. - A bullet item for the Security category.
38+
..

docs/configuration.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ format
270270
The format to use for fragments and for the output changelog
271271
file. Can be either "rst" or "md".
272272

273-
Default: ``rst``
273+
Default: Derived from the changelog file name if
274+
provided, otherwise "rst".
274275

275276

276277
.. _config_fragment_directory:
@@ -386,7 +387,7 @@ source file.
386387

387388
Default: (empty)
388389

389-
.. [[[end]]] (checksum: 1b528f5aa913cecca1337c5dda4aa206)
390+
.. [[[end]]] (checksum: 6b03fa55831395ac304313bd43e01ff2)
390391
391392
392393
.. _deprecated_config:

src/scriv/config.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
logger = logging.getLogger(__name__)
2121

2222

23+
DEFAULT_FORMAT = "rst"
24+
DEFAULT_CHANGELOG = "CHANGELOG.${config:format}"
25+
26+
2327
@attr.s
2428
class _Options:
2529
"""
@@ -42,13 +46,17 @@ class _Options:
4246
# What format for fragments? reStructuredText ("rst") or Markdown ("md").
4347
format = attr.ib(
4448
type=str,
45-
default="rst",
46-
validator=attr.validators.in_(["rst", "md"]),
49+
default=None,
50+
validator=attr.validators.optional(attr.validators.in_(["rst", "md"])),
4751
metadata={
4852
"doc": """\
4953
The format to use for fragments and for the output changelog
5054
file. Can be either "rst" or "md".
5155
""",
56+
"doc_default": f"""\
57+
Derived from the changelog file name if provided,
58+
otherwise "{DEFAULT_FORMAT}".
59+
""",
5260
},
5361
)
5462

@@ -74,12 +82,13 @@ class _Options:
7482

7583
changelog = attr.ib(
7684
type=str,
77-
default="CHANGELOG.${config:format}",
85+
default=None,
7886
metadata={
7987
"doc": """\
8088
The changelog file managed and read by scriv. The old name
8189
for this setting is :ref:`output_file <deprecated_config>`.
8290
""",
91+
"doc_default": f"``{DEFAULT_CHANGELOG}``",
8392
},
8493
)
8594

@@ -223,6 +232,15 @@ class _Options:
223232
},
224233
)
225234

235+
def post_create(self):
236+
if self.format is None:
237+
if self.changelog is not None and "${" not in self.changelog:
238+
self.format = Path(self.changelog).suffix[1:]
239+
else:
240+
self.format = DEFAULT_FORMAT
241+
if self.changelog is None:
242+
self.changelog = DEFAULT_CHANGELOG
243+
226244

227245
# Map of old config names to new config names.
228246
DEPRECATED_NAMES = [
@@ -255,10 +273,12 @@ class Config:
255273
256274
"""
257275

258-
def __init__(self, **kwargs):
276+
def __init__(self, post_create_=True, **kwargs):
259277
"""All values in _Options can be set as keywords."""
260278
with validator_exceptions():
261279
self._options = _Options(**kwargs)
280+
if post_create_:
281+
self._options.post_create()
262282

263283
def __getattr__(self, name):
264284
"""Proxy to self._options, and resolve the value."""
@@ -292,7 +312,7 @@ def read(cls) -> Config:
292312
The section can be named ``[scriv]`` or ``[tool.scriv]``.
293313
294314
"""
295-
config = cls()
315+
config = cls(post_create_=False)
296316
config.read_one_config("setup.cfg")
297317
config.read_one_config("tox.ini")
298318
config.read_one_toml("pyproject.toml")
@@ -301,6 +321,7 @@ def read(cls) -> Config:
301321
)
302322
with validator_exceptions():
303323
attr.validate(config._options)
324+
config._options.post_create()
304325
return config
305326

306327
def get_set_option(self, scriv_data, config_name, opt_name):
@@ -393,7 +414,8 @@ def resolve_value(self, value: str) -> str:
393414
"command:" read the output of a shell command.
394415
395416
"""
396-
value = value.replace("${config:format}", self._options.format)
417+
if self._options.format is not None:
418+
value = value.replace("${config:format}", self._options.format)
397419
if value.startswith("file:"):
398420
file_name = value.partition(":")[2].strip()
399421
value = self.read_file_value(file_name)

tests/test_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def test_reading_config(config_text, temp_dir):
116116
config = Config.read()
117117
assert config.fragment_directory == "changelog.d"
118118
assert config.changelog == "README.md"
119+
assert config.format == "md"
119120
assert config.categories == ["New", "Different", "Gone", "Bad"]
120121
assert config.start_marker == "FIRST!"
121122

@@ -329,6 +330,7 @@ def test_reading_toml_file(self, config_text, temp_dir):
329330
(temp_dir / "pyproject.toml").write_text(config_text)
330331
config = Config.read()
331332
assert config.changelog == "README.md"
333+
assert config.format == "md"
332334
assert config.categories == ["New", "Different", "Gone", "Bad"]
333335
assert config.start_marker == "FIRST!"
334336

0 commit comments

Comments
 (0)