You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.9 KiB

# Copyright (c) Stephen Finucane, 2019
# See LICENSE for details.
Create a new fragment.
import os
import click
from ._settings import load_config_from_options
@click.option("--dir", "directory", default=None)
@click.option("--config", "config", default=None)
help="Open an editor for writing the newsfragment content.",
) # TODO: default should be true
default="Add your info here",
help="Sets the content of the new fragment.",
def _main(ctx, directory, config, filename, edit, content):
return __main(ctx, directory, config, filename, edit, content)
def __main(ctx, directory, config, filename, edit, content):
The main entry point.
base_directory, config = load_config_from_options(directory, config)
definitions = config["types"] or []
if len(filename.split(".")) < 2 or (
filename.split(".")[-1] not in definitions
and filename.split(".")[-2] not in definitions
raise click.BadParameter(
"Expected filename '{}' to be of format '{{name}}.{{type}}', "
"where '{{name}}' is an arbitrary slug and '{{type}}' is "
"one of: {}".format(filename, ", ".join(definitions))
if config.get("directory"):
fragments_directory = os.path.abspath(
os.path.join(base_directory, config["directory"])
fragments_directory = os.path.abspath(
if not os.path.exists(fragments_directory):
segment_file = os.path.join(fragments_directory, filename)
if os.path.exists(segment_file):
raise click.ClickException(f"{segment_file} already exists")
if edit:
content = _get_news_content_from_user(content)
if content is None:
click.echo("Abort creating news fragment.")
with open(segment_file, "w") as f:
click.echo(f"Created news fragment at {segment_file}")
def _get_news_content_from_user(message):
initial_content = (
"# Please write your news content. When finished, save the file.\n"
"# In order to abort, exit without saving.\n"
'# Lines starting with "#" are ignored.\n'
if message is not None:
initial_content += "\n" "{message}\n".format(message=message)
content = click.edit(initial_content)
if content is None:
return None
all_lines = content.split("\n")
lines = [line.rstrip() for line in all_lines if not line.lstrip().startswith("#")]
return "\n".join(lines)
if __name__ == "__main__": # pragma: no cover