Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial work to use date type objects for the date and not strings #350

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"setuptools",
"toml; python_version < '3.6'",
"tomli; python_version >= '3.6'",
"python-dateutil",
],
extras_require={"dev": ["packaging"]},
package_dir={"": "src"},
Expand Down
9 changes: 4 additions & 5 deletions src/towncrier/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,10 +21,6 @@
from ._git import remove_files, stage_newsfile


def _get_date():
return date.today().isoformat()


@click.command(name="build")
@click.option(
"--draft",
Expand Down Expand Up @@ -135,7 +132,9 @@ def __main(
project_name = ""

if project_date is None:
project_date = _get_date().strip()
project_date = date.today()
else:
project_date = parse(project_date).date()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor comment

Can we just support ISO date format and then use stdlib datetime.strptime to parse the date ?

Not a big deal... just a shame that we dont't have "smart" parse function in stdlib.

At the same time, I think that we should encourage standard date representation, and only support ISO format.
... and it this way, we don't need to add yet another dependency


if config["title_format"]:
top_line = config["title_format"].format(
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/349.enhancement
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
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")}}`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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")}}`.
The `versiondata.date` is now a `date` instance.
This allows users to customize the date rendering in the template with `strftime`.
For example by using `{{versiondata.date.strftime("%B")}}`.

4 changes: 2 additions & 2 deletions src/towncrier/templates/default.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these changes to the default template?

I was thinking that the scope and purpose of the PR is to just change the internal of the date object.

{% 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}}
Expand Down
4 changes: 2 additions & 2 deletions src/towncrier/templates/hr-between-versions.rst
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down
4 changes: 2 additions & 2 deletions src/towncrier/templates/single-file-no-bullets.rst
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down
45 changes: 45 additions & 0 deletions src/towncrier/test/templates/date.rst
Original file line number Diff line number Diff line change
@@ -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 %}


44 changes: 44 additions & 0 deletions src/towncrier/test/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this file should hint the actual test in which it is used.

Suggested change
"towncrier", "test/templates/date.rst"
"towncrier", "test/templates/test_write_test_dateformat.rst"

but I hope that we can have a very simple and minimal template

something like

    template = """
We have a full datetime object: versiondata.date|date:"D d M Y" }
""".strip()

And check the expected output.

src/towncrier/test/templates/date.rst has some many side-effects and things that are not related to version datetime functionaliyt that is hard to see what is the target of the test.

).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)