init_conf.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Author : liuyuqi
  5. @Contact : liuyuqi.gov@msn.cn
  6. @Time : 2019/08/11 06:48:32
  7. @Version : 1.0
  8. @License : (C)Copyright 2019
  9. @Desc : 初始化配置文件
  10. '''
  11. import yaml
  12. import os
  13. # current_path = os.path.dirname(os.path.abspath(__file__))
  14. config_path = os.path.join( "conf", 'config.yaml')
  15. class YamlConf:
  16. '''
  17. yaml配置
  18. '''
  19. @staticmethod
  20. def save(data):
  21. global config_path
  22. try:
  23. yaml.dump(data, open(config_path, "w"))
  24. except Exception as e:
  25. print(e)
  26. @staticmethod
  27. def load():
  28. global config_path
  29. config = {}
  30. try:
  31. config = yaml.load(
  32. open(config_path, "r", encoding="utf-8"), Loader=yaml.SafeLoader)
  33. if config is None:
  34. config = {}
  35. except Exception as e:
  36. print(e)
  37. return config
  38. @staticmethod
  39. def set(data_dict):
  40. json_obj = YamlConf.load()
  41. for key in data_dict:
  42. json_obj[key] = data_dict[key]
  43. YamlConf.save(json_obj)
  44. @staticmethod
  45. def get(key, default_val=""):
  46. try:
  47. result = YamlConf.load()[key]
  48. return result
  49. except Exception as e:
  50. print(e)
  51. if __name__ == '__main__':
  52. # config = {"user": "小舟", "pass": "123", "address": ["shanghai", "beijing"]}
  53. # print(YamlConf.get("addresss", "default_val"))
  54. config2 = {"url_date_seed": "https://short-msg-ms.juejin.im/v1/pinList/topic?uid=&device_id=&token=&src=web&topicId=5abcaa67092dcb4620ca335c&page=3&pageSize=20&sortType=rank"}
  55. YamlConf.set(config2)