From 27fcb5130710b72c11cc989a41b6a4d35f57e856 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Wed, 13 Aug 2025 00:25:50 +0100 Subject: [PATCH 1/2] Move template and section utilities to ``blurb._template`` --- src/blurb/_template.py | 84 ++++++++++++++++++++++++++++++++++++++++ src/blurb/blurb.py | 87 ++---------------------------------------- 2 files changed, 88 insertions(+), 83 deletions(-) create mode 100644 src/blurb/_template.py diff --git a/src/blurb/_template.py b/src/blurb/_template.py new file mode 100644 index 0000000..1b0fc9c --- /dev/null +++ b/src/blurb/_template.py @@ -0,0 +1,84 @@ +from __future__ import annotations + +# +# This template is the canonical list of acceptable section names! +# It's parsed internally into the "sections" set. +# + +template = """ + +# +# Please enter the relevant GitHub issue number here: +# +.. gh-issue: + +# +# Uncomment one of these "section:" lines to specify which section +# this entry should go in in Misc/NEWS.d. +# +#.. section: Security +#.. section: Core and Builtins +#.. section: Library +#.. section: Documentation +#.. section: Tests +#.. section: Build +#.. section: Windows +#.. section: macOS +#.. section: IDLE +#.. section: Tools/Demos +#.. section: C API + +# 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. +########################################################################### + + +""".lstrip() + +sections: list[str] = [] +for line in template.split('\n'): + line = line.strip() + prefix, found, section = line.partition('#.. section: ') + if found and not prefix: + sections.append(section.strip()) + +_sanitize_section = { + 'C API': 'C_API', + 'Core and Builtins': 'Core_and_Builtins', + 'Tools/Demos': 'Tools-Demos', +} + +_unsanitize_section = { + 'C_API': 'C API', + 'Core_and_Builtins': 'Core and Builtins', + 'Tools-Demos': 'Tools/Demos', + } + + +def sanitize_section(section: str, /) -> str: + """Clean up a section string. + + This makes it viable as a directory name. + """ + return _sanitize_section.get(section, section) + + +def sanitize_section_legacy(section: str, /) -> str: + """Clean up a section string, allowing spaces. + + This makes it viable as a directory name. + """ + return section.replace('/', '-') + + +def unsanitize_section(section: str, /) -> str: + return _unsanitize_section.get(section, section) + + +def next_filename_unsanitize_sections(filename: str, /) -> str: + for key, value in _unsanitize_section.items(): + for separator in ('/', '\\'): + key = f'{separator}{key}{separator}' + value = f'{separator}{value}{separator}' + filename = filename.replace(key, value) + return filename diff --git a/src/blurb/blurb.py b/src/blurb/blurb.py index 2184251..13c7345 100755 --- a/src/blurb/blurb.py +++ b/src/blurb/blurb.py @@ -58,92 +58,13 @@ import time from blurb._cli import main, subcommand - - -# -# This template is the canonical list of acceptable section names! -# It's parsed internally into the "sections" set. -# - -template = """ - -# -# Please enter the relevant GitHub issue number here: -# -.. gh-issue: - -# -# Uncomment one of these "section:" lines to specify which section -# this entry should go in in Misc/NEWS.d. -# -#.. section: Security -#.. section: Core and Builtins -#.. section: Library -#.. section: Documentation -#.. section: Tests -#.. section: Build -#.. section: Windows -#.. section: macOS -#.. section: IDLE -#.. section: Tools/Demos -#.. section: C API - -# 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. -########################################################################### - - -""".lstrip() +from blurb._template import ( + next_filename_unsanitize_sections, sanitize_section, + sanitize_section_legacy, sections, template, unsanitize_section, +) root = None # Set by chdir_to_repo_root() original_dir = None -sections = [] - -for line in template.split('\n'): - line = line.strip() - prefix, found, section = line.partition("#.. section: ") - if found and not prefix: - sections.append(section.strip()) - - -_sanitize_section = { - "C API": "C_API", - "Core and Builtins": "Core_and_Builtins", - "Tools/Demos": "Tools-Demos", - } - - -def sanitize_section(section): - """ - Clean up a section string, making it viable as a directory name. - """ - return _sanitize_section.get(section, section) - - -def sanitize_section_legacy(section): - """ - Clean up a section string, making it viable as a directory name (allow spaces). - """ - return section.replace("/", "-") - - -_unsanitize_section = { - "C_API": "C API", - "Core_and_Builtins": "Core and Builtins", - "Tools-Demos": "Tools/Demos", - } - - -def unsanitize_section(section): - return _unsanitize_section.get(section, section) - -def next_filename_unsanitize_sections(filename): - for key, value in _unsanitize_section.items(): - for separator in "/\\": - key = f"{separator}{key}{separator}" - value = f"{separator}{value}{separator}" - filename = filename.replace(key, value) - return filename def textwrap_body(body, *, subsequent_indent=''): From c2f4185f8ea983f06157e327f60a0aea9cf9a079 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Wed, 13 Aug 2025 00:27:36 +0100 Subject: [PATCH 2/2] Split tests --- tests/test_blurb.py | 16 ---------------- tests/test_template.py | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 tests/test_template.py diff --git a/tests/test_blurb.py b/tests/test_blurb.py index e3da4b5..2339366 100644 --- a/tests/test_blurb.py +++ b/tests/test_blurb.py @@ -9,22 +9,6 @@ ) -def test_section_names(): - assert tuple(blurb.sections) == ( - 'Security', - 'Core and Builtins', - 'Library', - 'Documentation', - 'Tests', - 'Build', - 'Windows', - 'macOS', - 'IDLE', - 'Tools/Demos', - 'C API', - ) - - @pytest.mark.parametrize("section", UNCHANGED_SECTIONS) def test_sanitize_section_no_change(section): sanitized = blurb.sanitize_section(section) diff --git a/tests/test_template.py b/tests/test_template.py new file mode 100644 index 0000000..b9fe0d9 --- /dev/null +++ b/tests/test_template.py @@ -0,0 +1,17 @@ +import blurb._template + + +def test_section_names(): + assert tuple(blurb._template.sections) == ( + 'Security', + 'Core and Builtins', + 'Library', + 'Documentation', + 'Tests', + 'Build', + 'Windows', + 'macOS', + 'IDLE', + 'Tools/Demos', + 'C API', + )