Skip to content
Merged
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
84 changes: 84 additions & 0 deletions src/blurb/_template.py
Original file line number Diff line number Diff line change
@@ -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 #<n>: " or "- gh-issue-<n>: " 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
87 changes: 4 additions & 83 deletions src/blurb/blurb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #<n>: " or "- gh-issue-<n>: " 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=''):
Expand Down
16 changes: 0 additions & 16 deletions tests/test_blurb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_template.py
Original file line number Diff line number Diff line change
@@ -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',
)