完成配置读取与写入
This commit is contained in:
5
.idea/copyright/lzybetter.xml
generated
Normal file
5
.idea/copyright/lzybetter.xml
generated
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<component name="CopyrightManager">
|
||||||
|
<copyright>
|
||||||
|
<option name="myName" value="lzybetter" />
|
||||||
|
</copyright>
|
||||||
|
</component>
|
||||||
63
util/config.py
Normal file
63
util/config.py
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import yaml
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
__BASE_PATH = os.getcwd()
|
||||||
|
__CONFIG_PATH = os.path.join(__BASE_PATH, 'config')
|
||||||
|
__CONFIG_NAME = 'test.yaml'
|
||||||
|
__SCHEDULER_DB_FILE_NAME = 'scheduler.db'
|
||||||
|
__LOG_FILE_NAME = 'myAssistant.log'
|
||||||
|
__CONFIG_DICT = {}
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
if self.__CONFIG_DICT == {}:
|
||||||
|
with open(os.path.join(self.__CONFIG_PATH, self.__CONFIG_NAME)) as f:
|
||||||
|
self.__CONFIG_DICT = yaml.safe_load(f)
|
||||||
|
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
if not hasattr(Config, "_instance"):
|
||||||
|
Config._instance = object.__new__(cls)
|
||||||
|
return Config._instance
|
||||||
|
|
||||||
|
@property
|
||||||
|
def CONFIG_PATH(self):
|
||||||
|
return self.__CONFIG_PATH
|
||||||
|
|
||||||
|
@property
|
||||||
|
def BASE_PATH(self):
|
||||||
|
return self.__BASE_PATH
|
||||||
|
|
||||||
|
def get_scheduler_db_file_path(self):
|
||||||
|
try:
|
||||||
|
return os.path.join(self.__CONFIG_PATH, self.__CONFIG_DICT['SCHEDULER_DB_PATH'],
|
||||||
|
self.__SCHEDULER_DB_FILE_NAME)
|
||||||
|
except:
|
||||||
|
return os.path.join(self.__CONFIG_PATH, 'schedule_db', self.__SCHEDULER_DB_FILE_NAME)
|
||||||
|
|
||||||
|
def set_scheduler_db_path(self, new_path):
|
||||||
|
self.__CONFIG_DICT['SCHEDULER_DB_PATH'] = new_path
|
||||||
|
with open(os.path.join(self.__CONFIG_PATH, self.__CONFIG_NAME), 'w') as f:
|
||||||
|
f.write(yaml.safe_dump(self.__CONFIG_DICT, sort_keys=False))
|
||||||
|
|
||||||
|
def get_log_file_path(self):
|
||||||
|
try:
|
||||||
|
return os.path.join(self.__CONFIG_PATH, self.__CONFIG_DICT['LOG_PATH'], self.__LOG_FILE_NAME)
|
||||||
|
except:
|
||||||
|
return os.path.join(self.__CONFIG_PATH, 'log', self.__LOG_FILE_NAME)
|
||||||
|
|
||||||
|
def set_log_path(self, new_path):
|
||||||
|
self.__CONFIG_DICT['LOG_PATH'] = new_path
|
||||||
|
with open(os.path.join(self.__CONFIG_PATH, self.__CONFIG_NAME), 'w') as f:
|
||||||
|
f.write(yaml.safe_dump(self.__CONFIG_DICT, sort_keys=False))
|
||||||
|
|
||||||
|
def get_proxy(self):
|
||||||
|
try:
|
||||||
|
return self.__CONFIG_DICT['PROXY']
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def set_proxy(self, new_proxy):
|
||||||
|
self.__CONFIG_DICT['PROXY'] = new_proxy
|
||||||
|
with open(os.path.join(self.__CONFIG_PATH, self.__CONFIG_NAME), 'w') as f:
|
||||||
|
f.write(yaml.safe_dump(self.__CONFIG_DICT, sort_keys=False))
|
||||||
46
util/requests_with_proxy.py
Normal file
46
util/requests_with_proxy.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import requests
|
||||||
|
import os
|
||||||
|
from util import config
|
||||||
|
import json
|
||||||
|
|
||||||
|
class RequestWithProxy:
|
||||||
|
|
||||||
|
__proxies = {}
|
||||||
|
__no_proxy = []
|
||||||
|
def __init__(self):
|
||||||
|
c = config.Config()
|
||||||
|
proxies_tmp = c.get_proxy()
|
||||||
|
if proxies_tmp:
|
||||||
|
for k in sorted(proxies_tmp):
|
||||||
|
if k in ('http', 'https'):
|
||||||
|
if 'http://' in proxies_tmp[k] or "https://" in proxies_tmp[k]:
|
||||||
|
self.__proxies[k] = proxies_tmp[k]
|
||||||
|
else:
|
||||||
|
self.__proxies[k] = "http://" + proxies_tmp[k]
|
||||||
|
elif k == 'proxy_web':
|
||||||
|
for w in proxies_tmp[k]:
|
||||||
|
if ('http' in self.__proxies and self.__proxies['http']) or ('https' in self.__proxies and self.__proxies['https']):
|
||||||
|
if 'http://' in w:
|
||||||
|
self.__proxies[w] = self.__proxies['http']
|
||||||
|
elif 'https://' in w:
|
||||||
|
self.__proxies[w] = self.__proxies['https']
|
||||||
|
else:
|
||||||
|
self.__proxies["https://" + w] = self.__proxies['https']
|
||||||
|
elif k == 'no_proxy_web':
|
||||||
|
for w in proxies_tmp[k]:
|
||||||
|
self.__no_proxy.append(w.replace("https://", "").replace("http://", ""))
|
||||||
|
if self.__no_proxy:
|
||||||
|
os.environ['NO_PROXY'] = ','.join(self.__no_proxy)
|
||||||
|
|
||||||
|
def get(self, url, headers=None):
|
||||||
|
try:
|
||||||
|
if url.replace("https://", "").replace("http://", "") in self.__no_proxy:
|
||||||
|
r = requests.get(url=url, headers=headers)
|
||||||
|
else:
|
||||||
|
r = requests.get(url=url, headers=headers, proxies=self.__proxies)
|
||||||
|
return r
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def post(self, url, data=None, json_d=None, headers=None):
|
||||||
|
pass
|
||||||
Reference in New Issue
Block a user