mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
调整格式
This commit is contained in:
parent
3a5501ddac
commit
dbdea2a00f
@ -23,6 +23,7 @@ class OptionsManager(object):
|
|||||||
self.path = path or Path(__file__).parent / 'configs.ini'
|
self.path = path or Path(__file__).parent / 'configs.ini'
|
||||||
self._conf = ConfigParser()
|
self._conf = ConfigParser()
|
||||||
self._conf.read(self.path, encoding='utf-8')
|
self._conf.read(self.path, encoding='utf-8')
|
||||||
|
|
||||||
if 'global_tmp_path' not in self.get_option('paths') or not self.get_value('paths', 'global_tmp_path'):
|
if 'global_tmp_path' not in self.get_option('paths') or not self.get_value('paths', 'global_tmp_path'):
|
||||||
global_tmp_path = str((Path(__file__).parent / 'tmp').absolute())
|
global_tmp_path = str((Path(__file__).parent / 'tmp').absolute())
|
||||||
Path(global_tmp_path).mkdir(parents=True, exist_ok=True)
|
Path(global_tmp_path).mkdir(parents=True, exist_ok=True)
|
||||||
@ -49,11 +50,13 @@ class OptionsManager(object):
|
|||||||
"""
|
"""
|
||||||
items = self._conf.items(section)
|
items = self._conf.items(section)
|
||||||
option = dict()
|
option = dict()
|
||||||
|
|
||||||
for j in items:
|
for j in items:
|
||||||
try:
|
try:
|
||||||
option[j[0]] = eval(self._conf.get(section, j[0]).replace('\\', '\\\\'))
|
option[j[0]] = eval(self._conf.get(section, j[0]).replace('\\', '\\\\'))
|
||||||
except:
|
except:
|
||||||
option[j[0]] = self._conf.get(section, j[0])
|
option[j[0]] = self._conf.get(section, j[0])
|
||||||
|
|
||||||
return option
|
return option
|
||||||
|
|
||||||
def set_item(self, section: str, item: str, value: Any):
|
def set_item(self, section: str, item: str, value: Any):
|
||||||
@ -83,6 +86,7 @@ class DriverOptions(Options):
|
|||||||
"""
|
"""
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._driver_path = None
|
self._driver_path = None
|
||||||
|
|
||||||
if read_file:
|
if read_file:
|
||||||
options_dict = OptionsManager().get_option('chrome_options')
|
options_dict = OptionsManager().get_option('chrome_options')
|
||||||
paths_dict = OptionsManager().get_option('paths')
|
paths_dict = OptionsManager().get_option('paths')
|
||||||
@ -123,11 +127,14 @@ class DriverOptions(Options):
|
|||||||
:return: 当前对象
|
:return: 当前对象
|
||||||
"""
|
"""
|
||||||
del_list = []
|
del_list = []
|
||||||
|
|
||||||
for argument in self._arguments:
|
for argument in self._arguments:
|
||||||
if argument.startswith(value):
|
if argument.startswith(value):
|
||||||
del_list.append(argument)
|
del_list.append(argument)
|
||||||
|
|
||||||
for del_arg in del_list:
|
for del_arg in del_list:
|
||||||
self._arguments.remove(del_arg)
|
self._arguments.remove(del_arg)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def remove_experimental_option(self, key: str):
|
def remove_experimental_option(self, key: str):
|
||||||
@ -137,6 +144,7 @@ class DriverOptions(Options):
|
|||||||
"""
|
"""
|
||||||
if key in self._experimental_options:
|
if key in self._experimental_options:
|
||||||
self._experimental_options.pop(key)
|
self._experimental_options.pop(key)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def remove_all_extensions(self):
|
def remove_all_extensions(self):
|
||||||
@ -154,9 +162,11 @@ class DriverOptions(Options):
|
|||||||
:return: 当前对象
|
:return: 当前对象
|
||||||
"""
|
"""
|
||||||
self.remove_argument(arg)
|
self.remove_argument(arg)
|
||||||
|
|
||||||
if value:
|
if value:
|
||||||
arg_str = arg if isinstance(value, bool) else f'{arg}={value}'
|
arg_str = arg if isinstance(value, bool) else f'{arg}={value}'
|
||||||
self.add_argument(arg_str)
|
self.add_argument(arg_str)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def set_headless(self, on_off: bool = True):
|
def set_headless(self, on_off: bool = True):
|
||||||
|
@ -38,21 +38,29 @@ def set_paths(driver_path: str = None,
|
|||||||
|
|
||||||
if driver_path is not None:
|
if driver_path is not None:
|
||||||
om.set_item('paths', 'chromedriver_path', format_path(driver_path))
|
om.set_item('paths', 'chromedriver_path', format_path(driver_path))
|
||||||
|
|
||||||
if chrome_path is not None:
|
if chrome_path is not None:
|
||||||
om.set_item('chrome_options', 'binary_location', format_path(chrome_path))
|
om.set_item('chrome_options', 'binary_location', format_path(chrome_path))
|
||||||
|
|
||||||
if debugger_address is not None:
|
if debugger_address is not None:
|
||||||
om.set_item('chrome_options', 'debugger_address', format_path(debugger_address))
|
om.set_item('chrome_options', 'debugger_address', format_path(debugger_address))
|
||||||
|
|
||||||
if tmp_path is not None:
|
if tmp_path is not None:
|
||||||
om.set_item('paths', 'global_tmp_path', format_path(tmp_path))
|
om.set_item('paths', 'global_tmp_path', format_path(tmp_path))
|
||||||
|
|
||||||
if download_path is not None:
|
if download_path is not None:
|
||||||
experimental_options = om.get_value('chrome_options', 'experimental_options')
|
experimental_options = om.get_value('chrome_options', 'experimental_options')
|
||||||
experimental_options['prefs']['download.default_directory'] = format_path(download_path)
|
experimental_options['prefs']['download.default_directory'] = format_path(download_path)
|
||||||
om.set_item('chrome_options', 'experimental_options', experimental_options)
|
om.set_item('chrome_options', 'experimental_options', experimental_options)
|
||||||
|
|
||||||
om.save()
|
om.save()
|
||||||
|
|
||||||
if user_data_path is not None:
|
if user_data_path is not None:
|
||||||
set_argument('--user-data-dir', format_path(user_data_path))
|
set_argument('--user-data-dir', format_path(user_data_path))
|
||||||
|
|
||||||
if cache_path is not None:
|
if cache_path is not None:
|
||||||
set_argument('--disk-cache-dir', format_path(cache_path))
|
set_argument('--disk-cache-dir', format_path(cache_path))
|
||||||
|
|
||||||
if check_version:
|
if check_version:
|
||||||
check_driver_version(format_path(driver_path), format_path(chrome_path))
|
check_driver_version(format_path(driver_path), format_path(chrome_path))
|
||||||
|
|
||||||
@ -65,9 +73,11 @@ def set_argument(arg: str, value: Union[bool, str]) -> None:
|
|||||||
"""
|
"""
|
||||||
do = DriverOptions()
|
do = DriverOptions()
|
||||||
do.remove_argument(arg)
|
do.remove_argument(arg)
|
||||||
|
|
||||||
if value:
|
if value:
|
||||||
arg_str = arg if isinstance(value, bool) else f'{arg}={value}'
|
arg_str = arg if isinstance(value, bool) else f'{arg}={value}'
|
||||||
do.add_argument(arg_str)
|
do.add_argument(arg_str)
|
||||||
|
|
||||||
do.save()
|
do.save()
|
||||||
|
|
||||||
|
|
||||||
@ -131,8 +141,10 @@ def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bo
|
|||||||
chrome_path = chrome_path or om.get_value('chrome_options', 'binary_location')
|
chrome_path = chrome_path or om.get_value('chrome_options', 'binary_location')
|
||||||
do = DriverOptions(read_file=False)
|
do = DriverOptions(read_file=False)
|
||||||
do.add_argument('--headless')
|
do.add_argument('--headless')
|
||||||
|
|
||||||
if chrome_path:
|
if chrome_path:
|
||||||
do.binary_location = chrome_path
|
do.binary_location = chrome_path
|
||||||
|
|
||||||
try:
|
try:
|
||||||
driver = webdriver.Chrome(driver_path, options=do)
|
driver = webdriver.Chrome(driver_path, options=do)
|
||||||
driver.quit()
|
driver.quit()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user