From 5d019f8fd8df46a6d0141cc623b38ebde5733e2d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 24 Jun 2024 18:53:00 +0300 Subject: [PATCH] WIP: Add --gh and --section flags to "blurb add" --- src/blurb/blurb.py | 68 +++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/src/blurb/blurb.py b/src/blurb/blurb.py index cb1c4c8..081389e 100755 --- a/src/blurb/blurb.py +++ b/src/blurb/blurb.py @@ -73,7 +73,7 @@ # # Please enter the relevant GitHub issue number here: # -.. gh-issue: +.. gh-issue: {gh} # # Uncomment one of these "section:" lines to specify which section @@ -90,6 +90,7 @@ #.. section: IDLE #.. section: Tools/Demos #.. section: C API +{section} # Write your Misc/NEWS.d entry below. It should be a simple ReST paragraph. # Don't start with "- Issue #: " or "- gh-issue-: " or that sort of stuff. @@ -451,6 +452,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(): @@ -870,7 +873,7 @@ def find_editor(): @subcommand -def add(): +def add(*, gh="", section=""): """ Add a blurb (a Misc/NEWS.d/next entry) to the current CPython repo. """ @@ -881,24 +884,20 @@ 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 gh-issue line. - text = template - - issue_line = ".. gh-issue:" - without_space = "\n" + issue_line + "\n" - with_space = "\n" + issue_line + " \n" - if without_space not in text: - sys.exit("Can't find gh-issue line to ensure there's a space on the end!") - text = text.replace(without_space, with_space) - file.write(text) + if gh: + try: + int(gh) + except ValueError: + error(f"blurb add --gh argument {gh} is not a valid integer!") - init_tmp_with_template() + if section: + if section not in sections: + error( + f"blurb add --section argument {section!r} is not a valid section!" + " Use one of:\n" + "\n".join(sections) + ) + + section = f".. section: {section}\n" # We need to be clever about EDITOR. # On the one hand, it might be a legitimate path to an @@ -1221,36 +1220,55 @@ 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" 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} most be followed by an option argument" + ) sys.exit(fn(*filtered_args, **kwargs)) except TypeError as e: