Skip to content

Commit 267e005

Browse files
committed
remove unnecessary imports, use find_spec
This was flagged by ruff check - if we just want to find out if a package is available, and don't need to actually import it, we can use importlib.util.find_spec() to resolve it. This can lead to a moderate speedup too, since the import might be slow.
1 parent f9cc2ea commit 267e005

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

brozzler/__init__.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020
import datetime
21+
import importlib.util
2122
import logging
2223
import threading
2324
from importlib.metadata import version as _version
@@ -414,25 +415,20 @@ def suggest_default_chrome_exe():
414415
"suggest_default_chrome_exe",
415416
]
416417

417-
# TODO try using importlib.util.find_spec to test for dependency presence
418-
# rather than try/except on import.
419-
# See https://docs.astral.sh/ruff/rules/unused-import/#example
420-
try:
421-
import doublethink # noqa: F401
422-
418+
if importlib.util.find_spec("doublethink"):
423419
# All of these imports use doublethink for real and are unsafe
424420
# to do if doublethink is unavailable.
425-
from brozzler.frontier import RethinkDbFrontier # noqa: F401
421+
from brozzler.frontier import RethinkDbFrontier
426422
from brozzler.model import (
427-
InvalidJobConf, # noqa: F401
428-
Job, # noqa: F401
429-
Page, # noqa: F401
430-
Site, # noqa: F401
431-
new_job, # noqa: F401
432-
new_job_file, # noqa: F401
433-
new_site, # noqa: F401
423+
InvalidJobConf,
424+
Job,
425+
Page,
426+
Site,
427+
new_job,
428+
new_job_file,
429+
new_site,
434430
)
435-
from brozzler.worker import BrozzlerWorker # noqa: F401
431+
from brozzler.worker import BrozzlerWorker
436432

437433
__all__.extend(
438434
[
@@ -447,8 +443,6 @@ def suggest_default_chrome_exe():
447443
"InvalidJobConf",
448444
]
449445
)
450-
except ImportError:
451-
pass
452446

453447
# we could make this configurable if there's a good reason
454448
MAX_PAGE_FAILURES = 3

brozzler/worker.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""
2020

2121
import datetime
22+
import importlib.util
2223
import io
2324
import json
2425
import socket
@@ -100,14 +101,8 @@ def __init__(
100101
if worker_id is not None:
101102
self.logger = self.logger.bind(worker_id=worker_id)
102103

103-
# TODO try using importlib.util.find_spec to test for dependency
104-
# presence rather than try/except on import.
105-
# See https://docs.astral.sh/ruff/rules/unused-import/#example
106-
107104
# We definitely shouldn't ytdlp if the optional extra is missing
108-
try:
109-
import yt_dlp # noqa: F401
110-
except ImportError:
105+
if not importlib.util.find_spec("yt_dlp"):
111106
self.logger.info(
112107
"optional yt-dlp extra not installed; setting skip_youtube_dl to True"
113108
)

tests/test_cli.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020
import importlib.metadata
21+
import importlib.util
2122
import os
2223
import subprocess
2324

@@ -47,14 +48,12 @@ def console_scripts():
4748
def cli_commands():
4849
commands = set(console_scripts().keys())
4950
commands.remove("brozzler-wayback")
50-
try:
51-
import gunicorn # noqa: F401
52-
except ImportError:
51+
if not importlib.util.find_spec("gunicorn"):
5352
commands.remove("brozzler-dashboard")
54-
try:
55-
import pywb # noqa: F401
56-
except ImportError:
53+
54+
if not importlib.util.find_spec("pywb"):
5755
commands.remove("brozzler-easy")
56+
5857
return commands
5958

6059

0 commit comments

Comments
 (0)