-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CLI with Typer and add new export options
Replaced the previous CLI implementation with Typer for better functionality and added new export options including removing effects, removing duplicates, and outputting dialogues. Updated dependencies and configurations to support this new structure, adjusted script paths, and included support for Python 3.13.0-rc.1 in GitHub workflows.
- Loading branch information
Showing
5 changed files
with
111 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from pathlib import Path | ||
from typing import Optional, List | ||
|
||
try: | ||
import typer | ||
from rich.console import Console | ||
from rich.panel import Panel | ||
from rich.progress import Progress | ||
except ModuleNotFoundError as e: | ||
raise ImportError( | ||
"pyasstosrt was installed without the cli extra. Please reinstall it with: pip install 'pyasstosrt[cli]'" | ||
) from e | ||
|
||
from pyasstosrt import Subtitle, __version__ | ||
|
||
app = typer.Typer(help="Convert ASS subtitles to SRT format") | ||
console = Console() | ||
|
||
|
||
def version_callback(value: bool): | ||
if value: | ||
console.print(f"[bold green]PyAssToSrt[/bold green] version: {__version__}") | ||
raise typer.Exit() | ||
|
||
|
||
@app.callback() | ||
def callback( | ||
version: Optional[bool] = typer.Option( | ||
None, "--version", "-v", callback=version_callback, help="Show version and exit" | ||
), | ||
): | ||
""" | ||
PyAssToSrt - Convert ASS subtitles to SRT format | ||
""" | ||
pass | ||
|
||
|
||
@app.command() | ||
def export( | ||
filepath: List[Path] = typer.Argument( | ||
..., | ||
help="Path to the ASS file(s)", | ||
exists=True, | ||
file_okay=True, | ||
dir_okay=False, | ||
readable=True, | ||
), | ||
removing_effects: bool = typer.Option( | ||
False, "--remove-effects", "-r", help="Remove effects from subtitles" | ||
), | ||
remove_duplicates: bool = typer.Option( | ||
False, "--remove-duplicates", "-d", help="Remove duplicate subtitles" | ||
), | ||
output_dir: Optional[Path] = typer.Option( | ||
None, | ||
"--output-dir", | ||
"-o", | ||
help="Output directory for the SRT file(s)", | ||
file_okay=False, | ||
dir_okay=True, | ||
writable=True, | ||
), | ||
encoding: str = typer.Option( | ||
"utf8", "--encoding", "-e", help="Encoding for the output file" | ||
), | ||
output_dialogues: bool = typer.Option( | ||
False, "--output-dialogues", "-p", help="Print dialogues to console" | ||
), | ||
): | ||
"""Convert ASS subtitle file(s) to SRT format""" | ||
with Progress() as progress: | ||
task = progress.add_task("[green]Converting...", total=len(filepath)) | ||
|
||
for file in filepath: | ||
progress.console.print(f"\n[bold blue]Processing: {file.name}[/bold blue]") | ||
|
||
try: | ||
sub = Subtitle(file, removing_effects, remove_duplicates) | ||
result = sub.export(output_dir, encoding, output_dialogues) | ||
|
||
if output_dialogues: | ||
progress.console.print( | ||
Panel(f"Dialogues for {file.name}:", expand=False) | ||
) | ||
for dialogue in result: | ||
progress.console.print(str(dialogue)) | ||
|
||
output_file = ( | ||
Path(output_dir) / f"{file.stem}.srt" | ||
if output_dir | ||
else file.with_suffix(".srt") | ||
) | ||
progress.console.print( | ||
f"[green]Success:[/green] Converted {file.name} to {output_file}" | ||
) | ||
except Exception as e: | ||
progress.console.print( | ||
f"[red]Error:[/red] Failed to convert {file.name}. {str(e)}", | ||
style="bold red", | ||
) | ||
|
||
progress.update(task, advance=1) | ||
|
||
console.print("\n[bold green]Conversion completed![/bold green]") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters