Skip to content

Commit

Permalink
Refactor CLI with Typer and add new export options
Browse files Browse the repository at this point in the history
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
GitBib committed Sep 1, 2024
1 parent 1c095d5 commit 50bed7c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13.0-rc.1"]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- id: detect-private-key

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.2
rev: v0.6.3
hooks:
- id: ruff
args: [ --fix ]
Expand Down
23 changes: 0 additions & 23 deletions batch.py

This file was deleted.

108 changes: 108 additions & 0 deletions pyasstosrt/batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from pathlib import Path
from typing import Optional, List

Check warning on line 2 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L1-L2

Added lines #L1 - L2 were not covered by tests

try:
import typer
from rich.console import Console
from rich.panel import Panel
from rich.progress import Progress
except ModuleNotFoundError as e:
raise ImportError(

Check warning on line 10 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L4-L10

Added lines #L4 - L10 were not covered by tests
"pyasstosrt was installed without the cli extra. Please reinstall it with: pip install 'pyasstosrt[cli]'"
) from e

from pyasstosrt import Subtitle, __version__

Check warning on line 14 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L14

Added line #L14 was not covered by tests

app = typer.Typer(help="Convert ASS subtitles to SRT format")
console = Console()

Check warning on line 17 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L16-L17

Added lines #L16 - L17 were not covered by tests


def version_callback(value: bool):
if value:
console.print(f"[bold green]PyAssToSrt[/bold green] version: {__version__}")
raise typer.Exit()

Check warning on line 23 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L20-L23

Added lines #L20 - L23 were not covered by tests


@app.callback()
def callback(

Check warning on line 27 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L26-L27

Added lines #L26 - L27 were not covered by tests
version: Optional[bool] = typer.Option(
None, "--version", "-v", callback=version_callback, help="Show version and exit"
),
):
"""
PyAssToSrt - Convert ASS subtitles to SRT format
"""
pass

Check warning on line 35 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L35

Added line #L35 was not covered by tests


@app.command()
def export(

Check warning on line 39 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L38-L39

Added lines #L38 - L39 were not covered by tests
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))

Check warning on line 72 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L71-L72

Added lines #L71 - L72 were not covered by tests

for file in filepath:
progress.console.print(f"\n[bold blue]Processing: {file.name}[/bold blue]")

Check warning on line 75 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L74-L75

Added lines #L74 - L75 were not covered by tests

try:
sub = Subtitle(file, removing_effects, remove_duplicates)
result = sub.export(output_dir, encoding, output_dialogues)

Check warning on line 79 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L77-L79

Added lines #L77 - L79 were not covered by tests

if output_dialogues:
progress.console.print(

Check warning on line 82 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L81-L82

Added lines #L81 - L82 were not covered by tests
Panel(f"Dialogues for {file.name}:", expand=False)
)
for dialogue in result:
progress.console.print(str(dialogue))

Check warning on line 86 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L85-L86

Added lines #L85 - L86 were not covered by tests

output_file = (

Check warning on line 88 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L88

Added line #L88 was not covered by tests
Path(output_dir) / f"{file.stem}.srt"
if output_dir
else file.with_suffix(".srt")
)
progress.console.print(

Check warning on line 93 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L93

Added line #L93 was not covered by tests
f"[green]Success:[/green] Converted {file.name} to {output_file}"
)
except Exception as e:
progress.console.print(

Check warning on line 97 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L96-L97

Added lines #L96 - L97 were not covered by tests
f"[red]Error:[/red] Failed to convert {file.name}. {str(e)}",
style="bold red",
)

progress.update(task, advance=1)

Check warning on line 102 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L102

Added line #L102 was not covered by tests

console.print("\n[bold green]Conversion completed![/bold green]")

Check warning on line 104 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L104

Added line #L104 was not covered by tests


if __name__ == "__main__":
app()

Check warning on line 108 in pyasstosrt/batch.py

View check run for this annotation

Codecov / codecov/patch

pyasstosrt/batch.py#L107-L108

Added lines #L107 - L108 were not covered by tests
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ all = true
include = ["pyasstosrt", "tests"]

[tool.poetry.scripts]
pyasstosrt = "batch:app"
pyasstosrt = "pyasstosrt.batch:app"

0 comments on commit 50bed7c

Please sign in to comment.