-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Utilities for srt translation, including srt wrap. | ||
""" | ||
|
||
import subprocess | ||
import warnings | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
from isolated_environment import isolated_environment # type: ignore | ||
|
||
HERE = Path(__file__).parent | ||
WRAP_SRT_PY = HERE / "srt_wrap.py" | ||
|
||
|
||
def get_environment() -> dict[str, Any]: | ||
"""Returns the environment.""" | ||
venv_dir = HERE / "venv" / "srttranslator" | ||
env = isolated_environment(venv_dir, ["srtranslator==0.2.6"]) | ||
return env | ||
|
||
|
||
def srt_wrap_to_string(srt_file: Path) -> str: | ||
"""Wrap lines in a srt file.""" | ||
env = get_environment() | ||
process = subprocess.run( | ||
["python", str(WRAP_SRT_PY), str(srt_file)], | ||
env=env, | ||
capture_output=True, | ||
text=True, | ||
shell=True, | ||
check=True, | ||
) | ||
out = process.stdout | ||
return out | ||
|
||
|
||
def srt_wrap(srt_file: Path) -> None: | ||
"""Wrap lines in a srt file.""" | ||
try: | ||
assert WRAP_SRT_PY.exists() | ||
out = srt_wrap_to_string(srt_file) | ||
srt_file.write_text(out, encoding="utf-8") | ||
except subprocess.CalledProcessError as exc: | ||
warnings.warn(f"Failed to run srt_wrap: {exc}") | ||
return |