# Path: asciinema/file_writer.py
# class file_writer:
#     def __init__(
#         self,
#         path: str,
#         on_error: Optional[Callable[[str], None]] = None,
#     ) -> None:
#         self.path = path
#         self.file: Optional[IO[Any]] = None
#         self.on_error = on_error
# 
#     def __enter__(self) -> Any:
#         self._open_file()
#         return self
# 
#     def __exit__(
#         self, exc_type: str, exc_value: str, exc_traceback: str
#     ) -> None:
#         assert self.file is not None
#         self.file.close()
# 
#     def _open_file(self) -> None:
#         raise NotImplementedError
# 
#     def _write(self, data: Any) -> None:
#         try:
#             self.file.write(data)  # type: ignore
#         except BrokenPipeError as e:
#             if stat.S_ISFIFO(os.stat(self.path).st_mode):
#                 if self.on_error:
#                     self.on_error("Broken pipe, reopening...")
#                     self._open_file()
#                     self.on_error("Output pipe reopened successfully")
#                 else:
#                     self._open_file()
# 
#                 self.file.write(data)  # type: ignore
#             else:
#                 raise e


import codecs
import json
from codecs import StreamReader
from json.decoder import JSONDecodeError
from typing import (
    Any,
    Callable,
    Dict,
    Generator,
    List,
    Optional,
    TextIO,
    Union,
)
from ..file_writer import file_writer
        if append:
            self.mode = "a"
            self.header = None
        else:
            self.mode = "w"
            self.header = build_header(width, height, metadata or {})

    def __enter__(self) -> Any:
        self._open_file()

        if self.header:
            self.__write_line(self.header)

        return self

    def write_stdout(self, ts: float, data: Union[str, bytes]) -> None:
        if isinstance(data, str):
            data = data.encode(encoding="utf-8", errors="strict")
        data = self.stdout_decoder.decode(data)
        self.__write_event(ts, "o", data)

    def write_stdin(self, ts: float, data: Union[str, bytes]) -> None:
        if isinstance(data, str):
            data = data.encode(encoding="utf-8", errors="strict")
        data = self.stdin_decoder.decode(data)
        self.__write_event(ts, "i", data)

    # pylint: disable=consider-using-with
    def _open_file(self) -> None:
        self.file = open(

# Path: asciinema/file_writer.py
# class file_writer:
#     def __init__(
#         self,
#         path: str,
#         on_error: Optional[Callable[[str], None]] = None,
#     ) -> None:
#         self.path = path
#         self.file: Optional[IO[Any]] = None
#         self.on_error = on_error
# 
#     def __enter__(self) -> Any:
#         self._open_file()
#         return self
# 
#     def __exit__(
#         self, exc_type: str, exc_value: str, exc_traceback: str
#     ) -> None:
#         assert self.file is not None
#         self.file.close()
# 
#     def _open_file(self) -> None:
#         raise NotImplementedError
# 
#     def _write(self, data: Any) -> None:
#         try:
#             self.file.write(data)  # type: ignore
#         except BrokenPipeError as e:
#             if stat.S_ISFIFO(os.stat(self.path).st_mode):
#                 if self.on_error:
#                     self.on_error("Broken pipe, reopening...")
#                     self._open_file()
#                     self.on_error("Output pipe reopened successfully")
#                 else:
#                     self._open_file()
# 
#                 self.file.write(data)  # type: ignore
#             else:
#                 raise e


# Path: asciinema/asciicast/v2.py
import codecs
import json
from codecs import StreamReader
from json.decoder import JSONDecodeError
from typing import (
    Any,
    Callable,
    Dict,
    Generator,
    List,
    Optional,
    TextIO,
    Union,
)
from ..file_writer import file_writer

        if append:
            self.mode = "a"
            self.header = None
        else:
            self.mode = "w"
            self.header = build_header(width, height, metadata or {})

    def __enter__(self) -> Any:
        self._open_file()

        if self.header:
            self.__write_line(self.header)

        return self

    def write_stdout(self, ts: float, data: Union[str, bytes]) -> None:
        if isinstance(data, str):
            data = data.encode(encoding="utf-8", errors="strict")
        data = self.stdout_decoder.decode(data)
        self.__write_event(ts, "o", data)

    def write_stdin(self, ts: float, data: Union[str, bytes]) -> None:
        if isinstance(data, str):
            data = data.encode(encoding="utf-8", errors="strict")
        data = self.stdin_decoder.decode(data)
        self.__write_event(ts, "i", data)

    # pylint: disable=consider-using-with
    def _open_file(self) -> None:
        self.file = open(

            self.path,