yaml_util.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. # 写入
  14. def write_yaml(data):
  15. with open(os.getcwd() + '/extract.yaml', mode='a', encoding='utf-8') as f:
  16. value = yaml.dump(data, stream=f, allow_unicode=True)
  17. return value
  18. # 清空
  19. def clear_yaml():
  20. with open(os.getcwd() + '/extract.yaml', mode='w', encoding='utf-8') as f:
  21. f.truncate()
  22. def clear_out_yaml():
  23. with open(os.getcwd() + '/out_time_url.yaml', mode='w', encoding='utf-8') as f:
  24. f.truncate()
  25. # 读取测试用列
  26. def read_testcase(yaml_path):
  27. with open(yaml_path, mode='r', encoding='utf-8') as f:
  28. value = yaml.load(stream=f, Loader=yaml.FullLoader)
  29. return value
  30. # 读取conftest,yaml
  31. def read_config_yaml(one_node, two_nede):
  32. with open(get_project_path() + '/config.yaml', mode='r', encoding='utf-8') as f:
  33. value = yaml.load(f, Loader=yaml.FullLoader)
  34. return value[one_node][two_nede]
  35. def read_yaml_out_time_url():
  36. with open(get_project_path() + '/out_time_url.yaml', mode='r', encoding='utf-8') as f:
  37. value = yaml.load(f, Loader=yaml.FullLoader)
  38. return value
  39. def write_yaml_out_time_url(data):
  40. with open(os.getcwd() + '/out_time_url.yaml', mode='a', encoding='utf-8') as f:
  41. value = yaml.dump(data, stream=f, allow_unicode=True)
  42. return value
  43. # #读取数据得yaml
  44. def read_data_yaml(yaml_path):
  45. with open(get_project_path() + yaml_path, mode='r', encoding='utf-8') as f:
  46. value = yaml.load(stream=f, Loader=yaml.FullLoader)
  47. return value
  48. def read_case(yaml_path):
  49. with open(os.getcwd() + yaml_path, mode='r', encoding='utf-8') as f:
  50. value = yaml.load(stream=f, Loader=yaml.FullLoader)
  51. return value
  52. if __name__ == '__main__':
  53. print(os.path.abspath(os.getcwd().split("common")[0]))