Files
mytoolkit/bin/utils.py
T
Zhengshou Lai ac605c2c17 feat(mytoolkit): add voice command and improve docx post-processing
- Add voice/TTS CLI command via Volcengine WebSocket API
- Inject suppressAutoHyphens into all docx paragraphs in post-process
- Apply post-process fixes to all templates (not just default)
- Update review template: Cambria+SimSun fonts, 11pt for headings
- Sync default/review docx template formatting
2026-05-27 16:17:47 +08:00

47 lines
1.3 KiB
Python

"""Utility functions and decorators for bin."""
import functools
import subprocess
import sys
import click
def handle_errors(func):
"""Decorator to handle common errors gracefully."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except FileNotFoundError as e:
click.secho(f"Command not found: {e.filename}", fg="red", err=True)
sys.exit(1)
except subprocess.CalledProcessError as e:
click.secho(f"Command failed with exit code {e.returncode}", fg="red", err=True)
if e.stderr:
click.echo(e.stderr, err=True)
sys.exit(1)
except RuntimeError as e:
click.secho(str(e), fg="red", err=True)
sys.exit(1)
except KeyboardInterrupt:
click.echo("\nAborted.")
sys.exit(130)
return wrapper
def run_command(cmd: list[str], check: bool = True, **kwargs) -> subprocess.CompletedProcess:
"""Run a subprocess command with consistent error handling.
Args:
cmd: Command and arguments as list
check: Whether to check return code
**kwargs: Additional args for subprocess.run
Returns:
CompletedProcess instance
"""
return subprocess.run(cmd, check=check, **kwargs)