jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/984129 )
Change subject: [doc] update documentation
......................................................................
[doc] update documentation
Change-Id: I764ae26ef8dceca9bb62a2534e6ab772c6b3f0dd
---
M pywikibot/site/_apisite.py
M pywikibot/tools/collections.py
2 files changed, 14 insertions(+), 4 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index 250c427..3c46cad 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -572,11 +572,11 @@
>>> limit.delay, limit.ratio # doctest: +SKIP
(0.0, inf)
- .. seealso:: :class:`tools.collections.RateLimit` for RateLimit
- examples.
.. note:: It is not verified whether ``action`` parameter has a
valid value.
- .. seealso:: :api:`Ratelimit`
+ .. seealso::
+ - :class:`tools.collections.RateLimit` for RateLimit examples.
+ - :api:`Ratelimit`
.. versionadded:: 9.0
:param action: action which might be limited
diff --git a/pywikibot/tools/collections.py b/pywikibot/tools/collections.py
index 38e330a..cd4d9a9 100644
--- a/pywikibot/tools/collections.py
+++ b/pywikibot/tools/collections.py
@@ -305,7 +305,8 @@
<pywikibot.site._apisite.APISite.ratelimit>`.
.. note:: :meth:`delay` and :meth:`ratio` properties cannot be
- sliced or used with tuple indices. The must be used as attributes.
+ sliced or used with tuple indices. They must be used as
+ attributes.
>>> limit = RateLimit('user', 500, 10)
>>> limit
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/984129
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: I764ae26ef8dceca9bb62a2534e6ab772c6b3f0dd
Gerrit-Change-Number: 984129
Gerrit-PatchSet: 2
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/+/973395 )
Change subject: Pass global -nolog argument into bot script from wrapper.
......................................................................
Pass global -nolog argument into bot script from wrapper.
Scripts using handle_args function will create command log even -nolog is
given as global option to pwb wrapper script. Therefore pass -nolog option
as script option.
Bug: T328900
Change-Id: Ib8203600a2925680e979867d8603e0c3e05d8188
---
M pywikibot/scripts/wrapper.py
1 file changed, 19 insertions(+), 0 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/scripts/wrapper.py b/pywikibot/scripts/wrapper.py
index c53d6af..5b08cc8 100755
--- a/pywikibot/scripts/wrapper.py
+++ b/pywikibot/scripts/wrapper.py
@@ -433,6 +433,11 @@
"""
global filename
+ # If -nolog options are in global args, add them to
+ # script args.
+ if '-nolog' in global_args:
+ script_args.append('-nolog')
+
if global_args: # don't use sys.argv
unknown_args = pwb.handle_args(global_args)
if unknown_args: # pragma: no cover
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/973395
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: Ib8203600a2925680e979867d8603e0c3e05d8188
Gerrit-Change-Number: 973395
Gerrit-PatchSet: 5
Gerrit-Owner: Enag2000 <enag2000(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/+/836251 )
Change subject: [IMPR] Add APISite.ratelimit() method
......................................................................
[IMPR] Add APISite.ratelimit() method
- add a RateLimit namedtuple to tools/collections.py
- add APISite.ratelimit() method
- use it in api.Request instead of private _ratelimited() method
to handle ratelimited warning
- add some tests
Bug: T304808
Change-Id: I5db248dc4ef4bea03ad3ac4589830c209b961a04
---
M pywikibot/site/_apisite.py
M pywikibot/data/api/_requests.py
M pywikibot/tools/collections.py
M tests/site_tests.py
4 files changed, 204 insertions(+), 16 deletions(-)
Approvals:
JJMC89: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api/_requests.py b/pywikibot/data/api/_requests.py
index 3b63cb2..916fc41 100644
--- a/pywikibot/data/api/_requests.py
+++ b/pywikibot/data/api/_requests.py
@@ -918,21 +918,11 @@
return True
def _ratelimited(self) -> None:
- """Handle ratelimited warning."""
- ratelimits = self.site.userinfo['ratelimits']
- delay = None
+ """Handle ratelimited warning.
- ratelimit = ratelimits.get(self.action, {})
- # find the lowest wait time for the given action
- for limit in ratelimit.values():
- seconds = limit['seconds']
- hits = limit['hits']
- delay = min(delay or seconds, seconds / hits)
-
- if not delay:
- pywikibot.warning(
- f'No rate limit found for action {self.action}')
- self.wait(delay)
+ This is also called from :meth:`_default_warning_handler`.
+ """
+ self.wait(self.site.ratelimit(self.action).delay)
def _bad_token(self, code) -> bool:
"""Check for bad token.
diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index 1b4b1e6..2135184 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -78,6 +78,7 @@
merge_unique_dicts,
normalize_username,
)
+from pywikibot.tools.collections import RateLimit
if TYPE_CHECKING:
@@ -85,8 +86,8 @@
__all__ = ('APISite', )
-_mw_msg_cache: DefaultDict[str, dict[str, str]] = defaultdict(dict)
+_mw_msg_cache: DefaultDict[str, dict[str, str]] = defaultdict(dict)
_CompType = Union[int, str, 'pywikibot.page.Page', 'pywikibot.page.Revision']
_RequestWrapperT = TypeVar('_RequestWrapperT', bound='api._RequestWrapper')
@@ -498,6 +499,108 @@
return int(parameter['limit']) # T78333, T161783
+ def ratelimit(self, action: str) -> RateLimit:
+ """Get the rate limit for a given action.
+
+ This method get the ratelimit for a given action and returns a
+ :class:`tools.collections.RateLimit` namedtuple which has the
+ following fields and properties:
+
+ - ``group`` --- The current user group returned by the API. If
+ the user is not logged in, the group will be 'ip'.
+ - ``hits`` --- rate limit hits; API requests should not exceed
+ this limit value for the given action.
+ - ``seconds`` --- time base in seconds for the maximum hits
+ - ``delay`` --- *(property)* calculated as seconds per hits
+ which may be used for wait cycles.
+ - ``ratio`` --- *(property)* inverse of delay, calculated as
+ hits per seconds. The result may be Infinite.
+
+ If the user has 'noratelimit' rights, :meth:`maxlimit` is used
+ for ``hits`` and ``seconds`` will be 0. 'noratelimit' is
+ returned as group parameter in that case.
+
+ If no rate limit is found for the given action, :meth:`maxlimit`
+ is used for ``hits`` and ``seconds`` will be
+ :ref:`config.put_throttle<Settings to Avoid Server Overload>`.
+ As group parameter 'unknown' is returned in that case.
+
+ **Examples:**
+
+ This is an example for a bot user which is not logged in. The
+ rate limit user group is 'ip'
+
+ >>> site = pywikibot.Site()
+ >>> limit = site.ratelimit('edit') # get rate limit for 'edit' action
+ >>> limit
+ RateLimit(group='ip', hits=8, seconds=60)
+ >>> limit.delay # delay and ratio must be get as attributes
+ 7.5
+ >>> site.ratelimit('purge').hits # get purge hits
+ 30
+ >>> group, *limit = site.ratelimit('urlshortcode')
+ >>> group # the user is not logged in, we get 'ip' as group
+ 'ip'
+ >>> limit # starred assignment is allowed for the fields
+ [10, 120]
+
+ After login to the site and the rate limit will change. The
+ limit user group might be 'user':
+
+ >>> limit = site.ratelimit('edit') # doctest: +SKIP
+ >>> limit # doctest: +SKIP
+ RateLimit(group='user', hits=90, seconds=60)
+ >>> limit.ratio # doctest: +SKIP
+ 1.5
+ >>> limit = site.ratelimit('urlshortcode') # no action limit found
+ >>> group, *limits = limit
+ >>> group # doctest: +SKIP
+ 'unknown' # the group is 'unknown' because action was not found
+ >>> limits # doctest: +SKIP
+ (50, 10) # hits is maxlimit and seconds is config.put_throttle
+ >>> site.maxlimit, pywikibot.config.put_throttle
+ (50, 10)
+
+ If a user is logged in and has no rate limit, e.g bot accounts,
+ we always get a default RateLimit namedtuple like this:
+
+ >>> site.has_right['noratelimit'] # doctest: +SKIP
+ True
+ >>> limit = site.ratelimit('any_action') # maxlimit is used
+ >>> limit # doctest: +SKIP
+ RateLimit(group='noratelimit', hits=500, seconds=0)
+ >>> limit.delay, limit.ratio # doctest: +SKIP
+ (0.0, inf)
+
+ .. seealso:: :class:`tools.collections.RateLimit` for RateLimit
+ examples.
+ .. note:: It is not verified whether ``action`` parameter has a
+ valid value.
+ .. seealso:: :api:`Ratelimit`
+ .. versionadded:: 9.0
+
+ :param action: action which might be limited
+ :return: RateLimit tuple with ``group``, ``hits`` and ``seconds``
+ fields and properties for ``delay`` and ``ratio``.
+ """
+ ratio = 0
+ for key, value in self.userinfo['ratelimits'].get(action, {}).items():
+ h, s = value['hits'], value['seconds']
+ # find the highest ratio hits per seconds
+ if h / s > ratio:
+ limit = value
+ limit['group'] = key
+ ratio = h / s
+
+ if not ratio: # no limits found
+ limit = {'hits': self.maxlimit}
+ if self.has_right('noratelimit'):
+ limit['group'] = 'noratelimit'
+ else:
+ limit['seconds'] = pywikibot.config.put_throttle
+
+ return RateLimit(**limit)
+
@property
def userinfo(self) -> dict[str, Any]:
"""Retrieve userinfo from site and store in _userinfo attribute.
diff --git a/pywikibot/tools/collections.py b/pywikibot/tools/collections.py
index 63fbe43..38e330a 100644
--- a/pywikibot/tools/collections.py
+++ b/pywikibot/tools/collections.py
@@ -11,7 +11,7 @@
from collections.abc import Collection, Generator, Iterator, Mapping
from contextlib import suppress
from itertools import chain
-from typing import Any
+from typing import Any, NamedTuple
from pywikibot.backports import Generator as GeneratorType
@@ -21,6 +21,7 @@
'DequeGenerator',
'EmptyDefault',
'GeneratorWrapper',
+ 'RateLimit',
'SizedKeyCollection',
'EMPTY_DEFAULT',
)
@@ -294,3 +295,55 @@
"""Restart the generator."""
with suppress(AttributeError):
del self._started_gen
+
+
+class RateLimit(NamedTuple):
+
+ """A namedtuple which can hold rate limit content.
+
+ This class is used by :meth:`APISite.ratelimit()
+ <pywikibot.site._apisite.APISite.ratelimit>`.
+
+ .. note:: :meth:`delay` and :meth:`ratio` properties cannot be
+ sliced or used with tuple indices. The must be used as attributes.
+
+ >>> limit = RateLimit('user', 500, 10)
+ >>> limit
+ RateLimit(group='user', hits=500, seconds=10)
+ >>> limit.delay
+ 0.02
+ >>> limit.ratio
+ 50.0
+ >>> limit._fields
+ ('group', 'hits', 'seconds')
+ >>> limit._asdict() # doctest: +SKIP
+ {'group': 'user', 'hits': 500, 'seconds': 10}
+ >>> limit[0]
+ 'user'
+ >>> limit[-1]
+ 10
+ >>> user, hits, seconds = limit
+ >>> hits, seconds
+ (500, 10)
+ >>> newlimit = limit._replace(seconds=0)
+ >>> newlimit.delay
+ 0.0
+ >>> newlimit.ratio
+ inf
+
+ .. versionadded:: 9.0
+ """
+
+ group: str = 'unknown'
+ hits: int = 50
+ seconds: int = 0
+
+ @property
+ def delay(self) -> float:
+ """Calculate a delay value which is the inverse of :meth:`ratio`."""
+ return self.seconds / self.hits
+
+ @property
+ def ratio(self) -> float:
+ """Calculate a ratio how many hits can be done within one second."""
+ return self.hits / self.seconds if self.seconds != 0 else float('inf')
diff --git a/tests/site_tests.py b/tests/site_tests.py
index d2a51cf..840b5b2 100755
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -277,6 +277,32 @@
if a:
self.assertEqual(a[0], mainpage)
+ def test_maxlimit(self):
+ """Test maxlimit property."""
+ limit = self.site.maxlimit
+ self.assertIsInstance(limit, int)
+ self.assertIn(limit, [10, 50, 500, 5000])
+
+ def test_ratelimit(self):
+ """Test ratelimit method."""
+ actions = ('edit', 'move', 'purge', 'invalid')
+ if self.site.logged_in():
+ groups = ['user', 'unknown', 'noratelimit']
+ else:
+ groups = ['ip', 'unknown']
+ self.assertFalse(self.site.has_right('noratelimit'))
+ for action in actions:
+ with self.subTest(action=action):
+ limit = self.site.ratelimit(action)
+ self.assertIn(limit.group, groups)
+ self.assertEqual(limit.seconds / limit.hits, limit.delay)
+ self.assertEqual(
+ 1 / limit.delay if limit.seconds else float('inf'),
+ limit.ratio)
+ if limit.group == 'unknown':
+ self.assertEqual(limit.hits, self.site.maxlimit)
+ self.assertEqual(limit.seconds, config.put_throttle)
+
class TestLockingPage(DefaultSiteTestCase):
"""Test cases for lock/unlock a page within threads."""
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/836251
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: I5db248dc4ef4bea03ad3ac4589830c209b961a04
Gerrit-Change-Number: 836251
Gerrit-PatchSet: 19
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: JJMC89 <JJMC89.Wikimedia(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/983522 )
Change subject: [bugfix] Allow to run tests with oauth
......................................................................
[bugfix] Allow to run tests with oauth
Use api.php instead of index.php to check if site in alive.
Bug: T352606
Change-Id: I2a436d0f1cd2c06201688d4ebe211bf97c787640
---
M tests/aspects.py
1 file changed, 14 insertions(+), 1 deletion(-)
Approvals:
JJMC89: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/aspects.py b/tests/aspects.py
index ff191ee..f6dc453 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -940,7 +940,7 @@
# obsolete without a mapping to a hostname.
with suppress(KeyError):
data['hostname'] = (
- data['site'].base_url(data['site'].path()))
+ data['site'].base_url(data['site'].apipath()))
cm.__exit__(None, None, None)
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/983522
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: I2a436d0f1cd2c06201688d4ebe211bf97c787640
Gerrit-Change-Number: 983522
Gerrit-PatchSet: 2
Gerrit-Owner: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: JJMC89 <JJMC89.Wikimedia(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged