aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/check.yml5
-rw-r--r--.gitignore1
-rw-r--r--Justfile9
-rw-r--r--README.md3
-rw-r--r--pyproject.toml27
-rw-r--r--pyrightconfig.json5
-rw-r--r--src/srtfilter/__init__.py0
-rw-r--r--src/srtfilter/__main__.py4
-rw-r--r--[-rwxr-xr-x]src/srtfilter/filters/rebreak_lines.py (renamed from rebreak_lines.py)31
-rw-r--r--src/srtfilter/parse.py (renamed from parse_srt.py)0
-rwxr-xr-xsrc/srtfilter/srtfilter_cli.py32
11 files changed, 87 insertions, 30 deletions
diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml
index 3b19e6b..7d40006 100644
--- a/.github/workflows/check.yml
+++ b/.github/workflows/check.yml
@@ -8,6 +8,5 @@ jobs:
with:
python-version: "3.12"
- uses: taiki-e/install-action@just
- - run: |
- pip install -r requirements.txt
- just check
+ - run: pip install -r requirements.txt
+ - run: just check
diff --git a/.gitignore b/.gitignore
index 1c43c45..9ba1833 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
/venv/
__pycache__/
+/dist/
diff --git a/Justfile b/Justfile
index 5c62e93..76bebd4 100644
--- a/Justfile
+++ b/Justfile
@@ -1,7 +1,10 @@
-check: typecheck lint
+check: typecheck check_style
typecheck:
pyright
-lint:
- black --check *.py
+check_style:
+ black --check src
+
+format:
+ black src
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..a9c5262
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# srtfilter
+
+Parser for the SubRip/SRT format and framework for filters that modify SRT files.
diff --git a/pyproject.toml b/pyproject.toml
new file mode 100644
index 0000000..dc0a102
--- /dev/null
+++ b/pyproject.toml
@@ -0,0 +1,27 @@
+[project]
+name = "srtfilter"
+version = "0.1.0"
+authors = [
+ { name="Arjun Satarkar", email="me@arjunsatarkar.net" },
+]
+requires-python = ">=3.12"
+readme = "README.md"
+classifiers = [
+ "Programming Language :: Python :: 3",
+ "License :: OSI Approved :: MIT License",
+ "Operating System :: OS Independent",
+]
+dependencies = [
+ "click==8.1.*"
+]
+
+[project.scripts]
+srtfilter = "srtfilter.srtfilter_cli:main"
+
+[project.urls]
+Homepage = "https://github.com/arjunsatarkar/srtfilter"
+Issues = "https://github.com/arjunsatarkar/srtfilter/issues"
+
+[build-system]
+requires = ["hatchling"]
+build-backend = "hatchling.build"
diff --git a/pyrightconfig.json b/pyrightconfig.json
index 864fa90..5bd770c 100644
--- a/pyrightconfig.json
+++ b/pyrightconfig.json
@@ -1,3 +1,4 @@
{
- "strict": ["."]
-} \ No newline at end of file
+ "include": ["src"],
+ "strict": ["src"]
+}
diff --git a/src/srtfilter/__init__.py b/src/srtfilter/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/srtfilter/__init__.py
diff --git a/src/srtfilter/__main__.py b/src/srtfilter/__main__.py
new file mode 100644
index 0000000..16b5050
--- /dev/null
+++ b/src/srtfilter/__main__.py
@@ -0,0 +1,4 @@
+if __name__ == "__main__":
+ from .srtfilter_cli import main
+
+ main()
diff --git a/rebreak_lines.py b/src/srtfilter/filters/rebreak_lines.py
index 54473e2..5c94aa6 100755..100644
--- a/rebreak_lines.py
+++ b/src/srtfilter/filters/rebreak_lines.py
@@ -1,35 +1,26 @@
#!/usr/bin/env python3
"""
-Given an SRT file, remove existing line breaks and reinsert them automatically
-at word boundaries so as not to exceed 42 characters per line, trying to keep
-lines within a single subtitle event at roughly the same length.
+Given an SRT event, remove existing line breaks and insert new ones. All
+insertion will be at word boundaries, lines will usually not exceed 42
+characters, and lines within the event will if possible be roughly the same
+length.
-NOTE: Using this script is *generally a bad idea*; like many aspects of
+NOTE: Using this filter is *generally a bad idea*; like many aspects of
subtitling, placing line breaks benefits from contextual judgement. However,
if an existing subtitle file has no line breaks or far too many, as is the case
sometimes, this is an easy way to improve readability.
"""
-import click
-import parse_srt
import math
-import sys
import typing
+from .. import parse
# May still be exceeded if there are no word boundaries to wrap at
MAX_LINE_LENGTH = 42
-@click.command()
-@click.argument("in_file_path")
-def main(in_file_path: str):
- with open(in_file_path) as f:
- text = f.read()
- srt = parse_srt.SRT.from_str(text)
-
- for event in srt.events:
- event.content = rebreak(event.content)
-
- sys.stdout.write(str(srt))
+def filter(event: parse.Event) -> parse.Event:
+ event.content = rebreak(event.content)
+ return event
def rebreak(text: str) -> str:
@@ -64,7 +55,3 @@ def rebreak(text: str) -> str:
target_line_num = get_target_line_num(len(text))
return ("\n".join(lines) if lines else text) + "\n"
-
-
-if __name__ == "__main__":
- main()
diff --git a/parse_srt.py b/src/srtfilter/parse.py
index 2e710ea..2e710ea 100644
--- a/parse_srt.py
+++ b/src/srtfilter/parse.py
diff --git a/src/srtfilter/srtfilter_cli.py b/src/srtfilter/srtfilter_cli.py
new file mode 100755
index 0000000..bc44409
--- /dev/null
+++ b/src/srtfilter/srtfilter_cli.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+import click
+import sys
+from . import parse
+from .filters import rebreak_lines
+
+
+@click.command()
+@click.argument("in_file_path")
+@click.option("--filter", "filter_arg", default="")
+def main(in_file_path: str, filter_arg: str):
+ with open(in_file_path) as f:
+ text = f.read()
+ srt = parse.SRT.from_str(text)
+
+ for filter_name in filter_arg.split():
+ match filter_name:
+ case "rebreak_lines":
+ filter_module = rebreak_lines
+ case unknown:
+ raise InvalidFilterError(unknown)
+ srt.events = [filter_module.filter(event) for event in srt.events]
+
+ sys.stdout.write(str(srt))
+
+
+class InvalidFilterError(Exception):
+ pass
+
+
+if __name__ == "__main__":
+ main()