env.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : env.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/11/21
  6. from utils.cfg import cfg
  7. import ujson
  8. from controllers.service import storage_service
  9. def get_env():
  10. new_conf = cfg
  11. lsg = storage_service()
  12. store_conf_dict = lsg.getStoreConfDict()
  13. new_conf.update(store_conf_dict)
  14. # print(new_conf)
  15. env = {
  16. 'ali_token': new_conf.ALI_TOKEN,
  17. 'js_proxy':new_conf.JS_PROXY,
  18. 'fl':'{{fl}}' # 防止被依赖代理
  19. }
  20. ENV = new_conf.ENV.strip()
  21. if ENV:
  22. # print(ENV)
  23. try:
  24. ENV = ujson.loads(ENV)
  25. except Exception as e:
  26. print(f'自定义环境变量有误,不是合法json:{e}')
  27. ENV = {}
  28. if ENV:
  29. env.update(ENV)
  30. # print(env)
  31. return env
  32. def update_env(env_key:str,env_value:str):
  33. lsg = storage_service()
  34. env = lsg.getItem('ENV')
  35. ENV = {}
  36. try:
  37. ENV = ujson.loads(env)
  38. except:
  39. env = '{}'
  40. if env_key:
  41. ENV[env_key] = env_value
  42. new_env = ujson.dumps(ENV,ensure_ascii=False)
  43. print(new_env)
  44. lsg.setItem('ENV',new_env)
  45. return ENV