Skip to content

Add --bpo and --section flags to "blurb add". #312

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions blurb/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
blurb version 1.0
blurb version 1.0.8
Part of the blurb package.
Copyright 2015-2018 by Larry Hastings
Copyright 2015-2019 by Larry Hastings

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
Expand Down
7 changes: 7 additions & 0 deletions blurb/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ part of the cherry-picking process.
Changelog
---------

1.0.8
~~~~~

- Added the ``--bpo`` and ``--section`` flags to
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to use the bpo abbreviation, or would it be better as issue now?

the ``add`` command. This lets you pre-fill-in
the bpo and section fields in the template.

1.0.7
~~~~~

Expand Down
63 changes: 36 additions & 27 deletions blurb/blurb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
"""Command-line tool to manage CPython Misc/NEWS.d entries."""
__version__ = "1.0.7"
__version__ = "1.0.8"

##
## blurb version 1.0
Expand Down Expand Up @@ -76,7 +76,7 @@
#
# Please enter the relevant bugs.python.org issue number here:
#
.. bpo:
.. bpo: {bpo}

#
# Uncomment one of these "section:" lines to specify which section
Expand All @@ -93,7 +93,7 @@
#.. section: IDLE
#.. section: Tools/Demos
#.. section: C API

{section}
Copy link
Member

Choose a reason for hiding this comment

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

why don't you keep the same principle with the .. bpo: {bpo}? at this line, you already have a constructed section.
You build the section at that line https://github.com/python/core-workflow/pull/312/files#diff-1bd652c5f766ddebec6cd6dc8f27b3b1R927

Do you make the construction before because you don't want to have a conditional template? is it right?

# Write your Misc/NEWS entry below. It should be a simple ReST paragraph.
# Don't start with "- Issue #<n>: " or "- bpo-<n>: " or that sort of stuff.
###########################################################################
Expand Down Expand Up @@ -483,6 +483,8 @@ def parse(self, text, *, metadata=None, filename="input"):
line_number = None

def throw(s):
nonlocal filename
nonlocal line_number
raise BlurbError(f("Error in {filename}:{line_number}:\n{s}"))

def finish_entry():
Expand Down Expand Up @@ -902,7 +904,7 @@ def find_editor():


@subcommand
def add():
def add(*, bpo="", section=""):
"""
Add a blurb (a Misc/NEWS entry) to the current CPython repo.
"""
Expand All @@ -913,24 +915,19 @@ def add():
os.close(handle)
atexit.register(lambda : os.unlink(tmp_path))

def init_tmp_with_template():
with open(tmp_path, "wt", encoding="utf-8") as file:
# hack:
# my editor likes to strip trailing whitespace from lines.
# normally this is a good idea. but in the case of the template
# it's unhelpful.
# so, manually ensure there's a space at the end of the bpo line.
text = template

bpo_line = ".. bpo:"
without_space = "\n" + bpo_line + "\n"
with_space = "\n" + bpo_line + " \n"
if without_space not in text:
sys.exit("Can't find BPO line to ensure there's a space on the end!")
text = text.replace(without_space, with_space)
file.write(text)
if bpo:
try:
int(bpo)
except ValueError:
error("blurb add --bpo argument " + repr(bpo) + " is not a valid integer!")

init_tmp_with_template()
if section:
if section not in sections:
error("blurb add --section argument " + repr(section) + " is not a valid section!")
Copy link
Member

Choose a reason for hiding this comment

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

How about printing the valid sections when this fails?

Suggested change
error("blurb add --section argument " + repr(section) + " is not a valid section!")
error("blurb add --section argument " + repr(section) + " is not a valid section! Use one of:\n" + "\n".join(sections))

For example:

$ python blurb.py add --bpo 123 --section aaa
Error: blurb add --section argument 'aaa' is not a valid section! Use one of:
Security
Core and Builtins
Library
Documentation
Tests
Build
Windows
macOS
IDLE
Tools/Demos
C API

section = ".. section: " + section + "\n"

with open(tmp_path, "wt", encoding="utf-8") as file:
file.write(template.format(bpo=bpo, section=section))

# We need to be clever about EDITOR.
# On the one hand, it might be a legitimate path to an
Expand Down Expand Up @@ -1617,36 +1614,48 @@ def main():
kwargs = {}
for name, p in inspect.signature(fn).parameters.items():
if p.kind == inspect.Parameter.KEYWORD_ONLY:
assert isinstance(p.default, bool), "blurb command-line processing only handles boolean options"
assert isinstance(p.default, (bool, str)), "blurb command-line processing only handles boolean options and strings"
kwargs[name] = p.default
short_options[name[0]] = name
long_options[name] = name

filtered_args = []
done_with_options = False
needs_oparg = None

def handle_option(s, dict):
def handle_option(s, dict, fn_name):
nonlocal needs_oparg
name = dict.get(s, None)
if not name:
sys.exit(f('blurb: Unknown option for {subcommand}: "{s}"'))
kwargs[name] = not kwargs[name]
sys.exit(f('blurb: Unknown option for {fn_name}: "{s}"'))
value = kwargs[name]
if isinstance(value, bool):
kwargs[name] = not value
else:
needs_oparg = name

# print(f("short_options {short_options} long_options {long_options}"))
for a in args:
if needs_oparg:
kwargs[needs_oparg] = a
needs_oparg = None
continue
if done_with_options:
filtered_args.append(a)
continue
if a.startswith('-'):
if a == "--":
done_with_options = True
elif a.startswith("--"):
handle_option(a[2:], long_options)
handle_option(a[2:], long_options, fn.__name__)
else:
for s in a[1:]:
handle_option(s, short_options)
handle_option(s, short_options, fn.__name__)
continue
filtered_args.append(a)

if needs_oparg:
sys.exit(f("Error: blurb {fn_name} {needs_oparg} must be followed by an option argument!"))

sys.exit(fn(*filtered_args, **kwargs))
except TypeError as e:
Expand Down