jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/843508 )
Change subject: [IMPR] Allow all Site.categorymembers() parameters for Category.members()
......................................................................
[IMPR] Allow all Site.categorymembers() parameters for Category.members()
- enable all parameters of Site.categorymembers() with
Category.members() method
- call Category.members() to implement Category.articles to prevent
code duplication
Change-Id: I85be8e7730430e7d8e20b230866858e03d347203
---
M pywikibot/page/_category.py
1 file changed, 48 insertions(+), 45 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page/_category.py b/pywikibot/page/_category.py
index 69bdf0d..f94b8bd 100644
--- a/pywikibot/page/_category.py
+++ b/pywikibot/page/_category.py
@@ -121,7 +121,6 @@
def articles(self, *,
recurse: Union[int, bool] = False,
- total: Optional[int] = None,
**kwargs: Any) -> Iterable[Page]:
"""
Yield all articles in the current category.
@@ -142,29 +141,62 @@
3
4
- .. versionchanged:: 8.0.0
+ .. versionchanged:: 8.0
all parameters are keyword arguments only.
:param recurse: if not False or 0, also iterate articles in
subcategories. If an int, limit recursion to this number of
levels. (Example: ``recurse=1`` will iterate articles in
first-level subcats, but no deeper.)
+ :param kwargs: Additional parameters. Refer to
+ :meth:`APISite.categorymembers()
+ <pywikibot.site._generators.GeneratorsMixin.categorymembers>`
+ for complete list (*member_type* excluded).
+ """
+ if kwargs.pop('member_type', False):
+ raise TypeError(
+ "articles() got an unexpected keyword argument 'member_type'")
+
+ return self.members(
+ member_type=['page', 'file'], recurse=recurse, **kwargs)
+
+ def members(self, *,
+ recurse: bool = False,
+ total: Optional[int] = None,
+ **kwargs: Any) -> Iterable[Page]:
+ """Yield all category contents (subcats, pages, and files).
+
+ **Usage:**
+
+ >>> site = pywikibot.Site('wikipedia:test')
+ >>> cat = pywikibot.Category(site, 'Pywikibot')
+ >>> list(cat.members(member_type='subcat'))
+ [Category('Category:Subpage testing')]
+ >>> list(cat.members(member_type=['page', 'file']))
+ [Page('Pywikibot nobots test')]
+
+ Calling this method with ``member_type='subcat'`` is *almost*
+ equal to calling :meth:`subcategories`. Calling this method with
+ ``member_type=['page', 'file']`` is equal to calling
+ :meth:`articles`.
+
+ .. versionchanged:: 8.0
+ all parameters are keyword arguments only. Additional
+ parameters are supported.
+
+ :param recurse: if not False or 0, also iterate articles in
+ subcategories. If an int, limit recursion to this number of
+ levels. (Example: ``recurse=1`` will iterate articles in
+ first-level subcats, but no deeper.)
:param total: iterate no more than this number of pages in
total (at all levels)
:param kwargs: Additional parameters. Refer to
:meth:`APISite.categorymembers()
<pywikibot.site._generators.GeneratorsMixin.categorymembers>`
- for complete list (*member_type* excluded).
+ for complete list.
"""
- if kwargs.pop('member_type', False):
- raise TypeError(
- "articles() got an unexpected keyword argument 'member_type'")
-
seen = set()
- for member in self.site.categorymembers(self,
- total=total,
- member_type=['page', 'file'],
- **kwargs):
+ for member in self.site.categorymembers(self, total=total, **kwargs):
if recurse:
seen.add(hash(member))
yield member
@@ -176,45 +208,16 @@
if recurse:
if not isinstance(recurse, bool) and recurse:
recurse -= 1
+
for subcat in self.subcategories():
- for article in subcat.articles(recurse=recurse,
- total=total,
- **kwargs):
- hash_value = hash(article)
+ for member in subcat.members(
+ recurse=recurse, total=total, **kwargs):
+ hash_value = hash(member)
if hash_value in seen:
continue
seen.add(hash_value)
- yield article
- if total is None:
- continue
-
- total -= 1
- if total == 0:
- return
-
- def members(self, recurse: bool = False,
- namespaces=None,
- total: Optional[int] = None,
- content: bool = False):
- """Yield all category contents (subcats, pages, and files).
-
- :rtype: typing.Iterable[pywikibot.Page]
- """
- for member in self.site.categorymembers(
- self, namespaces=namespaces, total=total, content=content):
- yield member
- if total is not None:
- total -= 1
- if total == 0:
- return
- if recurse:
- if not isinstance(recurse, bool) and recurse:
- recurse -= 1
- for subcat in self.subcategories():
- for article in subcat.members(
- recurse, namespaces, total=total, content=content):
- yield article
+ yield member
if total is None:
continue
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/843508
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I85be8e7730430e7d8e20b230866858e03d347203
Gerrit-Change-Number: 843508
Gerrit-PatchSet: 9
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/846940 )
Change subject: [bugfix] fix call of wrapper.py script in pwb.py caller
......................................................................
[bugfix] fix call of wrapper.py script in pwb.py caller
invoking any scripts from pwb.py caller fails because importing wrapper.py
also imports pywikibot and leads config.py to install user-config.py
even if PYWIKIBOT_NO_USER_CONFIG is set.
- use runpy.run_path to call the wrapper.py script from pwb.py caller
- fix the generate_user_files.py path in wrapper.py
- adjust exc_text in config.get_base_dir()
Bug: T321452
Change-Id: I33ac035ef8b2ea1f2600ff650dff4c3397926bd7
---
M pwb.py
M pywikibot/config.py
M pywikibot/scripts/wrapper.py
3 files changed, 13 insertions(+), 9 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pwb.py b/pwb.py
index 0e12919..e515776 100644
--- a/pwb.py
+++ b/pwb.py
@@ -7,7 +7,9 @@
#
# Distributed under the terms of the MIT license.
#
+import runpy
import sys
+from pathlib import Path
VERSIONS_REQUIRED_MESSAGE = """
Pywikibot is not available on:
@@ -28,8 +30,8 @@
def main():
"""Entry point for :func:`tests.utils.execute_pwb`."""
- from pywikibot.scripts import wrapper
- wrapper.main()
+ path = Path().resolve() / 'pywikibot' / 'scripts' / 'wrapper.py'
+ runpy.run_path(str(path), run_name='__main__')
if __name__ == '__main__':
diff --git a/pywikibot/config.py b/pywikibot/config.py
index 11d7b30..ae751f4 100644
--- a/pywikibot/config.py
+++ b/pywikibot/config.py
@@ -377,20 +377,22 @@
if not os.path.isabs(base_dir):
base_dir = os.path.normpath(os.path.join(os.getcwd(), base_dir))
+
# make sure this path is valid and that it contains user-config file
if not os.path.isdir(base_dir):
raise RuntimeError(f"Directory '{base_dir}' does not exist.")
+
# check if config_file is in base_dir
if not exists(base_dir):
- exc_text = 'No {} found in directory {!r}.\n'.format(
- config_file, base_dir)
+ exc_text = f'No {config_file} found in directory {base_dir!r}.\n'
if __no_user_config is None:
assert get_base_dir.__doc__ is not None
exc_text += (
- ' Please check that {0} is stored in the correct location.\n'
- ' Directory where {0} is searched is determined as follows:'
- '\n\n '.format(config_file)) + get_base_dir.__doc__
+ '\nPlease check that {0} is stored in the correct location.'
+ '\nDirectory where {0} is searched is determined as follows:'
+ '\n\n '.format(config_file)
+ ) + get_base_dir.__doc__
raise RuntimeError(exc_text)
if __no_user_config != '2':
diff --git a/pywikibot/scripts/wrapper.py b/pywikibot/scripts/wrapper.py
index db59e06..4446ae8 100755
--- a/pywikibot/scripts/wrapper.py
+++ b/pywikibot/scripts/wrapper.py
@@ -308,8 +308,8 @@
print('NOTE: user-config.py was not found!')
print('Please follow the prompts to create it:')
- run_python_file(str(wrapper_dir.joinpath(
- 'pywikibot', 'scripts', 'generate_user_files.py')), [])
+ run_python_file(
+ str(wrapper_dir.joinpath('generate_user_files.py')), [])
# because we have loaded pywikibot without user-config.py loaded,
# we need to re-start the entire process. Ask the user to do so.
print('Now, you have to re-execute the command to start your script.')
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/846940
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I33ac035ef8b2ea1f2600ff650dff4c3397926bd7
Gerrit-Change-Number: 846940
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged