Skip to content

Commit 27fcb51

Browse files
committed
Move template and section utilities to blurb._template
1 parent d7ee9e4 commit 27fcb51

2 files changed

Lines changed: 88 additions & 83 deletions

File tree

src/blurb/_template.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from __future__ import annotations
2+
3+
#
4+
# This template is the canonical list of acceptable section names!
5+
# It's parsed internally into the "sections" set.
6+
#
7+
8+
template = """
9+
10+
#
11+
# Please enter the relevant GitHub issue number here:
12+
#
13+
.. gh-issue:
14+
15+
#
16+
# Uncomment one of these "section:" lines to specify which section
17+
# this entry should go in in Misc/NEWS.d.
18+
#
19+
#.. section: Security
20+
#.. section: Core and Builtins
21+
#.. section: Library
22+
#.. section: Documentation
23+
#.. section: Tests
24+
#.. section: Build
25+
#.. section: Windows
26+
#.. section: macOS
27+
#.. section: IDLE
28+
#.. section: Tools/Demos
29+
#.. section: C API
30+
31+
# Write your Misc/NEWS.d entry below. It should be a simple ReST paragraph.
32+
# Don't start with "- Issue #<n>: " or "- gh-issue-<n>: " or that sort of stuff.
33+
###########################################################################
34+
35+
36+
""".lstrip()
37+
38+
sections: list[str] = []
39+
for line in template.split('\n'):
40+
line = line.strip()
41+
prefix, found, section = line.partition('#.. section: ')
42+
if found and not prefix:
43+
sections.append(section.strip())
44+
45+
_sanitize_section = {
46+
'C API': 'C_API',
47+
'Core and Builtins': 'Core_and_Builtins',
48+
'Tools/Demos': 'Tools-Demos',
49+
}
50+
51+
_unsanitize_section = {
52+
'C_API': 'C API',
53+
'Core_and_Builtins': 'Core and Builtins',
54+
'Tools-Demos': 'Tools/Demos',
55+
}
56+
57+
58+
def sanitize_section(section: str, /) -> str:
59+
"""Clean up a section string.
60+
61+
This makes it viable as a directory name.
62+
"""
63+
return _sanitize_section.get(section, section)
64+
65+
66+
def sanitize_section_legacy(section: str, /) -> str:
67+
"""Clean up a section string, allowing spaces.
68+
69+
This makes it viable as a directory name.
70+
"""
71+
return section.replace('/', '-')
72+
73+
74+
def unsanitize_section(section: str, /) -> str:
75+
return _unsanitize_section.get(section, section)
76+
77+
78+
def next_filename_unsanitize_sections(filename: str, /) -> str:
79+
for key, value in _unsanitize_section.items():
80+
for separator in ('/', '\\'):
81+
key = f'{separator}{key}{separator}'
82+
value = f'{separator}{value}{separator}'
83+
filename = filename.replace(key, value)
84+
return filename

src/blurb/blurb.py

Lines changed: 4 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -58,92 +58,13 @@
5858
import time
5959

6060
from blurb._cli import main, subcommand
61-
62-
63-
#
64-
# This template is the canonical list of acceptable section names!
65-
# It's parsed internally into the "sections" set.
66-
#
67-
68-
template = """
69-
70-
#
71-
# Please enter the relevant GitHub issue number here:
72-
#
73-
.. gh-issue:
74-
75-
#
76-
# Uncomment one of these "section:" lines to specify which section
77-
# this entry should go in in Misc/NEWS.d.
78-
#
79-
#.. section: Security
80-
#.. section: Core and Builtins
81-
#.. section: Library
82-
#.. section: Documentation
83-
#.. section: Tests
84-
#.. section: Build
85-
#.. section: Windows
86-
#.. section: macOS
87-
#.. section: IDLE
88-
#.. section: Tools/Demos
89-
#.. section: C API
90-
91-
# Write your Misc/NEWS.d entry below. It should be a simple ReST paragraph.
92-
# Don't start with "- Issue #<n>: " or "- gh-issue-<n>: " or that sort of stuff.
93-
###########################################################################
94-
95-
96-
""".lstrip()
61+
from blurb._template import (
62+
next_filename_unsanitize_sections, sanitize_section,
63+
sanitize_section_legacy, sections, template, unsanitize_section,
64+
)
9765

9866
root = None # Set by chdir_to_repo_root()
9967
original_dir = None
100-
sections = []
101-
102-
for line in template.split('\n'):
103-
line = line.strip()
104-
prefix, found, section = line.partition("#.. section: ")
105-
if found and not prefix:
106-
sections.append(section.strip())
107-
108-
109-
_sanitize_section = {
110-
"C API": "C_API",
111-
"Core and Builtins": "Core_and_Builtins",
112-
"Tools/Demos": "Tools-Demos",
113-
}
114-
115-
116-
def sanitize_section(section):
117-
"""
118-
Clean up a section string, making it viable as a directory name.
119-
"""
120-
return _sanitize_section.get(section, section)
121-
122-
123-
def sanitize_section_legacy(section):
124-
"""
125-
Clean up a section string, making it viable as a directory name (allow spaces).
126-
"""
127-
return section.replace("/", "-")
128-
129-
130-
_unsanitize_section = {
131-
"C_API": "C API",
132-
"Core_and_Builtins": "Core and Builtins",
133-
"Tools-Demos": "Tools/Demos",
134-
}
135-
136-
137-
def unsanitize_section(section):
138-
return _unsanitize_section.get(section, section)
139-
140-
def next_filename_unsanitize_sections(filename):
141-
for key, value in _unsanitize_section.items():
142-
for separator in "/\\":
143-
key = f"{separator}{key}{separator}"
144-
value = f"{separator}{value}{separator}"
145-
filename = filename.replace(key, value)
146-
return filename
14768

14869

14970
def textwrap_body(body, *, subsequent_indent=''):

0 commit comments

Comments
 (0)