How to use the pysubs2.common.text_type function in pysubs2

To help you get started, we’ve selected a few pysubs2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tkarabela / pysubs2 / pysubs2 / microdvd.py View on Github external
raise UnknownFPSError("Framerate must be specified when writing MicroDVD.")
        to_frames = partial(ms_to_frames, fps=fps)

        def is_entirely_italic(line):
            style = subs.styles.get(line.style, SSAStyle.DEFAULT_STYLE)
            for fragment, sty in parse_tags(line.text, style, subs.styles):
                fragment = fragment.replace(r"\h", " ")
                fragment = fragment.replace(r"\n", "\n")
                fragment = fragment.replace(r"\N", "\n")
                if not sty.italic and fragment and not fragment.isspace():
                    return False
            return True

        # insert an artificial first line telling the framerate
        if write_fps_declaration:
            subs.insert(0, SSAEvent(start=0, end=0, text=text_type(fps)))

        for line in (ev for ev in subs if not ev.is_comment):
            text = "|".join(line.plaintext.splitlines())
            if is_entirely_italic(line):
                text = "{Y:i}" + text

            start, end = map(to_frames, (line.start, line.end))

            # XXX warn on underflow?
            if start < 0: start = 0
            if end < 0: end = 0

            print("{%d}{%d}%s" % (start, end, text), file=fp)

        # remove the artificial framerate-telling line
        if write_fps_declaration:
github tkarabela / pysubs2 / pysubs2 / substation.py View on Github external
def field_to_string(f, v):
            if f in {"start", "end"}:
                return ms_to_timestamp(v)
            elif f == "marked":
                return "Marked=%d" % v
            elif f == "alignment" and format_ == "ssa":
                return text_type(ass_to_ssa_alignment(v))
            elif isinstance(v, bool):
                return "-1" if v else "0"
            elif isinstance(v, (text_type, Number)):
                return text_type(v)
            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")
github tkarabela / pysubs2 / pysubs2 / substation.py View on Github external
def field_to_string(f, v):
            if f in {"start", "end"}:
                return ms_to_timestamp(v)
            elif f == "marked":
                return "Marked=%d" % v
            elif f == "alignment" and format_ == "ssa":
                return text_type(ass_to_ssa_alignment(v))
            elif isinstance(v, bool):
                return "-1" if v else "0"
            elif isinstance(v, (text_type, Number)):
                return text_type(v)
            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")
github tkarabela / pysubs2 / pysubs2 / substation.py View on Github external
def field_to_string(f, v):
            if f in {"start", "end"}:
                return ms_to_timestamp(v)
            elif f == "marked":
                return "Marked=%d" % v
            elif f == "alignment" and format_ == "ssa":
                return text_type(ass_to_ssa_alignment(v))
            elif isinstance(v, bool):
                return "-1" if v else "0"
            elif isinstance(v, (text_type, Number)):
                return text_type(v)
            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")