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._conf = ConfigParser()
|
||||
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'):
|
||||
global_tmp_path = str((Path(__file__).parent / 'tmp').absolute())
|
||||
Path(global_tmp_path).mkdir(parents=True, exist_ok=True)
|
||||
@ -49,11 +50,13 @@ class OptionsManager(object):
|
||||
"""
|
||||
items = self._conf.items(section)
|
||||
option = dict()
|
||||
|
||||
for j in items:
|
||||
try:
|
||||
option[j[0]] = eval(self._conf.get(section, j[0]).replace('\\', '\\\\'))
|
||||
except:
|
||||
option[j[0]] = self._conf.get(section, j[0])
|
||||
|
||||
return option
|
||||
|
||||
def set_item(self, section: str, item: str, value: Any):
|
||||
@ -83,6 +86,7 @@ class DriverOptions(Options):
|
||||
"""
|
||||
super().__init__()
|
||||
self._driver_path = None
|
||||
|
||||
if read_file:
|
||||
options_dict = OptionsManager().get_option('chrome_options')
|
||||
paths_dict = OptionsManager().get_option('paths')
|
||||
@ -123,11 +127,14 @@ class DriverOptions(Options):
|
||||
:return: 当前对象
|
||||
"""
|
||||
del_list = []
|
||||
|
||||
for argument in self._arguments:
|
||||
if argument.startswith(value):
|
||||
del_list.append(argument)
|
||||
|
||||
for del_arg in del_list:
|
||||
self._arguments.remove(del_arg)
|
||||
|
||||
return self
|
||||
|
||||
def remove_experimental_option(self, key: str):
|
||||
@ -137,6 +144,7 @@ class DriverOptions(Options):
|
||||
"""
|
||||
if key in self._experimental_options:
|
||||
self._experimental_options.pop(key)
|
||||
|
||||
return self
|
||||
|
||||
def remove_all_extensions(self):
|
||||
@ -154,9 +162,11 @@ class DriverOptions(Options):
|
||||
:return: 当前对象
|
||||
"""
|
||||
self.remove_argument(arg)
|
||||
|
||||
if value:
|
||||
arg_str = arg if isinstance(value, bool) else f'{arg}={value}'
|
||||
self.add_argument(arg_str)
|
||||
|
||||
return self
|
||||
|
||||
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:
|
||||
om.set_item('paths', 'chromedriver_path', format_path(driver_path))
|
||||
|
||||
if chrome_path is not None:
|
||||
om.set_item('chrome_options', 'binary_location', format_path(chrome_path))
|
||||
|
||||
if debugger_address is not None:
|
||||
om.set_item('chrome_options', 'debugger_address', format_path(debugger_address))
|
||||
|
||||
if tmp_path is not None:
|
||||
om.set_item('paths', 'global_tmp_path', format_path(tmp_path))
|
||||
|
||||
if download_path is not None:
|
||||
experimental_options = om.get_value('chrome_options', 'experimental_options')
|
||||
experimental_options['prefs']['download.default_directory'] = format_path(download_path)
|
||||
om.set_item('chrome_options', 'experimental_options', experimental_options)
|
||||
|
||||
om.save()
|
||||
|
||||
if user_data_path is not None:
|
||||
set_argument('--user-data-dir', format_path(user_data_path))
|
||||
|
||||
if cache_path is not None:
|
||||
set_argument('--disk-cache-dir', format_path(cache_path))
|
||||
|
||||
if check_version:
|
||||
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.remove_argument(arg)
|
||||
|
||||
if value:
|
||||
arg_str = arg if isinstance(value, bool) else f'{arg}={value}'
|
||||
do.add_argument(arg_str)
|
||||
|
||||
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')
|
||||
do = DriverOptions(read_file=False)
|
||||
do.add_argument('--headless')
|
||||
|
||||
if chrome_path:
|
||||
do.binary_location = chrome_path
|
||||
|
||||
try:
|
||||
driver = webdriver.Chrome(driver_path, options=do)
|
||||
driver.quit()
|
||||
|
Loading…
x
Reference in New Issue
Block a user