diff options
author | panni <[email protected]> | 2019-05-12 06:23:46 +0200 |
---|---|---|
committer | panni <[email protected]> | 2019-05-12 06:23:46 +0200 |
commit | 927b7b640661617f764f687967ea6e21078fb8fa (patch) | |
tree | 816c9c400a8bcdd4cc02d17f64e0e24028d3ccf0 /libs/pysubs2 | |
parent | 702963901482bc140d43d5d6cf78ff2b78e0033a (diff) | |
download | bazarr-927b7b640661617f764f687967ea6e21078fb8fa.tar.gz bazarr-927b7b640661617f764f687967ea6e21078fb8fa.zip |
core: update to subliminal_patch:head; fix subscene; solve cf almost instantly; fix chinese subs; fix titlovi;
Diffstat (limited to 'libs/pysubs2')
-rw-r--r-- | libs/pysubs2/cli.py | 10 | ||||
-rw-r--r-- | libs/pysubs2/common.py | 4 | ||||
-rw-r--r-- | libs/pysubs2/formats.py | 4 | ||||
-rw-r--r-- | libs/pysubs2/ssastyle.py | 2 | ||||
-rw-r--r-- | libs/pysubs2/subrip.py | 10 | ||||
-rw-r--r-- | libs/pysubs2/substation.py | 27 |
6 files changed, 38 insertions, 19 deletions
diff --git a/libs/pysubs2/cli.py b/libs/pysubs2/cli.py index f28cfcba6..fc82bf9b5 100644 --- a/libs/pysubs2/cli.py +++ b/libs/pysubs2/cli.py @@ -163,3 +163,13 @@ class Pysubs2CLI(object): elif args.transform_framerate is not None: in_fps, out_fps = args.transform_framerate subs.transform_framerate(in_fps, out_fps) + + +def __main__(): + cli = Pysubs2CLI() + rv = cli(sys.argv[1:]) + sys.exit(rv) + + +if __name__ == "__main__": + __main__() diff --git a/libs/pysubs2/common.py b/libs/pysubs2/common.py index 08738eb5c..2f95ccf44 100644 --- a/libs/pysubs2/common.py +++ b/libs/pysubs2/common.py @@ -17,12 +17,14 @@ class Color(_Color): return _Color.__new__(cls, r, g, b, a) #: Version of the pysubs2 library. -VERSION = "0.2.1" +VERSION = "0.2.3" PY3 = sys.version_info.major == 3 if PY3: text_type = str + binary_string_type = bytes else: text_type = unicode + binary_string_type = str diff --git a/libs/pysubs2/formats.py b/libs/pysubs2/formats.py index 03fba8e60..5c25a6e96 100644 --- a/libs/pysubs2/formats.py +++ b/libs/pysubs2/formats.py @@ -3,7 +3,7 @@ from .microdvd import MicroDVDFormat from .subrip import SubripFormat from .jsonformat import JSONFormat from .substation import SubstationFormat -from .txt_generic import TXTGenericFormat, MPL2Format +from .mpl2 import MPL2Format from .exceptions import * #: Dict mapping file extensions to format identifiers. @@ -13,7 +13,6 @@ FILE_EXTENSION_TO_FORMAT_IDENTIFIER = { ".ssa": "ssa", ".sub": "microdvd", ".json": "json", - ".txt": "txt_generic", } #: Dict mapping format identifiers to implementations (FormatBase subclasses). @@ -23,7 +22,6 @@ FORMAT_IDENTIFIER_TO_FORMAT_CLASS = { "ssa": SubstationFormat, "microdvd": MicroDVDFormat, "json": JSONFormat, - "txt_generic": TXTGenericFormat, "mpl2": MPL2Format, } diff --git a/libs/pysubs2/ssastyle.py b/libs/pysubs2/ssastyle.py index 522f8ce0d..2fcadc7ed 100644 --- a/libs/pysubs2/ssastyle.py +++ b/libs/pysubs2/ssastyle.py @@ -78,7 +78,7 @@ class SSAStyle(object): s += "%rpx " % self.fontsize if self.bold: s += "bold " if self.italic: s += "italic " - s += "'%s'>" % self.fontname + s += "{!r}>".format(self.fontname) if not PY3: s = s.encode("utf-8") return s diff --git a/libs/pysubs2/subrip.py b/libs/pysubs2/subrip.py index 7fa3f29b2..fea4eade6 100644 --- a/libs/pysubs2/subrip.py +++ b/libs/pysubs2/subrip.py @@ -46,8 +46,16 @@ class SubripFormat(FormatBase): following_lines[-1].append(line) def prepare_text(lines): + # Handle the "happy" empty subtitle case, which is timestamp line followed by blank line(s) + # followed by number line and timestamp line of the next subtitle. Fixes issue #11. + if (len(lines) >= 2 + and all(re.match("\s*$", line) for line in lines[:-1]) + and re.match("\s*\d+\s*$", lines[-1])): + return "" + + # Handle the general case. s = "".join(lines).strip() - s = re.sub(r"\n* *\d+ *$", "", s) # strip number of next subtitle + s = re.sub(r"\n+ *\d+ *$", "", s) # strip number of next subtitle s = re.sub(r"< *i *>", r"{\i1}", s) s = re.sub(r"< */ *i *>", r"{\i0}", s) s = re.sub(r"< *s *>", r"{\s1}", s) diff --git a/libs/pysubs2/substation.py b/libs/pysubs2/substation.py index fc4172a49..f810a4776 100644 --- a/libs/pysubs2/substation.py +++ b/libs/pysubs2/substation.py @@ -4,7 +4,7 @@ from numbers import Number from .formatbase import FormatBase from .ssaevent import SSAEvent from .ssastyle import SSAStyle -from .common import text_type, Color +from .common import text_type, Color, PY3, binary_string_type from .time import make_time, ms_to_times, timestamp_to_ms, TIMESTAMP SSA_ALIGNMENT = (1, 2, 3, 9, 10, 11, 5, 6, 7) @@ -150,14 +150,7 @@ class SubstationFormat(FormatBase): if format_ == "ass": return ass_rgba_to_color(v) else: - try: - return ssa_rgb_to_color(v) - except ValueError: - try: - return ass_rgba_to_color(v) - except: - return Color(255, 255, 255, 0) - + return ssa_rgb_to_color(v) elif f in {"bold", "underline", "italic", "strikeout"}: return v == "-1" elif f in {"borderstyle", "encoding", "marginl", "marginr", "marginv", "layer", "alphalevel"}: @@ -229,7 +222,7 @@ class SubstationFormat(FormatBase): for k, v in subs.aegisub_project.items(): print(k, v, sep=": ", file=fp) - def field_to_string(f, v): + def field_to_string(f, v, line): if f in {"start", "end"}: return ms_to_timestamp(v) elif f == "marked": @@ -240,23 +233,31 @@ class SubstationFormat(FormatBase): return "-1" if v else "0" elif isinstance(v, (text_type, Number)): return text_type(v) + elif not PY3 and isinstance(v, binary_string_type): + # A convenience feature, see issue #12 - accept non-unicode strings + # when they are ASCII; this is useful in Python 2, especially for non-text + # fields like style names, where requiring Unicode type seems too stringent + if all(ord(c) < 128 for c in v): + return text_type(v) + else: + raise TypeError("Encountered binary string with non-ASCII codepoint in SubStation field {!r} for line {!r} - please use unicode string instead of str".format(f, line)) elif isinstance(v, Color): if format_ == "ass": return color_to_ass_rgba(v) else: return color_to_ssa_rgb(v) else: - raise TypeError("Unexpected type when writing a SubStation field") + raise TypeError("Unexpected type when writing a SubStation field {!r} for line {!r}".format(f, line)) print("\n[V4+ Styles]" if format_ == "ass" else "\n[V4 Styles]", file=fp) print(STYLE_FORMAT_LINE[format_], file=fp) for name, sty in subs.styles.items(): - fields = [field_to_string(f, getattr(sty, f)) for f in STYLE_FIELDS[format_]] + fields = [field_to_string(f, getattr(sty, f), sty) for f in STYLE_FIELDS[format_]] print("Style: %s" % name, *fields, sep=",", file=fp) print("\n[Events]", file=fp) print(EVENT_FORMAT_LINE[format_], file=fp) for ev in subs.events: - fields = [field_to_string(f, getattr(ev, f)) for f in EVENT_FIELDS[format_]] + fields = [field_to_string(f, getattr(ev, f), ev) for f in EVENT_FIELDS[format_]] print(ev.type, end=": ", file=fp) print(*fields, sep=",", file=fp) |