jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/920662 )
Change subject: [IMPR] Make textlib._get_regexes a puplic function
......................................................................
[IMPR] Make textlib._get_regexes a puplic function
- Make textlib._get_regexes a puplic function as get_regexes
- keys parameter may be a plain str
- site may be optional because it is only used for several keys
- use plain str if only one key is used
- remove site parameter if is is not used
- remove unused site parameter of _extract_headings function
- replace _get_regexes with get_regexes for all its usage
- add _create_default_regexes to documentation because it is used in
replace.py
Bug: T336144
Change-Id: Ic9f673e508228998b28375563027d6e9631f4e4a
---
M pywikibot/textlib.py
M pywikibot/cosmetic_changes.py
M scripts/replace.py
M scripts/category.py
4 files changed, 64 insertions(+), 23 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index 003d670..9235462 100644
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -51,7 +51,7 @@
'your_script_name_2']
"""
#
-# (C) Pywikibot team, 2006-2022
+# (C) Pywikibot team, 2006-2023
#
# Distributed under the terms of the MIT license.
#
@@ -68,7 +68,7 @@
from pywikibot.textlib import (
FILE_LINK_REGEX,
MultiTemplateMatchBuilder,
- _get_regexes,
+ get_regexes,
)
from pywikibot.tools import first_lower, first_upper
from pywikibot.tools.chars import url2string
@@ -682,7 +682,7 @@
return text
skippings = ['comment', 'category']
- skip_regexes = _get_regexes(skippings, self.site)
+ skip_regexes = get_regexes(skippings, self.site)
# site defined templates
skip_templates = {
'cs': ('Pahýl[ _]část',), # stub section
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index ce63410..3c73b34 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -46,7 +46,7 @@
ETPType = List[Tuple[str, OrderedDictType[str, str]]]
# cache for replaceExcept to avoid recompile or regexes each call
-_regex_cache = {}
+_regex_cache: Dict[str, Pattern[str]] = {}
# The regex below collects nested templates, providing simpler
# identification of templates used at the top-level of wikitext.
@@ -253,7 +253,15 @@
def _create_default_regexes() -> None:
- """Fill (and possibly overwrite) _regex_cache with default regexes."""
+ """Fill (and possibly overwrite) ``_regex_cache`` with default regexes.
+
+ The following keys are provided: ``category``, ``comment``, ``file``,
+ ``header``, ``hyperlink``, ``interwiki``, ``invoke``, ``link``,
+ ``pagelist``, ``property``, ``startcolon``, ``startspace``, ``table``,
+ ``template``.
+
+ :meta public:
+ """
_regex_cache.update({
# categories
'category': (r'\[\[ *(?:%s)\s*:.*?\]\]',
@@ -304,16 +312,29 @@
})
-def _get_regexes(keys: Iterable, site) -> List[Pattern[str]]:
+def get_regexes(
+ keys: Union[str, Iterable[str]],
+ site: Optional['pywikibot.site.BaseSite'] = None
+) -> List[Pattern[str]]:
"""Fetch compiled regexes.
- :meta public:
+ .. versionchanged:: 8.2
+ ``_get_regexes`` becomes a public function.
+ *keys* may be a single string; *site* is optional.
+
+ :param keys: a single key or an iterable of keys whose regex pattern
+ should be given
+ :param site: a BaseSite object needed for ``category``, ``file``,
+ ``interwiki``, ``invoke`` and ``property`` keys
+ :raises ValueError: site cannot be None.
"""
if not _regex_cache:
_create_default_regexes()
- result = []
+ if isinstance(keys, str):
+ keys = [keys]
+ result = []
for exc in keys:
if not isinstance(exc, str):
# assume it's a regular expression
@@ -332,7 +353,7 @@
else:
if not site and exc in ('interwiki', 'property', 'invoke',
'category', 'file'):
- raise ValueError(f'Site cannot be None for the {exc!r} regex')
+ raise ValueError(f'site cannot be None for the {exc!r} regex')
if (exc, site) not in _regex_cache:
re_text, re_var = _regex_cache[exc]
@@ -396,7 +417,7 @@
if not old.search(text):
return text + marker
- dontTouchRegexes = _get_regexes(exceptions, site)
+ dontTouchRegexes = get_regexes(exceptions, site)
index = 0
replaced = 0
@@ -500,7 +521,7 @@
if provided as an ordered collection (list, tuple)
:param tags: The exact set of parts which should be removed using
- keywords from textlib._get_regexes().
+ keywords from :func:`get_regexes`.
:param include: Or, in alternative, default parts that shall not
be removed.
:param site: Site to be used for site-dependent regexes. Default
@@ -518,7 +539,7 @@
# ("Note" at the end of the section)
if include:
tags = [tag for tag in tags if tag not in include]
- regexes = _get_regexes(tags, site)
+ regexes = get_regexes(tags, site)
for regex in regexes:
text = regex.sub('', text)
return text
@@ -917,10 +938,10 @@
_Content = namedtuple('_Content', ('header', 'sections', 'footer'))
-def _extract_headings(text: str, site) -> list:
+def _extract_headings(text: str) -> list:
"""Return _Heading objects."""
headings = []
- heading_regex = _get_regexes(['header'], site)[0]
+ heading_regex = get_regexes('header')[0]
for match in heading_regex.finditer(text):
start, end = match.span()
if not isDisabled(text, start) and not isDisabled(text, end):
@@ -981,11 +1002,11 @@
.. versionadded:: 3.0
"""
- headings = _extract_headings(text, site)
+ headings = _extract_headings(text)
sections = _extract_sections(text, headings)
# Find header and footer contents
header = text[:headings[0].start] if headings else text
- cat_regex, interwiki_regex = _get_regexes(('category', 'interwiki'), site)
+ cat_regex, interwiki_regex = get_regexes(['category', 'interwiki'], site)
langlink_pattern = interwiki_regex.pattern.replace(':?', '')
last_section_content = sections[-1].content if sections else header
footer = re.search(
@@ -1251,7 +1272,7 @@
above_interwiki.append(comment)
if above_interwiki:
- interwiki = _get_regexes(['interwiki'], site)[0]
+ interwiki = get_regexes('interwiki', site)[0]
first_interwiki = interwiki.search(newtext)
for reg in above_interwiki:
special = reg.search(newtext)
@@ -1565,7 +1586,7 @@
under_categories.append(stub)
if under_categories:
- category = _get_regexes(['category'], site)[0]
+ category = get_regexes('category', site)[0]
for last_category in category.finditer(newtext):
pass
for reg in under_categories:
diff --git a/scripts/category.py b/scripts/category.py
index 6df0aea..4c3a7a9 100755
--- a/scripts/category.py
+++ b/scripts/category.py
@@ -143,7 +143,7 @@
:mod:`pagegenerators` are supported with "move" and "remove" action.
"""
#
-# (C) Pywikibot team, 2004-2022
+# (C) Pywikibot team, 2004-2023
#
# Distributed under the terms of the MIT license.
#
@@ -1184,7 +1184,7 @@
# skip initial templates, images and comments for articles.
if member.namespace() == member.site.namespaces.MAIN:
excludes = ('template', 'file', 'comment')
- regexes = textlib._get_regexes(excludes, member.site)
+ regexes = textlib.get_regexes(excludes, member.site)
i = 0
while i < 3:
i = 0
diff --git a/scripts/replace.py b/scripts/replace.py
index 2a19090..b9d2b66 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -142,7 +142,7 @@
the top of the help.
"""
#
-# (C) Pywikibot team, 2004-2022
+# (C) Pywikibot team, 2004-2023
#
# Distributed under the terms of the MIT license.
#
@@ -504,8 +504,8 @@
regular expressions.
inside-tags
A list of strings. These strings must be keys from the
- dictionary in textlib._create_default_regexes() or must be
- accepted by textlib._get_regexes().
+ dictionary in :func:`textlib._create_default_regexes` or must be
+ accepted by :func:`textlib.get_regexes`.
:keyword allowoverlap: when matches overlap, all of them are replaced.
:type allowoverlap: bool
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/920662
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: Ic9f673e508228998b28375563027d6e9631f4e4a
Gerrit-Change-Number: 920662
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: JJMC89 <JJMC89.Wikimedia(a)gmail.com>
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/920200 )
Change subject: [IMPR] use some keyword-only parameters in _deprecate functions
......................................................................
[IMPR] use some keyword-only parameters in _deprecate functions
- use some keyword-only parameters only with issue_deprecation_warning
and redirect_func
- add some type hints
Patch detached from I3f24af286
Change-Id: I8039d6d0fcb3a28fed8cb974ddf572a5a8d6fe5c
---
M pywikibot/tools/_deprecate.py
1 file changed, 37 insertions(+), 12 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/tools/_deprecate.py b/pywikibot/tools/_deprecate.py
index 1cb2fd9..ca6f1eb 100644
--- a/pywikibot/tools/_deprecate.py
+++ b/pywikibot/tools/_deprecate.py
@@ -28,10 +28,11 @@
import re
import sys
import types
+
from contextlib import suppress
from importlib import import_module
from inspect import getfullargspec
-from typing import Any, Optional
+from typing import Any, Optional, Union
from warnings import warn
@@ -162,7 +163,7 @@
return outer_wrapper
-def _build_msg_string(instead: str, since: str) -> str:
+def _build_msg_string(instead: Optional[str], since: Optional[str]) -> str:
"""Build a deprecation warning message format string.
.. versionadded:: 3.0
@@ -183,19 +184,24 @@
return msg.format(since=' since release ' + since if since else '')
-def issue_deprecation_warning(name: str, instead: str = '', depth: int = 2,
- warning_class=None, since: str = '') -> None:
+def issue_deprecation_warning(name: str,
+ instead: Optional[str] = None,
+ depth: int = 2, *,
+ warning_class: Optional[type] = None,
+ since: Optional[str] = None):
"""Issue a deprecation warning.
.. versionchanged:: 7.0
- `since` parameter must be a release number, not a timestamp.
+ *since* parameter must be a release number, not a timestamp.
+
+ .. versionchanged:: 8.2
+ *warning_class* and *since* are keyword-only parameters.
:param name: the name of the deprecated object
:param instead: suggested replacement for the deprecated object
:param depth: depth + 1 will be used as stacklevel for the warnings
- :param warning_class: a warning class (category) to be used, defaults to
- FutureWarning
- :type warning_class: type
+ :param warning_class: a warning class (category) to be used,
+ defaults to FutureWarning
:param since: a version string string when the method was deprecated
"""
msg = _build_msg_string(instead, since)
@@ -298,7 +304,7 @@
return decorator
-def deprecate_arg(old_arg: str, new_arg):
+def deprecate_arg(old_arg: str, new_arg: Union[str, None, bool]):
"""Decorator to declare old_arg deprecated and replace it with new_arg.
Usage:
@@ -317,7 +323,6 @@
:param old_arg: old keyword
:param new_arg: new keyword
- :type new_arg: str or None or bool
"""
return deprecated_args(**{old_arg: new_arg})
@@ -484,7 +489,8 @@
return decorator
-def redirect_func(target, source_module: Optional[str] = None,
+def redirect_func(target, *,
+ source_module: Optional[str] = None,
target_module: Optional[str] = None,
old_name: Optional[str] = None,
class_name: Optional[str] = None,
@@ -497,7 +503,10 @@
parameters.
.. versionchanged:: 7.0
- ``since`` parameter must be a release number, not a timestamp.
+ *since* parameter must be a release number, not a timestamp.
+
+ .. versionchanged:: 8.2
+ All parameters except *target* are keyword-only parameters.
:param target: The targeted function which is to be executed.
:type target: callable
@@ -522,6 +531,7 @@
old_name, new_name, since=since,
warning_class=None if future_warning else DeprecationWarning)
return target(*a, **kw)
+
if target_module is None:
target_module = target.__module__
if target_module and target_module[-1] != '.':
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/920200
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: I8039d6d0fcb3a28fed8cb974ddf572a5a8d6fe5c
Gerrit-Change-Number: 920200
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info(a)gno.de>
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/+/433720 )
Change subject: [IMPR] Derive NoReferencesBot from AutomaticTWSummaryBot
......................................................................
[IMPR] Derive NoReferencesBot from AutomaticTWSummaryBot
Change-Id: I74aca014cb31dd5f71bd34af0d6ca998639ae61c
---
M scripts/noreferences.py
1 file changed, 18 insertions(+), 10 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/noreferences.py b/scripts/noreferences.py
index 5be051a..65c8308 100755
--- a/scripts/noreferences.py
+++ b/scripts/noreferences.py
@@ -29,7 +29,7 @@
a list of affected articles
"""
#
-# (C) Pywikibot team, 2007-2022
+# (C) Pywikibot team, 2007-2023
#
# Distributed under the terms of the MIT license.
#
@@ -38,7 +38,7 @@
import pywikibot
from pywikibot import i18n, pagegenerators, textlib
-from pywikibot.bot import ExistingPageBot, SingleSiteBot
+from pywikibot.bot import AutomaticTWSummaryBot, ExistingPageBot, SingleSiteBot
from pywikibot.exceptions import LockedPageError
from pywikibot.pagegenerators import XMLDumpPageGenerator
@@ -511,7 +511,7 @@
XMLDumpPageGenerator, text_predicate=_match_xml_page_text)
-class NoReferencesBot(SingleSiteBot, ExistingPageBot):
+class NoReferencesBot(AutomaticTWSummaryBot, SingleSiteBot, ExistingPageBot):
"""References section bot."""
@@ -579,8 +579,8 @@
:return: The modified pagetext
"""
# Do we have a malformed <reference> tag which could be repaired?
- # Set the edit summary for this case
- self.comment = i18n.twtranslate(self.site, 'noreferences-fix-tag')
+ # Set the edit summary key for this case
+ self.summary_key = 'noreferences-fix-tag'
# Repair two opening tags or an opening and an empty tag
pattern = re.compile(r'< *references *>(.*?)'
@@ -595,8 +595,8 @@
return re.sub(pattern, '<references />', oldText)
# Is there an existing section where we can add the references tag?
- # Set the edit summary for this case
- self.comment = i18n.twtranslate(self.site, 'noreferences-add-tag')
+ # Set the edit summary key for this case
+ self.summary_key = 'noreferences-add-tag'
for section in i18n.translate(self.site, referencesSections):
sectionR = re.compile(fr'\r?\n=+ *{section} *=+ *\r?\n')
index = 0
@@ -725,12 +725,11 @@
try:
text = page.text
except LockedPageError:
- pywikibot.warning('Page {} is locked?!'
- .format(page.title(as_link=True)))
+ pywikibot.warning(f'Page {page} is locked?!')
return
if self.lacksReferences(text):
- self.put_current(self.addReferences(text), summary=self.comment)
+ self.put_current(self.addReferences(text))
def main(*args: str) -> None:
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/433720
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: I74aca014cb31dd5f71bd34af0d6ca998639ae61c
Gerrit-Change-Number: 433720
Gerrit-PatchSet: 10
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Framawiki <framawiki(a)tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb(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/+/921708 )
Change subject: [IMPR] improve documentation
......................................................................
[IMPR] improve documentation
- add documentation about how to leave shell
- add exitmsg parameter to code.interact which is available since
Python 3.6
Change-Id: Ie1176e22495d330bb3fb0db96a309d8e4a5fe12e
---
M pywikibot/scripts/shell.py
1 file changed, 25 insertions(+), 5 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/scripts/shell.py b/pywikibot/scripts/shell.py
index 1b53993..bdfe181 100755
--- a/pywikibot/scripts/shell.py
+++ b/pywikibot/scripts/shell.py
@@ -1,6 +1,8 @@
#!/usr/bin/env python3
-"""
-Spawns an interactive Python shell and imports the pywikibot library.
+"""Spawns an interactive Python shell and imports the pywikibot library.
+
+To exit the shell, type :kbd:`ctrl-D` (Linux) or :kbd:`ctrl-Z` (Windows)
+or use the :func:`exit` function.
The following local option is supported::
@@ -14,7 +16,7 @@
.. versionchanged:: 7.0
moved to pywikibot.scripts
"""
-# (C) Pywikibot team, 2014-2022
+# (C) Pywikibot team, 2014-2023
#
# Distributed under the terms of the MIT license.
#
@@ -23,7 +25,11 @@
def main(*args: str) -> None:
- """Script entry point."""
+ """Script entry point.
+
+ .. versionchanged:: 8.2
+ *exitmsg* was added for :func:`code.interact`.
+ """
args = list(args)
if '-noimport' in args:
args.remove('-noimport')
@@ -45,7 +51,8 @@
if hasattr(sys, '__interactivehook__'):
sys.__interactivehook__()
- code.interact("""Welcome to the Pywikibot interactive shell!""", local=env)
+ code.interact('Welcome to the Pywikibot interactive shell!', local=env,
+ exitmsg='Thank you for using Pywikibot; exiting now...\n')
if __name__ == '__main__': # pragma: no cover
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/921708
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: Ie1176e22495d330bb3fb0db96a309d8e4a5fe12e
Gerrit-Change-Number: 921708
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged