Skip to content

Commit

Permalink
flexible category_members ns params, tag for release
Browse files Browse the repository at this point in the history
  • Loading branch information
fastily committed Feb 21, 2021
1 parent 91cf282 commit fd152e0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pwiki/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ['wgen', 'wiki']
__all__ = ['gquery', 'mquery', 'wgen', 'wiki']
14 changes: 8 additions & 6 deletions pwiki/wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def load_cookies(self, cookie_jar: Path = _DEFAULT_COOKIE_JAR) -> bool:
self._refresh_rights()
self.is_logged_in = True

log.debug("%s: successfully loaded cookies from '%s'", self, cookie_jar)

return True

def save_cookies(self, output_path: Path = _DEFAULT_COOKIE_JAR):
Expand Down Expand Up @@ -296,18 +298,18 @@ def categories_on_page(self, title: str) -> list[str]:
log.info("%s: fetching categories on pages: %s", self, title)
return self._xq_simple(MQuery.categories_on_page, title)

def category_members(self, title: str, ns: list[Union[NS, str]] = []) -> list[str]:
def category_members(self, title: str, ns: Union[list[Union[NS, str]], NS, str] = []) -> list[str]:
"""Fetches the elements in a category.
Args:
title (str): The title of the category to fetch elements from. Must include `Category:` prefix.
ns (list[Union[NS, str]], optional): Only return results that are in these namespaces. Optional, set empty list to disable. Defaults to [].
ns (Union[list[Union[NS, str]], NS, str], optional): Only return results that are in these namespaces. Optional, set empty list to disable. Defaults to [].
Returns:
list[str]: a `list` containing `title`'s category members.
"""
log.info("%s: fetching category members of '%s'", self, title)
return flatten_generator(GQuery.category_members(self, title, ns, MAX))
return flatten_generator(GQuery.category_members(self, title, ns if isinstance(ns, list) else [ns], MAX))

def category_size(self, title: str) -> int:
"""Queries the wiki and gets the number of pages categorized in `title`.
Expand Down Expand Up @@ -452,7 +454,7 @@ def links_on_page(self, title: str, *ns: Union[NS, str]) -> list[str]:
Returns:
list[str]: The `list` of wiki links contained in the text of `title`
"""
log.info("%s: fetching wikilinks on %s", self, title)
log.info("%s: fetching wikilinks on '%s'", self, title)
return self._xq_simple(MQuery.links_on_page, title, *ns)

def list_duplicate_files(self) -> list[str]:
Expand Down Expand Up @@ -517,7 +519,7 @@ def page_text(self, title: str) -> str:
Returns:
str: The text of `title`. `None` if `title` does not exist.
"""
log.info("%s: fetching page text of %s", self, title)
log.info("%s: fetching page text of '%s'", self, title)
return self._xq_simple(MQuery.page_text, title)

def prefix_index(self, ns: Union[NS, str], prefix: str) -> list[str]:
Expand Down Expand Up @@ -641,7 +643,7 @@ def what_transcludes_here(self, title: str, *ns: Union[NS, str]) -> list[str]:
Returns:
list[str]: The list of pages that transclude `title`.
"""
log.info("%s: fetching transclusions of %s", self, title)
log.info("%s: fetching transclusions of '%s'", self, title)
return self._xq_simple(MQuery.what_transcludes_here, title, *ns)

def whoami(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pwiki",
version="0.0.2",
version="1.0.0",
author="Fastily",
author_email="[email protected]",
description="A Python library for effortlessly interacting with Wikipedia/MediaWiki",
Expand Down
2 changes: 1 addition & 1 deletion test/test_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_categories_on_page(self):
self.assertListEqual(["Category:Fastily Test"], self.wiki.categories_on_page("User:Fastily/Sandbox/Page/3"))

def test_category_members(self):
self.assertCountEqual(["File:FastilyTest.png"], self.wiki.category_members("Category:Fastily Test2", [NS.FILE]))
self.assertCountEqual(["File:FastilyTest.png"], self.wiki.category_members("Category:Fastily Test2", NS.FILE))

def test_category_size(self):
self.assertEqual(2, self.wiki.category_size("Category:Fastily Test2"))
Expand Down

0 comments on commit fd152e0

Please sign in to comment.