import yaml
import os

class DubugTalk:
    # 获取项目路径
    def get_object_path(self):
        return os.path.abspath(os.getcwd().split('Utils')[0])

    # 读取extract.yml文件
    def read_extract_file(self,one_node):
        with open(self.get_object_path() + "\\extract.yml", encoding='utf-8') as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value[one_node]

    # 写入extract.yml文件
    def write_extract_file(self, data):
        with open(self.get_object_path() + "\\extract.yml", encoding='utf-8', mode='a') as f:
            yaml.dump(data=data, stream=f, allow_unicode=True)

    # 清空extract.yml文件a
    def clear_extract_file(self):
        with open(self.get_object_path() + "\\extract.yml", encoding='utf-8', mode='w') as f:
            f.truncate()

    def clear_folder_contents(self, folder_path):
        """
        清空指定文件夹下的所有文件（不包括子文件夹）。
        """
        # 确保提供的路径确实是一个文件夹
        if not os.path.isdir(folder_path):
            print(f"Error: {folder_path} is not a directory.")
            return

        # 遍历文件夹中的所有内容
        for filename in os.listdir(folder_path):
            # 构建文件的完整路径
            file_path = os.path.join(folder_path, filename)
            # 检查是否为文件（排除子文件夹）
            if os.path.isfile(file_path):
                try:
                    # 删除文件
                    os.remove(file_path)
                except Exception as e:
                    # 打印错误（可能由于权限问题等）
                    print(f"Failed to delete {file_path}. Reason: {e}")

# if __name__ == '__main__':
#     print(read_config_file('base','base_url'))
#     print(get_object_path())
