Skip to content

Commit 5ddcc22

Browse files
hugovklarryhastings
andcommitted
Add --gh and --section flags to "blurb add"
Co-authored-by: Larry Hastings <[email protected]>
1 parent 68193f0 commit 5ddcc22

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,13 @@ Here's how you interact with the file:
110110

111111
* Add the GitHub issue number for this commit to the
112112
end of the `.. gh-issue:` line.
113+
Can be passed on the CLI via the `--gh` flag.
113114

114115
* Uncomment the line with the relevant `Misc/NEWS` section for this entry.
115116
For example, if this should go in the `Library` section, uncomment
116117
the line reading `#.. section: Library`. To uncomment, just delete
117118
the `#` at the front of the line.
119+
Can be passed on the CLI via the `--section` flag.
118120

119121
* Finally, go to the end of the file, and enter your `NEWS` entry.
120122
This should be a single paragraph of English text using
@@ -239,6 +241,12 @@ part of the cherry-picking process.
239241

240242
## Changelog
241243

244+
### 1.2.0
245+
246+
- Add the `--gh` and `--section` flags to the `add` command.
247+
This lets you pre-fill-in the `gh-issue` and `section` fields
248+
in the template.
249+
242250
### 1.1.0
243251

244252
- Support GitHub Issues in addition to b.p.o (bugs.python.org).

src/blurb/blurb.py

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
#
7474
# Please enter the relevant GitHub issue number here:
7575
#
76-
.. gh-issue:
76+
.. gh-issue: {gh}
7777
7878
#
7979
# Uncomment one of these "section:" lines to specify which section
@@ -90,6 +90,7 @@
9090
#.. section: IDLE
9191
#.. section: Tools/Demos
9292
#.. section: C API
93+
{section}
9394
9495
# Write your Misc/NEWS.d entry below. It should be a simple ReST paragraph.
9596
# Don't start with "- Issue #<n>: " or "- gh-issue-<n>: " or that sort of stuff.
@@ -451,6 +452,8 @@ def parse(self, text, *, metadata=None, filename="input"):
451452
line_number = None
452453

453454
def throw(s):
455+
nonlocal filename
456+
nonlocal line_number
454457
raise BlurbError(f"Error in {filename}:{line_number}:\n{s}")
455458

456459
def finish_entry():
@@ -870,7 +873,7 @@ def find_editor():
870873

871874

872875
@subcommand
873-
def add():
876+
def add(*, gh="", section=""):
874877
"""
875878
Add a blurb (a Misc/NEWS.d/next entry) to the current CPython repo.
876879
"""
@@ -881,24 +884,23 @@ def add():
881884
os.close(handle)
882885
atexit.register(lambda : os.unlink(tmp_path))
883886

884-
def init_tmp_with_template():
885-
with open(tmp_path, "wt", encoding="utf-8") as file:
886-
# hack:
887-
# my editor likes to strip trailing whitespace from lines.
888-
# normally this is a good idea. but in the case of the template
889-
# it's unhelpful.
890-
# so, manually ensure there's a space at the end of the gh-issue line.
891-
text = template
892-
893-
issue_line = ".. gh-issue:"
894-
without_space = "\n" + issue_line + "\n"
895-
with_space = "\n" + issue_line + " \n"
896-
if without_space not in text:
897-
sys.exit("Can't find gh-issue line to ensure there's a space on the end!")
898-
text = text.replace(without_space, with_space)
899-
file.write(text)
887+
if gh:
888+
try:
889+
int(gh)
890+
except ValueError:
891+
error(f"blurb add --gh argument {gh} is not a valid integer!")
892+
893+
if section:
894+
if section not in sections:
895+
error(
896+
f"blurb add --section argument {section!r} is not a valid section!"
897+
" Use one of:\n" + "\n".join(sections)
898+
)
900899

901-
init_tmp_with_template()
900+
section = f".. section: {section}\n"
901+
902+
with open(tmp_path, "wt", encoding="utf-8") as file:
903+
file.write(template.format(gh=gh, section=section))
902904

903905
# We need to be clever about EDITOR.
904906
# On the one hand, it might be a legitimate path to an
@@ -1221,36 +1223,55 @@ def main():
12211223
kwargs = {}
12221224
for name, p in inspect.signature(fn).parameters.items():
12231225
if p.kind == inspect.Parameter.KEYWORD_ONLY:
1224-
assert isinstance(p.default, bool), "blurb command-line processing only handles boolean options"
1226+
assert isinstance(
1227+
p.default, (bool, str)
1228+
), "blurb command-line processing only handles boolean options"
12251229
kwargs[name] = p.default
12261230
short_options[name[0]] = name
12271231
long_options[name] = name
12281232

12291233
filtered_args = []
12301234
done_with_options = False
1235+
needs_oparg = None
12311236

1232-
def handle_option(s, dict):
1237+
def handle_option(s, dict, fn_name):
1238+
nonlocal needs_oparg
12331239
name = dict.get(s, None)
12341240
if not name:
1235-
sys.exit(f'blurb: Unknown option for {subcommand}: "{s}"')
1236-
kwargs[name] = not kwargs[name]
1241+
sys.exit(f'blurb: Unknown option for {fn_name}: "{s}"')
1242+
1243+
value = kwargs[name]
1244+
if isinstance(value, bool):
1245+
kwargs[name] = not value
1246+
else:
1247+
needs_oparg = name
12371248

12381249
# print(f"short_options {short_options} long_options {long_options}")
12391250
for a in args:
1251+
if needs_oparg:
1252+
kwargs[needs_oparg] = a
1253+
needs_oparg = None
1254+
continue
1255+
12401256
if done_with_options:
12411257
filtered_args.append(a)
12421258
continue
1259+
12431260
if a.startswith('-'):
12441261
if a == "--":
12451262
done_with_options = True
12461263
elif a.startswith("--"):
1247-
handle_option(a[2:], long_options)
1264+
handle_option(a[2:], long_options, fn.__name__)
12481265
else:
12491266
for s in a[1:]:
1250-
handle_option(s, short_options)
1267+
handle_option(s, short_options, fn.__name__)
12511268
continue
12521269
filtered_args.append(a)
12531270

1271+
if needs_oparg:
1272+
sys.exit(
1273+
f"Error: blurb: {fn_name} {needs_oparg} most be followed by an option argument"
1274+
)
12541275

12551276
sys.exit(fn(*filtered_args, **kwargs))
12561277
except TypeError as e:

0 commit comments

Comments
 (0)