Skip to content

Commit

Permalink
[refine] Refine GetConfig 使用方法
Browse files Browse the repository at this point in the history
基于配置化的管理思想, 
假设项目的任何地方都需要使用GetConfig
于是可以在GetConfig模块里生成一个config对象.
任何地方需要只要import即可.
  • Loading branch information
ozhiwei committed Nov 9, 2018
1 parent 4eaaa7d commit d77e111
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
3 changes: 1 addition & 2 deletions Api/ProxyApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

sys.path.append('../')

from Util.GetConfig import GetConfig
from Util.GetConfig import config
from Manager.ProxyManager import ProxyManager

app = Flask(__name__)
Expand Down Expand Up @@ -84,7 +84,6 @@ def getStatus():


def run():
config = GetConfig()
if sys.platform.startswith("win"):
app.run(host=config.host_ip, port=config.host_port)
else:
Expand Down
19 changes: 9 additions & 10 deletions DB/DbClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os
import sys

from Util.GetConfig import GetConfig
from Util.GetConfig import config
from Util.utilClass import Singleton

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -55,7 +55,6 @@ def __init__(self):
init
:return:
"""
self.config = GetConfig()
self.__initDbClient()

def __initDbClient(self):
Expand All @@ -64,19 +63,19 @@ def __initDbClient(self):
:return:
"""
__type = None
if "SSDB" == self.config.db_type:
if "SSDB" == config.db_type:
__type = "SsdbClient"
elif "REDIS" == self.config.db_type:
elif "REDIS" == config.db_type:
__type = "RedisClient"
elif "MONGODB" == self.config.db_type:
elif "MONGODB" == config.db_type:
__type = "MongodbClient"
else:
pass
assert __type, 'type error, Not support DB type: {}'.format(self.config.db_type)
self.client = getattr(__import__(__type), __type)(name=self.config.db_name,
host=self.config.db_host,
port=self.config.db_port,
password=self.config.db_password)
assert __type, 'type error, Not support DB type: {}'.format(config.db_type)
self.client = getattr(__import__(__type), __type)(name=config.db_name,
host=config.db_host,
port=config.db_port,
password=config.db_password)

def get(self, key, **kwargs):
return self.client.get(key, **kwargs)
Expand Down
5 changes: 2 additions & 3 deletions Manager/ProxyManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from Util import EnvUtil
from DB.DbClient import DbClient
from Util.GetConfig import GetConfig
from Util.GetConfig import config
from Util.LogHandler import LogHandler
from Util.utilFunction import verifyProxyFormat
from ProxyGetter.getFreeProxy import GetFreeProxy
Expand All @@ -30,7 +30,6 @@ class ProxyManager(object):

def __init__(self):
self.db = DbClient()
self.config = GetConfig()
self.raw_proxy_queue = 'raw_proxy'
self.log = LogHandler('proxy_manager')
self.useful_proxy_queue = 'useful_proxy'
Expand All @@ -41,7 +40,7 @@ def refresh(self):
:return:
"""
self.db.changeTable(self.raw_proxy_queue)
for proxyGetter in self.config.proxy_getter_functions:
for proxyGetter in config.proxy_getter_functions:
# fetch
try:
self.log.info("{func}: fetch proxy start".format(func=proxyGetter))
Expand Down
2 changes: 2 additions & 0 deletions Util/GetConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def host_port(self):
def processes(self):
return int(self.config_file.get('API', 'processes'))

config = GetConfig()

if __name__ == '__main__':
gg = GetConfig()
print(gg.db_type)
Expand Down

0 comments on commit d77e111

Please sign in to comment.