From a4e1d856d7651741f5bd3fe910526aba863dccf6 Mon Sep 17 00:00:00 2001 From: Dennis Schwertel Date: Fri, 28 May 2021 12:00:16 +0200 Subject: [PATCH 1/4] initial work to use date type objects for the date and not strings --- setup.py | 2 +- src/towncrier/build.py | 9 ++++----- src/towncrier/newsfragments/349.enhancement | 1 + 3 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 src/towncrier/newsfragments/349.enhancement diff --git a/setup.py b/setup.py index 91e839f2..ddbb7d7c 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,7 @@ "Programming Language :: Python :: Implementation :: PyPy", ], use_incremental=True, - install_requires=["click", "click-default-group", "incremental", "jinja2", "setuptools", "toml"], + install_requires=["click", "click-default-group", "incremental", "jinja2", "setuptools", "toml", "python-dateutil"], extras_require={"dev": ["packaging"]}, package_dir={"": "src"}, packages=find_packages("src"), diff --git a/src/towncrier/build.py b/src/towncrier/build.py index 2f02eec2..092031d8 100644 --- a/src/towncrier/build.py +++ b/src/towncrier/build.py @@ -20,10 +20,6 @@ from ._git import remove_files, stage_newsfile -def _get_date(): - return date.today().isoformat() - - @click.command(name="build") @click.option( "--draft", @@ -135,7 +131,10 @@ def __main( project_name = "" if project_date is None: - project_date = _get_date().strip() + project_date = date.today() + else: + from dateutil.parser import parse + project_date = parse(project_date).date() if config["title_format"]: top_line = config["title_format"].format( diff --git a/src/towncrier/newsfragments/349.enhancement b/src/towncrier/newsfragments/349.enhancement new file mode 100644 index 00000000..aa7b85e5 --- /dev/null +++ b/src/towncrier/newsfragments/349.enhancement @@ -0,0 +1 @@ +Changed type of `versiondata.date` in the main template from `string` to `date`. This allows users to customize the date rendering in the template with `strftime`, like so: `{{versiondata.date.strftime("%B")}}`. From ebc97c67538a9f90ba13c9178b775adbf6ac690a Mon Sep 17 00:00:00 2001 From: Dennis Schwertel Date: Sun, 13 Mar 2022 09:27:48 +0100 Subject: [PATCH 2/4] Update src/towncrier/newsfragments/349.enhancement Co-authored-by: Adi Roiban --- src/towncrier/newsfragments/349.enhancement | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/towncrier/newsfragments/349.enhancement b/src/towncrier/newsfragments/349.enhancement index aa7b85e5..38bab88c 100644 --- a/src/towncrier/newsfragments/349.enhancement +++ b/src/towncrier/newsfragments/349.enhancement @@ -1 +1 @@ -Changed type of `versiondata.date` in the main template from `string` to `date`. This allows users to customize the date rendering in the template with `strftime`, like so: `{{versiondata.date.strftime("%B")}}`. +The `versiondata.date` is not `date` instance. This allows users to customize the date rendering in the template with `strftime`, like so: `{{versiondata.date.strftime("%B")}}`. From 39fc3713532a3ab5f978e719b0dd9c6f071c9858 Mon Sep 17 00:00:00 2001 From: Dennis Schwertel Date: Sun, 13 Mar 2022 09:31:13 +0100 Subject: [PATCH 3/4] moved include --- src/towncrier/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/towncrier/build.py b/src/towncrier/build.py index 092031d8..e83e3581 100644 --- a/src/towncrier/build.py +++ b/src/towncrier/build.py @@ -12,6 +12,7 @@ import sys from datetime import date +from dateutil.parser import parse from ._settings import load_config_from_options, ConfigError from ._builder import find_fragments, split_fragments, render_fragments @@ -133,7 +134,6 @@ def __main( if project_date is None: project_date = date.today() else: - from dateutil.parser import parse project_date = parse(project_date).date() if config["title_format"]: From 84070c12b56702cc77dee2bd1a10b5f4b97b1fab Mon Sep 17 00:00:00 2001 From: Dennis Schwertel Date: Sun, 13 Mar 2022 10:08:42 +0100 Subject: [PATCH 4/4] added datetests --- src/towncrier/templates/default.rst | 4 +- .../templates/hr-between-versions.rst | 4 +- .../templates/single-file-no-bullets.rst | 4 +- src/towncrier/test/templates/date.rst | 45 +++++++++++++++++++ src/towncrier/test/test_write.py | 44 ++++++++++++++++++ 5 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 src/towncrier/test/templates/date.rst diff --git a/src/towncrier/templates/default.rst b/src/towncrier/templates/default.rst index ac551dfa..f754c0d7 100644 --- a/src/towncrier/templates/default.rst +++ b/src/towncrier/templates/default.rst @@ -3,10 +3,10 @@ {{ top_underline * ((top_line)|length)}} {% elif versiondata.name %} {{ versiondata.name }} {{ versiondata.version }} ({{ versiondata.date }}) -{{ top_underline * ((versiondata.name + versiondata.version + versiondata.date)|length + 4)}} +{{ top_underline * ((versiondata.name + versiondata.version)|length + 4)}}{{ top_underline * (versiondata.date|title|length)}} {% else %} {{ versiondata.version }} ({{ versiondata.date }}) -{{ top_underline * ((versiondata.version + versiondata.date)|length + 3)}} +{{ top_underline * (versiondata.version|length + 3)}}{{ top_underline * (versiondata.date|title|length)}} {% endif %} {% for section, _ in sections.items() %} {% set underline = underlines[0] %}{% if section %}{{section}} diff --git a/src/towncrier/templates/hr-between-versions.rst b/src/towncrier/templates/hr-between-versions.rst index 8c655571..6f18de2f 100644 --- a/src/towncrier/templates/hr-between-versions.rst +++ b/src/towncrier/templates/hr-between-versions.rst @@ -1,9 +1,9 @@ {% if versiondata.name %} {{ versiondata.name }} {{ versiondata.version }} ({{ versiondata.date }}) -{{ top_underline * ((versiondata.name + versiondata.version + versiondata.date)|length + 4)}} +{{ top_underline * ((versiondata.name + versiondata.versiondata)|length + 4)}}{{ top_underline * (versiondata.date|title|length)}} {% else %} {{ versiondata.version }} ({{ versiondata.date }}) -{{ top_underline * ((versiondata.version + versiondata.date)|length + 3)}} +{{ top_underline * (versiondata.version|length + 3)}}{{ top_underline * (versiondata.date|title|length)}} {% endif %} {% for section, _ in sections.items() %} {% set underline = underlines[0] %}{% if section %}{{section}} diff --git a/src/towncrier/templates/single-file-no-bullets.rst b/src/towncrier/templates/single-file-no-bullets.rst index 7b065446..77af195e 100644 --- a/src/towncrier/templates/single-file-no-bullets.rst +++ b/src/towncrier/templates/single-file-no-bullets.rst @@ -1,9 +1,9 @@ {% if versiondata.name %} {{ versiondata.name }} {{ versiondata.version }} ({{ versiondata.date }}) -{{ top_underline * ((versiondata.name + versiondata.version + versiondata.date)|length + 4)}} +{{ top_underline * ((versiondata.name + versiondata.version)|length + 4)}}{{ top_underline * (versiondata.date|title|length)}} {% else %} {{ versiondata.version }} ({{ versiondata.date }}) -{{ top_underline * ((versiondata.version + versiondata.date)|length + 3)}} +{{ top_underline * (versiondata.version|length + 3)}}{{ top_underline * (versiondata.date|title|length)}} {% endif %} {% for section, _ in sections.items() %} {% set underline = underlines[0] %}{% if section %}{{section}} diff --git a/src/towncrier/test/templates/date.rst b/src/towncrier/test/templates/date.rst new file mode 100644 index 00000000..3e8879b2 --- /dev/null +++ b/src/towncrier/test/templates/date.rst @@ -0,0 +1,45 @@ +{% if top_line %} +{{ top_line }} +{{ top_underline * ((top_line)|length)}} +{% elif versiondata.name %} +{{ versiondata.name }} {{ versiondata.version }} ({{ versiondata.date }} / {{ versiondata.date|date:"D d M Y" }}) +{{ top_underline * ((versiondata.name + versiondata.version)|length + 4)}}{{ top_underline * (versiondata.date|title|length)}} +{% else %} +{{ versiondata.version }} ({{ versiondata.date }} # {{ versiondata.date|date:"D d M Y" }}) +{{ top_underline * (versiondata.version|length + 3)}}{{ top_underline * (versiondata.date|title|length)}} +{% endif %} +{% for section, _ in sections.items() %} +{% set underline = underlines[0] %}{% if section %}{{section}} +{{ underline * section|length }}{% set underline = underlines[1] %} + +{% endif %} + +{% if sections[section] %} +{% for category, val in definitions.items() if category in sections[section]%} +{{ definitions[category]['name'] }} +{{ underline * definitions[category]['name']|length }} + +{% if definitions[category]['showcontent'] %} +{% for text, values in sections[section][category].items() %} +- {{ text }} ({{ values|join(', ') }}) +{% endfor %} + +{% else %} +- {{ sections[section][category]['']|join(', ') }} + +{% endif %} +{% if sections[section][category]|length == 0 %} +No significant changes. + +{% else %} +{% endif %} + +{% endfor %} +{% else %} +No significant changes. + + +{% endif %} +{% endfor %} + + diff --git a/src/towncrier/test/test_write.py b/src/towncrier/test/test_write.py index 12c5bad8..d678ceed 100644 --- a/src/towncrier/test/test_write.py +++ b/src/towncrier/test/test_write.py @@ -271,3 +271,47 @@ def test_multiple_file_no_start_string(self): """) self.assertEqual(expected_output, output) + + +def test_dateformat(self): + """ + Testing support of dateformat filter during template rendering. + """ + tempdir = self.mktemp() + os.mkdir(tempdir) + + definitions = {} + fragments = split_fragments(fragments={}, definitions=definitions) + + template = pkg_resources.resource_string( + "towncrier", "test/templates/date.rst" + ).decode("utf8") + + content = render_fragments( + template=template, + issue_format=None, + top_line="", + fragments=fragments, + definitions=definitions, + underlines=["-", "~"], + wrap=True, + versiondata={"name": "MyProject", "version": "1.0", "date": "13.3.2022"}, + ) + + append_to_newsfile( + directory=tempdir, + filename="NEWS.rst", + start_string=None, + top_line="", + content=content, + ) + + with open(os.path.join(tempdir, "NEWS.rst"), "r") as f: + output = f.read() + + expected_output = dedent("""\ + MyProject 1.0 (1.1.2001 / Sun 13 Mar 2022) + ========================================== + """) + + self.assertEqual(expected_output, output)