yaml_util.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import json
  2. import os
  3. import yaml
  4. # 获取项目根目录
  5. def get_project_path():
  6. return os.path.abspath(os.getcwd().split("common")[0])
  7. # 读取
  8. def read_yaml(key):
  9. with open(os.getcwd() + '/extract.yaml', mode='r', encoding='utf-8') as f:
  10. value = yaml.load(stream=f, Loader=yaml.FullLoader)
  11. return value[key]
  12. # 读取
  13. def read_yaml_projectCode(key):
  14. with open(os.getcwd() + '/projectCode.yaml', mode='r', encoding='utf-8') as f:
  15. value = yaml.load(stream=f, Loader=yaml.FullLoader)
  16. return value[key]
  17. # 写入
  18. def write_yaml(data):
  19. with open(os.getcwd() + '/extract.yaml', mode='a', encoding='utf-8') as f:
  20. value = yaml.dump(data, stream=f, allow_unicode=True)
  21. return value
  22. # 写入
  23. def write_yaml_projectCode(data):
  24. with open(os.getcwd() + '/projectCode.yaml', mode='a', encoding='utf-8') as f:
  25. value = yaml.dump(data, stream=f, allow_unicode=True)
  26. return value
  27. # 清空
  28. def clear_yaml():
  29. with open(os.getcwd() + '/extract.yaml', mode='w', encoding='utf-8') as f:
  30. f.truncate()
  31. # 清空projectCode.yaml
  32. def clear_yaml_projectCode():
  33. with open(os.getcwd() + '/projectCode.yaml', mode='w', encoding='utf-8') as f:
  34. f.truncate()
  35. # 读取测试用列
  36. def read_testcase(yaml_path):
  37. with open(yaml_path, mode='r', encoding='utf-8') as f:
  38. value = yaml.load(stream=f, Loader=yaml.FullLoader)
  39. return value
  40. # 读取conftest,yaml
  41. def read_config_yaml(one_node, two_nede):
  42. with open(get_project_path() + '/config.yaml', mode='r', encoding='utf-8') as f:
  43. value = yaml.load(f, Loader=yaml.FullLoader)
  44. return value[one_node][two_nede]
  45. # #读取数据得yaml
  46. def read_data_yaml(yaml_path):
  47. with open(get_project_path() + yaml_path, mode='r', encoding='utf-8') as f:
  48. value = yaml.load(stream=f, Loader=yaml.FullLoader)
  49. return value
  50. def read_case(yaml_path):
  51. with open(os.getcwd() + yaml_path, mode='r', encoding='utf-8') as f:
  52. value = yaml.load(stream=f, Loader=yaml.FullLoader)
  53. return value
  54. if __name__ == '__main__':
  55. print(os.path.abspath(os.getcwd().split("common")[0]))