config.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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)
  14. self.local_config = json.load(f)
  15. except Exception as e:
  16. logging.error("[CONFIG] read config with path: %s error: %s", self.path, e)
  17. def read_redis_config(self):
  18. if self.local_config is None:
  19. return None
  20. host = self.local_config["redis_host"]
  21. port = self.local_config["redis_port"]
  22. db = self.local_config['redis_db']
  23. password = self.local_config['redis_password']
  24. try:
  25. r = redis.Redis(host, port, db=db, password=password)
  26. config = r.get('config').decode('utf8')
  27. if config is None or config is "":
  28. return
  29. self.redis_config = json.loads(config)
  30. except Exception as e:
  31. print(e)