Quellcode durchsuchen

[refine] Refine GetConfig 使用方法

基于配置化的管理思想, 
假设项目的任何地方都需要使用GetConfig
于是可以在GetConfig模块里生成一个config对象.
任何地方需要只要import即可.
1again vor 7 Jahren
Ursprung
Commit
d77e1110e9
4 geänderte Dateien mit 14 neuen und 15 gelöschten Zeilen
  1. 1 2
      Api/ProxyApi.py
  2. 9 10
      DB/DbClient.py
  3. 2 3
      Manager/ProxyManager.py
  4. 2 0
      Util/GetConfig.py

+ 1 - 2
Api/ProxyApi.py

@@ -19,7 +19,7 @@ from flask import Flask, jsonify, request
 
 sys.path.append('../')
 
-from Util.GetConfig import GetConfig
+from Util.GetConfig import config
 from Manager.ProxyManager import ProxyManager
 
 app = Flask(__name__)
@@ -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:

+ 9 - 10
DB/DbClient.py

@@ -16,7 +16,7 @@ __author__ = 'JHao'
 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__)))
@@ -55,7 +55,6 @@ class DbClient(object):
         init
         :return:
         """
-        self.config = GetConfig()
         self.__initDbClient()
 
     def __initDbClient(self):
@@ -64,19 +63,19 @@ class DbClient(object):
         :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)

+ 2 - 3
Manager/ProxyManager.py

@@ -17,7 +17,7 @@ import random
 
 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
@@ -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'
@@ -41,7 +40,7 @@ class ProxyManager(object):
         :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))

+ 2 - 0
Util/GetConfig.py

@@ -70,6 +70,8 @@ class GetConfig(object):
     def processes(self):
         return int(self.config_file.get('API', 'processes'))
 
+config = GetConfig()
+
 if __name__ == '__main__':
     gg = GetConfig()
     print(gg.db_type)