# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2022/11/1
import logging

import yaml

log = logging.getLogger(__name__)


class UtilYaml(object):
    def __init__(self, file_path):
        self.yaml_file = file_path

    def load_yaml(self):
        with open(self.yaml_file, 'r', encoding='utf-8') as f:
            text = yaml.load(f, Loader=yaml.FullLoader)
            return text

    def get_yaml_value(self, key_lev):
        value = self.load_yaml()
        for key in key_lev:
            try:
                value = value[key]
            except KeyError:
                log.info('没有key：{}'.format(key))
                value = None
            except TypeError:
                log.info('{}不是json字符串'.format(value))
                value = None
        return value

    def write_yaml(self, data):
        with open(self.yaml_file, 'a', encoding='utf-8') as f:
            text = yaml.dump(data, f, allow_unicode=True)

    def clean_yaml(self):
        with open(self.yaml_file, 'w', encoding='utf-8') as f:
            f.truncate()


if __name__ == '__main__':
    yaml_file = '../../data/surgeryTestData.yaml'
    res = UtilYaml(yaml_file).get_yaml_value(['goods', 'val'])
    print(res)
