Skip to content

Commit

Permalink
Merge pull request #147 from hawkowl/versionstrings
Browse files Browse the repository at this point in the history
Move version top strings to the Jinja template
  • Loading branch information
hawkowl authored Aug 11, 2019
2 parents 018a6d8 + bbb4035 commit 2cf6dd6
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 88 deletions.
41 changes: 25 additions & 16 deletions src/towncrier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __main(draft, directory, config_file, project_name, project_version, project
to_err = draft

click.echo("Loading template...", err=to_err)
if config["template"] is None:
if config.get("template") is None:
template = pkg_resources.resource_string(
__name__, "templates/template.rst"
).decode("utf8")
Expand All @@ -96,20 +96,11 @@ def __main(draft, directory, config_file, project_name, project_version, project

click.echo("Rendering news fragments...", err=to_err)
fragments = split_fragments(fragments, definitions)
rendered = render_fragments(
# The 0th underline is used for the top line
template,
config["issue_format"],
fragments,
definitions,
config["underlines"][1:],
config["wrap"],
)

if project_version is None:
project_version = get_version(
os.path.join(directory, config["package_dir"]), config["package"]
)
).strip()

if project_name is None:
package = config.get("package")
Expand All @@ -122,20 +113,38 @@ def __main(draft, directory, config_file, project_name, project_version, project
project_name = ""

if project_date is None:
project_date = _get_date()
project_date = _get_date().strip()

if config["title_format"]:
top_line = config["title_format"].format(
name=project_name, version=project_version, project_date=project_date
)
top_line += u"\n" + (config["underlines"][0] * len(top_line)) + u"\n"
else:
top_line = ""

top_line = config["title_format"].format(
name=project_name, version=project_version, project_date=project_date
rendered = render_fragments(
# The 0th underline is used for the top line
template,
config["issue_format"],
fragments,
definitions,
config["underlines"][1:],
config["wrap"],
{"name": project_name, "version": project_version, "date": project_date},
top_underline=config["underlines"][0],
)
top_line += u"\n" + (config["underlines"][0] * len(top_line)) + u"\n"

if draft:
click.echo(
"Draft only -- nothing has been written.\n"
"What is seen below is what would be written.\n",
err=to_err,
)
click.echo("%s\n%s" % (top_line, rendered))
if top_line:
click.echo("\n%s\n%s" % (top_line, rendered))
else:
click.echo(rendered)
else:
click.echo("Writing to newsfile...", err=to_err)
start_line = config["start_line"]
Expand Down
18 changes: 16 additions & 2 deletions src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def indent(text, prefix):
def prefixed_lines():
for line in text.splitlines(True):
yield (prefix + line if line.strip() else line)

return u"".join(prefixed_lines())


Expand Down Expand Up @@ -170,7 +171,16 @@ def render_issue(issue_format, issue):
return issue_format.format(issue=issue)


def render_fragments(template, issue_format, fragments, definitions, underlines, wrap):
def render_fragments(
template,
issue_format,
fragments,
definitions,
underlines,
wrap,
versiondata,
top_underline="=",
):
"""
Render the fragments into a news file.
"""
Expand Down Expand Up @@ -215,7 +225,11 @@ def render_fragments(template, issue_format, fragments, definitions, underlines,
done = []

res = jinja_template.render(
sections=data, definitions=definitions, underlines=underlines
sections=data,
definitions=definitions,
underlines=underlines,
versiondata=versiondata,
top_underline=top_underline,
)

for line in res.split(u"\n"):
Expand Down
6 changes: 3 additions & 3 deletions src/towncrier/_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def get_version(package_dir, package):
raise Exception("No __version__, I don't know how else to look")

if isinstance(version, str):
return version
return version.strip()

if isinstance(version, Version):
return version.base()
return version.base().strip()

if isinstance(version, tuple):
return ".".join(map(str, version))
return ".".join(map(str, version)).strip()

raise Exception(
(
Expand Down
9 changes: 5 additions & 4 deletions src/towncrier/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


_start_string = u".. towncrier release notes start\n"
_title_format = u"{name} {version} ({project_date})"
_title_format = None
_template_fname = None
_default_types = OrderedDict(
[
Expand All @@ -28,9 +28,10 @@ def load_config(directory):

def load_config_from_file(from_file):
if not os.path.exists(from_file):
return None
with open(from_file, "r") as conffile:
config = toml.load(conffile)
config = {"tool": {"towncrier": {}}}
else:
with open(from_file, "r") as conffile:
config = toml.load(conffile)

return parse_toml(config)

Expand Down
5 changes: 3 additions & 2 deletions src/towncrier/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ def append_to_newsfile(directory, filename, start_line, top_line, content):

existing_content = existing_content.split(start_line, 1)

if top_line in existing_content:
if top_line and top_line in existing_content:
raise ValueError("It seems you've already produced newsfiles for this version?")

with open(os.path.join(directory, filename), "wb") as f:

if len(existing_content) > 1:
f.write(existing_content.pop(0).rstrip().encode("utf8"))
f.write((u"\n\n" + start_line + u"\n").encode("utf8"))
if start_line:
f.write((u"\n\n" + start_line + u"\n").encode("utf8"))

f.write(top_line.encode("utf8"))
f.write(content.encode("utf8"))
Expand Down
10 changes: 4 additions & 6 deletions src/towncrier/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

import click

from subprocess import (
CalledProcessError,
check_output,
STDOUT,
)
from subprocess import CalledProcessError, check_output, STDOUT

from ._settings import load_config, load_config_from_file
from ._builder import find_fragments
Expand Down Expand Up @@ -40,7 +36,9 @@ def __main(comparewith, directory, pyproject):

try:
files_changed = (
_run(["git", "diff", "--name-only", comparewith + "..."], cwd=base_directory)
_run(
["git", "diff", "--name-only", comparewith + "..."], cwd=base_directory
)
.decode(getattr(sys.stdout, "encoding", "utf8"))
.strip()
)
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/147.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Towncrier's templating now allows configuration of the version header.
7 changes: 7 additions & 0 deletions src/towncrier/templates/template.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{% if versiondata.name %}
{{ versiondata.name }} {{ versiondata.version }} ({{ versiondata.date }})
{{ top_underline * ((versiondata.name + versiondata.version + versiondata.date)|length + 4)}}
{% else %}
{{ versiondata.version }} ({{ versiondata.date }})
{{ top_underline * ((versiondata.version + versiondata.date)|length + 3)}}
{% endif %}
{% for section, _ in sections.items() %}
{% set underline = underlines[0] %}{% if section %}{{section}}
{{ underline * section|length }}{% set underline = underlines[1] %}
Expand Down
22 changes: 5 additions & 17 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,20 @@ def test_git_fails(self):
create_project("pyproject.toml")

result = runner.invoke(_main, ["--compare-with", "hblaugh"])
self.assertIn(
"git produced output while failing",
result.output,
)
self.assertIn(
"hblaugh",
result.output,
)
self.assertIn("git produced output while failing", result.output)
self.assertIn("hblaugh", result.output)

def test_no_changes_made(self):
self._test_no_changes_made(
"pyproject.toml",
lambda runner, main, argv: runner.invoke(main, argv),
"pyproject.toml", lambda runner, main, argv: runner.invoke(main, argv)
)

def test_no_changes_made_pyproject_path(self):
pyproject = "not-pyproject.toml"
self._test_no_changes_made(
pyproject,
lambda runner, main, argv: runner.invoke(
main,
argv + ["--pyproject", pyproject],
main, argv + ["--pyproject", pyproject]
),
)

Expand Down Expand Up @@ -108,11 +100,7 @@ def test_fragment_exists(self):
),
result,
)
self.assertEqual(
0,
result.exit_code,
result,
)
self.assertEqual(0, result.exit_code, result)

def test_fragment_missing(self):
runner = CliRunner()
Expand Down
11 changes: 2 additions & 9 deletions src/towncrier/test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def test_happy_path(self):
Foo 1.2.3 (01-01-2001)
======================
Features
--------
Expand Down Expand Up @@ -161,7 +160,7 @@ def run_order_scenario(sections, types):
u"Loading template...\nFinding news fragments...\nRendering news "
u"fragments...\nDraft only -- nothing has been written.\nWhat is "
u"seen below is what would be written.\n\nFoo 1.2.3 (01-01-2001)"
u"\n======================\n"
u"\n======================"
+ dedent(
"""
section-a
Expand Down Expand Up @@ -204,7 +203,7 @@ def run_order_scenario(sections, types):
u"Loading template...\nFinding news fragments...\nRendering news "
u"fragments...\nDraft only -- nothing has been written.\nWhat is "
u"seen below is what would be written.\n\nFoo 1.2.3 (01-01-2001)"
u"\n======================\n"
u"\n======================"
+ dedent(
"""
section-b
Expand Down Expand Up @@ -312,7 +311,6 @@ def test_projectless_changelog(self):
FooBarBaz 7.8.9 (01-01-2001)
============================
Features
--------
Expand All @@ -335,10 +333,6 @@ def test_no_package_changelog(self):
runner = CliRunner()

with runner.isolated_filesystem():
with open("pyproject.toml", "w") as f:
f.write(
"[tool.towncrier]\n" 'title_format = "{version} ({project_date})"\n'
)
os.mkdir("newsfragments")
with open("newsfragments/123.feature", "w") as f:
f.write("Adds levitation")
Expand All @@ -361,7 +355,6 @@ def test_no_package_changelog(self):
7.8.9 (01-01-2001)
==================
Features
--------
Expand Down
Loading

0 comments on commit 2cf6dd6

Please sign in to comment.