Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ass_tag_parser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from .ass_composer import compose_ass
from .ass_parser import ass_to_plaintext, parse_ass
from .ass_struct import *
Expand Down
1 change: 1 addition & 0 deletions ass_tag_parser/ass_composer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from dataclasses import dataclass

from ass_tag_parser.ass_struct import (
Expand Down
59 changes: 42 additions & 17 deletions ass_tag_parser/ass_parser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from __future__ import annotations
import re
from dataclasses import dataclass
from functools import cache
from typing import Any, Iterable, Optional, Union

import sys
sys_version=int(sys.version_info[1])
if sys_version >= 9:
from functools import cache
elif sys_version < 9:
pass

from ass_tag_parser.ass_struct import (
AssItem,
AssTag,
Expand Down Expand Up @@ -698,20 +705,38 @@ def parse_ass(text: str) -> list[AssItem]:
return list(_parse_ass(ctx))


@cache
def ass_to_plaintext(text: str) -> str:
"""Strip ASS tags from an ASS line.
if sys_version >= 9:
@cache
def ass_to_plaintext(text: str) -> str:
"""Strip ASS tags from an ASS line.

:param text: input ASS line
:return: plain text
"""
try:
ass_line = parse_ass(text)
except ParseError:
ret = str(re.sub("{[^}]*}", "", text))
else:
ret = ""
for item in ass_line:
if isinstance(item, AssText):
ret += item.text
return ret.replace("\\h", " ").replace("\\n", " ").replace("\\N", "\n")
:param text: input ASS line
:return: plain text
"""
try:
ass_line = parse_ass(text)
except ParseError:
ret = str(re.sub("{[^}]*}", "", text))
else:
ret = ""
for item in ass_line:
if isinstance(item, AssText):
ret += item.text
return ret.replace("\\h", " ").replace("\\n", " ").replace("\\N", "\n")
elif sys_version < 9:
def ass_to_plaintext(text: str) -> str:
"""Strip ASS tags from an ASS line.

:param text: input ASS line
:return: plain text
"""
try:
ass_line = parse_ass(text)
except ParseError:
ret = str(re.sub("{[^}]*}", "", text))
else:
ret = ""
for item in ass_line:
if isinstance(item, AssText):
ret += item.text
return ret.replace("\\h", " ").replace("\\n", " ").replace("\\N", "\n")
1 change: 1 addition & 0 deletions ass_tag_parser/ass_struct.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Optional

Expand Down
2 changes: 1 addition & 1 deletion ass_tag_parser/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Union


@dataclass
class Meta:
start: int
Expand Down
1 change: 1 addition & 0 deletions ass_tag_parser/draw_composer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from typing import Any

from ass_tag_parser.common import smart_float
Expand Down
14 changes: 8 additions & 6 deletions ass_tag_parser/draw_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Iterable, Optional, Union, cast

import sys
sys_version=int(sys.version_info[1])

from ass_tag_parser.common import Meta
from ass_tag_parser.draw_struct import (
AssDrawCmd,
Expand Down Expand Up @@ -81,12 +85,10 @@ def _parse_draw_commands(ctx: _ParseContext) -> Iterable[AssDrawCmd]:
elif cmd == "l":
ret = AssDrawCmdLine(list(_read_points(ctx.io, min_count=1)))
elif cmd == "b":
ret = AssDrawCmdBezier(
cast(
tuple[AssDrawPoint, AssDrawPoint, AssDrawPoint],
tuple(_read_points(ctx.io, min_count=3, max_count=3)),
)
)
if sys_version >= 9:
ret = AssDrawCmdBezier(cast(tuple[AssDrawPoint, AssDrawPoint, AssDrawPoint], tuple(_read_points(ctx.io, min_count=3, max_count=3)),))
elif sys_version < 9:
ret = AssDrawCmdBezier(cast(tuple, tuple(_read_points(ctx.io, min_count=3, max_count=3)),))
elif cmd == "s":
ret = AssDrawCmdSpline(
list(_read_points(ctx.io, min_count=3, max_count=None))
Expand Down
1 change: 1 addition & 0 deletions ass_tag_parser/draw_struct.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional

Expand Down
2 changes: 1 addition & 1 deletion ass_tag_parser/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
from typing import Optional


class BaseError(Exception):
pass

Expand Down
2 changes: 1 addition & 1 deletion ass_tag_parser/io.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
import io
from typing import Optional


class MyIO:
def __init__(
self,
Expand Down