# /*
#  * Copyright 2024 KylinSoft Co., Ltd.
#  *
#  * This program is free software: you can redistribute it and/or modify it under
#  * the terms of the GNU General Public License as published by the Free Software
#  * Foundation, either version 3 of the License, or (at your option) any later
#  * version.
#  *
#  * This program is distributed in the hope that it will be useful, but WITHOUT
#  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#  * details.
#  *
#  * You should have received a copy of the GNU General Public License along with
#  * this program. If not, see <https://www.gnu.org/licenses/>.
#  */

import sys
sys.path.append('/usr/lib/kylin-ai-parser-extention/')

from .base_parser import BaseParser
import importlib.util

class OfficeParser(BaseParser):
    def __init__(self) -> None:
        self.module_available = self._module_exists('libparser_extension')

        if self.module_available:
            import libparser_extension
            self.object = libparser_extension.ParserExtension()
        else:
            print("Warning: The required module 'libparser_extension' is not found. Parsing functionality will be limited.")

    def _module_exists(self, module_name: str) -> bool:
        """
        检查指定的模块是否存在。
        """
        spec = importlib.util.find_spec(module_name)
        return spec is not None

    def parser(self, file_path) -> list:
        """
        解析 .doc, .wps, .ppt 文件
        """
        if not self.module_available:
            print("Error: Cannot parse the file because 'libparser_extension' is not available.")
            return []  # 返回空列表作为默认值

        try:
            result = self.object.parserOffice(file_path)
            content = result.split('\r')
            texts = " ".join(content)
            return texts
        except Exception as e:
            print(f"Error during parsing: {e}")
            raise


if __name__ == "__main__":
    parser = OfficeParser()
    result = parser.parser('/home/wangyan/操作系统大会演示素材v0.2/111.wps')
    print(result)