style: replacing os with Pathlib and small optimizations

This commit is contained in:
Karim shoair
2025-07-30 01:15:28 +03:00
parent 18660f8132
commit e7cdd39695
4 changed files with 18 additions and 16 deletions
+4 -4
View File
@@ -6,7 +6,6 @@ from http import cookies as Cookie
from collections import namedtuple from collections import namedtuple
from shlex import split as shlex_split from shlex import split as shlex_split
from tempfile import mkstemp as make_temp_file from tempfile import mkstemp as make_temp_file
from os import write as os_write, close as os_close
from urllib.parse import urlparse, urlunparse, parse_qsl from urllib.parse import urlparse, urlunparse, parse_qsl
from argparse import ArgumentParser, SUPPRESS from argparse import ArgumentParser, SUPPRESS
from webbrowser import open as open_in_browser from webbrowser import open as open_in_browser
@@ -405,9 +404,10 @@ def show_page_in_browser(page: Selector):
return return
try: try:
fd, fname = make_temp_file(".html") fd, fname = make_temp_file(prefix="scrapling_view_", suffix=".html")
os_write(fd, page.body.encode("utf-8")) with open(fd, "w", encoding="utf-8") as f:
os_close(fd) f.write(page.body)
open_in_browser(f"file://{fname}") open_in_browser(f"file://{fname}")
except IOError as e: except IOError as e:
log.error(f"Failed to write temporary file for viewing: {e}") log.error(f"Failed to write temporary file for viewing: {e}")
+4 -4
View File
@@ -1,13 +1,12 @@
from msgspec import Struct, convert, ValidationError from msgspec import Struct, convert, ValidationError
from urllib.parse import urlparse from urllib.parse import urlparse
from os.path import exists, isdir from pathlib import Path
from scrapling.core._types import ( from scrapling.core._types import (
Optional, Optional,
Union, Union,
Dict, Dict,
Callable, Callable,
Literal,
List, List,
SelectorWaitStates, SelectorWaitStates,
) )
@@ -125,9 +124,10 @@ class CamoufoxConfig(Struct, kw_only=True, frozen=False):
self.addons = [] self.addons = []
else: else:
for addon in self.addons: for addon in self.addons:
if not exists(addon): addon_path = Path(addon)
if not addon_path.exists():
raise FileNotFoundError(f"Addon's path not found: {addon}") raise FileNotFoundError(f"Addon's path not found: {addon}")
elif not isdir(addon): elif not addon_path.is_dir():
raise ValueError( raise ValueError(
f"Addon's path is not a folder, you need to pass a folder of the extracted addon: {addon}" f"Addon's path is not a folder, you need to pass a folder of the extracted addon: {addon}"
) )
+6 -4
View File
@@ -2,17 +2,20 @@
Functions related to files and URLs Functions related to files and URLs
""" """
import os from pathlib import Path
from functools import lru_cache
from urllib.parse import urlencode, urlparse from urllib.parse import urlencode, urlparse
from playwright.async_api import Route as async_Route from playwright.async_api import Route as async_Route
from msgspec import Struct, structs, convert, ValidationError from msgspec import Struct, structs, convert, ValidationError
from playwright.sync_api import Route from playwright.sync_api import Route
from scrapling.core.utils import log
from scrapling.core._types import Dict, Optional, Union, Tuple from scrapling.core._types import Dict, Optional, Union, Tuple
from scrapling.core.utils import log, lru_cache
from scrapling.engines.constants import DEFAULT_DISABLED_RESOURCES from scrapling.engines.constants import DEFAULT_DISABLED_RESOURCES
__BYPASSES_DIR__ = Path(__file__).parent / "bypasses"
class ProxyDict(Struct): class ProxyDict(Struct):
server: str server: str
@@ -129,5 +132,4 @@ def js_bypass_path(filename: str) -> str:
:param filename: The base filename of the JS file. :param filename: The base filename of the JS file.
:return: The full path of the JS file. :return: The full path of the JS file.
""" """
current_directory = os.path.dirname(__file__) return str(__BYPASSES_DIR__ / filename)
return os.path.join(current_directory, "bypasses", filename)
+4 -4
View File
@@ -1,4 +1,4 @@
import os from pathlib import Path
import re import re
from inspect import signature from inspect import signature
from difflib import SequenceMatcher from difflib import SequenceMatcher
@@ -39,6 +39,8 @@ from scrapling.core.storage import (
from scrapling.core.translator import translator_instance from scrapling.core.translator import translator_instance
from scrapling.core.utils import clean_spaces, flatten, html_forbidden, is_jsonable, log from scrapling.core.utils import clean_spaces, flatten, html_forbidden, is_jsonable, log
__DEFAULT_DB_FILE__ = str(Path(__file__).parent / "elements_storage.db")
class Selector(SelectorsGeneration): class Selector(SelectorsGeneration):
__slots__ = ( __slots__ = (
@@ -145,9 +147,7 @@ class Selector(SelectorsGeneration):
else: else:
if not storage_args: if not storage_args:
storage_args = { storage_args = {
"storage_file": os.path.join( "storage_file": __DEFAULT_DB_FILE__,
os.path.dirname(__file__), "elements_storage.db"
),
"url": url, "url": url,
} }