|
6 | 6 | from contextlib import suppress |
7 | 7 | from functools import partial |
8 | 8 | from random import randint |
| 9 | +from tempfile import TemporaryDirectory |
9 | 10 | from typing import Any, Callable, Optional, TypeVar |
10 | 11 |
|
11 | 12 | from pydoll.browser.interfaces import BrowserOptionsManager |
@@ -598,7 +599,7 @@ def _setup_user_dir(self): |
598 | 599 | if self.options.browser_preferences: |
599 | 600 | self._set_browser_preferences_in_temp_dir(temp_dir) |
600 | 601 |
|
601 | | - def _set_browser_preferences_in_temp_dir(self, temp_dir): |
| 602 | + def _set_browser_preferences_in_temp_dir(self, temp_dir: TemporaryDirectory): |
602 | 603 | os.mkdir(os.path.join(temp_dir.name, 'Default')) |
603 | 604 | preferences = self.options.browser_preferences |
604 | 605 | with open( |
@@ -628,21 +629,16 @@ def _set_browser_preferences_in_user_data_dir(self, user_data_dir: str): |
628 | 629 | # Backup existing Preferences file |
629 | 630 | shutil.copy2(preferences_path, self._backup_preferences_dir) |
630 | 631 |
|
631 | | - existing_prefs = {} |
| 632 | + preferences = {} |
632 | 633 | if os.path.exists(preferences_path): |
633 | 634 | with suppress(json.JSONDecodeError): |
634 | 635 | 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) |
642 | 638 | with open(preferences_path, 'w', encoding='utf-8') as json_file: |
643 | 639 | json.dump(preferences, json_file, indent=2) |
644 | 640 |
|
645 | | - def _get_user_data_dir(self): |
| 641 | + def _get_user_data_dir(self) -> Optional[str]: |
646 | 642 | for arg in self.options.arguments: |
647 | 643 | if arg.startswith('--user-data-dir='): |
648 | 644 | return arg.split('=', 1)[1] |
|
0 commit comments