123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- import json
- import logging
- import redis
- class Config:
- local_config = None
- redis_config = None
- def __init__(self, path):
- self.path = path
- def read_local_config(self):
- try:
- f = open(self.path, encoding='utf-8')
- self.local_config = json.load(f)
- f.close()
- except Exception as e:
- logging.error("[CONFIG] read config with path: %s error: %s", self.path, e)
- return self.local_config
- def read_redis_config(self):
- if self.local_config is None:
- return None
- host = self.local_config["redis_host"]
- port = self.local_config["redis_port"]
- db = self.local_config['redis_db']
- password = self.local_config['redis_password']
- try:
- r = redis.Redis(host, port, db=db, password=password)
- config = r.get('config').decode('utf-8')
- if config is None or config is "":
- return
- self.redis_config = json.loads(config)
- except Exception as e:
- logging.error("[CONFIG] read redis config with local_config(%s) error: %s", self.local_config, e)
- return self.redis_config
|