config.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import json
  4. import logging
  5. import redis
  6. class Config:
  7. local_config = None
  8. redis_config = None
  9. def __init__(self, path):
  10. self.path = path
  11. def read_local_config(self):
  12. try:
  13. f = open(self.path, encoding='utf-8')
  14. self.local_config = json.load(f)
  15. f.close()
  16. except Exception as e:
  17. logging.error("[CONFIG] read config with path: %s error: %s", self.path, e)
  18. return self.local_config
  19. def read_redis_config(self):
  20. if self.local_config is None:
  21. return None
  22. host = self.local_config["redis_host"]
  23. port = self.local_config["redis_port"]
  24. db = self.local_config['redis_db']
  25. password = self.local_config['redis_password']
  26. try:
  27. r = redis.Redis(host, port, db=db, password=password)
  28. config = r.get('config').decode('utf-8')
  29. if config is None or config is "":
  30. return
  31. self.redis_config = json.loads(config)
  32. except Exception as e:
  33. logging.error("[CONFIG] read redis config with local_config(%s) error: %s", self.local_config, e)
  34. return self.redis_config
  35. def load_remote_host_config(self):
  36. """
  37. Load host ip, port, username, password, file path of remote host.
  38. :return:
  39. """
  40. if self.redis_config is None:
  41. return None
  42. if not self.redis_config.__contains__('scp_host_ip'):
  43. return None
  44. if not self.redis_config.__contains__('scp_host_port'):
  45. return None
  46. if not self.redis_config.__contains__('scp_host_username'):
  47. return None
  48. if not self.redis_config.__contains__('scp_host_password'):
  49. return None
  50. if not self.redis_config.__contains__('file_upload_path'):
  51. return None
  52. if not self.redis_config.__contains__('img_upload_path'):
  53. return None
  54. return {
  55. 'host': self.redis_config['scp_host_ip'],
  56. 'port': self.redis_config['scp_host_port'],
  57. 'username': self.redis_config['scp_host_username'],
  58. 'password': self.redis_config['scp_host_password'],
  59. 'file_path': self.redis_config['file_upload_path'],
  60. 'img_path': self.redis_config['img_upload_path'],
  61. }