#!/usr/bin/env python
# coding: utf-8
# Copyright (c) 2026 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

from abc import ABC, abstractmethod
from collections import defaultdict
from typing import Optional, Tuple, List, Dict


class BaseValidator(ABC):
    """Redfish 验证器基类"""

    @property
    @abstractmethod
    def name(self) -> str:
        """验证器名称"""
        pass

    @property
    def package_name(self) -> str:
        """包名"""
        return f"redfish_{self.name}"

    @property
    def command_name(self) -> str:
        """命令名称"""
        return f"rf_{self.name}_validator"

    @property
    def supported_formats(self) -> list:
        """支持的解析格式列表，子类可覆盖"""
        return []

    @abstractmethod
    def parse_tsv_excluded(self, file_path: str, white_list: Optional[dict] = None) -> Tuple[List[str], str, dict]:
        """
        解析TSV文件并返回过滤后的行和统计结果
        :param file_path: TSV文件路径
        :param white_list: 白名单字典
        :return: (过滤后的行列表, 表头行, 统计结果字典)
        """
        pass

    @abstractmethod
    def parse_html_excluded(self, file_path: str, white_list: Optional[dict] = None) -> Tuple[str, dict]:
        """
        解析HTML文件并返回过滤后的内容和统计结果
        :param file_path: HTML文件路径
        :param white_list: 白名单字典
        :return: (过滤后的HTML内容, 统计结果字典)
        """
        pass

    def get_run_args(self) -> list:
        """获取运行校验器的命令参数"""
        import sys
        module_map = {
            "protocol": "redfish_protocol_validator.console_scripts",
            "service": "redfish_service_validator.RedfishServiceValidator",
            "interop": "redfish_interop_validator.RedfishInteropValidator",
        }
        module = module_map.get(self.name, f"redfish_{self.name}.console_scripts")
        script = f"from {module} import main; main()"
        return [sys.executable, "-c", script]

    def get_mapped_args(self) -> dict:
        """返回参数映射字典，子类可覆盖以提供自定义映射"""
        return {}

    def build_run_args(self, args) -> list:
        """
        构建校验器特有的运行参数
        :param args: 参数对象
        :return: 参数列表
        """
        return []