jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] Use api.APIGenerator through site._generator

- Create APIGenerator through site_generator method which is common for
all other api generators.

Bug: T129013
Change-Id: I9adf0b53dcd9dbcac5ae76aef9d4ddd86804d1b2
---
M pywikibot/site/__init__.py
1 file changed, 26 insertions(+), 24 deletions(-)

diff --git a/pywikibot/site/__init__.py b/pywikibot/site/__init__.py
index 98ce79a..ef6becb 100644
--- a/pywikibot/site/__init__.py
+++ b/pywikibot/site/__init__.py
@@ -29,6 +29,7 @@
from enum import IntEnum
from itertools import zip_longest
from textwrap import fill
+from typing import Optional
from warnings import warn

import pywikibot
@@ -1368,37 +1369,38 @@
raise ValueError('Cannot parse a site out of %s.' % dbname)

@deprecated_args(step=None)
- def _generator(self, gen_class, type_arg=None, namespaces=None,
- total=None, **args):
+ def _generator(self, gen_class, type_arg: Optional[str] = None,
+ namespaces=None, total: Optional[int] = None, **args):
"""Convenience method that returns an API generator.

- All generic keyword arguments are passed as MW API parameter except for
- 'g_content' which is passed as a normal parameter to the generator's
- Initializer.
+ All generic keyword arguments are passed as MW API parameter
+ except for 'g_content' which is passed as a normal parameter to
+ the generator's Initializer.

@param gen_class: the type of generator to construct (must be
- a subclass of pywikibot.data.api.QueryGenerator)
+ a subclass of pywikibot.data.api._RequestWrapper)
@param type_arg: query type argument to be passed to generator's
constructor unchanged (not all types require this)
- @type type_arg: str
- @param namespaces: if not None, limit the query to namespaces in this
- list
+ @param namespaces: if not None, limit the query to namespaces in
+ this list
@type namespaces: iterable of basestring or Namespace key,
or a single instance of those types. May be a '|' separated
list of namespace identifiers.
- @param total: if not None, limit the generator to yielding this many
- items in total
- @type total: int
+ @param total: if not None, limit the generator to yielding this
+ many items in total
@return: iterable with parameters set
- @rtype: api.QueryGenerator
+ @rtype: _RequestWrapper
@raises KeyError: a namespace identifier was not resolved
@raises TypeError: a namespace identifier has an inappropriate
type such as NoneType or bool
"""
- # TODO: Support parameters/simple modes?
- req_args = {'site': self, 'parameters': args}
+ req_args = {'site': self}
if 'g_content' in args:
req_args['g_content'] = args.pop('g_content')
+ if 'parameters' in args:
+ req_args.update(args)
+ else:
+ req_args['parameters'] = args
if type_arg is not None:
gen = gen_class(type_arg, **req_args)
else:
@@ -7630,18 +7632,17 @@
return pywikibot.ItemPage(self, result['entity']['id'])

@deprecated_args(limit='total')
- def search_entities(self, search: str, language: str, total=None,
- **kwargs):
+ def search_entities(self, search: str, language: str,
+ total: Optional[int] = None, **kwargs):
"""
Search for pages or properties that contain the given text.

@param search: Text to find.
@param language: Language to search in.
- @param total: Maximum number of pages to retrieve in total, or None in
- case of no limit.
- @type total: int or None
+ @param total: Maximum number of pages to retrieve in total, or
+ None in case of no limit.
@return: 'search' list from API output.
- @rtype: api.APIGenerator
+ @rtype: Generator
"""
lang_codes = self._paraminfo.parameter('wbsearchentities',
'language')['type']
@@ -7658,7 +7659,8 @@
del kwargs['site']

parameters = dict(search=search, language=language, **kwargs)
- gen = api.APIGenerator('wbsearchentities', data_name='search',
- site=self, parameters=parameters)
- gen.set_maximum_items(total)
+ gen = self._generator(api.APIGenerator,
+ type_arg='wbsearchentities',
+ data_name='search',
+ total=total, parameters=parameters)
return gen

To view, visit change 275169. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I9adf0b53dcd9dbcac5ae76aef9d4ddd86804d1b2
Gerrit-Change-Number: 275169
Gerrit-PatchSet: 18
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski@gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw@arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged