# Path: mfr/core/extension.py
# class BaseExporter(metaclass=abc.ABCMeta):
# 
#     def __init__(self, ext, source_file_path, output_file_path, format, metadata):
# 
#         """Initialize the base exporter.
# 
#         :param ext: the name of the extension to be exported
#         :param source_file_path: the path of the input file
#         :param output_file_path: the path of the output file
#         :param format: the format of the exported file (e.g. 1200*1200.jpg)
#         """
# 
#         self.ext = ext
#         self.source_file_path = source_file_path
#         self.output_file_path = output_file_path
#         self.format = format
#         self.metadata = metadata
#         self.exporter_metrics = MetricsRecord('exporter')
#         if self._get_module_name():
#             self.metrics = self.exporter_metrics.new_subrecord(self._get_module_name())
# 
#         self.exporter_metrics.merge({
#             'class': self._get_module_name(),
#             'format': self.format,
#             'source_path': str(self.source_file_path),
#             'output_path': str(self.output_file_path),
#             # 'error': 'error_t',
#             # 'elapsed': 'elpased_t',
#         })
# 
#     @abc.abstractmethod
#     def export(self):
#         pass
# 
#     def _get_module_name(self):
#         return self.__module__ \
#             .replace('mfr.extensions.', '', 1) \
#             .replace('.export', '', 1)
# 
# Path: mfr/core/exceptions.py
# class SubprocessError(ExporterError):
#     """The MFR related errors raised from a :class:`mfr.core.extension` and relating to subprocess
#     should inherit from SubprocessError
#     """
# 
#     __TYPE = 'subprocess'
# 
#     def __init__(self, message, *args, code: int=500, process: str='', cmd: str='',
#                  returncode: int=None, path: str='', **kwargs):
#         super().__init__(message, *args, code=code, **kwargs)
#         self.process = process
#         self.cmd = cmd
#         self.return_code = returncode
#         self.path = path
#         self.attr_stack.append([self.__TYPE, {
#             'process': self.process,
#             'cmd': self.cmd,
#             'returncode': self.return_code,
#             'path': self.path,
#         }])


from http import HTTPStatus
from os.path import basename, splitext
from subprocess import run, CalledProcessError
from mfr.core.extension import BaseExporter
from mfr.core.exceptions import SubprocessError
from mfr.extensions.unoconv.settings import (PORT,
                                             ADDRESS,
                                             UNOCONV_BIN,
                                             UNOCONV_TIMEOUT)



class UnoconvExporter(BaseExporter):

    def export(self):
        try:
            run([
                UNOCONV_BIN,
                '-n',
                '-c', 'socket,host={},port={};urp;StarOffice.ComponentContext'.format(ADDRESS, PORT),
                '-f', self.format,
                '-o', self.output_file_path,
                '-vvv',
                self.source_file_path
            ], check=True, timeout=UNOCONV_TIMEOUT)
        except CalledProcessError as err:
            name, extension = splitext(basename(self.source_file_path))

# Path: mfr/core/extension.py
# class BaseExporter(metaclass=abc.ABCMeta):
# 
#     def __init__(self, ext, source_file_path, output_file_path, format, metadata):
# 
#         """Initialize the base exporter.
# 
#         :param ext: the name of the extension to be exported
#         :param source_file_path: the path of the input file
#         :param output_file_path: the path of the output file
#         :param format: the format of the exported file (e.g. 1200*1200.jpg)
#         """
# 
#         self.ext = ext
#         self.source_file_path = source_file_path
#         self.output_file_path = output_file_path
#         self.format = format
#         self.metadata = metadata
#         self.exporter_metrics = MetricsRecord('exporter')
#         if self._get_module_name():
#             self.metrics = self.exporter_metrics.new_subrecord(self._get_module_name())
# 
#         self.exporter_metrics.merge({
#             'class': self._get_module_name(),
#             'format': self.format,
#             'source_path': str(self.source_file_path),
#             'output_path': str(self.output_file_path),
#             # 'error': 'error_t',
#             # 'elapsed': 'elpased_t',
#         })
# 
#     @abc.abstractmethod
#     def export(self):
#         pass
# 
#     def _get_module_name(self):
#         return self.__module__ \
#             .replace('mfr.extensions.', '', 1) \
#             .replace('.export', '', 1)
# 
# Path: mfr/core/exceptions.py
# class SubprocessError(ExporterError):
#     """The MFR related errors raised from a :class:`mfr.core.extension` and relating to subprocess
#     should inherit from SubprocessError
#     """
# 
#     __TYPE = 'subprocess'
# 
#     def __init__(self, message, *args, code: int=500, process: str='', cmd: str='',
#                  returncode: int=None, path: str='', **kwargs):
#         super().__init__(message, *args, code=code, **kwargs)
#         self.process = process
#         self.cmd = cmd
#         self.return_code = returncode
#         self.path = path
#         self.attr_stack.append([self.__TYPE, {
#             'process': self.process,
#             'cmd': self.cmd,
#             'returncode': self.return_code,
#             'path': self.path,
#         }])


# Path: mfr/extensions/unoconv/export.py
from http import HTTPStatus
from os.path import basename, splitext
from subprocess import run, CalledProcessError
from mfr.core.extension import BaseExporter
from mfr.core.exceptions import SubprocessError
from mfr.extensions.unoconv.settings import (PORT,
                                             ADDRESS,
                                             UNOCONV_BIN,
                                             UNOCONV_TIMEOUT)




class UnoconvExporter(BaseExporter):

    def export(self):
        try:
            run([
                UNOCONV_BIN,
                '-n',
                '-c', 'socket,host={},port={};urp;StarOffice.ComponentContext'.format(ADDRESS, PORT),
                '-f', self.format,
                '-o', self.output_file_path,
                '-vvv',
                self.source_file_path
            ], check=True, timeout=UNOCONV_TIMEOUT)
        except CalledProcessError as err:
            name, extension = splitext(basename(self.source_file_path))

            raise SubprocessError(