Skip to content

Commit

Permalink
Import command classes inline (#33)
Browse files Browse the repository at this point in the history
* Import command classes inline

* ruff

* Change test patch location
  • Loading branch information
freddyheppell authored Jul 12, 2024
1 parent cacb910 commit f762faf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/wpextract/cli/_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
from click import Choice
from click_option_group import optgroup

from wpextract import WPDownloader
from wpextract.cli._shared import (
CMD_ARGS,
empty_directory,
logging_options,
setup_logging,
setup_tqdm_redirect,
)
from wpextract.download import RequestSession
from wpextract.util.str import ensure_prefixes, ensure_suffix

dl_types = ["categories", "media", "pages", "posts", "tags", "users"]
Expand Down Expand Up @@ -141,6 +139,9 @@ def download(
OUT_JSON is the directory to output the downloaded JSON to. It must be an existing empty directory or a non-existent directory which will be created.
"""
from wpextract import WPDownloader
from wpextract.download import RequestSession

setup_logging(verbose, log)

types_to_dl = set(dl_types) - set(skip_types)
Expand Down
3 changes: 2 additions & 1 deletion src/wpextract/cli/_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import click

from wpextract import WPExtractor
from wpextract.cli._shared import (
CMD_ARGS,
directory,
Expand Down Expand Up @@ -40,6 +39,8 @@ def extract(
OUT_DIR is the directory to output the extracted JSON to. It must be an existing empty directory or a non-existent directory which will be created.
"""
from wpextract import WPExtractor

setup_logging(verbose, log)

with setup_tqdm_redirect(log is None):
Expand Down
4 changes: 3 additions & 1 deletion tests/cli/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
def mock_cls_invoke(mocker, runner, datadir, args=None):
if args is None:
args = []
dl_mock = mocker.patch("wpextract.cli._download.WPDownloader")
# Patch at source since the CLI imports within a function
dl_mock = mocker.patch("wpextract.WPDownloader")

result = runner.invoke(
cli, ["download", "https://example.org", str(datadir), *args]
Expand All @@ -17,6 +18,7 @@ def test_default_args(mocker, runner, datadir):
dl_mock, result = mock_cls_invoke(mocker, runner, datadir)
assert result.exit_code == 0

dl_mock.assert_called_once()
assert dl_mock.call_args.kwargs["target"] == "https://example.org/"
assert dl_mock.call_args.kwargs["out_path"] == datadir
assert len(dl_mock.call_args.kwargs["data_types"]) == 6
Expand Down
5 changes: 4 additions & 1 deletion tests/cli/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def mock_cls_invoke(mocker, runner, datadir, args=None, mkdirs=True):

if args is None:
args = []
dl_mock = mocker.patch("wpextract.cli._extract.WPExtractor")

# Patch at source since the CLI imports within a function
dl_mock = mocker.patch("wpextract.WPExtractor")

result = runner.invoke(cli, ["extract", str(in_dir), str(out_dir), *args])

Expand All @@ -21,6 +23,7 @@ def test_default_args(mocker, runner, datadir):
dl_mock, result = mock_cls_invoke(mocker, runner, datadir)
assert result.exit_code == 0

dl_mock.assert_called_once()
assert dl_mock.call_args.kwargs["json_root"] == datadir / "json_in"
assert dl_mock.call_args.kwargs["scrape_root"] is None
assert dl_mock.call_args.kwargs["json_prefix"] is None
Expand Down

0 comments on commit f762faf

Please sign in to comment.