import json
import os

import yaml


# 获取项目根目录
def get_project_path():
    return os.path.abspath(os.getcwd().split("common")[0])


# 读取
def read_yaml(key):
    with open(os.getcwd() + '/extract.yaml', mode='r', encoding='utf-8') as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        return value[key]


# 读取
def read_yaml_projectCode(key):
    with open(os.getcwd() + '/projectCode.yaml', mode='r', encoding='utf-8') as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        return value[key]


# 写入

def write_yaml(data):
    with open(os.getcwd() + '/extract.yaml', mode='a', encoding='utf-8') as f:
        value = yaml.dump(data, stream=f, allow_unicode=True)
        return value


# 写入

def write_yaml_projectCode(data):
    with open(os.getcwd() + '/projectCode.yaml', mode='a', encoding='utf-8') as f:
        value = yaml.dump(data, stream=f, allow_unicode=True)
        return value


# 清空
def clear_yaml():
    with open(os.getcwd() + '/extract.yaml', mode='w', encoding='utf-8') as f:
        f.truncate()


# 清空projectCode.yaml
def clear_yaml_projectCode():
    with open(os.getcwd() + '/projectCode.yaml', mode='w', encoding='utf-8') as f:
        f.truncate()


# 读取测试用列
def read_testcase(yaml_path):
    with open(yaml_path, mode='r', encoding='utf-8') as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        return value


# 读取conftest,yaml
def read_config_yaml(one_node, two_nede):
    with open(get_project_path() + '/config.yaml', mode='r', encoding='utf-8') as f:
        value = yaml.load(f, Loader=yaml.FullLoader)
        return value[one_node][two_nede]


# #读取数据得yaml
def read_data_yaml(yaml_path):
    with open(get_project_path() + yaml_path, mode='r', encoding='utf-8') as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        return value


def read_case(yaml_path):
    with open(os.getcwd() + yaml_path, mode='r', encoding='utf-8') as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        return value
