12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #!/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
- def load_remote_host_config(self):
- """
- Load host ip, port, username, password, file path of remote host.
- :return:
- """
- if self.redis_config is None:
- return None
- if not self.redis_config.__contains__('scp_host_ip'):
- return None
- if not self.redis_config.__contains__('scp_host_port'):
- return None
- if not self.redis_config.__contains__('scp_host_username'):
- return None
- if not self.redis_config.__contains__('scp_host_password'):
- return None
- if not self.redis_config.__contains__('file_upload_path'):
- return None
- if not self.redis_config.__contains__('img_upload_path'):
- return None
- return {
- 'host': self.redis_config['scp_host_ip'],
- 'port': self.redis_config['scp_host_port'],
- 'username': self.redis_config['scp_host_username'],
- 'password': self.redis_config['scp_host_password'],
- 'file_path': self.redis_config['file_upload_path'],
- 'img_path': self.redis_config['img_upload_path'],
- }
|