From a98ad2d0b82876ed186a71828c03819d8d467617 Mon Sep 17 00:00:00 2001 From: Larry Hastings Date: Wed, 27 Feb 2019 17:35:05 -0800 Subject: [PATCH 1/2] Add --bpo and --section flags to "blurb add". --- blurb/LICENSE.txt | 2 +- blurb/README.rst | 7 ++++++ blurb/blurb.py | 63 +++++++++++++++++++++++++++-------------------- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/blurb/LICENSE.txt b/blurb/LICENSE.txt index cb9d831..9285911 100644 --- a/blurb/LICENSE.txt +++ b/blurb/LICENSE.txt @@ -1,4 +1,4 @@ -blurb version 1.0 +blurb version 1.0.8 Part of the blurb package. Copyright 2015-2018 by Larry Hastings diff --git a/blurb/README.rst b/blurb/README.rst index 1962427..f0b3bc5 100644 --- a/blurb/README.rst +++ b/blurb/README.rst @@ -247,6 +247,13 @@ part of the cherry-picking process. Changelog --------- +1.0.8 +~~~~~ + +- Added the ``--bpo`` and ``--section`` flags to + the ``add`` command. This lets you pre-fill-in + the bpo and section fields in the template. + 1.0.7 ~~~~~ diff --git a/blurb/blurb.py b/blurb/blurb.py index cd975ba..52a4002 100755 --- a/blurb/blurb.py +++ b/blurb/blurb.py @@ -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 @@ -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 @@ -93,7 +93,7 @@ #.. section: IDLE #.. section: Tools/Demos #.. section: C API - +{section} # Write your Misc/NEWS entry below. It should be a simple ReST paragraph. # Don't start with "- Issue #: " or "- bpo-: " or that sort of stuff. ########################################################################### @@ -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(): @@ -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. """ @@ -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!") + 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 @@ -1617,22 +1614,32 @@ 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 @@ -1640,13 +1647,15 @@ def handle_option(s, dict): 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: From 3551d957561e520bef12b54dbed9a076f4be096a Mon Sep 17 00:00:00 2001 From: Mariatta Date: Wed, 27 Feb 2019 20:19:15 -0800 Subject: [PATCH 2/2] Update blurb/LICENSE.txt with current year. Co-Authored-By: larryhastings --- blurb/LICENSE.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blurb/LICENSE.txt b/blurb/LICENSE.txt index 9285911..c2aa4bd 100644 --- a/blurb/LICENSE.txt +++ b/blurb/LICENSE.txt @@ -1,6 +1,6 @@ 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