Skip to content

Remove the @subcommand decorator #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2025
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
2 changes: 2 additions & 0 deletions src/blurb/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Run blurb using ``python3 -m blurb``."""

from __future__ import annotations

from blurb._cli import main

if __name__ == '__main__':
Expand Down
3 changes: 1 addition & 2 deletions src/blurb/_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import tempfile

from blurb._blurb_file import BlurbError, Blurbs
from blurb._cli import error, prompt, subcommand
from blurb._cli import error, prompt
from blurb._git import flush_git_add_files, git_add_files
from blurb._template import sections, template

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


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

Expand Down
40 changes: 26 additions & 14 deletions src/blurb/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@
readme_re = re.compile(r'This is \w+ version \d+\.\d+').match


def initialise_subcommands() -> None:
global subcommands

from blurb._add import add
from blurb._export import export
from blurb._merge import merge
from blurb._populate import populate
from blurb._release import release

subcommands = {
'version': version,
'help': help,
'add': add,
'export': export,
'merge': merge,
'populate': populate,
'release': release,

# Make 'blurb --help/--version/-V' work.
'--help': help,
'--version': version,
'-V': version,
}


def error(msg: str, /) -> NoReturn:
raise SystemExit(f'Error: {msg}')

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


def subcommand(fn: CommandFunc):
global subcommands
subcommands[fn.__name__] = fn
return fn


def get_subcommand(subcommand: str, /) -> CommandFunc:
fn = subcommands.get(subcommand)
if not fn:
error(f"Unknown subcommand: {subcommand}\nRun 'blurb help' for help.")
return fn


@subcommand
def version() -> None:
"""Print blurb version."""
print('blurb version', blurb.__version__)


@subcommand
def help(subcommand: str | None = None) -> None:
"""Print help for subcommands.

Expand Down Expand Up @@ -102,12 +119,6 @@ def help(subcommand: str | None = None) -> None:
raise SystemExit(0)


# Make 'blurb --help/--version/-V' work.
subcommands['--help'] = help
subcommands['--version'] = version
subcommands['-V'] = version


def _blurb_help() -> None:
"""Print default help for blurb."""

Expand Down Expand Up @@ -157,6 +168,7 @@ def main() -> None:
subcommand = args[0]
args = args[1:]

initialise_subcommands()
fn = get_subcommand(subcommand)

# hack
Expand Down
5 changes: 2 additions & 3 deletions src/blurb/_export.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

import os
import shutil

from blurb._cli import subcommand


@subcommand
def export() -> None:
"""Removes blurb data files, for building release tarballs/installers."""
os.chdir('Misc')
Expand Down
2 changes: 2 additions & 0 deletions src/blurb/_git.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
import subprocess

Expand Down
5 changes: 3 additions & 2 deletions src/blurb/_merge.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from __future__ import annotations

import os
import sys
from pathlib import Path

from blurb._blurb_file import Blurbs
from blurb._cli import require_ok, subcommand
from blurb._cli import require_ok
from blurb._utils.globs import glob_blurbs
from blurb._utils.text import textwrap_body
from blurb._versions import glob_versions, printable_version

original_dir: str = os.getcwd()


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

Expand Down
4 changes: 2 additions & 2 deletions src/blurb/_populate.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import os

from blurb._cli import subcommand
from blurb._git import flush_git_add_files, git_add_files
from blurb._template import sanitize_section, sections


@subcommand
def populate() -> None:
"""Creates and populates the Misc/NEWS.d directory tree."""
os.chdir('Misc')
Expand Down
3 changes: 1 addition & 2 deletions src/blurb/_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import blurb._blurb_file
from blurb._blurb_file import Blurbs
from blurb._cli import error, subcommand
from blurb._cli import error
from blurb._git import (
flush_git_add_files,
flush_git_rm_files,
Expand All @@ -16,7 +16,6 @@
from blurb._utils.text import generate_nonce


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

Expand Down
2 changes: 2 additions & 0 deletions src/blurb/_utils/globs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import glob
import os

Expand Down
2 changes: 2 additions & 0 deletions src/blurb/_versions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import glob
import sys

Expand Down