Skip to content

Commit 23b5f53

Browse files
authored
Remove the @subcommand decorator (#64)
1 parent 7b7e7aa commit 23b5f53

File tree

10 files changed

+43
-25
lines changed

10 files changed

+43
-25
lines changed

src/blurb/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Run blurb using ``python3 -m blurb``."""
22

3+
from __future__ import annotations
4+
35
from blurb._cli import main
46

57
if __name__ == '__main__':

src/blurb/_add.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import tempfile
1010

1111
from blurb._blurb_file import BlurbError, Blurbs
12-
from blurb._cli import error, prompt, subcommand
12+
from blurb._cli import error, prompt
1313
from blurb._git import flush_git_add_files, git_add_files
1414
from blurb._template import sections, template
1515

@@ -23,7 +23,6 @@
2323
FALLBACK_EDITORS = ('/etc/alternatives/editor', 'nano')
2424

2525

26-
@subcommand
2726
def add(*, issue: str | None = None, section: str | None = None):
2827
"""Add a blurb (a Misc/NEWS.d/next entry) to the current CPython repo.
2928

src/blurb/_cli.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@
1919
readme_re = re.compile(r'This is \w+ version \d+\.\d+').match
2020

2121

22+
def initialise_subcommands() -> None:
23+
global subcommands
24+
25+
from blurb._add import add
26+
from blurb._export import export
27+
from blurb._merge import merge
28+
from blurb._populate import populate
29+
from blurb._release import release
30+
31+
subcommands = {
32+
'version': version,
33+
'help': help,
34+
'add': add,
35+
'export': export,
36+
'merge': merge,
37+
'populate': populate,
38+
'release': release,
39+
40+
# Make 'blurb --help/--version/-V' work.
41+
'--help': help,
42+
'--version': version,
43+
'-V': version,
44+
}
45+
46+
2247
def error(msg: str, /) -> NoReturn:
2348
raise SystemExit(f'Error: {msg}')
2449

@@ -35,26 +60,18 @@ def require_ok(prompt: str, /) -> str:
3560
return s
3661

3762

38-
def subcommand(fn: CommandFunc):
39-
global subcommands
40-
subcommands[fn.__name__] = fn
41-
return fn
42-
43-
4463
def get_subcommand(subcommand: str, /) -> CommandFunc:
4564
fn = subcommands.get(subcommand)
4665
if not fn:
4766
error(f"Unknown subcommand: {subcommand}\nRun 'blurb help' for help.")
4867
return fn
4968

5069

51-
@subcommand
5270
def version() -> None:
5371
"""Print blurb version."""
5472
print('blurb version', blurb.__version__)
5573

5674

57-
@subcommand
5875
def help(subcommand: str | None = None) -> None:
5976
"""Print help for subcommands.
6077
@@ -102,12 +119,6 @@ def help(subcommand: str | None = None) -> None:
102119
raise SystemExit(0)
103120

104121

105-
# Make 'blurb --help/--version/-V' work.
106-
subcommands['--help'] = help
107-
subcommands['--version'] = version
108-
subcommands['-V'] = version
109-
110-
111122
def _blurb_help() -> None:
112123
"""Print default help for blurb."""
113124

@@ -157,6 +168,7 @@ def main() -> None:
157168
subcommand = args[0]
158169
args = args[1:]
159170

171+
initialise_subcommands()
160172
fn = get_subcommand(subcommand)
161173

162174
# hack

src/blurb/_export.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
from __future__ import annotations
2+
13
import os
24
import shutil
35

4-
from blurb._cli import subcommand
5-
66

7-
@subcommand
87
def export() -> None:
98
"""Removes blurb data files, for building release tarballs/installers."""
109
os.chdir('Misc')

src/blurb/_git.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import subprocess
35

src/blurb/_merge.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
from __future__ import annotations
2+
13
import os
24
import sys
35
from pathlib import Path
46

57
from blurb._blurb_file import Blurbs
6-
from blurb._cli import require_ok, subcommand
8+
from blurb._cli import require_ok
79
from blurb._utils.globs import glob_blurbs
810
from blurb._utils.text import textwrap_body
911
from blurb._versions import glob_versions, printable_version
1012

1113
original_dir: str = os.getcwd()
1214

1315

14-
@subcommand
1516
def merge(output: str | None = None, *, forced: bool = False) -> None:
1617
"""Merge all blurbs together into a single Misc/NEWS file.
1718

src/blurb/_populate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
from __future__ import annotations
2+
13
import os
24

3-
from blurb._cli import subcommand
45
from blurb._git import flush_git_add_files, git_add_files
56
from blurb._template import sanitize_section, sections
67

78

8-
@subcommand
99
def populate() -> None:
1010
"""Creates and populates the Misc/NEWS.d directory tree."""
1111
os.chdir('Misc')

src/blurb/_release.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import blurb._blurb_file
77
from blurb._blurb_file import Blurbs
8-
from blurb._cli import error, subcommand
8+
from blurb._cli import error
99
from blurb._git import (
1010
flush_git_add_files,
1111
flush_git_rm_files,
@@ -16,7 +16,6 @@
1616
from blurb._utils.text import generate_nonce
1717

1818

19-
@subcommand
2019
def release(version: str) -> None:
2120
"""Move all new blurbs to a single blurb file for the release.
2221

src/blurb/_utils/globs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import glob
24
import os
35

src/blurb/_versions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import glob
24
import sys
35

0 commit comments

Comments
 (0)