Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CSV support in --slug #34

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Page navigation is now dynamically rendered reducing file sizes. (#24, #31)
- The `--slug` flag now supports comma delimited values. (#28)

## [0.1.0] - 2024-09-27

Expand Down
21 changes: 17 additions & 4 deletions src/devdocs2zim/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ class DocFilter(BaseModel):
all: bool | None
# If > 0 and not None, allow the first N of each slug without version.
first: int | None
# If specified, only the given slugs are allowed.
slugs: list[str] | None
# If specified, only the given slugs are allowed. Each entry may be comma delimited.
csv_slugs: list[str] | None

# If specified, slugs matching the regex are skipped.
skip_slug_regex: str | None
Expand All @@ -234,9 +234,10 @@ def add_flags(parser: argparse.ArgumentParser):
help="Fetch the provided Devdocs resource. "
"Slugs are the first path entry in the Devdocs URL. "
"For example, the slug for: `https://devdocs.io/gcc~12/` is `gcc~12`. "
"Use --slug several times to fetch multiple docs.",
"Use --slug several times or with values separated by a comma to "
"fetch multiple docs.",
action="append",
dest="slugs",
dest="csv_slugs",
metavar="SLUG",
)
doc_selection.add_argument(
Expand All @@ -257,6 +258,18 @@ def of(namespace: argparse.Namespace) -> "DocFilter":
"""Parses a namespace to create a new DocFilter."""
return DocFilter.model_validate(namespace, from_attributes=True)

@property
def slugs(self) -> list[str] | None:
"""Returns the parsed list of user supplied slugs, if specified."""
if self.csv_slugs is None:
return None

out: list[str] = []
for slug in self.csv_slugs:
out.extend(slug.split(","))

return out

def filter(self, docs: list[DevdocsMetadata]) -> list[DevdocsMetadata]:
"""Filters docs based on the user's choices."""

Expand Down
63 changes: 53 additions & 10 deletions tests/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_flags_all(self):
DocFilter(
all=True,
first=None,
slugs=None,
csv_slugs=None,
skip_slug_regex=None,
),
got,
Expand All @@ -175,11 +175,35 @@ def test_flags_slug(self):
DocFilter(
all=False,
first=None,
slugs=["first", "second"],
csv_slugs=["first", "second"],
skip_slug_regex=None,
),
got,
)
self.assertEqual(
["first", "second"],
got.slugs,
)

def test_flags_slug_csv(self):
parser = argparse.ArgumentParser()
DocFilter.add_flags(parser)

got = DocFilter.of(parser.parse_args(args=["--slug", "first,second"]))

self.assertEqual(
DocFilter(
all=False,
first=None,
csv_slugs=["first,second"],
skip_slug_regex=None,
),
got,
)
self.assertEqual(
["first", "second"],
got.slugs,
)

def test_flags_first(self):
parser = argparse.ArgumentParser()
Expand All @@ -191,7 +215,7 @@ def test_flags_first(self):
DocFilter(
all=False,
first=3,
slugs=None,
csv_slugs=None,
skip_slug_regex=None,
),
got,
Expand All @@ -213,14 +237,16 @@ def test_flags_regex(self):
DocFilter(
all=True,
first=None,
slugs=None,
csv_slugs=None,
skip_slug_regex="^$",
),
got,
)

def test_filter_all(self):
doc_filter = DocFilter(all=True, first=None, slugs=None, skip_slug_regex=None)
doc_filter = DocFilter(
all=True, first=None, csv_slugs=None, skip_slug_regex=None
)

got = doc_filter.filter(
[
Expand All @@ -234,7 +260,22 @@ def test_filter_all(self):

def test_filter_slugs(self):
doc_filter = DocFilter(
all=False, first=None, slugs=["foo", "bazz"], skip_slug_regex=None
all=False, first=None, csv_slugs=["foo", "bazz"], skip_slug_regex=None
)

got = doc_filter.filter(
[
DevdocsMetadata(name="foo", slug="foo"),
DevdocsMetadata(name="bar", slug="bar"),
DevdocsMetadata(name="bazz", slug="bazz"),
]
)

self.assertEqual(2, len(got))

def test_filter_slugs_csvs(self):
doc_filter = DocFilter(
all=False, first=None, csv_slugs=["foo,bazz"], skip_slug_regex=None
)

got = doc_filter.filter(
Expand All @@ -248,7 +289,9 @@ def test_filter_slugs(self):
self.assertEqual(2, len(got))

def test_filter_all_regex(self):
doc_filter = DocFilter(all=True, first=None, slugs=None, skip_slug_regex="^b")
doc_filter = DocFilter(
all=True, first=None, csv_slugs=None, skip_slug_regex="^b"
)

got = doc_filter.filter(
[
Expand All @@ -262,13 +305,13 @@ def test_filter_all_regex(self):

def test_filter_slugs_missing(self):
doc_filter = DocFilter(
all=False, first=None, slugs=["does_not_exist"], skip_slug_regex=None
all=False, first=None, csv_slugs=["does_not_exist"], skip_slug_regex=None
)

self.assertRaises(MissingDocumentError, doc_filter.filter, [])

def test_filter_first(self):
doc_filter = DocFilter(all=False, first=2, slugs=None, skip_slug_regex=None)
doc_filter = DocFilter(all=False, first=2, csv_slugs=None, skip_slug_regex=None)

got = doc_filter.filter(
[
Expand Down Expand Up @@ -303,7 +346,7 @@ def setUp(self):
self.generator = Generator(
devdocs_client=self.mock_client,
doc_filter=DocFilter(
all=True, first=None, slugs=None, skip_slug_regex=None
all=True, first=None, csv_slugs=None, skip_slug_regex=None
),
output_folder=output_folder,
zim_config=zim_defaults(),
Expand Down