Browse Source

Allow configuration of template, magic comment and titles

pull/36/head
meejah 6 years ago
parent
commit
c9a1ea69f5
  1. 2
      LICENSE
  2. 43
      src/towncrier/__init__.py
  3. 18
      src/towncrier/_settings.py
  4. 11
      src/towncrier/_writer.py
  5. 1
      src/towncrier/newsfragments/35.feature
  6. 6
      src/towncrier/test/test_write.py

2
LICENSE

@ -1,4 +1,4 @@
Copyright (c) 2015-2016, Amber Brown
Copyright (c) 2015-2016, Amber Brown, meejah
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

43
src/towncrier/__init__.py

@ -44,13 +44,18 @@ def __main(draft, directory, project_version, project_date):
"""
directory = os.path.abspath(directory)
config = load_config(directory)
to_err = draft
click.echo("Loading template...")
template = pkg_resources.resource_string(
__name__,
"templates/template.rst").decode('utf8')
click.echo("Loading template...", err=to_err)
if config['template'] is None:
template = pkg_resources.resource_string(
__name__,
"templates/template.rst").decode('utf8')
else:
with open(config['template'], 'rb') as tmpl:
template = tmpl.read()
click.echo("Finding news fragments...")
click.echo("Finding news fragments...", err=to_err)
# TODO make these customisable
definitions = OrderedDict([
@ -73,7 +78,7 @@ def __main(draft, directory, project_version, project_date):
fragments = find_fragments(
base_directory, config['sections'], fragment_directory)
click.echo("Rendering news fragments...")
click.echo("Rendering news fragments...", err=to_err)
fragments = split_fragments(fragments, definitions)
rendered = render_fragments(template, fragments, definitions)
@ -95,25 +100,33 @@ def __main(draft, directory, project_version, project_date):
if project_date != "":
name_and_version += " (" + project_date + ")"
top_line = config['title_format'].format(
name=project_name,
version=project_version,
)
if draft:
click.echo("Draft only -- nothing has been written.")
click.echo("What is seen below is what would be written.\n")
click.echo(name_and_version)
click.echo("=" * len(name_and_version) + "\n")
click.echo(
"Draft only -- nothing has been written.\n"
"What is seen below is what would be written.\n",
err=to_err,
)
click.echo(top_line, err=to_err)
click.echo(rendered)
else:
click.echo("Writing to newsfile...")
click.echo("Writing to newsfile...", err=to_err)
start_line = config['start_line']
append_to_newsfile(directory, config['filename'],
name_and_version, rendered)
start_line, top_line, rendered)
click.echo("Staging newsfile...")
click.echo("Staging newsfile...", err=to_err)
stage_newsfile(directory, config['filename'])
click.echo("Removing news fragments...")
click.echo("Removing news fragments...", err=to_err)
remove_files(
base_directory, fragment_directory, config['sections'], fragments)
click.echo("Done!")
click.echo("Done!", err=to_err)
__all__ = ["__version__"]

18
src/towncrier/_settings.py

@ -27,10 +27,28 @@ def load_config(from_dir):
raise ValueError(
"The [towncrier] section has no required 'package' key.")
try:
start_string = config.get('towncrier', 'start_string')
except configparser.NoOptionError:
start_string = '.. towncrier release notes start\n'
try:
title_format = config.get('towncrier', 'title_format')
except configparser.NoOptionError:
title_format = '{name} {version}\n==========\n'
try:
template_fname = config.get('towncrier', 'template')
except configparser.NoOptionError:
template_fname = None
return {
'package': config.get('towncrier', 'package'),
'package_dir': config.get('towncrier', 'package_dir'),
'filename': config.get('towncrier', 'filename'),
'directory': config.get('towncrier', 'directory'),
'sections': {'': ''},
'template': template_fname,
'start_string': start_string,
'title_format': title_format,
}

11
src/towncrier/_writer.py

@ -11,10 +11,7 @@ from __future__ import absolute_import, division
import os
TOWNCRIER_START = ".. towncrier release notes start\n"
def append_to_newsfile(directory, filename, name_and_version, content):
def append_to_newsfile(directory, filename, start_line, top_line, content):
news_file = os.path.join(directory, filename)
@ -24,9 +21,7 @@ def append_to_newsfile(directory, filename, name_and_version, content):
with open(news_file, "r") as f:
existing_content = f.read()
existing_content = existing_content.split(TOWNCRIER_START)
top_line = name_and_version + "\n" + "=" * len(name_and_version) + "\n"
existing_content = existing_content.split(start_line, 1)
if top_line in existing_content:
raise ValueError(
@ -36,7 +31,7 @@ def append_to_newsfile(directory, filename, name_and_version, content):
if len(existing_content) > 1:
f.write(existing_content.pop(0).rstrip())
f.write("\n\n" + TOWNCRIER_START + "\n")
f.write("\n\n" + start_line + "\n")
f.write(top_line)
f.write(content)

1
src/towncrier/newsfragments/35.feature

@ -0,0 +1 @@
Allow configration of the template file, title text and "magic comment"

6
src/towncrier/test/test_write.py

@ -83,7 +83,8 @@ Old text.
append_to_newsfile(tempdir,
"NEWS.rst",
"MyProject 1.0",
".. towncrier release notes start\n",
"MyProject 1.0\n=============\n",
render_fragments(template, fragments, definitions))
with open(os.path.join(tempdir, "NEWS.rst"), "r") as f:
@ -176,7 +177,8 @@ Old text.
append_to_newsfile(tempdir,
"NEWS.rst",
"MyProject 1.0",
".. towncrier release notes start\n",
"MyProject 1.0\n=============\n",
render_fragments(template, fragments, definitions))
with open(os.path.join(tempdir, "NEWS.rst"), "r") as f:

Loading…
Cancel
Save