Skip to content

Commit 3e1dae4

Browse files
committed
fix: adding typehint and fixing some codes
1 parent 9d6c941 commit 3e1dae4

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

pydoll/browser/chromium/base.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from contextlib import suppress
77
from functools import partial
88
from random import randint
9+
from tempfile import TemporaryDirectory
910
from typing import Any, Callable, Optional, TypeVar
1011

1112
from pydoll.browser.interfaces import BrowserOptionsManager
@@ -598,7 +599,7 @@ def _setup_user_dir(self):
598599
if self.options.browser_preferences:
599600
self._set_browser_preferences_in_temp_dir(temp_dir)
600601

601-
def _set_browser_preferences_in_temp_dir(self, temp_dir):
602+
def _set_browser_preferences_in_temp_dir(self, temp_dir: TemporaryDirectory):
602603
os.mkdir(os.path.join(temp_dir.name, 'Default'))
603604
preferences = self.options.browser_preferences
604605
with open(
@@ -628,21 +629,16 @@ def _set_browser_preferences_in_user_data_dir(self, user_data_dir: str):
628629
# Backup existing Preferences file
629630
shutil.copy2(preferences_path, self._backup_preferences_dir)
630631

631-
existing_prefs = {}
632+
preferences = {}
632633
if os.path.exists(preferences_path):
633634
with suppress(json.JSONDecodeError):
634635
with open(preferences_path, 'r', encoding='utf-8') as preferences_file:
635-
existing_prefs = json.load(preferences_file)
636-
637-
if existing_prefs:
638-
existing_prefs.update(self.options.browser_preferences)
639-
preferences = existing_prefs
640-
else:
641-
preferences = self.options.browser_preferences
636+
preferences = json.load(preferences_file)
637+
preferences.update(self.options.browser_preferences)
642638
with open(preferences_path, 'w', encoding='utf-8') as json_file:
643639
json.dump(preferences, json_file, indent=2)
644640

645-
def _get_user_data_dir(self):
641+
def _get_user_data_dir(self) -> Optional[str]:
646642
for arg in self.options.arguments:
647643
if arg.startswith('--user-data-dir='):
648644
return arg.split('=', 1)[1]

pydoll/browser/options.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import suppress
2+
13
from pydoll.browser.interfaces import Options
24
from pydoll.exceptions import ArgumentAlreadyExistsInOptions, WrongPrefsDict
35

@@ -136,13 +138,12 @@ def _get_pref_path(self, path: list):
136138
Returns:
137139
The value at the given path, or None if path doesn't exist
138140
"""
139-
d = self._browser_preferences
140-
try:
141+
nested_preferences = self._browser_preferences
142+
with suppress(KeyError, TypeError):
141143
for key in path:
142-
d = d[key]
143-
return d
144-
except (KeyError, TypeError):
145-
return None
144+
nested_preferences = nested_preferences[key]
145+
return nested_preferences
146+
return None
146147

147148
def set_default_download_directory(self, path: str):
148149
"""

0 commit comments

Comments
 (0)