Skip to content

Commit

Permalink
Small changes following last review
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Dec 6, 2024
1 parent f6c0968 commit bc18cd0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 34 deletions.
2 changes: 2 additions & 0 deletions scraper/src/mindtouch2zim/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
VERSION,
)

MINDTOUCH_TMP = os.getenv("MINDTOUCH_TMP")


@dataclasses.dataclass(kw_only=True)
class Context:
Expand Down
23 changes: 10 additions & 13 deletions scraper/src/mindtouch2zim/entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import os
import re
import threading
from pathlib import Path
Expand All @@ -16,7 +15,7 @@
STANDARD_KNOWN_BAD_ASSETS_REGEX,
VERSION,
)
from mindtouch2zim.context import Context
from mindtouch2zim.context import MINDTOUCH_TMP, Context


def prepare_context(raw_args: list[str], tmpdir: str) -> None:
Expand All @@ -26,8 +25,6 @@ def prepare_context(raw_args: list[str], tmpdir: str) -> None:
prog=NAME,
)

parser.register("type", "Path", lambda x: Path(x))

parser.add_argument(
"--creator",
help="Name of content creator.",
Expand All @@ -36,13 +33,13 @@ def prepare_context(raw_args: list[str], tmpdir: str) -> None:

parser.add_argument(
"--publisher",
help=f"Publisher name. Default: {Context.publisher!r}",
help=f"Publisher name. Default: {Context.publisher!s}",
)

parser.add_argument(
"--file-name",
help="Custom file name format for individual ZIMs. "
f"Default: {Context.file_name!r}",
f"Default: {Context.file_name!s}",
)

parser.add_argument(
Expand Down Expand Up @@ -82,7 +79,7 @@ def prepare_context(raw_args: list[str], tmpdir: str) -> None:
parser.add_argument(
"--secondary-color",
help="Secondary (background) color of ZIM UI. Default: "
f"{Context.secondary_color!r}",
f"{Context.secondary_color!s}",
)

parser.add_argument(
Expand Down Expand Up @@ -141,22 +138,22 @@ def prepare_context(raw_args: list[str], tmpdir: str) -> None:
parser.add_argument(
"--output",
help="Output folder for ZIMs. Default: /output",
type="Path",
type=Path,
dest="output_folder",
)

parser.add_argument(
"--tmp",
help="Temporary folder for cache, intermediate files, ...",
type="Path",
type=Path,
dest="tmp_folder",
)

parser.add_argument("--debug", help="Enable verbose output", action="store_true")

parser.add_argument(
"--zimui-dist",
type="Path",
type=Path,
help=(
"Dev option to customize directory containing Vite build output from the "
"ZIM UI Vue.JS application"
Expand All @@ -165,7 +162,7 @@ def prepare_context(raw_args: list[str], tmpdir: str) -> None:

parser.add_argument(
"--stats-filename",
type="Path",
type=Path,
help="Path to store the progress JSON file to.",
)

Expand Down Expand Up @@ -216,8 +213,8 @@ def prepare_context(raw_args: list[str], tmpdir: str) -> None:
# initialize some context properties that are "dynamic" (i.e. not constant
# values like an int, a string, ...)
if not args_dict.get("tmp_folder", None):
if tmp_from_env := os.getenv("MINDTOUCH_TMP"):
args_dict["tmp_folder"] = Path(tmp_from_env)
if MINDTOUCH_TMP:
args_dict["tmp_folder"] = Path(MINDTOUCH_TMP)
else:
args_dict["tmp_folder"] = Path(tmpdir)

Expand Down
2 changes: 1 addition & 1 deletion scraper/src/mindtouch2zim/zimconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def fmt(string: str) -> str:
except KeyError as e:
valid_placeholders = ", ".join(sorted(placeholders.keys()))
raise InvalidFormatError(
f"Invalid placeholder {e!s} in {string!r}, "
f"Invalid placeholder {e!s} in {string!s}, "
f"valid placeholders are: {valid_placeholders}"
) from e

Expand Down
24 changes: 4 additions & 20 deletions scraper/tests/test_entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import re
import tempfile
from pathlib import Path
from typing import Any

import pytest

import mindtouch2zim.entrypoint
from mindtouch2zim.context import Context
from mindtouch2zim.entrypoint import prepare_context

Expand Down Expand Up @@ -101,33 +101,17 @@ def test_entrypoint_defaults(
assert context.__getattribute__(context_name) == expected_context_value


@pytest.mark.parametrize(
"env_var_name, env_var_value, context_name, expected_context_value",
[
pytest.param(
"MINDTOUCH_TMP",
"../foo/bar",
"tmp_folder",
Path("../foo/bar"),
id="tmp_folder",
),
],
)
def test_entrypoint_defaults_env_var(
good_cli_args: list[str],
tmpdir: str,
env_var_name: str,
env_var_value: str,
context_name: str,
expected_context_value: Any,
):
"""Not passing optional args sources Context default in environement variable."""
try:
os.environ[env_var_name] = env_var_value
mindtouch2zim.entrypoint.MINDTOUCH_TMP = "../foo/bar"
prepare_context(good_cli_args, tmpdir)
assert context.__getattribute__(context_name) == expected_context_value
assert context.tmp_folder == Path("../foo/bar")
finally:
os.environ.pop(env_var_name)
mindtouch2zim.entrypoint.MINDTOUCH_TMP = None


@pytest.mark.parametrize(
Expand Down

0 comments on commit bc18cd0

Please sign in to comment.