# 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 os
from os import path
from typing import Any, Callable, Optional
from ..file_writer import file_writer



class writer(file_writer):
    def __init__(  # pylint: disable=too-many-arguments
        self,
        path_: str,
        metadata: Any = None,
        append: bool = False,
        buffering: int = 0,
        on_error: Optional[Callable[[str], None]] = None,
    ) -> None:
        super().__init__(path_, on_error)

        if (
            append and path.exists(path_) and os.stat(path_).st_size == 0
        ):  # true for pipes
            append = False


# 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/raw.py
import os
from os import path
from typing import Any, Callable, Optional
from ..file_writer import file_writer




class writer(file_writer):
    def __init__(  # pylint: disable=too-many-arguments
        self,
        path_: str,
        metadata: Any = None,
        append: bool = False,
        buffering: int = 0,
        on_error: Optional[Callable[[str], None]] = None,
    ) -> None:
        super().__init__(path_, on_error)

        if (
            append and path.exists(path_) and os.stat(path_).st_size == 0
        ):  # true for pipes
            append = False


        self.buffering = buffering