pywikibot package

The initialization file for the Pywikibot framework.

class pywikibot.Bot(site: Optional[Any] = None, **kwargs: Any)[source]

Bases: pywikibot.bot.BaseBot

Generic bot subclass for multiple sites.

If possible the MultipleSitesBot or SingleSiteBot classes should be used instead which specifically handle multiple or single sites.

Create a Bot instance and initialize cached sites.

init_page(item: Any)pywikibot.page.BasePage[source]

Update site before calling treat.

run()None[source]

Check if it automatically updates the site before run.

property site

Get the current site.

class pywikibot.Category(source, title: str = '', sort_key=None, sortKey='[deprecated name of sort_key]')[source]

Bases: pywikibot.page.Page

A page in the Category: namespace.

All parameters are the same as for Page() Initializer.

articles(recurse: Union[int, bool] = False, total: Optional[int] = None, content: bool = False, namespaces: Union[int, list] = None, sortby: Optional[str] = None, reverse: bool = False, starttime=None, endtime=None, startprefix: Optional[str] = None, endprefix: Optional[str] = None, startFrom='[deprecated name of startprefix]', startsort=NotImplemented, endsort=NotImplemented)[source]

Yield all articles in the current category.

By default, yields all pages in the category that are not subcategories!

Parameters
  • 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.)

  • total – iterate no more than this number of pages in total (at all levels)

  • namespaces – only yield pages in the specified namespaces

  • content – if True, retrieve the content of the current version of each page (default False)

  • sortby – determines the order in which results are generated, valid values are “sortkey” (default, results ordered by category sort key) or “timestamp” (results ordered by time page was added to the category). This applies recursively.

  • reverse – if True, generate results in reverse order (default False)

  • starttime (pywikibot.Timestamp) – if provided, only generate pages added after this time; not valid unless sortby=”timestamp”

  • endtime (pywikibot.Timestamp) – if provided, only generate pages added before this time; not valid unless sortby=”timestamp”

  • startprefix – if provided, only generate pages >= this title lexically; not valid if sortby=”timestamp”

  • endprefix – if provided, only generate pages < this title lexically; not valid if sortby=”timestamp”

Return type

typing.Iterable[pywikibot.Page]

Return a link to place a page in this Category.

Use this only to generate a “true” category link, not for interwikis or text links to category pages.

Parameters

sort_key – The sort key for the article to be placed in this Category; if omitted, default sort key is used.

property categoryinfo

Return a dict containing information about the category.

The dict contains values for:

Numbers of pages, subcategories, files, and total contents.

isEmptyCategory()bool[source]

Return True if category has no members (including subcategories).

isHiddenCategory()bool[source]

Return True if the category is hidden.

members(recurse: bool = False, namespaces=None, total: Optional[int] = None, content=False)[source]

Yield all category contents (subcats, pages, and files).

Return type

typing.Iterable[pywikibot.Page]

newest_pages(total=None)[source]

Return pages in a category ordered by the creation date.

If two or more pages are created at the same time, the pages are returned in the order they were added to the category. The most recently added page is returned first.

It only allows to return the pages ordered from newest to oldest, as it is impossible to determine the oldest page in a category without checking all pages. But it is possible to check the category in order with the newly added first and it yields all pages which were created after the currently checked page was added (and thus there is no page created after any of the cached but added before the currently checked).

Parameters

total (int) – The total number of pages queried.

Returns

A page generator of all pages in a category ordered by the creation date. From newest to oldest. Note: It currently only returns Page instances and not a subclass of it if possible. This might change so don’t expect to only get Page instances.

Return type

generator

subcategories(recurse: Union[int, bool] = False, total: Optional[int] = None, content: bool = False)[source]

Iterate all subcategories of the current category.

Parameters
  • recurse – if not False or 0, also iterate subcategories of subcategories. If an int, limit recursion to this number of levels. (Example: recurse=1 will iterate direct subcats and first-level sub-sub-cats, but no deeper.)

  • total – iterate no more than this number of subcategories in total (at all levels)

  • content – if True, retrieve the content of the current version of each category description page (default False)

class pywikibot.Claim(site, pid, snak=None, hash=None, is_reference=False, is_qualifier=False, rank='normal', isReference='[deprecated name of is_reference]', isQualifier='[deprecated name of is_qualifier]', **kwargs)[source]

Bases: pywikibot.page.Property

A Claim on a Wikibase entity.

Claims are standard claims as well as references and qualifiers.

Defined by the “snak” value, supplemented by site + pid

Parameters
  • site (pywikibot.site.DataSite) – repository the claim is on

  • pid – property id, with “P” prefix

  • snak – snak identifier for claim

  • hash – hash identifier for references

  • is_reference – whether specified claim is a reference

  • is_qualifier – whether specified claim is a qualifier

  • rank – rank for claim

SNAK_TYPES = ('value', 'somevalue', 'novalue')
TARGET_CONVERTER = {'commonsMedia': <function Claim.<lambda>>, 'geo-shape': <bound method _WbDataPage.fromWikibase of <class 'pywikibot.WbGeoShape'>>, 'globe-coordinate': <bound method Coordinate.fromWikibase of <class 'pywikibot.Coordinate'>>, 'monolingualtext': <function Claim.<lambda>>, 'quantity': <bound method deprecated_args.<locals>.decorator.<locals>.wrapper of <class 'pywikibot.WbQuantity'>>, 'tabular-data': <bound method _WbDataPage.fromWikibase of <class 'pywikibot.WbTabularData'>>, 'time': <bound method deprecated_args.<locals>.decorator.<locals>.wrapper of <class 'pywikibot.WbTime'>>, 'wikibase-item': <function Claim.<lambda>>, 'wikibase-property': <function Claim.<lambda>>}
addQualifier(qualifier, **kwargs)[source]

Add the given qualifier.

Parameters

qualifier (pywikibot.page.Claim) – the qualifier to add

addSource(claim, **kwargs)[source]

Add the claim as a source.

Parameters

claim (pywikibot.Claim) – the claim to add

addSources(claims, **kwargs)[source]

Add the claims as one source.

Parameters

claims (list of pywikibot.Claim) – the claims to add

changeRank(rank, **kwargs)[source]

Change the rank of the Claim and save.

changeSnakType(value=None, **kwargs)[source]

Save the new snak value.

TODO: Is this function really needed?

changeTarget(value=None, snaktype='value', **kwargs)[source]

Set the target value in the data repository.

Parameters
  • value (object) – The new target value.

  • snaktype (str ('value', 'somevalue', or 'novalue')) – The new snak type.

copy()[source]

Create an independent copy of this object.

Return type

pywikibot.page.Claim

classmethod fromJSON(site, data)[source]

Create a claim object from JSON returned in the API call.

Parameters

data (dict) – JSON containing claim data

Return type

pywikibot.page.Claim

getRank()[source]

Return the rank of the Claim.

getSnakType()str[source]

Return the type of snak.

Returns

str (‘value’, ‘somevalue’ or ‘novalue’)

getSources()list[source]

Return a list of sources, each being a list of Claims.

getTarget()[source]

Return the target value of this Claim.

None is returned if no target is set

Returns

object

has_qualifier(qualifier_id: str, target)bool[source]

Check whether Claim contains specified qualifier.

Parameters
  • qualifier_id – id of the qualifier

  • target – qualifier target to check presence of

Returns

true if the qualifier was found, false otherwise

property on_item

Return item this claim is attached to.

classmethod qualifierFromJSON(site, data)[source]

Create a Claim for a qualifier from JSON.

Qualifier objects are represented a bit differently like references, but I’m not sure if this even requires it’s own function.

Return type

pywikibot.page.Claim

classmethod referenceFromJSON(site, data)dict[source]

Create a dict of claims from reference JSON returned in the API call.

Reference objects are represented a bit differently, and require some more handling.

removeQualifier(qualifier, **kwargs)[source]

Remove the qualifier. Call removeQualifiers().

Parameters

qualifier (pywikibot.page.Claim) – the qualifier to remove

removeQualifiers(qualifiers, **kwargs)[source]

Remove the qualifiers.

Parameters

qualifiers (list Claim) – the qualifiers to remove

removeSource(source, **kwargs)[source]

Remove the source. Call removeSources().

Parameters

source (pywikibot.Claim) – the source to remove

removeSources(sources, **kwargs)[source]

Remove the sources.

Parameters

sources (list of pywikibot.Claim) – the sources to remove

same_as(other, ignore_rank=True, ignore_quals=False, ignore_refs=True)[source]

Check if two claims are same.

setRank(rank)[source]

Set the rank of the Claim.

setSnakType(value)[source]

Set the type of snak.

Parameters

value (str ('value', 'somevalue', or 'novalue')) – Type of snak

setTarget(value)[source]

Set the target value in the local object.

Parameters

value (object) – The new target value.

Raises

ValueError – if value is not of the type required for the Claim type.

target_equals(value)bool[source]

Check whether the Claim’s target is equal to specified value.

The function checks for:

  • WikibasePage ID equality

  • WbTime year equality

  • Coordinate equality, regarding precision

  • WbMonolingualText text equality

  • direct equality

Parameters

value – the value to compare with

Returns

true if the Claim’s target is equal to the value provided, false otherwise

toJSON()dict[source]

Create dict suitable for the MediaWiki API.

class pywikibot.Coordinate(lat: float, lon: float, alt=None, precision: Optional[float] = None, globe: Optional[str] = None, typ: str = '', name: str = '', dim: Optional[int] = None, site: Optional[pywikibot.site._datasite.DataSite] = None, globe_item=None, primary: bool = False, entity='[deprecated name of globe_item]')[source]

Bases: pywikibot._wbtypes.WbRepresentation

Class for handling and storing Coordinates.

Represent a geo coordinate.

Parameters
  • lat – Latitude

  • lon – Longitude

  • alt – Altitude? TODO FIXME

  • precision – precision

  • globe – Which globe the point is on

  • typ – The type of coordinate point

  • name – The name

  • dim – Dimension (in meters)

  • site – The Wikibase site

  • globe_item (pywikibot.ItemPage or str) – The Wikibase item for the globe, or the entity URI of this Wikibase item. Takes precedence over ‘globe’ if present.

  • primary – True for a primary set of coordinates

property entity

Return the entity uri of the globe.

classmethod fromWikibase(data: dict, site: Optional[pywikibot.site._datasite.DataSite] = None)pywikibot.Coordinate[source]

Constructor to create an object from Wikibase’s JSON output.

Parameters
  • data – Wikibase JSON

  • site – The Wikibase site

get_globe_item(repo: Optional[pywikibot.site._datasite.DataSite] = None, lazy_load: bool = False)[source]

Return the ItemPage corresponding to the globe.

Note that the globe need not be in the same data repository as the Coordinate itself.

A successful lookup is stored as an internal value to avoid the need for repeated lookups.

Parameters
  • repo – the Wikibase site for the globe, if different from that provided with the Coordinate.

  • lazy_load – Do not raise NoPage if ItemPage does not exist.

Returns

pywikibot.ItemPage

property precision

Return the precision of the geo coordinate.

The precision is calculated if the Coordinate does not have a precision, and self._dim is set.

When no precision and no self._dim exists, None is returned.

The biggest error (in degrees) will be given by the longitudinal error; the same error in meters becomes larger (in degrees) further up north. We can thus ignore the latitudinal error.

The longitudinal can be derived as follows:

In small angle approximation (and thus in radians):

M{Δλ ≈ Δpos / r_φ}, where r_φ is the radius of earth at the given latitude. Δλ is the error in longitude.

M{r_φ = r cos φ}, where r is the radius of earth, φ the latitude

Therefore:

precision = math.degrees(
    self._dim/(radius*math.cos(math.radians(self.lat))))
precisionToDim()Optional[int][source]

Convert precision from Wikibase to GeoData’s dim and return the latter.

dim is calculated if the Coordinate doesn’t have a dimension, and precision is set. When neither dim nor precision are set, ValueError is thrown.

Carrying on from the earlier derivation of precision, since precision = math.degrees(dim/(radius*math.cos(math.radians(self.lat)))) we get:

dim = math.radians(
    precision)*radius*math.cos(math.radians(self.lat))

But this is not valid, since it returns a float value for dim which is an integer. We must round it off to the nearest integer.

Therefore:

dim = int(round(math.radians(
    precision)*radius*math.cos(math.radians(self.lat))))
toWikibase()dict[source]

Export the data to a JSON object for the Wikibase API.

FIXME: Should this be in the DataSite object?

Returns

Wikibase JSON

class pywikibot.CurrentPageBot(**kwargs: Any)[source]

Bases: pywikibot.bot.BaseBot

A bot which automatically sets ‘current_page’ on each treat().

This class should be always used together with either the MultipleSitesBot or SingleSiteBot class as there is no site management in this class.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

ignore_server_errors = False
put_current(new_text: str, ignore_save_related_errors: Optional[bool] = None, ignore_server_errors: Optional[bool] = None, comment='[deprecated name of summary]', **kwargs: Any)[source]

Call Bot.userPut but use the current page.

It compares the new_text to the current page text.

Parameters
  • new_text – The new text

  • ignore_save_related_errors – Ignore save related errors and automatically print a message. If None uses this instances default.

  • ignore_server_errors – Ignore server errors and automatically print a message. If None uses this instances default.

  • kwargs – Additional parameters directly given to Bot.userPut.

Returns

whether the page was saved successfully

treat(page: pywikibot.page.BasePage)None[source]

Set page to current page and treat that page.

treat_page()None[source]

Process one page (Abstract method).

class pywikibot.FilePage(source, title: str = '')[source]

Bases: pywikibot.page.Page

A subclass of Page representing a file description page.

Supports the same interface as Page, with some added methods.

data_item()[source]

Convenience function to get the associated Wikibase item of the file.

If WikibaseMediaInfo extension is available (e.g. on Commons), the method returns the associated mediainfo entity. Otherwise, it falls back to behavior of BasePage.data_item.

New in version 6.5.

Return type

pywikibot.page.WikibaseEntity

download(filename=None, chunk_size=102400, revision=None)[source]

Download to filename file of FilePage.

Parameters
  • filename (None or str) – filename where to save file: None: self.title(as_filename=True, with_ns=False) will be used str: provided filename will be used.

  • chunk_size (int) – the size of each chunk to be received and written to file.

  • revision (None or FileInfo) – file revision to download: None: self.latest_file_info will be used FileInfo: provided revision will be used.

Returns

True if download is successful, False otherwise.

Raises

IOError – if filename cannot be written for any reason.

file_is_shared()bool[source]

Check if the file is stored on any known shared repository.

getFileVersionHistoryTable()[source]

Return the version history in the form of a wiki table.

getImagePageHtml()str[source]

Download the file page, and return the HTML, as a string.

Caches the HTML code, so that if you run this method twice on the same FilePage object, the page will only be downloaded once.

get_file_history()dict[source]

Return the file’s version history.

Returns

dictionary with: key: timestamp of the entry value: instance of FileInfo()

get_file_url(url_width=None, url_height=None, url_param=None)str[source]

Return the url or the thumburl of the file described on this page.

Fetch the information if not available.

Once retrieved, thumburl information will also be accessible as latest_file_info attributes, named as in [1]: - url, thumburl, thumbwidth and thumbheight

Parameters correspond to iiprops in: [1] https://www.mediawiki.org/wiki/API:Imageinfo

Parameters validation and error handling left to the API call.

Parameters
  • url_width – see iiurlwidth in [1]

  • url_height – see iiurlheigth in [1]

  • url_param – see iiurlparam in [1]

Returns

latest file url or thumburl

globalusage(total=None)[source]

Iterate all global usage for this page.

Parameters

total – iterate no more than this number of pages in total

Returns

a generator that yields Pages also on sites different from self.site.

Return type

generator

property latest_file_info

Retrieve and store information of latest Image rev. of FilePage.

At the same time, the whole history of Image is fetched and cached in self._file_revisions

Returns

instance of FileInfo()

property oldest_file_info

Retrieve and store information of oldest Image rev. of FilePage.

At the same time, the whole history of Image is fetched and cached in self._file_revisions

Returns

instance of FileInfo()

upload(source: str, **kwargs)bool[source]

Upload this file to the wiki.

keyword arguments are from site.upload() method.

Parameters

source – Path or URL to the file to be uploaded.

Keyword Arguments
  • comment – Edit summary; if this is not provided, then filepage.text will be used. An empty summary is not permitted. This may also serve as the initial page text (see below).

  • text – Initial page text; if this is not set, then filepage.text will be used, or comment.

  • watch – If true, add filepage to the bot user’s watchlist

  • ignore_warnings – It may be a static boolean, a callable returning a boolean or an iterable. The callable gets a list of UploadError instances and the iterable should contain the warning codes for which an equivalent callable would return True if all UploadError codes are in thet list. If the result is False it’ll not continue uploading the file and otherwise disable any warning and reattempt to upload the file. NOTE: If report_success is True or None it’ll raise an UploadError exception if the static boolean is False.

  • chunk_size – The chunk size in bytesfor chunked uploading (see https://www.mediawiki.org/wiki/API:Upload#Chunked_uploading). It will only upload in chunks, if the chunk size is positive but lower than the file size.

  • _file_key – Reuses an already uploaded file using the filekey. If None (default) it will upload the file.

  • _offset – When file_key is not None this can be an integer to continue a previously canceled chunked upload. If False it treats that as a finished upload. If True it requests the stash info from the server to determine the offset. By default starts at 0.

  • _verify_stash – Requests the SHA1 and file size uploaded and compares it to the local file. Also verifies that _offset is matching the file size if the _offset is an int. If _offset is False if verifies that the file size match with the local file. If None it’ll verifies the stash when a file key and offset is given.

  • report_success – If the upload was successful it’ll print a success message and if ignore_warnings is set to False it’ll raise an UploadError if a warning occurred. If it’s None (default) it’ll be True if ignore_warnings is a bool and False otherwise. If it’s True or None ignore_warnings must be a bool.

Returns

It returns True if the upload was successful and False otherwise.

usingPages(total: Optional[int] = None, content: bool = False)[source]

Yield Pages on which the file is displayed.

Parameters
  • total – iterate no more than this number of pages in total

  • content – if True, load the current content of each iterated page (default False)

class pywikibot.ItemPage(site, title=None, ns=None)[source]

Bases: pywikibot.page.WikibasePage

Wikibase entity of type ‘item’.

A Wikibase item may be defined by either a ‘Q’ id (qid), or by a site & title.

If an item is defined by site & title, once an item’s qid has been looked up, the item is then defined by the qid.

Parameters
  • site (pywikibot.site.DataSite) – data repository

  • title (str) – identifier of item, “Q###”, -1 or None for an empty item.

DATA_ATTRIBUTES: dict = {'aliases': <class 'pywikibot.page._collections.AliasesDict'>, 'claims': <class 'pywikibot.page._collections.ClaimCollection'>, 'descriptions': <class 'pywikibot.page._collections.LanguageDict'>, 'labels': <class 'pywikibot.page._collections.LanguageDict'>, 'sitelinks': <class 'pywikibot.page._collections.SiteLinkCollection'>}
concept_url(**kw)
entity_type = 'item'
classmethod fromPage(page, lazy_load=False)[source]

Get the ItemPage for a Page that links to it.

Parameters
  • page (pywikibot.page.Page) – Page to look for corresponding data item

  • lazy_load (bool) – Do not raise NoPageError if either page or corresponding ItemPage does not exist.

Return type

pywikibot.page.ItemPage

Raises
classmethod from_entity_uri(site, uri: str, lazy_load: bool = False)[source]

Get the ItemPage from its entity uri.

Parameters
  • site (pywikibot.site.DataSite) – The Wikibase site for the item.

  • uri – Entity uri for the Wikibase item.

  • lazy_load – Do not raise NoPageError if ItemPage does not exist.

Return type

pywikibot.page.ItemPage

Raises
  • TypeError – Site is not a valid DataSite.

  • ValueError – Site does not match the base of the provided uri.

  • pywikibot.exceptions.NoPageError – Uri points to non-existent item.

get(force=False, get_redirect=False, *args, **kwargs)dict[source]

Fetch all item data, and cache it.

Parameters
  • force (bool) – override caching

  • get_redirect (bool) – return the item content, do not follow the redirect, do not raise an exception.

Raises

NotImplementedError – a value in args or kwargs

Returns

actual data which entity holds

Note

dicts returned by this method are references to content of this entity and their modifying may indirectly cause unwanted change to the live content

getID(numeric=False, force=False)[source]

Get the entity identifier.

Parameters
  • numeric (bool) – Strip the first letter and return an int

  • force (bool) – Force an update of new data

getRedirectTarget()[source]

Return the redirect target for this page.

Return the title for the specific site.

If the item doesn’t have that language, raise NoPageError.

Parameters
  • site (pywikibot.Site or database name) – Site to find the linked page of.

  • force – override caching

isRedirectPage()[source]

Return True if item is a redirect, False if not or not existing.

Iterate through all the sitelinks.

Parameters

family (str|pywikibot.family.Family) – string/Family object which represents what family of links to iterate

Returns

iterator of pywikibot.Page objects

Return type

iterator

mergeInto(item, **kwargs)[source]

Merge the item into another item.

Parameters

item (pywikibot.page.ItemPage) – The item to merge into

Remove a sitelink.

A site can either be a Site object, or it can be a dbName.

Remove sitelinks.

Sites should be a list, with values either being Site objects, or dbNames.

Set sitelinks. Calls setSitelinks().

A sitelink can be a Page object, a BaseLink object or a {‘site’:dbname,’title’:title} dictionary.

Set sitelinks.

Sitelinks should be a list. Each item in the list can either be a Page object, a BaseLink object, or a dict with a value for ‘site’ and ‘title’.

set_redirect_target(target_page, create=False, force=False, keep_section=False, save=True, **kwargs)[source]

Make the item redirect to another item.

You need to define an extra argument to make this work, like save=True

Parameters
  • target_page (pywikibot.page.ItemPage or string) – target of the redirect, this argument is required.

  • force (bool) – if true, it sets the redirect target even the page is not redirect.

title(**kwargs)[source]

Return ID as title of the ItemPage.

If the ItemPage was lazy-loaded via ItemPage.fromPage, this method will fetch the Wikibase item ID for the page, potentially raising NoPageError with the page on the linked wiki if it does not exist, or does not have a corresponding Wikibase item ID.

This method also refreshes the title if the id property was set. i.e. item.id = ‘Q60’

All optional keyword parameters are passed to the superclass.

title_pattern = 'Q[1-9]\\d*'

Bases: pywikibot.page.BaseLink

A MediaWiki wikitext link (local or interwiki).

Constructs a Link object based on a wikitext link and a source site.

Extends BaseLink by the following attributes:

  • section: The section of the page linked to (str or None); this contains any text following a ‘#’ character in the title

  • anchor: The anchor text (str or None); this contains any text following a ‘|’ character inside the link

Parameters
  • text (str) – the link text (everything appearing between [[ and ]] on a wiki page)

  • source (Site or BasePage) – the Site on which the link was found (not necessarily the site to which the link refers)

  • default_namespace (int) – a namespace to use if the link does not contain one (defaults to 0)

Raises

UnicodeError – text could not be converted to unicode.

property anchor

Return the anchor of the link.

astext(onsite=None)[source]

Return a text representation of the link.

Parameters

onsite – if specified, present as a (possibly interwiki) link from the given site; otherwise, present as an internal link on the source site.

classmethod create_separated(link, source, default_namespace=0, section=None, label=None)[source]

Create a new instance but overwrite section or label.

The returned Link instance is already parsed.

Parameters
  • link (str) – The original link text.

  • source (Site) – The source of the link.

  • default_namespace (int) – The namespace this link uses when no namespace is defined in the link text.

  • section (None or str) – The new section replacing the one in link. If None (default) it doesn’t replace it.

  • label – The new label replacing the one in link. If None (default) it doesn’t replace it.

classmethod fromPage(page, source=None)[source]

Create a Link to a Page.

Parameters
Return type

pywikibot.page.Link

illegal_titles_pattern = re.compile('[\\x00-\\x1f\\x23\\x3c\\x3e\\x5b\\x5d\\x7b\\x7c\\x7d\\x7f]|%[0-9A-Fa-f]{2}|&[A-Za-z0-9\x80-ÿ]+;|&#[0-9]+;|&#x[0-9A-Fa-f]+;')
classmethod langlinkUnsafe(lang, title, source)[source]

Create a “lang:title” Link linked from source.

Assumes that the lang & title come clean, no checks are made.

Parameters
  • lang (str) – target site code (language)

  • title (str) – target Page

  • source – Link from site source

  • source – Site

Return type

pywikibot.page.Link

property namespace

Return the namespace of the link.

Return type

pywikibot.Namespace

parse()[source]

Parse wikitext of the link.

Called internally when accessing attributes.

parse_site()tuple[source]

Parse only enough text to determine which site the link points to.

This method does not parse anything after the first “:”; links with multiple interwiki prefixes (such as “wikt:fr:Parlais”) need to be re-parsed on the first linked wiki to get the actual site.

Returns

The family name and site code for the linked site. If the site is not supported by the configured families it returns None instead of a str.

property section

Return the section of the link.

property site

Return the site of the link.

Return type

pywikibot.Site

property title

Return the title of the link.

class pywikibot.MediaInfo(repo, id_=None)[source]

Bases: pywikibot.page.WikibaseEntity

Interface for MediaInfo entities on Commons.

New in version 6.5.

Parameters
  • repo (DataSite) – Entity repository.

  • id (str or None, -1 and None mean non-existing) – Entity identifier.

DATA_ATTRIBUTES: dict = {'labels': <class 'pywikibot.page._collections.LanguageDict'>}
property file

Get the file associated with the mediainfo.

get(force: bool = False)dict[source]

Fetch all entity data and cache it.

Parameters

force – override caching

Raises

NoWikibaseEntityError – if this entity doesn’t exist

Returns

actual data which entity holds

getID(numeric=False)[source]

Get the entity identifier.

Parameters

numeric (bool) – Strip the first letter and return an int

title_pattern = 'M[1-9]\\d*'
class pywikibot.Page(source, title: str = '', ns=0, defaultNamespace='[deprecated name of ns]')[source]

Bases: pywikibot.page.BasePage

Page: A MediaWiki page.

Instantiate a Page object.

get_best_claim(prop: str)[source]

Return the first best Claim for this page.

Return the first ‘preferred’ ranked Claim specified by Wikibase property or the first ‘normal’ one otherwise.

New in version 3.0.

Parameters

prop – property id, “P###”

Returns

Claim object given by Wikibase property number for this page object.

Return type

pywikibot.Claim or None

Raises

UnknownExtensionError – site has no Wikibase extension

property raw_extracted_templates

Extract templates using textlib.extract_templates_and_params.

Disabled parts and whitespace are stripped, except for whitespace in anonymous positional arguments.

This value is cached.

Return type

list of (str, OrderedDict)

set_redirect_target(target_page, create=False, force=False, keep_section=False, save=True, **kwargs)[source]

Change the page’s text to point to the redirect page.

Parameters
  • target_page (pywikibot.Page or string) – target of the redirect, this argument is required.

  • create (bool) – if true, it creates the redirect even if the page doesn’t exist.

  • force (bool) – if true, it set the redirect target even the page doesn’t exist or it’s not redirect.

  • keep_section (bool) – if the old redirect links to a section and the new one doesn’t it uses the old redirect’s section.

  • save (bool) – if true, it saves the page immediately.

  • kwargs – Arguments which are used for saving the page directly afterwards, like ‘summary’ for edit summary.

templatesWithParams()[source]

Return templates used on this Page.

The templates are extracted by textlib.extract_templates_and_params, with positional arguments placed first in order, and each named argument appearing as ‘name=value’.

All parameter keys and values for each template are stripped of whitespace.

Returns

a list of tuples with one tuple for each template invocation in the page, with the template Page as the first entry and a list of parameters as the second entry.

Return type

list of (pywikibot.page.Page, list)

class pywikibot.PropertyPage(source, title=None, datatype=None)[source]

Bases: pywikibot.page.WikibasePage, pywikibot.page.Property

A Wikibase entity in the property namespace.

Should be created as:

PropertyPage(DataSite, 'P21')

or:

PropertyPage(DataSite, datatype='url')
Parameters
  • source (pywikibot.site.DataSite) – data repository property is on

  • title (str) – identifier of property, like “P##”, “-1” or None for an empty property.

  • datatype (str) – Datatype for a new property.

DATA_ATTRIBUTES: dict = {'aliases': <class 'pywikibot.page._collections.AliasesDict'>, 'claims': <class 'pywikibot.page._collections.ClaimCollection'>, 'descriptions': <class 'pywikibot.page._collections.LanguageDict'>, 'labels': <class 'pywikibot.page._collections.LanguageDict'>}
entity_type = 'property'
get(force: bool = False, *args, **kwargs)dict[source]

Fetch the property entity, and cache it.

Parameters

force – override caching

Raises

NotImplementedError – a value in args or kwargs

Returns

actual data which entity holds

Note

dicts returned by this method are references to content of this entity and their modifying may indirectly cause unwanted change to the live content

getID(numeric=False)[source]

Get the identifier of this property.

Parameters

numeric (bool) – Strip the first letter and return an int

get_data_for_new_entity()[source]

Return data required for creation of new property.

newClaim(*args, **kwargs)[source]

Helper function to create a new claim object for this property.

Return type

pywikibot.page.Claim

title_pattern = 'P[1-9]\\d*'
pywikibot.Site(code: Optional[str] = None, fam=None, user: Optional[str] = None, sysop=NotImplemented, *, interface=None, url: Optional[str] = None)[source]

A factory method to obtain a Site object.

Site objects are cached and reused by this method.

By default rely on config settings. These defaults may all be overridden using the method parameters.

Creating the default site using config.mylang and config.family:

site = pywikibot.Site()

Override default site code:

site = pywikibot.Site('fr')

Override default family:

site = pywikibot.Site(family='wikisource')

Setting a specific site:

site = pywikibot.Site('fr', 'wikisource')

which is equal to:

site = pywikibot.Site('wikisource:fr')
Note

An already created site is cached an a new variable points to the same object if interface, family, code and user are equal:

>>> import pywikibot
>>> site_1 = pywikibot.Site('wikisource:fr')
>>> site_2 = pywikibot.Site('fr', 'wikisource')
>>> site_1 is site_2
True
>>> site_1
APISite("fr", "wikisource")

APISite is the default interface. Refer pywikibot.site for other interface types.

Never create a site object via interface class directly. Always use this factory method.

Parameters
  • code – language code (override config.mylang) code may also be a sitename like ‘wikipedia:test’

  • fam (str or pywikibot.family.Family) – family name or object (override config.family)

  • user – bot user name to use on this site (override config.usernames)

  • interface (subclass of pywikibot.site.BaseSite or string) – site class or name of class in pywikibot.site (override config.site_interface)

  • url – Instead of code and fam, does try to get a Site based on the URL. Still requires that the family supporting that URL exists.

Raises
  • ValueError – URL and pair of code and family given

  • ValueError – Invalid interface name

  • ValueError – Missing Site code

  • ValueError – Missing Site family

Bases: pywikibot.page.BaseLink

A single sitelink in a Wikibase item.

Extends BaseLink by the following attribute:

  • badges: Any badges associated with the sitelink

New in version 3.0.

Parameters
  • title (str) – the title of the linked page including namespace

  • site (pywikibot.Site or str) – the Site object for the wiki linked to. Can be provided as either a Site instance or a db key, defaults to pywikibot.Site().

  • badges ([pywikibot.ItemPage]) – list of badges

property badges

Return a list of all badges associated with the link.

Return type

[pywikibot.ItemPage]

classmethod fromJSON(data: dict, site=None)[source]

Create a SiteLink object from JSON returned in the API call.

Parameters
Return type

pywikibot.page.SiteLink

toJSON()dict[source]

Convert the SiteLink to a JSON object for the Wikibase API.

Returns

Wikibase JSON

class pywikibot.Timestamp[source]

Bases: datetime.datetime

Class for handling MediaWiki timestamps.

This inherits from datetime.datetime, so it can use all of the methods and operations of a datetime object. To ensure that the results of any operation are also a Timestamp object, be sure to use only Timestamp objects (and datetime.timedeltas) in any operation.

Use Timestamp.fromISOformat() and Timestamp.fromtimestampformat() to create Timestamp objects from MediaWiki string formats. As these constructors are typically used to create objects using data passed provided by site and page methods, some of which return a Timestamp when previously they returned a MediaWiki string representation, these methods also accept a Timestamp object, in which case they return a clone.

Use Site.server_time() for the current time; this is more reliable than using Timestamp.utcnow().

ISO8601Format = '%Y-%m-%dT%H:%M:%SZ'
clone()[source]

Clone this instance.

classmethod fromISOformat(ts, sep: str = 'T')[source]

Convert an ISO 8601 timestamp to a Timestamp object.

Parameters
  • ts (str or Timestamp) – ISO 8601 timestamp or a Timestamp object already

  • sep – one-character separator, placed between the date and time

Returns

Timestamp object

Return type

Timestamp

classmethod fromtimestampformat(ts)[source]

Convert a MediaWiki internal timestamp to a Timestamp object.

isoformat(sep='T')[source]

Convert object to an ISO 8601 timestamp accepted by MediaWiki.

datetime.datetime.isoformat does not postfix the ISO formatted date with a ‘Z’ unless a timezone is included, which causes MediaWiki ~1.19 and earlier to fail.

mediawikiTSFormat = '%Y%m%d%H%M%S'
totimestampformat()[source]

Convert object to a MediaWiki internal timestamp.

class pywikibot.User(source, title='', site='[deprecated name of source]', name='[deprecated name of title]')[source]

Bases: pywikibot.page.Page

A class that represents a Wiki user.

This class also represents the Wiki page User:<username>

Initializer for a User object.

All parameters are the same as for Page() Initializer.

block(*args, **kwargs)[source]

Block user.

Refer APISite.blockuser method for parameters.

Returns

None

contributions(total: int = 500, limit='[deprecated name of total]', namespace='[deprecated name of namespaces]', **kwargs)[source]

Yield tuples describing this user edits.

Each tuple is composed of a pywikibot.Page object, the revision id (int), the edit timestamp (as a pywikibot.Timestamp object), and the comment (str). Pages returned are not guaranteed to be unique.

Parameters

total – limit result to this number of pages

Keyword Arguments
  • start – Iterate contributions starting at this Timestamp

  • end – Iterate contributions ending at this Timestamp

  • reverse – Iterate oldest contributions first (default: newest)

  • namespaces – only iterate pages in these namespaces

  • showMinor – if True, iterate only minor edits; if False and not None, iterate only non-minor edits (default: iterate both)

  • top_only – if True, iterate only edits which are the latest revision (default: False)

Returns

tuple of pywikibot.Page, revid, pywikibot.Timestamp, comment

deleted_contributions(*, total: int = 500, **kwargs)collections.abc.Iterable[tuple][source]

Yield tuples describing this user’s deleted edits.

New in version 5.5.

Parameters

total – Limit results to this number of pages

Keyword Arguments
  • start – Iterate contributions starting at this Timestamp

  • end – Iterate contributions ending at this Timestamp

  • reverse – Iterate oldest contributions first (default: newest)

  • namespaces – Only iterate pages in these namespaces

editCount(force: bool = False)int[source]

Return edit count for a registered user.

Always returns 0 for ‘anonymous’ users.

Parameters

force – if True, forces reloading the data from API

property first_edit

Return first user contribution.

Returns

first user contribution entry

Returns

tuple of pywikibot.Page, revid, pywikibot.Timestamp, comment

Return type

tuple or None

gender(force: bool = False)str[source]

Return the gender of the user.

Parameters

force – if True, forces reloading the data from API

Returns

return ‘male’, ‘female’, or ‘unknown’

getUserPage(subpage='')[source]

Return a Page object relative to this user’s main page.

Parameters

subpage (str) – subpage part to be appended to the main page title (optional)

Returns

Page object of user page or user subpage

Return type

pywikibot.Page

getUserTalkPage(subpage='')[source]

Return a Page object relative to this user’s main talk page.

Parameters

subpage (str) – subpage part to be appended to the main talk page title (optional)

Returns

Page object of user talk page or user talk subpage

Return type

pywikibot.Page

getprops(force: bool = False)dict[source]

Return a properties about the user.

Parameters

force – if True, forces reloading the data from API

groups(force: bool = False)list[source]

Return a list of groups to which this user belongs.

The list of groups may be empty.

Parameters

force – if True, forces reloading the data from API

Returns

groups property

isAnonymous()bool[source]

Determine if the user is editing as an IP address.

isBlocked(force: bool = False)bool[source]

Determine whether the user is currently blocked.

Parameters

force – if True, forces reloading the data from API

isEmailable(force: bool = False)bool[source]

Determine whether emails may be send to this user through MediaWiki.

Parameters

force – if True, forces reloading the data from API

isRegistered(force: bool = False)bool[source]

Determine if the user is registered on the site.

It is possible to have a page named User:xyz and not have a corresponding user with username xyz.

The page does not need to exist for this method to return True.

Parameters

force – if True, forces reloading the data from API

property is_thankable

Determine if the user has thanks notifications enabled.

NOTE: This doesn’t accurately determine if thanks is enabled for user.

Privacy of thanks preferences is under discussion, please see https://phabricator.wikimedia.org/T57401#2216861, and https://phabricator.wikimedia.org/T120753#1863894

property last_edit

Return last user contribution.

Returns

last user contribution entry

Returns

tuple of pywikibot.Page, revid, pywikibot.Timestamp, comment

Return type

tuple or None

property last_event

Return last user activity.

Returns

last user log entry

Return type

LogEntry or None

logevents(**kwargs)[source]

Yield user activities.

Keyword Arguments
  • logtype – only iterate entries of this type (see mediawiki api documentation for available types)

  • page – only iterate entries affecting this page

  • namespace – namespace to retrieve logevents from

  • start – only iterate entries from and after this Timestamp

  • end – only iterate entries up to and through this Timestamp

  • reverse – if True, iterate oldest entries first (default: newest)

  • tag – only iterate entries tagged with this tag

  • total – maximum number of events to iterate

Return type

iterable

registration(force=False)[source]

Fetch registration date for this user.

Parameters

force (bool) – if True, forces reloading the data from API

Return type

pywikibot.Timestamp or None

rights(force: bool = False)list[source]

Return user rights.

Parameters

force – if True, forces reloading the data from API

Returns

return user rights

send_email(subject: str, text: str, ccme: bool = False)bool[source]

Send an email to this user via MediaWiki’s email interface.

Parameters
  • subject – the subject header of the mail

  • text – mail body

  • ccme – if True, sends a copy of this email to the bot

Raises
Returns

operation successful indicator

unblock(reason: Optional[str] = None)[source]

Remove the block for the user.

Parameters

reason – Reason for the unblock.

uploadedImages(total=10, number='[deprecated name of total]')[source]

Yield tuples describing files uploaded by this user.

Each tuple is composed of a pywikibot.Page, the timestamp (str in ISO8601 format), comment (str) and a bool for pageid > 0. Pages returned are not guaranteed to be unique.

Parameters

total (int) – limit result to this number of pages

property username

The username.

Convenience method that returns the title of the page with namespace prefix omitted, which is the username.

class pywikibot.WbGeoShape(page, site: Optional[pywikibot.site._datasite.DataSite] = None)[source]

Bases: pywikibot._WbDataPage

A Wikibase geo-shape representation.

Create a new _WbDataPage object.

Parameters
  • page (pywikibot.Page) – page containing the data

  • site – The Wikibase site

class pywikibot.WbMonolingualText(text: str, language: str)[source]

Bases: pywikibot._wbtypes.WbRepresentation

A Wikibase monolingual text representation.

Create a new WbMonolingualText object.

Parameters
  • text – text string

  • language – language code of the string

classmethod fromWikibase(data: dict, site: Optional[pywikibot.site._datasite.DataSite] = None, wb='[deprecated name of data]')[source]

Create a WbMonolingualText from the JSON data given by Wikibase API.

Parameters
  • data – Wikibase JSON

  • site – The Wikibase site

toWikibase()dict[source]

Convert the data to a JSON object for the Wikibase API.

Returns

Wikibase JSON

class pywikibot.WbQuantity(amount, unit=None, error=None, site: Optional[pywikibot.site._datasite.DataSite] = None)[source]

Bases: pywikibot._wbtypes.WbRepresentation

A Wikibase quantity representation.

Create a new WbQuantity object.

Parameters
  • amount (str or Decimal. Other types are accepted, and converted via str to Decimal.) – number representing this quantity

  • unit (pywikibot.ItemPage, str or None) – the Wikibase item for the unit or the entity URI of this Wikibase item.

  • error (same as amount, or tuple of two values, where the first value is the upper error and the second is the lower error value.) – the uncertainty of the amount (e.g. ±1)

  • site – The Wikibase site

classmethod fromWikibase(data: dict, site: Optional[pywikibot.site._datasite.DataSite] = None, wb='[deprecated name of data]')[source]

Create a WbQuantity from the JSON data given by the Wikibase API.

Parameters
  • data – Wikibase JSON

  • site – The Wikibase site

get_unit_item(repo: Optional[pywikibot.site._datasite.DataSite] = None, lazy_load: bool = False)[source]

Return the ItemPage corresponding to the unit.

Note that the unit need not be in the same data repository as the WbQuantity itself.

A successful lookup is stored as an internal value to avoid the need for repeated lookups.

Parameters
  • repo – the Wikibase site for the unit, if different from that provided with the WbQuantity.

  • lazy_load – Do not raise NoPage if ItemPage does not exist.

Returns

pywikibot.ItemPage

toWikibase()dict[source]

Convert the data to a JSON object for the Wikibase API.

Returns

Wikibase JSON

property unit

Return _unit’s entity uri or ‘1’ if _unit is None.

class pywikibot.WbTabularData(page, site: Optional[pywikibot.site._datasite.DataSite] = None)[source]

Bases: pywikibot._WbDataPage

A Wikibase tabular-data representation.

Create a new _WbDataPage object.

Parameters
  • page (pywikibot.Page) – page containing the data

  • site – The Wikibase site

class pywikibot.WbTime(year: Optional[int] = None, month: Optional[int] = None, day: Optional[int] = None, hour: Optional[int] = None, minute: Optional[int] = None, second: Optional[int] = None, precision: Optional[Union[int, str]] = None, before: int = 0, after: int = 0, timezone: int = 0, calendarmodel: Optional[str] = None, site: Optional[pywikibot.site._datasite.DataSite] = None)[source]

Bases: pywikibot._wbtypes.WbRepresentation

A Wikibase time representation.

Create a new WbTime object.

The precision can be set by the Wikibase int value (0-14) or by a human readable string, e.g., ‘hour’. If no precision is given, it is set according to the given time units.

Timezone information is given in three different ways depending on the time:

  • Times after the implementation of UTC (1972): as an offset from UTC in minutes;

  • Times before the implementation of UTC: the offset of the time zone from universal time;

  • Before the implementation of time zones: The longitude of the place of the event, in the range −180° to 180°, multiplied by 4 to convert to minutes.

Parameters
  • year – The year as a signed integer of between 1 and 16 digits.

  • month – Month

  • day – Day

  • hour – Hour

  • minute – Minute

  • second – Second

  • precision – The unit of the precision of the time.

  • before – Number of units after the given time it could be, if uncertain. The unit is given by the precision.

  • after – Number of units before the given time it could be, if uncertain. The unit is given by the precision.

  • timezone – Timezone information in minutes.

  • calendarmodel – URI identifying the calendar model

  • site – The Wikibase site

FORMATSTR = '{0:+012d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}Z'
PRECISION = {'10000': 5, '100000': 4, '1000000': 3, '10000000': 2, '100000000': 1, '1000000000': 0, 'century': 7, 'day': 11, 'decade': 8, 'hour': 12, 'millenia': 6, 'minute': 13, 'month': 10, 'second': 14, 'year': 9}
classmethod fromTimestamp(timestamp, precision: Union[int, str] = 14, before: int = 0, after: int = 0, timezone: int = 0, calendarmodel: Optional[str] = None, site: Optional[pywikibot.site._datasite.DataSite] = None)[source]

Create a new WbTime object from a pywikibot.Timestamp.

Parameters
  • timestamp (pywikibot.Timestamp) – Timestamp

  • precision – The unit of the precision of the time.

  • before – Number of units after the given time it could be, if uncertain. The unit is given by the precision.

  • after – Number of units before the given time it could be, if uncertain. The unit is given by the precision.

  • timezone – Timezone information in minutes.

  • calendarmodel – URI identifying the calendar model

  • site – The Wikibase site

Return type

pywikibot.WbTime

classmethod fromTimestr(datetimestr: str, precision: Union[int, str] = 14, before: int = 0, after: int = 0, timezone: int = 0, calendarmodel: Optional[str] = None, site: Optional[pywikibot.site._datasite.DataSite] = None)[source]

Create a new WbTime object from a UTC date/time string.

The timestamp differs from ISO 8601 in that:

  • The year is always signed and having between 1 and 16 digits;

  • The month, day and time are zero if they are unknown;

  • The Z is discarded since time zone is determined from the timezone param.

Parameters
  • datetimestr – Timestamp in a format resembling ISO 8601, e.g. +2013-01-01T00:00:00Z

  • precision – The unit of the precision of the time.

  • before – Number of units after the given time it could be, if uncertain. The unit is given by the precision.

  • after – Number of units before the given time it could be, if uncertain. The unit is given by the precision.

  • timezone – Timezone information in minutes.

  • calendarmodel – URI identifying the calendar model

  • site – The Wikibase site

Return type

pywikibot.WbTime

classmethod fromWikibase(data: dict, site: Optional[pywikibot.site._datasite.DataSite] = None, wb='[deprecated name of data]')[source]

Create a WbTime from the JSON data given by the Wikibase API.

Parameters
  • data – Wikibase JSON

  • site – The Wikibase site

toTimestamp()pywikibot.Timestamp[source]

Convert the data to a pywikibot.Timestamp.

Raises

ValueError – instance value cannot be represented using Timestamp

toTimestr(force_iso: bool = False)str[source]

Convert the data to a UTC date/time string.

See fromTimestr() for differences between output with and without force_iso.

Parameters

force_iso – whether the output should be forced to ISO 8601

Returns

Timestamp in a format resembling ISO 8601

toWikibase()dict[source]

Convert the data to a JSON object for the Wikibase API.

Returns

Wikibase JSON

class pywikibot.WbUnknown(json)[source]

Bases: pywikibot._wbtypes.WbRepresentation

A Wikibase representation for unknown data type.

This will prevent the bot from breaking completely when a new type is introduced.

This data type is just a json container

New in version 3.0.

Create a new WbUnknown object.

Parameters

json – Wikibase JSON

classmethod fromWikibase(data: dict, site: Optional[pywikibot.site._datasite.DataSite] = None, json='[deprecated name of data]')[source]

Create a WbUnknown from the JSON data given by the Wikibase API.

Parameters
  • data – Wikibase JSON

  • site – The Wikibase site

toWikibase()dict[source]

Return the JSON object for the Wikibase API.

Returns

Wikibase JSON

class pywikibot.WikidataBot(use_from_page=NotImplemented, **kwargs: Any)[source]

Bases: pywikibot.bot.Bot, pywikibot.bot.ExistingPageBot

Generic Wikidata Bot to be subclassed.

Source claims (P143) can be created for specific sites

Variables
  • use_from_page – If True (default) it will apply ItemPage.fromPage for every item. If False it assumes that the pages are actually already ItemPage (page in treat_page_and_item will be None). If None it’ll use ItemPage.fromPage when the page is not in the site’s item namespace.

  • treat_missing_item – Whether pages without items should be treated. Note that this is checked after create_missing_item.

  • create_missing_item – If True, new items will be created if the current page doesn’t have one. Subclasses should override this in the initializer with a bool value or using self.opt attribute.

Initializer of the WikidataBot.

cacheSources()None[source]

Fetch the sources from the list on Wikidata.

It is stored internally and reused by getSource()

create_item_for_page(page: pywikibot.page.BasePage, data: Optional[dict] = None, summary: Optional[str] = None, **kwargs: Any)Any[source]

Create an ItemPage with the provided page as the sitelink.

Parameters
  • page – the page for which the item will be created

  • data – additional data to be included in the new item (optional). Note that data created from the page have higher priority.

  • summary – optional edit summary to replace the default one

Returns

pywikibot.ItemPage or None

getSource(site: pywikibot.site._basesite.BaseSite)Any[source]

Create a Claim usable as a source for Wikibase statements.

Parameters

site – site that is the source of assertions.

Returns

pywikibot.Claim or None

get_property_by_name(property_name: str)str[source]

Find given property and return its ID.

Method first uses site.search() and if the property isn’t found, then asks user to provide the property ID.

Parameters

property_name – property to find

treat_missing_item = False
treat_page()None[source]

Treat a page.

treat_page_and_item(page: pywikibot.page.BasePage, item: pywikibot.page.ItemPage)None[source]

Treat page together with its item (if it exists).

Must be implemented in subclasses.

use_from_page = True
user_add_claim(item: pywikibot.page.ItemPage, claim: pywikibot.page.Claim, source: Optional[Any] = None, bot: bool = True, **kwargs: Any)bool[source]

Add a claim to an item, with user confirmation as required.

Parameters
  • item – page to be edited

  • claim – claim to be saved

  • source – site where the claim comes from

  • bot – whether to flag as bot (if possible)

Keyword Arguments
  • ignore_server_errors – if True, server errors will be reported and ignored (default: False)

  • ignore_save_related_errors – if True, errors related to page save will be reported and ignored (default: False)

Returns

whether the item was saved successfully

user_add_claim_unless_exists(item: pywikibot.page.ItemPage, claim: pywikibot.page.Claim, exists_arg: str = '', source: Optional[Any] = None, logger_callback: collections.abc.Callable[str, Any] = <function log>, **kwargs: Any)bool[source]

Decorator of user_add_claim.

Before adding a new claim, it checks if we can add it, using provided filters.

See

documentation of claimit.py

Parameters
  • exists_arg – pattern for merging existing claims with new ones

  • logger_callback – function logging the output of the method

Returns

whether the claim could be added

user_edit_entity(entity: pywikibot.page.WikibasePage, data: Optional[dict] = None, ignore_save_related_errors: Optional[bool] = None, ignore_server_errors: Optional[bool] = None, **kwargs: Any)bool[source]

Edit entity with data provided, with user confirmation as required.

Parameters
  • entity – page to be edited

  • data – data to be saved, or None if the diff should be created automatically

  • ignore_save_related_errors – Ignore save related errors and automatically print a message. If None uses this instances default.

  • ignore_server_errors – Ignore server errors and automatically print a message. If None uses this instances default.

Keyword Arguments
  • summary – revision comment, passed to ItemPage.editEntity

  • show_diff – show changes between oldtext and newtext (default: True)

Returns

whether the item was saved successfully

pywikibot.calledModuleName()str[source]

Return the name of the module calling this function.

This is required because the -help option loads the module’s docstring and because the module name will be used for the filename of the log.

pywikibot.critical(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a critical record to the user via the userinterface.

Parameters
  • text – the critical message which is to be displayed to the user.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.debug(text: AnyStr, layer: str, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a debug record to the log file.

Parameters
  • text – the message of the debug record to be logged to the log file.

  • layer – logger to record this message upon

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

  • layer – The name of the logger that text will be sent to.

pywikibot.error(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output an error message to the user via the userinterface.

Parameters
  • text – the message containing the error which occurred.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.exception(msg: Optional[Exception] = None, decoder: Optional[str] = None, newline: bool = True, tb: bool = False, **kwargs: Any)None[source]

Output an error traceback to the user via the userinterface.

Use directly after an ‘except’ statement:

...
except Exception:
    pywikibot.exception()
...

or alternatively:

...
except Exception as e:
    pywikibot.exception(e)
...

This function should only be called from an Exception handler.

Parameters
  • msg – If not None, contains the description of the exception that occurred.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

  • tb – Set to True in order to output traceback also.

pywikibot.handle_args(args: Optional[collections.abc.Iterable[str]] = None, do_help: bool = True)list[source]

Handle standard command line arguments, and return the rest as a list.

Takes the command line arguments as strings, processes all global parameters such as -lang or -log, initialises the logging layer, which emits startup information into log at level ‘verbose’.

This makes sure that global arguments are applied first, regardless of the order in which the arguments were given.

args may be passed as an argument, thereby overriding sys.argv

Parameters
  • args – Command line arguments

  • do_help – Handle parameter ‘-help’ to show help and invoke sys.exit

Returns

list of arguments not recognised globally

pywikibot.html2unicode(text: str, ignore=None, exceptions=None)str[source]

Replace HTML entities with equivalent unicode.

Parameters
  • ignore – HTML entities to ignore

  • ignore – list of int

pywikibot.input(question: str, password: bool = False, default: str = '', force: bool = False)str[source]

Ask the user a question, return the user’s answer.

Parameters
  • question – a string that will be shown to the user. Don’t add a space after the question mark/colon, this method will do this for you.

  • password – if True, hides the user’s input (for password entry).

  • default – The default answer if none was entered. None to require an answer.

  • force – Automatically use the default

pywikibot.input_choice(question: str, answers: collections.abc.Iterable[Union[tuple, pywikibot.bot_choice.Option]], default: Optional[str] = None, return_shortcut: bool = True, automatic_quit: bool = True, force: bool = False)Union[int, str][source]

Ask the user the question and return one of the valid answers.

Parameters
  • question – The question asked without trailing spaces.

  • answers – The valid answers each containing a full length answer and a shortcut. Each value must be unique.

  • default – The result if no answer was entered. It must not be in the valid answers and can be disabled by setting it to None. If it should be linked with the valid answers it must be its shortcut.

  • return_shortcut – Whether the shortcut or the index of the answer is returned.

  • automatic_quit – Adds the option ‘Quit’ (‘q’) and throw a QuitKeyboardInterrupt if selected.

  • force – Automatically use the default

Returns

The selected answer shortcut or index. Is -1 if the default is selected, it does not return the shortcut and the default is not a valid shortcut.

pywikibot.input_yn(question: str, default: Optional[Union[bool, str]] = None, automatic_quit: bool = True, force: bool = False)bool[source]

Ask the user a yes/no question and return the answer as a bool.

Parameters
  • question – The question asked without trailing spaces.

  • default – The result if no answer was entered. It must be a bool or ‘y’ or ‘n’ and can be disabled by setting it to None.

  • automatic_quit – Adds the option ‘Quit’ (‘q’) and throw a QuitKeyboardInterrupt if selected.

  • force – Automatically use the default

Returns

Return True if the user selected yes and False if the user selected no. If the default is not None it’ll return True if default is True or ‘y’ and False if default is False or ‘n’.

pywikibot.log(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a record to the log file.

Parameters
  • text – the message which is to be logged to the log file.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.output(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a message to the user via the userinterface.

Works like print, but uses the encoding used by the user’s console (console_encoding in the configuration file) instead of ASCII.

If decoder is None, text should be a unicode string. Otherwise it should be encoded in the given encoding.

If newline is True, a line feed will be added after printing the text.

text can contain special sequences to create colored output. These consist of the escape character 03 and the color name in curly braces, e. g. 03{lightpurple}. 03{default} resets the color. By using the color_format method from pywikibot.tools.formatter, the escape character may be omitted.

Other keyword arguments are passed unchanged to the logger; so far, the only argument that is useful is “exc_info=True”, which causes the log message to include an exception traceback.

pywikibot.showDiff(oldtext, newtext, context=0)[source]

Output a string showing the differences between oldtext and newtext.

The differences are highlighted (only on compatible systems) to show which changes were made.

pywikibot.show_help(module_name: Optional[str] = None, show_global: bool = False)None[source]

Show help for the Bot.

Changed in version 4.0: Renamed from showHelp() to show_help().

pywikibot.stdout(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output script results to the user via the userinterface.

The text will be sent to standard output, so that it can be piped to another process. All other text will be sent to stderr. See: https://en.wikipedia.org/wiki/Pipeline_%28Unix%29

Parameters
  • text – the message printed via stdout logger to the user.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.translate(code, xdict: Union[dict, str], parameters: Optional[collections.abc.Mapping] = None, fallback=False)str[source]

Return the most appropriate localization from a localization dict.

Given a site code and a dictionary, returns the dictionary’s value for key ‘code’ if this key exists; otherwise tries to return a value for an alternative code that is most applicable to use on the wiki in language ‘code’ except fallback is False.

The code itself is always checked first, then these codes that have been defined to be alternatives, and finally English.

If fallback is False and the code is not found in the

For PLURAL support have a look at the twtranslate method.

Parameters
  • code (str or Site object) – The site code as string or Site object. If xdict is an extended dictionary the Site object should be used in favour of the code string. Otherwise localizations from a wrong family might be used.

  • xdict – dictionary with language codes as keys or extended dictionary with family names as keys containing code dictionaries or a single string. May contain PLURAL tags as described in twtranslate

  • parameters – For passing (plural) parameters

  • fallback (boolean or iterable) – Try an alternate language code. If it’s iterable it’ll also try those entries and choose the first match.

Returns

the localized string

Raises
  • IndexError – If the language supports and requires more plurals than defined for the given PLURAL pattern.

  • KeyError – No fallback key found if fallback is not False

pywikibot.warning(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a warning message to the user via the userinterface.

Parameters
  • text – the message the user wants to display.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

Subpackages

Submodules

pywikibot.backports module

This module contains backports to support older Python versions.

pywikibot.backports.removeprefix(self, prefix, /)

Return a str with the given prefix string removed if present.

If the string starts with the prefix string, return string[len(prefix):]. Otherwise, return a copy of the original string.

pywikibot.backports.removesuffix(self, suffix, /)

Return a str with the given suffix string removed if present.

If the string ends with the suffix string and that suffix is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string.

pywikibot.bot module

User-interface related functions for building bots.

This module supports several different bot classes which could be used in conjunction. Each bot should subclass at least one of these four classes:

  • BaseBot: Basic bot class in case where the site is handled differently, like working on multiple sites in parallel. No site attribute is provided. Instead site of the current page should be used. This class should normally not be used directly.

  • SingleSiteBot: Bot class which should only be run on a single site. They usually store site specific content and thus can’t be easily run when the generator returns a page on another site. It has a property site which can also be changed. If the generator returns a page of a different site it’ll skip that page.

  • MultipleSitesBot: An alias of BaseBot. Should not be used if any other bot class is used.

  • ConfigParserBot: Bot class which supports reading options from a scripts.ini configuration file. That file consists of sections, led by a [section] header and followed by option: value or option=value entries. The section is the script name without .py suffix. All options identified must be predefined in available_options dictionary.

  • Bot: The previous base class which should be avoided. This class is mainly used for bots which work with Wikibase or together with an image repository.

Additionally there is the CurrentPageBot class which automatically sets the current page to the page treated. It is recommended to use this class and to use treat_page instead of treat and put_current instead of userPut. It by default subclasses the BaseBot class.

With CurrentPageBot it’s possible to subclass one of the following classes to filter the pages which are ultimately handled by treat_page:

It is possible to combine filters by subclassing multiple of them. They are new-style classes so when a class is first subclassing ExistingPageBot and then FollowRedirectPageBot it will also work on pages which do not exist when a redirect pointed to that. If the order is inversed it’ll first follow them and then check whether they exist.

Additionally there is the AutomaticTWSummaryBot which subclasses CurrentPageBot and automatically defines the summary when put_current is used.

class pywikibot.bot.AlwaysChoice(replacer: Any, option: str = 'always', shortcut: str = 'a')[source]

Bases: pywikibot.bot_choice.Choice

Add an option to always apply the default.

property answer

Get the actual default answer instructing the replacement.

handle()Any[source]

Handle the custom shortcut.

Directly return answer whether it’s applying it always.

class pywikibot.bot.AutomaticTWSummaryBot(**kwargs: Any)[source]

Bases: pywikibot.bot.CurrentPageBot

A class which automatically defines summary for put_current.

The class must defined a summary_key string which contains the i18n key for pywikibot.i18n.twtranslate. It can also override the summary_parameters property to specify any parameters for the translated message.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

put_current(*args: Any, **kwargs: Any)None[source]

Defining a summary if not already defined and then call original.

summary_key = None
property summary_parameters

A dictionary of all parameters for i18n.

class pywikibot.bot.BaseBot(**kwargs: Any)[source]

Bases: pywikibot.bot.OptionHandler

Generic Bot to be subclassed.

This class provides a run() method for basic processing of a generator one page at a time.

If the subclass places a page generator in self.generator, Bot will process each page in the generator, invoking the method treat() which must then be implemented by subclasses.

Each item processed by treat() must be a pywikibot.page.BasePage type. Use init_page() to upcast the type. To enable other types, set BaseBot.treat_page_type to an appropriate type; your bot should derive from BaseBot in that case and handle site properties.

If the subclass does not set a generator, or does not override treat() or run(), NotImplementedError is raised.

For bot options handling refer OptionHandler class above.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

available_options: dict = {'always': False}
property current_page

Return the current working page as a property.

exit()None[source]

Cleanup and exit processing.

Invoked when Bot.run() is finished. Prints treat and save counters and informs whether the script terminated gracefully or was halted by exception. May be overridden by subclasses.

init_page(item: Any)pywikibot.page.BasePage[source]

Initialize a generator item before treating.

Ensure that the result of init_page is always a pywikibot.Page object even when the generator returns something else.

Also used to set the arrange the current site. This is called before skip_page and treat.

Parameters

item – any item from self.generator

Returns

return the page object to be processed further

quit()None[source]

Cleanup and quit processing.

run()None[source]

Process all pages in generator.

Raises

AssertionError – “page” is not a pywikibot.page.BasePage object

setup()None[source]

Some initial setup before run operation starts.

This can be used for reading huge parts from life wiki or file operation which is more than just initialize the instance. Invoked by run() before running through generator loop.

New in version 3.0.

skip_page(page: pywikibot.page.BasePage)bool[source]

Return whether treat should be skipped for the page.

New in version 3.0.

Parameters

page – Page object to be processed

stop()None[source]

Stop iterating.

Deprecated since version Use: generator.close() instead.

teardown()None[source]

Some cleanups after run operation. Invoked by exit().

New in version 3.0.

treat(page: pywikibot.page.BasePage)None[source]

Process one page (abstract method).

Parameters

page – Page object to be processed

update_options: dict = {}
userPut(page: pywikibot.page.BasePage, oldtext: str, newtext: str, comment='[deprecated name of summary]', async='[deprecated name of asynchronous]', **kwargs: Any)[source]

Save a new revision of a page, with user confirmation as required.

Print differences, ask user for confirmation, and puts the page if needed.

Option used:

  • ‘always’

Keyword args used:

  • ‘asynchronous’ - passed to page.save

  • ‘summary’ - passed to page.save

  • ‘show_diff’ - show changes between oldtext and newtext (enabled)

  • ‘ignore_save_related_errors’ - report and ignore (disabled)

  • ‘ignore_server_errors’ - report and ignore (disabled)

Returns

whether the page was saved successfully

user_confirm(question: str)bool[source]

Obtain user response if bot option ‘always’ not enabled.

class pywikibot.bot.Bot(site: Optional[Any] = None, **kwargs: Any)[source]

Bases: pywikibot.bot.BaseBot

Generic bot subclass for multiple sites.

If possible the MultipleSitesBot or SingleSiteBot classes should be used instead which specifically handle multiple or single sites.

Create a Bot instance and initialize cached sites.

init_page(item: Any)pywikibot.page.BasePage[source]

Update site before calling treat.

run()None[source]

Check if it automatically updates the site before run.

property site

Get the current site.

class pywikibot.bot.Choice(option: str, shortcut: str, replacer: Any)[source]

Bases: pywikibot.bot_choice.StandardOption

A simple choice consisting of an option, shortcut and handler.

abstract handle()Any[source]

Handle this choice. Must be implemented.

The current link will be handled by this choice.

property replacer

The replacer.

exception pywikibot.bot.ChoiceException(option: str, shortcut: str, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.StandardOption, Exception

A choice for input_choice which result in this exception.

Parameters
  • option – option string

  • shortcut – Shortcut of the option

result(value: Any)Any[source]

Return itself to raise the exception.

class pywikibot.bot.ConfigParserBot(**kwargs: Any)[source]

Bases: pywikibot.bot.BaseBot

A bot class that can read options from scripts.ini file.

All options must be predefined in available_options dictionary. The type of these options is responsible for the correct interpretation of the options type given by the .ini file. They can be interpreted as bool, int, float or str (default). The settings file may be like:

[add_text]
# edit summary for the bot.
summary = Bot: Aggiungo template Categorizzare

[shell] ; Shell options
always: true

The option values are interpreted in this order:

  1. available_options default setting

  2. script.ini options settings

  3. command line arguments

New in version 3.0.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

INI = 'scripts.ini'
set_options(**kwargs: Any)None[source]

Read settings from scripts.ini file.

class pywikibot.bot.ContextOption(option: str, shortcut: str, text: str, context: int, delta: int = 100, start: int = 0, end: int = 0)[source]

Bases: pywikibot.bot_choice.OutputOption, pywikibot.bot_choice.StandardOption

An option to show more and more context.

property out

Output section of the text.

result(value: str)Any[source]

Add the delta to the context.

class pywikibot.bot.CreatingPageBot(**kwargs: Any)[source]

Bases: pywikibot.bot.CurrentPageBot

A CurrentPageBot class which only treats nonexistent pages.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

skip_page(page: pywikibot.page.BasePage)bool[source]

Treat page if doesn’t exist.

class pywikibot.bot.CurrentPageBot(**kwargs: Any)[source]

Bases: pywikibot.bot.BaseBot

A bot which automatically sets ‘current_page’ on each treat().

This class should be always used together with either the MultipleSitesBot or SingleSiteBot class as there is no site management in this class.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

ignore_server_errors = False
put_current(new_text: str, ignore_save_related_errors: Optional[bool] = None, ignore_server_errors: Optional[bool] = None, comment='[deprecated name of summary]', **kwargs: Any)[source]

Call Bot.userPut but use the current page.

It compares the new_text to the current page text.

Parameters
  • new_text – The new text

  • ignore_save_related_errors – Ignore save related errors and automatically print a message. If None uses this instances default.

  • ignore_server_errors – Ignore server errors and automatically print a message. If None uses this instances default.

  • kwargs – Additional parameters directly given to Bot.userPut.

Returns

whether the page was saved successfully

treat(page: pywikibot.page.BasePage)None[source]

Set page to current page and treat that page.

treat_page()None[source]

Process one page (Abstract method).

class pywikibot.bot.ExistingPageBot(**kwargs: Any)[source]

Bases: pywikibot.bot.CurrentPageBot

A CurrentPageBot class which only treats existing pages.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

skip_page(page: pywikibot.page.BasePage)bool[source]

Treat page if it exists and handle NoPageError.

class pywikibot.bot.FollowRedirectPageBot(**kwargs: Any)[source]

Bases: pywikibot.bot.CurrentPageBot

A CurrentPageBot class which follows the redirect.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

treat(page: pywikibot.page.BasePage)None[source]

Treat target if page is redirect and the page otherwise.

class pywikibot.bot.HighlightContextOption(option: str, shortcut: str, text: str, context: int, delta: int = 100, start: int = 0, end: int = 0)[source]

Bases: pywikibot.bot_choice.ContextOption

Show the original region highlighted.

color = 'lightred'
property out

Highlighted output section of the text.

class pywikibot.bot.IntegerOption(minimum: int = 1, maximum: Optional[int] = None, prefix: str = '', **kwargs: Any)[source]

Bases: pywikibot.bot_choice.Option

An option allowing a range of integers.

format(default: Optional[str] = None)str[source]

Return a formatted string showing the range.

property maximum

Return the upper bound of the range of allowed values.

property minimum

Return the lower bound of the range of allowed values.

parse(value: str)int[source]

Return integer from value with prefix removed.

result(value: str)Any[source]

Return a tuple with the prefix and value converted into an int.

test(value: str)bool[source]

Return whether the value is an int and in the specified range.

class pywikibot.bot.InteractiveReplace(old_link: Any, new_link: Union[Any, bool], default: Optional[str] = None, automatic_quit: bool = True)[source]

Bases: object

A callback class for textlib’s replace_links.

It shows various options which can be switched on and off: * allow_skip_link = True (skip the current link) * allow_unlink = True (unlink) * allow_replace = False (just replace target, keep section and label) * allow_replace_section = False (replace target and section, keep label) * allow_replace_label = False (replace target and label, keep section) * allow_replace_all = False (replace target, section and label) (The boolean values are the default values)

It has also a context attribute which must be a non-negative integer. If it is greater 0 it shows that many characters before and after the link in question. The context_delta attribute can be defined too and adds an option to increase context by the given amount each time the option is selected.

Additional choices can be defined using the ‘additional_choices’ and will be amended to the choices defined by this class. This list is mutable and the Choice instance returned and created by this class are too.

Parameters
  • old_link – The old link which is searched. The label and section are ignored.

  • new_link – The new link with which it should be replaced. Depending on the replacement mode it’ll use this link’s label and section. If False it’ll unlink all and the attributes beginning with allow_replace are ignored.

  • default – The default answer as the shortcut

  • automatic_quit – Add an option to quit and raise a QuitKeyboardException.

property choices

Return the tuple of choices.

property current_groups

Get the current groups when it’s handling one currently.

Get the current link when it’s handling one currently.

property current_range

Get the current range when it’s handling one currently.

property current_text

Get the current text when it’s handling one currently.

handle_answer(choice: str)Any[source]

Return the result for replace_links.

Handle the currently given replacement.

class pywikibot.bot.LinkChoice(option: str, shortcut: str, replacer: Any, replace_section: bool, replace_label: bool)[source]

Bases: pywikibot.bot_choice.Choice

A choice returning a mix of the link new and current link.

handle()Any[source]

Handle by either applying the new section or label.

class pywikibot.bot.ListOption(sequence: collections.abc.Sequence[str], prefix: str = '', **kwargs: Any)[source]

Bases: pywikibot.bot_choice.IntegerOption

An option to select something from a list.

format(default: Optional[str] = None)str[source]

Return a string showing the range.

property maximum

Return the maximum value.

result(value: str)Any[source]

Return a tuple with the prefix and selected value.

class pywikibot.bot.LoggingFormatter(fmt=None, datefmt=None, style='%', validate=True)[source]

Bases: logging.Formatter

Format LogRecords for output to file.

Initialize the formatter with specified format strings.

Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.

Use a style parameter of ‘%’, ‘{‘ or ‘$’ to specify that you want to use one of %-formatting, str.format() ({}) formatting or string.Template formatting in your format string.

Changed in version 3.2: Added the style parameter.

format(record)[source]

Strip trailing newlines before outputting text to file.

class pywikibot.bot.MultipleChoiceList(sequence: collections.abc.Sequence[str], prefix: str = '', **kwargs: Any)[source]

Bases: pywikibot.bot_choice.ListOption

An option to select multiple items from a list.

New in version 3.0.

result(value: str)Any[source]

Return a tuple with the prefix and selected values as a list.

test(value: str)bool[source]

Return whether the values are int and in the specified range.

class pywikibot.bot.MultipleSitesBot(**kwargs: Any)[source]

Bases: pywikibot.bot.BaseBot

A bot class working on multiple sites.

The bot should accommodate for that case and not store site specific information on only one site.

Changed in version 6.2: Site attribute has been dropped.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

class pywikibot.bot.NestedOption(option: str, shortcut: str, description: str, options: collections.abc.Iterable[pywikibot.bot_choice.Option])[source]

Bases: pywikibot.bot_choice.OutputOption, pywikibot.bot_choice.StandardOption

An option containing other options.

It will return True in test if this option applies but False if a sub option applies while handle returns the sub option.

format(default: Optional[str] = None)str[source]

Return a formatted string for that option.

handled(value: str)Optional[pywikibot.bot_choice.Option][source]

Return itself if it applies or the applying sub option.

property out

Output of suboptions.

class pywikibot.bot.NoRedirectPageBot(**kwargs: Any)[source]

Bases: pywikibot.bot.CurrentPageBot

A NoRedirectPageBot class which only treats non-redirects.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

skip_page(page: pywikibot.page.BasePage)bool[source]

Treat only non-redirect pages and handle IsRedirectPageError.

class pywikibot.bot.Option(stop: bool = True)[source]

Bases: abc.ABC

A basic option for input_choice.

The following methods need to be implemented:

  • format(default=None)

  • result(value)

  • test(value)

The methods test and handled are in such a relationship that when handled returns itself that test must return True for that value. So if test returns False handled may not return itself but it may return not None.

Also result only returns a sensible value when test returns True for the same value.

format(default: Optional[str] = None)str[source]

Return a formatted string for that option.

static formatted(text: str, options: collections.abc.Iterable[Option], default: Optional[str] = None)str[source]

Create a text with the options formatted into it.

This static method is used by pywikibot.input_choice(). It calls format for all options to combine the question for pywikibot.input().

Parameters
  • text – Text into which options are to be formatted

  • options – Option instances to be formatted

  • default – filler for any option’s ‘default’ placeholder

Returns

Text with the options formatted into it

handled(value: str)Optional[pywikibot.bot_choice.Option][source]

Return the Option object that applies to the given value.

If this Option object doesn’t know which applies it returns None.

abstract result(value: str)Any[source]

Return the actual value which is associated by the given one.

New in version 6.2: result() is an abstract method and must be defined in subclasses

property stop

Return whether this option stops asking.

test(value: str)bool[source]

Return True whether this option applies.

class pywikibot.bot.OptionHandler(**kwargs: Any)[source]

Bases: object

Class to get and set options.

How to use options of OptionHandler and its BaseBot subclasses: First define an available_options class attribute for your own option handler to define all available options:

>>> default_options = {'foo': 'bar', 'bar': 42, 'baz': False}
>>> class MyHandler(OptionHandler): available_options = default_options

Or you may update the predefined setting in the class initializer. BaseBot predefines a ‘always’ options and sets it to False:

self.available_options.update(always=True, another_option=’Yes’)

Now you can instantiate an OptionHandler or BaseBot class passing options other than default values:

>>> bot = MyHandler(baz=True)

You can access bot options either as keyword item or attribute:

>>> bot.opt.foo
'bar'
>>> bot.opt['bar']
42
>>> bot.opt.baz  # default was overridden
True

You can set the options in the same way:

>>> bot.opt.bar = 4711
>>> bot.opt['baz'] = None
>>>

Or you can use the option as a dict:

>>> 'Option opt.{foo} is {bar}'.format_map(bot.opt)
'Option opt.bar is 4711'

Only accept options defined in available_options.

Parameters

kwargs – bot options

available_options: dict = {}
set_options(**options: Any)None[source]

Set the instance options.

class pywikibot.bot.OutputProxyOption(option: str, shortcut: str, output: pywikibot.bot_choice.OutputOption, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.OutputOption, pywikibot.bot_choice.StandardOption

An option which calls out property of the given output class.

Create a new option for the given sequence.

property out

Return the contents.

exception pywikibot.bot.QuitKeyboardInterrupt[source]

Bases: pywikibot.bot_choice.ChoiceException, KeyboardInterrupt

The user has cancelled processing at a prompt.

Constructor using the ‘quit’ (‘q’) in input_choice.

class pywikibot.bot.RedirectPageBot(**kwargs: Any)[source]

Bases: pywikibot.bot.CurrentPageBot

A RedirectPageBot class which only treats redirects.

Only accept ‘generator’ and options defined in available_options.

Parameters

kwargs – bot options

Keyword Arguments

generator – a generator processed by run method

skip_page(page: pywikibot.page.BasePage)bool[source]

Treat only redirect pages and handle IsNotRedirectPageError.

pywikibot.bot.RotatingFileHandler(*a, **kw)
class pywikibot.bot.ShowingListOption(sequence: collections.abc.Sequence[str], prefix: str = '', pre: Optional[str] = None, post: Optional[str] = None, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.ListOption, pywikibot.bot_choice.OutputOption

An option to show a list and select an item.

New in version 3.0.

Parameters
  • pre – Additional comment printed before the list.

  • post – Additional comment printed after the list.

before_question: bool = True

Place output before or after the question

property out

Output text of the enumerated list.

property stop

Return whether this option stops asking.

class pywikibot.bot.ShowingMultipleChoiceList(sequence: collections.abc.Sequence[str], prefix: str = '', pre: Optional[str] = None, post: Optional[str] = None, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.ShowingListOption, pywikibot.bot_choice.MultipleChoiceList

An option to show a list and select multiple items.

New in version 3.0.

Parameters
  • pre – Additional comment printed before the list.

  • post – Additional comment printed after the list.

class pywikibot.bot.SingleSiteBot(site: Union[Any, bool] = True, **kwargs: Any)[source]

Bases: pywikibot.bot.BaseBot

A bot only working on one site and ignoring the others.

If no site is given from the start it’ll use the first page’s site. Any page after the site has been defined and is not on the defined site will be ignored.

Create a SingleSiteBot instance.

Parameters

site – If True it’ll be set to the configured site using pywikibot.Site.

init_page(item: Any)pywikibot.page.BasePage[source]

Set site if not defined.

property site

Site that the bot is using.

skip_page(page: pywikibot.page.BasePage)bool[source]

Skip page if it is not on the defined site.

class pywikibot.bot.StandardOption(option: str, shortcut: str, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.Option

An option with a description and shortcut and returning the shortcut.

Parameters
  • option – option string

  • shortcut – Shortcut of the option

format(default: Optional[str] = None)str[source]

Return a formatted string for that option.

result(value: str)Any[source]

Return the lowercased shortcut.

test(value: str)bool[source]

Return True whether this option applies.

class pywikibot.bot.StaticChoice(option: str, shortcut: str, result: Any)[source]

Bases: pywikibot.bot_choice.Choice

A static choice which just returns the given value.

Create instance with replacer set to None.

handle()Any[source]

Return the predefined value.

exception pywikibot.bot.UnhandledAnswer[source]

Bases: Exception

The given answer didn’t suffice.

class pywikibot.bot.WikidataBot(use_from_page=NotImplemented, **kwargs: Any)[source]

Bases: pywikibot.bot.Bot, pywikibot.bot.ExistingPageBot

Generic Wikidata Bot to be subclassed.

Source claims (P143) can be created for specific sites

Variables
  • use_from_page – If True (default) it will apply ItemPage.fromPage for every item. If False it assumes that the pages are actually already ItemPage (page in treat_page_and_item will be None). If None it’ll use ItemPage.fromPage when the page is not in the site’s item namespace.

  • treat_missing_item – Whether pages without items should be treated. Note that this is checked after create_missing_item.

  • create_missing_item – If True, new items will be created if the current page doesn’t have one. Subclasses should override this in the initializer with a bool value or using self.opt attribute.

Initializer of the WikidataBot.

cacheSources()None[source]

Fetch the sources from the list on Wikidata.

It is stored internally and reused by getSource()

create_item_for_page(page: pywikibot.page.BasePage, data: Optional[dict] = None, summary: Optional[str] = None, **kwargs: Any)Any[source]

Create an ItemPage with the provided page as the sitelink.

Parameters
  • page – the page for which the item will be created

  • data – additional data to be included in the new item (optional). Note that data created from the page have higher priority.

  • summary – optional edit summary to replace the default one

Returns

pywikibot.ItemPage or None

getSource(site: pywikibot.site._basesite.BaseSite)Any[source]

Create a Claim usable as a source for Wikibase statements.

Parameters

site – site that is the source of assertions.

Returns

pywikibot.Claim or None

get_property_by_name(property_name: str)str[source]

Find given property and return its ID.

Method first uses site.search() and if the property isn’t found, then asks user to provide the property ID.

Parameters

property_name – property to find

treat_missing_item = False
treat_page()None[source]

Treat a page.

treat_page_and_item(page: pywikibot.page.BasePage, item: pywikibot.page.ItemPage)None[source]

Treat page together with its item (if it exists).

Must be implemented in subclasses.

use_from_page = True
user_add_claim(item: pywikibot.page.ItemPage, claim: pywikibot.page.Claim, source: Optional[Any] = None, bot: bool = True, **kwargs: Any)bool[source]

Add a claim to an item, with user confirmation as required.

Parameters
  • item – page to be edited

  • claim – claim to be saved

  • source – site where the claim comes from

  • bot – whether to flag as bot (if possible)

Keyword Arguments
  • ignore_server_errors – if True, server errors will be reported and ignored (default: False)

  • ignore_save_related_errors – if True, errors related to page save will be reported and ignored (default: False)

Returns

whether the item was saved successfully

user_add_claim_unless_exists(item: pywikibot.page.ItemPage, claim: pywikibot.page.Claim, exists_arg: str = '', source: Optional[Any] = None, logger_callback: collections.abc.Callable[str, Any] = <function log>, **kwargs: Any)bool[source]

Decorator of user_add_claim.

Before adding a new claim, it checks if we can add it, using provided filters.

See

documentation of claimit.py

Parameters
  • exists_arg – pattern for merging existing claims with new ones

  • logger_callback – function logging the output of the method

Returns

whether the claim could be added

user_edit_entity(entity: pywikibot.page.WikibasePage, data: Optional[dict] = None, ignore_save_related_errors: Optional[bool] = None, ignore_server_errors: Optional[bool] = None, **kwargs: Any)bool[source]

Edit entity with data provided, with user confirmation as required.

Parameters
  • entity – page to be edited

  • data – data to be saved, or None if the diff should be created automatically

  • ignore_save_related_errors – Ignore save related errors and automatically print a message. If None uses this instances default.

  • ignore_server_errors – Ignore server errors and automatically print a message. If None uses this instances default.

Keyword Arguments
  • summary – revision comment, passed to ItemPage.editEntity

  • show_diff – show changes between oldtext and newtext (default: True)

Returns

whether the item was saved successfully

pywikibot.bot.calledModuleName()str[source]

Return the name of the module calling this function.

This is required because the -help option loads the module’s docstring and because the module name will be used for the filename of the log.

pywikibot.bot.critical(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a critical record to the user via the userinterface.

Parameters
  • text – the critical message which is to be displayed to the user.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.bot.debug(text: AnyStr, layer: str, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a debug record to the log file.

Parameters
  • text – the message of the debug record to be logged to the log file.

  • layer – logger to record this message upon

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

  • layer – The name of the logger that text will be sent to.

pywikibot.bot.error(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output an error message to the user via the userinterface.

Parameters
  • text – the message containing the error which occurred.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.bot.exception(msg: Optional[Exception] = None, decoder: Optional[str] = None, newline: bool = True, tb: bool = False, **kwargs: Any)None[source]

Output an error traceback to the user via the userinterface.

Use directly after an ‘except’ statement:

...
except Exception:
    pywikibot.exception()
...

or alternatively:

...
except Exception as e:
    pywikibot.exception(e)
...

This function should only be called from an Exception handler.

Parameters
  • msg – If not None, contains the description of the exception that occurred.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

  • tb – Set to True in order to output traceback also.

pywikibot.bot.handle_args(args: Optional[collections.abc.Iterable[str]] = None, do_help: bool = True)list[source]

Handle standard command line arguments, and return the rest as a list.

Takes the command line arguments as strings, processes all global parameters such as -lang or -log, initialises the logging layer, which emits startup information into log at level ‘verbose’.

This makes sure that global arguments are applied first, regardless of the order in which the arguments were given.

args may be passed as an argument, thereby overriding sys.argv

Parameters
  • args – Command line arguments

  • do_help – Handle parameter ‘-help’ to show help and invoke sys.exit

Returns

list of arguments not recognised globally

pywikibot.bot.init_handlers()None[source]

Initialize logging system for terminal-based bots.

This function must be called before using pywikibot.output(); and must be called again if the destination stream is changed.

Note: this function is called by handle_args(), so it should normally not need to be called explicitly

All user output is routed through the logging module. Each type of output is handled by an appropriate handler object. This structure is used to permit eventual development of other user interfaces (GUIs) without modifying the core bot code.

The following output levels are defined:

  • DEBUG: only for file logging; debugging messages.

  • STDOUT: output that must be sent to sys.stdout (for bots that may have their output redirected to a file or other destination).

  • VERBOSE: optional progress information for display to user.

  • INFO: normal (non-optional) progress information for display to user.

  • INPUT: prompts requiring user response.

  • WARN: user warning messages.

  • ERROR: user error messages.

  • CRITICAL: fatal error messages.

Accordingly, do not use print statements in bot code; instead, use pywikibot.output function.

Changed in version 6.2: Different logfiles are used if multiple processes of the same script are running.

pywikibot.bot.input(question: str, password: bool = False, default: str = '', force: bool = False)str[source]

Ask the user a question, return the user’s answer.

Parameters
  • question – a string that will be shown to the user. Don’t add a space after the question mark/colon, this method will do this for you.

  • password – if True, hides the user’s input (for password entry).

  • default – The default answer if none was entered. None to require an answer.

  • force – Automatically use the default

pywikibot.bot.input_choice(question: str, answers: collections.abc.Iterable[Union[tuple, pywikibot.bot_choice.Option]], default: Optional[str] = None, return_shortcut: bool = True, automatic_quit: bool = True, force: bool = False)Union[int, str][source]

Ask the user the question and return one of the valid answers.

Parameters
  • question – The question asked without trailing spaces.

  • answers – The valid answers each containing a full length answer and a shortcut. Each value must be unique.

  • default – The result if no answer was entered. It must not be in the valid answers and can be disabled by setting it to None. If it should be linked with the valid answers it must be its shortcut.

  • return_shortcut – Whether the shortcut or the index of the answer is returned.

  • automatic_quit – Adds the option ‘Quit’ (‘q’) and throw a QuitKeyboardInterrupt if selected.

  • force – Automatically use the default

Returns

The selected answer shortcut or index. Is -1 if the default is selected, it does not return the shortcut and the default is not a valid shortcut.

pywikibot.bot.input_list_choice(question: str, answers: collections.abc.Iterable[Union[tuple, pywikibot.bot_choice.Option]], default: Optional[Union[int, str]] = None, force: bool = False)str[source]

Ask the user the question and return one of the valid answers.

Parameters
  • question – The question asked without trailing spaces.

  • answers – The valid answers each containing a full length answer.

  • default – The result if no answer was entered. It must not be in the valid answers and can be disabled by setting it to None.

  • force – Automatically use the default

Returns

The selected answer.

pywikibot.bot.input_yn(question: str, default: Optional[Union[bool, str]] = None, automatic_quit: bool = True, force: bool = False)bool[source]

Ask the user a yes/no question and return the answer as a bool.

Parameters
  • question – The question asked without trailing spaces.

  • default – The result if no answer was entered. It must be a bool or ‘y’ or ‘n’ and can be disabled by setting it to None.

  • automatic_quit – Adds the option ‘Quit’ (‘q’) and throw a QuitKeyboardInterrupt if selected.

  • force – Automatically use the default

Returns

Return True if the user selected yes and False if the user selected no. If the default is not None it’ll return True if default is True or ‘y’ and False if default is False or ‘n’.

pywikibot.bot.log(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a record to the log file.

Parameters
  • text – the message which is to be logged to the log file.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.bot.open_webbrowser(page: pywikibot.page.BasePage)None[source]

Open the web browser displaying the page and wait for input.

pywikibot.bot.output(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a message to the user via the userinterface.

Works like print, but uses the encoding used by the user’s console (console_encoding in the configuration file) instead of ASCII.

If decoder is None, text should be a unicode string. Otherwise it should be encoded in the given encoding.

If newline is True, a line feed will be added after printing the text.

text can contain special sequences to create colored output. These consist of the escape character 03 and the color name in curly braces, e. g. 03{lightpurple}. 03{default} resets the color. By using the color_format method from pywikibot.tools.formatter, the escape character may be omitted.

Other keyword arguments are passed unchanged to the logger; so far, the only argument that is useful is “exc_info=True”, which causes the log message to include an exception traceback.

pywikibot.bot.show_help(module_name: Optional[str] = None, show_global: bool = False)None[source]

Show help for the Bot.

Changed in version 4.0: Renamed from showHelp() to show_help().

pywikibot.bot.stdout(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output script results to the user via the userinterface.

The text will be sent to standard output, so that it can be piped to another process. All other text will be sent to stderr. See: https://en.wikipedia.org/wiki/Pipeline_%28Unix%29

Parameters
  • text – the message printed via stdout logger to the user.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.bot.suggest_help(missing_parameters: Optional[collections.abc.Sequence[str]] = None, missing_generator: bool = False, unknown_parameters: Optional[collections.abc.Sequence[str]] = None, exception: Optional[Exception] = None, missing_action: bool = False, additional_text: str = '', missing_dependencies: Optional[collections.abc.Sequence[str]] = None)bool[source]

Output error message to use -help with additional text before it.

Parameters
  • missing_parameters – A list of parameters which are missing.

  • missing_generator – Whether a generator is missing.

  • unknown_parameters – A list of parameters which are unknown.

  • exception – An exception thrown.

  • missing_action – Add an entry that no action was defined.

  • additional_text – Additional text added to the end.

  • missing_dependencies – A list of dependencies which cannot be imported.

Returns

True if an error message was printed, False otherwise

pywikibot.bot.warning(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a warning message to the user via the userinterface.

Parameters
  • text – the message the user wants to display.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.bot.writeToCommandLogFile()None[source]

Save name of the called module along with all params to logs/commands.log.

This can be used by user later to track errors or report bugs.

pywikibot.bot.writelogheader()None[source]

Save additional version, system and status info to the log file in use.

This may help the user to track errors or report bugs.

pywikibot.bot_choice module

Options and Choices for pywikibot.input_choice().

class pywikibot.bot_choice.AlwaysChoice(replacer: Any, option: str = 'always', shortcut: str = 'a')[source]

Bases: pywikibot.bot_choice.Choice

Add an option to always apply the default.

property answer

Get the actual default answer instructing the replacement.

handle()Any[source]

Handle the custom shortcut.

Directly return answer whether it’s applying it always.

class pywikibot.bot_choice.Choice(option: str, shortcut: str, replacer: Any)[source]

Bases: pywikibot.bot_choice.StandardOption

A simple choice consisting of an option, shortcut and handler.

abstract handle()Any[source]

Handle this choice. Must be implemented.

The current link will be handled by this choice.

property replacer

The replacer.

exception pywikibot.bot_choice.ChoiceException(option: str, shortcut: str, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.StandardOption, Exception

A choice for input_choice which result in this exception.

Parameters
  • option – option string

  • shortcut – Shortcut of the option

result(value: Any)Any[source]

Return itself to raise the exception.

class pywikibot.bot_choice.ContextOption(option: str, shortcut: str, text: str, context: int, delta: int = 100, start: int = 0, end: int = 0)[source]

Bases: pywikibot.bot_choice.OutputOption, pywikibot.bot_choice.StandardOption

An option to show more and more context.

property out

Output section of the text.

result(value: str)Any[source]

Add the delta to the context.

class pywikibot.bot_choice.HighlightContextOption(option: str, shortcut: str, text: str, context: int, delta: int = 100, start: int = 0, end: int = 0)[source]

Bases: pywikibot.bot_choice.ContextOption

Show the original region highlighted.

color = 'lightred'
property out

Highlighted output section of the text.

class pywikibot.bot_choice.IntegerOption(minimum: int = 1, maximum: Optional[int] = None, prefix: str = '', **kwargs: Any)[source]

Bases: pywikibot.bot_choice.Option

An option allowing a range of integers.

format(default: Optional[str] = None)str[source]

Return a formatted string showing the range.

property maximum

Return the upper bound of the range of allowed values.

property minimum

Return the lower bound of the range of allowed values.

parse(value: str)int[source]

Return integer from value with prefix removed.

result(value: str)Any[source]

Return a tuple with the prefix and value converted into an int.

test(value: str)bool[source]

Return whether the value is an int and in the specified range.

class pywikibot.bot_choice.LinkChoice(option: str, shortcut: str, replacer: Any, replace_section: bool, replace_label: bool)[source]

Bases: pywikibot.bot_choice.Choice

A choice returning a mix of the link new and current link.

handle()Any[source]

Handle by either applying the new section or label.

class pywikibot.bot_choice.ListOption(sequence: collections.abc.Sequence[str], prefix: str = '', **kwargs: Any)[source]

Bases: pywikibot.bot_choice.IntegerOption

An option to select something from a list.

format(default: Optional[str] = None)str[source]

Return a string showing the range.

property maximum

Return the maximum value.

result(value: str)Any[source]

Return a tuple with the prefix and selected value.

class pywikibot.bot_choice.MultipleChoiceList(sequence: collections.abc.Sequence[str], prefix: str = '', **kwargs: Any)[source]

Bases: pywikibot.bot_choice.ListOption

An option to select multiple items from a list.

New in version 3.0.

result(value: str)Any[source]

Return a tuple with the prefix and selected values as a list.

test(value: str)bool[source]

Return whether the values are int and in the specified range.

class pywikibot.bot_choice.NestedOption(option: str, shortcut: str, description: str, options: collections.abc.Iterable[pywikibot.bot_choice.Option])[source]

Bases: pywikibot.bot_choice.OutputOption, pywikibot.bot_choice.StandardOption

An option containing other options.

It will return True in test if this option applies but False if a sub option applies while handle returns the sub option.

format(default: Optional[str] = None)str[source]

Return a formatted string for that option.

handled(value: str)Optional[pywikibot.bot_choice.Option][source]

Return itself if it applies or the applying sub option.

property out

Output of suboptions.

class pywikibot.bot_choice.Option(stop: bool = True)[source]

Bases: abc.ABC

A basic option for input_choice.

The following methods need to be implemented:

  • format(default=None)

  • result(value)

  • test(value)

The methods test and handled are in such a relationship that when handled returns itself that test must return True for that value. So if test returns False handled may not return itself but it may return not None.

Also result only returns a sensible value when test returns True for the same value.

format(default: Optional[str] = None)str[source]

Return a formatted string for that option.

static formatted(text: str, options: collections.abc.Iterable[Option], default: Optional[str] = None)str[source]

Create a text with the options formatted into it.

This static method is used by pywikibot.input_choice(). It calls format for all options to combine the question for pywikibot.input().

Parameters
  • text – Text into which options are to be formatted

  • options – Option instances to be formatted

  • default – filler for any option’s ‘default’ placeholder

Returns

Text with the options formatted into it

handled(value: str)Optional[pywikibot.bot_choice.Option][source]

Return the Option object that applies to the given value.

If this Option object doesn’t know which applies it returns None.

abstract result(value: str)Any[source]

Return the actual value which is associated by the given one.

New in version 6.2: result() is an abstract method and must be defined in subclasses

property stop

Return whether this option stops asking.

test(value: str)bool[source]

Return True whether this option applies.

class pywikibot.bot_choice.OutputOption(stop: bool = True)[source]

Bases: pywikibot.bot_choice.Option

An option that never stops and can output on each question.

pywikibot.input_choice() uses before_question attribute to decide whether to output before or after the question.

Note

OutputOption must have an out property which returns a string for userinterface output() method.

before_question: bool = False

Place output before or after the question

property out

String to be used when selected before or after the question.

Note

This method is used by ui.input_choice instead of output().

New in version 6.2.

output()None[source]

Output string.

Deprecated since version 6.5: This method was replaced by out property and is no no longer used by the userinterfaces system.

result(value: str)Any[source]

Just return None.

property stop

Never stop asking.

class pywikibot.bot_choice.OutputProxyOption(option: str, shortcut: str, output: pywikibot.bot_choice.OutputOption, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.OutputOption, pywikibot.bot_choice.StandardOption

An option which calls out property of the given output class.

Create a new option for the given sequence.

property out

Return the contents.

exception pywikibot.bot_choice.QuitKeyboardInterrupt[source]

Bases: pywikibot.bot_choice.ChoiceException, KeyboardInterrupt

The user has cancelled processing at a prompt.

Constructor using the ‘quit’ (‘q’) in input_choice.

class pywikibot.bot_choice.ShowingListOption(sequence: collections.abc.Sequence[str], prefix: str = '', pre: Optional[str] = None, post: Optional[str] = None, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.ListOption, pywikibot.bot_choice.OutputOption

An option to show a list and select an item.

New in version 3.0.

Parameters
  • pre – Additional comment printed before the list.

  • post – Additional comment printed after the list.

before_question: bool = True

Place output before or after the question

property out

Output text of the enumerated list.

property stop

Return whether this option stops asking.

class pywikibot.bot_choice.ShowingMultipleChoiceList(sequence: collections.abc.Sequence[str], prefix: str = '', pre: Optional[str] = None, post: Optional[str] = None, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.ShowingListOption, pywikibot.bot_choice.MultipleChoiceList

An option to show a list and select multiple items.

New in version 3.0.

Parameters
  • pre – Additional comment printed before the list.

  • post – Additional comment printed after the list.

class pywikibot.bot_choice.StandardOption(option: str, shortcut: str, **kwargs: Any)[source]

Bases: pywikibot.bot_choice.Option

An option with a description and shortcut and returning the shortcut.

Parameters
  • option – option string

  • shortcut – Shortcut of the option

format(default: Optional[str] = None)str[source]

Return a formatted string for that option.

result(value: str)Any[source]

Return the lowercased shortcut.

test(value: str)bool[source]

Return True whether this option applies.

class pywikibot.bot_choice.StaticChoice(option: str, shortcut: str, result: Any)[source]

Bases: pywikibot.bot_choice.Choice

A static choice which just returns the given value.

Create instance with replacer set to None.

handle()Any[source]

Return the predefined value.

exception pywikibot.bot_choice.UnhandledAnswer[source]

Bases: Exception

The given answer didn’t suffice.

pywikibot.config module

API reference

Module to define and load pywikibot configuration default and user preferences.

User preferences are loaded from a python file called user-config.py, which may be located in directory specified by the environment variable PYWIKIBOT_DIR, or the same directory as pwb.py, or in a directory within the users home. See get_base_dir for more information.

If user-config.py cannot be found in any of those locations, this module will fail to load unless the environment variable PYWIKIBOT_NO_USER_CONFIG is set to a value other than '0'. i.e. PYWIKIBOT_NO_USER_CONFIG=1 will allow config to load without a user-config.py. However, warnings will be shown if user-config.py was not loaded. To prevent these warnings, set PYWIKIBOT_NO_USER_CONFIG=2. If Pywikibot is installed as a site-package the behaviour is like PYWIKIBOT_NO_USER_CONFIG=2 is set.

Functions made available to user-config:

  • user_home_path

Sets module global base_dir and provides utility methods to build paths relative to base_dir:

  • makepath

  • datafilepath

  • shortpath

Renamed in version 6.2

pywikibot.config.datafilepath(*filename: str, create: bool = True)str[source]

Return an absolute path to a data file in a standard location.

Argument(s) are zero or more directory names, optionally followed by a data file name. The return path is offset to config.base_dir. Any directories in the path that do not already exist are created if create is True, otherwise the filesystem keeps unchanged.

Parameters
  • filename – path in the filesystem

  • create – create the directory if it is True. Otherwise don’t change the filesystem. Default is True.

pywikibot.config.get_base_dir(test_directory: Optional[str] = None)str[source]

Return the directory in which user-specific information is stored.

This is determined in the following order:
  1. If the script was called with a -dir: argument, use the directory provided in this argument.

  2. If the user has a PYWIKIBOT_DIR environment variable, use the value of it.

  3. If user-config is present in current directory, use the current directory.

  4. If user-config is present in pwb.py directory, use that directory

  5. Use (and if necessary create) a 'pywikibot' folder under 'Application Data' or 'AppData\Roaming' (Windows) or '.pywikibot' directory (Unix and similar) under the user’s home directory.

Set PYWIKIBOT_NO_USER_CONFIG=1 to disable loading user-config.py or install Pywikibot as a site-package.

Parameters

test_directory – Assume that a user config file exists in this directory. Used to test whether placing a user config file in this directory will cause it to be selected as the base directory.

pywikibot.config.makepath(path: str, create: bool = True)str[source]

Return a normalized absolute version of the path argument.

If the given path already exists in the filesystem or create is False the filesystem is not modified. Otherwise if create is True makepath creates directories along the given path using the dirname() of the path. You may append a ‘/’ to the path if you want it to be a directory path.

from holger@trillke.net 2002/03/18

Parameters
  • path – path in the filesystem

  • create – create the directory if it is True. Otherwise do not change the filesystem. Default is True.

pywikibot.config.register_families_folder(folder_path: str)None[source]

Register all family class files contained in a directory.

Parameters

folder_path – The path of a folder containing family files. The families may also be inside a zip archive structure.

Raises

NotADirectoryError – folder_path is not a directory

pywikibot.config.removesuffix(self, suffix, /)

Return a str with the given suffix string removed if present.

If the string ends with the suffix string and that suffix is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string.

pywikibot.config.shortpath(path: str)str[source]

Return a file path relative to config.base_dir.

pywikibot.config.user_home_path(path: str)str[source]

Return a file path to a file in the user home.

Option setting

Account Settings

# ############# ACCOUNT SETTINGS ##############

# The family of sites we are working on. pywikibot will import
# families/xxx_family.py so if you want to change this variable,
# you need to write such a file if one does not exist.
family = 'wikipedia'
# The language code of the site we're working on.
mylang = 'language'
# If family and mylang are not modified from the above, the default is changed
# to test:test, which is test.wikipedia.org, at the end of this module.

# The dictionary usernames should contain a username for each site where you
# have a bot account. Please set your usernames by adding such lines to your
# user-config.py:
#
# usernames['wikipedia']['de'] = 'myGermanUsername'
# usernames['wiktionary']['en'] = 'myEnglishUsername'
#
# If you have a unique username for all languages of a family,
# you can use '*'
# usernames['wikibooks']['*'] = 'mySingleUsername'
# You may use '*' for family name in a similar manner.
#
usernames = collections.defaultdict(dict)  # type: Dict[str, Dict[str, str]]
disambiguation_comment = collections.defaultdict(dict)  # type: _DabComDict

# User agent format.
# For the meaning and more help in customization see:
# https://www.mediawiki.org/wiki/Manual:Pywikibot/User-agent
user_agent_format = ('{script_product} ({script_comments}) {pwb} ({revision}) '
                     '{http_backend} {python}')

# User agent description
# This is a free-form string that can be user to describe specific bot/tool,
# provide contact information, etc.
user_agent_description = None
# Fake user agent.
# Some external websites reject bot-like user agents. It is possible to use
# fake user agents in requests to these websites.
# It is recommended to default this to False and use on an as-needed basis.
#
# Default behaviours in modules that can utilize fake UAs.
# True for enabling fake UA, False for disabling / using pywikibot's own UA,
# str to specify custom UA.
fake_user_agent_default = {'reflinks': False, 'weblinkchecker': False}
# Website domains excepted to the default behaviour.
# True for enabling, False for disabling, str to hardcode a UA.
# Example: {'problematic.site.example': True,
#           'prefers.specific.ua.example': 'snakeoil/4.2'}
fake_user_agent_exceptions = {}  # type: Dict[str, Union[bool, str]]
# This following option is deprecated in favour of finer control options above.
fake_user_agent = False

# The default interface for communicating with the site
# currently the only defined interface is 'APISite', so don't change this!
site_interface = 'APISite'
# number of days to cache namespaces, api configuration, etc.
API_config_expiry = 30

# The maximum number of bytes which uses a GET request, if not positive
# it'll always use POST requests
maximum_GET_length = 255
# Some networks modify GET requests when they are not encrypted, to avoid
# bug reports related to that disable those. If we are confident that bug
# related to this are really because of the network this could be changed.
enable_GET_without_SSL = False

# Solve captchas in the webbrowser. Setting this to False will result in the
# exception CaptchaError being thrown if a captcha is encountered.
solve_captcha = True

# Some sites will require password authentication to access the HTML pages at
# the site. If you have any such site, add lines to your user-config.py of
# the following form:
#
# authenticate['en.wikipedia.org'] = ('John','XXXXX')
# authenticate['*.wikipedia.org'] = ('John','XXXXX')
#
# where John is your login name, and XXXXX your password.
# Note:
# 1. This is only for sites that use authentication in the form that gives
#    you a popup for name and password when you try to access any data, NOT
#    for, for example, wiki usernames
# 2. You must use the hostname of the site, not its family/language pair.
#    Pywikibot supports wildcard (*) in the prefix of hostname and select the
#    best match authentication. So you can specify authentication not only for
#    one site
#
# Pywikibot also support OAuth 1.0a via mwoauth
# https://pypi.org/project/mwoauth
#
# You can add OAuth tokens to your user-config.py of the following form:
#
# authenticate['en.wikipedia.org'] = ('consumer_key','consumer_secret',
#                                     'access_key', 'access_secret')
# authenticate['*.wikipedia.org'] = ('consumer_key','consumer_secret',
#                                    'access_key', 'access_secret')
#
# Note: the target wiki site must install OAuth extension
authenticate = {}  # type: Dict[str, Tuple[str, ...]]

# By default you are asked for a password on the terminal.
# A password file may be used, e.g. password_file = '.passwd'
# The path to the password file is relative to that of the user_config file.
# The password file should consist of lines containing Python tuples of any
# of the following formats:
# (code, family, username, password)
# (family, username, password)
# (username, password)
# It's also possible (and safer) for bot users to use BotPasswords to limit
# the permissions given to a bot. When using BotPasswords, each instance gets
# keys. This combination can only access the API, not the normal web interface.
# See https://www.mediawiki.org/wiki/Manual:Pywikibot/BotPasswords to know how
# use them. In this case, the password file should contain a BotPassword object
# in the following format:
# (username, BotPassword(botname, botpassword))
password_file = None

# edit summary to use if not supplied by bot script
# WARNING: this should NEVER be used in practice, ALWAYS supply a more
#          relevant summary for bot edits
default_edit_summary = 'Pywikibot ' + pwb_version

# What permissions to use to set private files to it
# such as password file.
#
# stat.S_IRWXU 0o700 mask for owner permissions
# stat.S_IRUSR 0o400 read permission for owner
# stat.S_IWUSR 0o200 write permission for owner
# stat.S_IXUSR 0o100 execute permission for owner
# stat.S_IRWXG 0o070 mask for group permissions
# stat.S_IRGRP 0o040 read permission for group
# stat.S_IWGRP 0o020 write permission for group
# stat.S_IXGRP 0o010 execute permission for group
# stat.S_IRWXO 0o007 mask for others permissions
# stat.S_IROTH 0o004 read permission for others
# stat.S_IWOTH 0o002 write permission for others
# stat.S_IXOTH 0o001 execute permission for others
private_files_permission = stat.S_IRUSR | stat.S_IWUSR

# Allow user to stop warnings about file security
# by setting this to true.
ignore_file_security_warnings = False

# Custom headers to send on all requests.
# This is mainly intended to support setting the
# X-Wikimedia-Debug header, which is sometimes
# needed to debug issues with Wikimedia sites:
# https://wikitech.wikimedia.org/wiki/Debugging_in_production
#
# Note that these headers will be sent with all requests,
# not just MediaWiki API calls.
extra_headers = {}  # type: Mapping[str, str]

# Set to True to override the {{bots}} exclusion protocol (at your own risk!)
ignore_bot_templates = False

User Interface Settings

# ############# USER INTERFACE SETTINGS ##############

# The encoding that's used in the user's console, i.e. how strings are encoded
# when they are read by raw_input(). On Windows systems' DOS box, this should
# be 'cp850' ('cp437' for older versions). Linux users might try 'iso-8859-1'
# or 'utf-8'.
# This default code should work fine, so you don't have to think about it.
# When using pywikibot inside a daemonized twisted application, we get
# "StdioOnnaStick instance has no attribute 'encoding'"; assign None instead.
# TODO: consider getting rid of this config variable.
console_encoding = getattr(sys.stdout, 'encoding', None)

# The encoding the user would like to see text transliterated to. This can be
# set to a charset (e.g. 'ascii', 'iso-8859-1' or 'cp850'), and we will output
# only characters that exist in that charset. However, the characters will be
# output using console_encoding.
# If this is not defined on Windows, we emit a Warning explaining the user
# to either switch to a Unicode-able font and use
#    transliteration_target = None
# or to keep using raster fonts and set
#    transliteration_target = console_encoding
# After emitting the warning, this last option will be set.

transliteration_target = None

# The encoding in which textfiles are stored, which contain lists of page
# titles. The most used is 'utf-8'; 'utf-8-sig' recognizes BOM.
# For a complete list please see:
# https://docs.python.org/3/library/codecs.html#standard-encodings
textfile_encoding = 'utf-8'

# currently terminal and buffer are the only supported userinterfaces
userinterface = 'terminal'

# this can be used to pass variables to the UI init function
# useful for e.g.
# userinterface_init_kwargs = {'default_stream': 'stdout'}
userinterface_init_kwargs = {}  # type: Dict[str, str]

# i18n setting for user interface language
# default is obtained from :py:obj:`locale.getdefaultlocale`
userinterface_lang = None

# Should we transliterate characters that do not exist in the console
# character set?
# True: whenever possible
# False: never - always replace them by question marks
# Currently only works if interface 'terminal' is set.
transliterate = True

# The pwb.py wrapper calls the script given as parameter in this way
# python pwb.py <name_of_script> <options>
# If there is a misspelling in <name_of_script> the most similar script
# scripts are displayed or if only one is found, it will be started.
# There are some configuration values to change the behavior
#
# pwb_close_matches: the maximum number of simular scripts to be found
pwb_close_matches = 10  # must be greater than 0
# pwb_cut_off: similarity of scripts to be found
pwb_cut_off = 0.7  # must be a float in the range [0, 1]
# pwb_autostart_waittime: wait time until the most similar script starts
pwb_autostart_waittime = 5.0

# Should the system bell ring if the bot expects user input?
ring_bell = False

# Colorization can be used to markup important text parts of the output.
# On Linux/Unix terminals, ANSI escape codes are used for this. On Windows,
# it is done by a DLL call via ctypes.
# Set this to False if you're using Linux and your tty doesn't support
# ANSI colors.
try:
    # Don't print colorized when the output is, for example, piped to a file.
    colorized_output = sys.stdout.isatty()
except AttributeError:
    # When using pywikibot inside a daemonized twisted application,
    # we get "StdioOnnaStick instance has no attribute 'isatty'"
    colorized_output = False

# An indication of the size of your screen, or rather the size of the screen
# to be shown, for flickrripper
tkhorsize = 1600
tkvertsize = 1000

External Editor Settings

# ############# EXTERNAL EDITOR SETTINGS ##############
# The command for the editor you want to use. If set to None, a simple Tkinter
# editor will be used.
editor = os.environ.get('EDITOR', None)

# Warning: DO NOT use an editor which doesn't support Unicode to edit pages!
# You will BREAK non-ASCII symbols!
editor_encoding = 'utf-8'

# The temporary file name extension can be set in order to use syntax
# highlighting in your text editor.
editor_filename_extension = 'wiki'

Logfile Settings

# ############# LOGFILE SETTINGS ##############

# Defines for which scripts a logfile should be enabled. Logfiles will be
# saved in the 'logs' subdirectory.
#
# Example:
#     log = ['redirect', 'replace', 'weblinkchecker']
# It is also possible to enable logging for all scripts, using this line:
#     log = ['*']
# To disable all logging, use this:
#     log = []
# Per default, no logging is enabled.
# This setting can be overridden by the -log or -nolog command-line arguments.
log = []  # type: List[str]
# filename defaults to modulename-bot.log
logfilename = None  # type: Optional[str]
# maximal size of a logfile in kilobytes. If the size reached that limit the
# logfile will be renamed (if logfilecount is not 0) and the old file is filled
# again. logfilesize must be an integer value
logfilesize = 1024
# Number of rotating logfiles are created. The older files get the higher
# number. If logfilecount is 0, no logfile will be archived but the current
# logfile will be overwritten if the file size reached the logfilesize above.
# If logfilecount is -1 there are no rotating logfiles but the files where
# renamed if the logfile is full. The newest file gets the highest number until
# some logfiles where deleted.
logfilecount = 5
# set to 1 (or higher) to generate "informative" messages to terminal
verbose_output = 0
# set to True to fetch the pywiki version online
log_pywiki_repo_version = False
# if True, include a lot of debugging info in logfile
# (overrides log setting above)
debug_log = []  # type: List[str]

External Script Path Settings

# ############# EXTERNAL SCRIPT PATH SETTINGS ##############
# Set your own script path to lookup for your script files.
#
# Your private script path must be located inside the
# framework folder, subfolders must be delimited by '.'.
# every folder must contain an (empty) __init__.py file.
#
# The search order is
# 1. user_script_paths in the given order
# 2. scripts/userscripts
# 3. scripts
# 4. scripts/maintenance
#
# sample:
# user_script_paths = ['scripts.myscripts']
user_script_paths = []  # type: List[str]

External Families Settings

# ############# EXTERNAL FAMILIES SETTINGS ##############
# Set your own family path to lookup for your family files.
#
# Your private family path may be either an absolute or a relative path.
# You may have multiple paths defined in user_families_paths list.
#
# You may also define various family files stored directly in
# family_files dict. Use the family name as dict key and the path or an
# url as value.
#
# samples:
# family_files['mywiki'] = 'https://de.wikipedia.org'
# user_families_paths = ['data/families']
user_families_paths = []  # type: List[str]

Interwiki Settings

# ############# INTERWIKI SETTINGS ##############

# Should interwiki.py report warnings for missing links between foreign
# languages?
interwiki_backlink = True

# Should interwiki.py display every new link it discovers?
interwiki_shownew = True

# Should interwiki.py output a graph PNG file on conflicts?
# You need pydot for this:
# https://pypi.org/project/pydot/
interwiki_graph = False

# Specifies that the robot should process that amount of subjects at a time,
# only starting to load new pages in the original language when the total
# falls below that number. Default is to process (at least) 100 subjects at
# once.
interwiki_min_subjects = 100

# If interwiki graphs are enabled, which format(s) should be used?
# Supported formats include png, jpg, ps, and svg. See:
# http://www.graphviz.org/doc/info/output.html
# If you want to also dump the dot files, you can use this in your
# user-config.py:
# interwiki_graph_formats = ['dot', 'png']
# If you need a PNG image with an HTML image map, use this:
# interwiki_graph_formats = ['png', 'cmap']
# If you only need SVG images, use:
# interwiki_graph_formats = ['svg']
interwiki_graph_formats = ['png']

# You can post the contents of your autonomous_problems.dat to the wiki,
# e.g. to https://de.wikipedia.org/wiki/Wikipedia:Interwiki-Konflikte .
# This allows others to assist you in resolving interwiki problems.
# To help these people, you can upload the interwiki graphs to your
# webspace somewhere. Set the base URL here, e.g.:
# 'https://www.example.org/~yourname/interwiki-graphs/'
interwiki_graph_url = None

# Save file with local articles without interwikis.
without_interwiki = False

Solve Disambiguations Settings

# ############# SOLVE_DISAMBIGUATION SETTINGS ############
#
# Set disambiguation_comment[FAMILY][LANG] to a non-empty string to override
# the default edit comment for the solve_disambiguation bot.
#
# Use %s to represent the name of the disambiguation page being treated.
# Example:
#
# disambiguation_comment['wikipedia']['en'] = \
#    'Robot-assisted disambiguation ([[WP:DPL|you can help!]]): %s'

# Sorting order for alternatives. Set to True to ignore case for sorting order.
sort_ignore_case = False

Settings to Avoid Server Overload

# ############# SETTINGS TO AVOID SERVER OVERLOAD ##############

# Slow down the robot such that it never requests a second page within
# 'minthrottle' seconds. This can be lengthened if the server is slow,
# but never more than 'maxthrottle' seconds. However - if you are running
# more than one bot in parallel the times are lengthened.
#
# 'maxlag' is used to control the rate of server access (see below).
# Set minthrottle to non-zero to use a throttle on read access.
minthrottle = 0
maxthrottle = 60

# Slow down the robot such that it never makes a second page edit within
# 'put_throttle' seconds.
put_throttle = 10  # type: Union[int, float]

# Sometimes you want to know when a delay is inserted. If a delay is larger
# than 'noisysleep' seconds, it is logged on the screen.
noisysleep = 3.0

# Defer bot edits during periods of database server lag. For details, see
# https://www.mediawiki.org/wiki/Manual:Maxlag_parameter
# You can set this variable to a number of seconds, or to None (or 0) to
# disable this behavior. Higher values are more aggressive in seeking
# access to the wiki.
# Non-Wikimedia wikis may or may not support this feature; for families
# that do not use it, it is recommended to set minthrottle (above) to
# at least 1 second.
maxlag = 5

# Maximum of pages which can be retrieved at one time from wiki server.
# -1 indicates limit by api restriction
step = -1

# Maximum number of times to retry an API request before quitting.
max_retries = 15
# Minimum time to wait before resubmitting a failed API request.
retry_wait = 5
# Maximum time to wait before resubmitting a failed API request.
retry_max = 120

Database Settings

# ############# DATABASE SETTINGS ##############
# Setting to connect the database or replica of the database of the wiki.
# db_name_format can be used to manipulate the dbName of site.
#
# Example for a pywikibot running on Wikimedia Cloud (Toolforge):
# db_hostname_format = '{0}.analytics.db.svc.wikimedia.cloud'
# db_name_format = '{0}_p'
# db_connect_file = user_home_path('replica.my.cnf')
db_hostname_format = 'localhost'
db_username = ''
db_password = ''
db_name_format = '{0}'
db_connect_file = user_home_path('.my.cnf')
# local port for mysql server
# ssh -L 4711:enwiki.analytics.db.svc.eqiad.wmflabs:3306 \
#     user@login.toolforge.org
db_port = 3306

Http Settings

# ############# HTTP SETTINGS ##############
# Default socket timeout in seconds.
# DO NOT set to None to disable timeouts. Otherwise this may freeze your
# script.
# You may assign either a tuple of two int or float values for connection and
# read timeout, or a single value for both in a tuple.
socket_timeout = (6.05, 45)


Cosmetic Changes Settings

# ############# COSMETIC CHANGES SETTINGS ##############
# The bot can make some additional changes to each page it edits, e.g. fix
# whitespace or positioning of category links.

# This is an experimental feature; handle with care and consider re-checking
# each bot edit if enabling this!
cosmetic_changes = False

# If cosmetic changes are switched on, and you also have several accounts at
# projects where you're not familiar with the local conventions, you probably
# only want the bot to do cosmetic changes on your "home" wiki which you
# specified in config.mylang and config.family.
# If you want the bot to also do cosmetic changes when editing a page on a
# foreign wiki, set cosmetic_changes_mylang_only to False, but be careful!
cosmetic_changes_mylang_only = True

# The dictionary cosmetic_changes_enable should contain a tuple of languages
# for each site where you wish to enable in addition to your own langlanguage
# (if cosmetic_changes_mylang_only is set)
# Please set your dictionary by adding such lines to your user-config.py:
# cosmetic_changes_enable['wikipedia'] = ('de', 'en', 'fr')
cosmetic_changes_enable = {}  # type: Dict[str, Tuple[str, ...]]

# The dictionary cosmetic_changes_disable should contain a tuple of languages
# for each site where you wish to disable cosmetic changes. You may use it with
# cosmetic_changes_mylang_only is False, but you can also disable your own
# language. This also overrides the settings in the cosmetic_changes_enable
# dictionary. Please set your dict by adding such lines to your user-config.py:
# cosmetic_changes_disable['wikipedia'] = ('de', 'en', 'fr')
cosmetic_changes_disable = {}  # type: Dict[str, Tuple[str, ...]]

# cosmetic_changes_deny_script is a list of scripts for which cosmetic changes
# are disabled. You may add additional scripts by appending script names in
# your user-config.py ("+=" operator is strictly recommended):
# cosmetic_changes_deny_script += ['your_script_name_1', 'your_script_name_2']
# Appending the script name also works:
# cosmetic_changes_deny_script.append('your_script_name')
cosmetic_changes_deny_script = ['category_redirect', 'cosmetic_changes',
                                'newitem', 'touch']

Replication Bot Settings

# ############# REPLICATION BOT SETTINGS ################
# You can add replicate_replace to your user-config.py.
#
# Use has the following format:
#
# replicate_replace = {
#            'wikipedia:li': {'Hoofdpagina': 'Veurblaad'}
# }
#
# to replace all occurrences of 'Hoofdpagina' with 'Veurblaad' when writing to
# liwiki. Note that this does not take the origin wiki into account.
replicate_replace = {}  # type: Dict[str, Dict[str, str]]

Further Settings

# ############# FURTHER SETTINGS ##############

# Simulate settings

# Defines what additional actions the bots are NOT allowed to do (e.g. 'edit')
# on the wiki server. Allows simulation runs of bots to be carried out without
# changing any page on the server side. Use this setting to add more actions
# in user-config.py for wikis with extra write actions.
actions_to_block = []  # type: List[str]

# Set simulate to True or use -simulate option to block all actions given
# above.
simulate = False  # type: Union[bool, str]

# How many pages should be put to a queue in asynchronous mode.
# If maxsize is <= 0, the queue size is infinite.
# Increasing this value will increase memory space but could speed up
# processing. As higher this value this effect will decrease.
max_queue_size = 64

# Pickle protocol version to use for storing dumps.
# This config variable is not used for loading dumps.
# Version 0 is a more or less human-readable protocol
# Version 2 is common to both Python 2 and 3, and should
# be used when dumps are accessed by both versions.
# Version 3 is only available for Python 3
# Version 4 is only available for Python 3.4+
# Version 5 was added with Python 3.8
pickle_protocol = 2

pywikibot.cosmetic_changes module

This module can do slight modifications to tidy a wiki page’s source code.

The changes are not supposed to change the look of the rendered wiki page.

If you wish to run this as an stand-alone script, use:

scripts/cosmetic_changes.py

For regular use, it is recommended to put this line into your user-config.py:

cosmetic_changes = True

You may enable cosmetic changes for additional languages by adding the dictionary cosmetic_changes_enable to your user-config.py. It should contain a tuple of languages for each site where you wish to enable in addition to your own langlanguage if cosmetic_changes_mylang_only is True (see below). Please set your dictionary by adding such lines to your user-config.py:

cosmetic_changes_enable['wikipedia'] = ('de', 'en', 'fr')

There is another config variable: You can set:

cosmetic_changes_mylang_only = False

if you’re running a bot on multiple sites and want to do cosmetic changes on all of them, but be careful if you do.

You may disable cosmetic changes by adding the all unwanted languages to the dictionary cosmetic_changes_disable in your user-config.py. It should contain a tuple of languages for each site where you wish to disable cosmetic changes. You may use it with cosmetic_changes_mylang_only is False, but you can also disable your own language. This also overrides the settings in the dictionary cosmetic_changes_enable. Please set this dictionary by adding such lines to your user-config.py:

cosmetic_changes_disable['wikipedia'] = ('de', 'en', 'fr')

You may disable cosmetic changes for a given script by appending the all unwanted scripts to the list cosmetic_changes_deny_script in your user-config.py. By default it contains cosmetic_changes.py itself and touch.py. This overrides all other enabling settings for cosmetic changes. Please modify the given list by adding such lines to your user-config.py:

cosmetic_changes_deny_script.append('your_script_name_1')

or by adding a list to the given one:

cosmetic_changes_deny_script += ['your_script_name_1',
                                 'your_script_name_2']
class pywikibot.cosmetic_changes.CANCEL(value)[source]

Bases: enum.IntEnum

Cancel level to ignore exceptions.

If an error occurred and either skips the page or the method or a single match. ALL raises the exception.

New in version 6.3.

ALL = 0
MATCH = 3
METHOD = 2
PAGE = 1
class pywikibot.cosmetic_changes.CosmeticChangesToolkit(page: pywikibot.page.BasePage, redirect=NotImplemented, diff='[deprecated name of show_diff]', site='[deprecated name of page]', *, show_diff: bool = False, namespace: Optional[int] = None, pageTitle: Optional[str] = None, ignore: enum.IntEnum = <CANCEL.ALL: 0>)[source]

Bases: object

Cosmetic changes toolkit.

Parameters
  • page – the Page object containing the text to be modified

  • show_diff – show difference after replacements

  • namespace – DEPRECATED namespace parameter

  • pageTitle – DEPRECATED page title parameter

  • ignore – ignores if an error occurred and either skips the page or only that method. It can be set one of the CANCEL constants

change(text: str)Union[bool, str][source]

Execute all clean up methods and catch errors if activated.

Tidy up wikilinks found in a string.

This function will: * Replace underscores with spaces

  • Move leading and trailing spaces out of the wikilink and into the surrounding text

  • Convert URL-encoded characters into Unicode-encoded characters

  • Move trailing characters out of the link and make the link without using a pipe, if possible

  • Capitalize the article title of the link, if appropriate

Parameters

text – string to perform the clean-up on

Returns

text with tidied wikilinks

cleanUpSectionHeaders(text: str)str[source]

Add a space between the equal signs and the section title.

Example:

==Section title==

becomes:

== Section title ==
NOTE

This space is recommended in the syntax help on the English and German Wikipedias. It is not wanted on Lojban and English Wiktionaries (T168399, T169064) and it might be that it is not wanted on other wikis. If there are any complaints, please file a bug report.

commonsfiledesc(text: str)str[source]

Clean up file descriptions on Wikimedia Commons.

It works according to [1] and works only on pages in the file namespace on Wikimedia Commons.

[1]: https://commons.wikimedia.org/wiki/Commons:Tools/pywiki_file_description_cleanup

fixArabicLetters(text: str)str[source]

Fix Arabic and Persian letters.

fixHtml(text: str)str[source]

Relace html markups with wikitext markups.

fixReferences(text: str)str[source]

Fix references tags.

fixSelfInterwiki(text: str)str[source]

Interwiki links to the site itself are displayed like local links.

Remove their language code prefix.

fixStyle(text: str)str[source]

Convert prettytable to wikitable class.

fixSyntaxSave(text: str)str[source]

Convert weblinks to wikilink, fix link syntax.

fixTypo(text: str)str[source]

Fix units.

fix_ISBN(text: str)str[source]

Hyphenate ISBN numbers.

putSpacesInLists(text: str)str[source]

Add a space between the * or # and the text.

NOTE

This space is recommended in the syntax help on the English, German and French Wikipedias. It might be that it is not wanted on other wikis. If there are any complaints, please file a bug report.

removeEmptySections(text: str)str[source]

Cleanup empty sections.

removeNonBreakingSpaceBeforePercent(text: str)str[source]

Remove a non-breaking space between number and percent sign.

Newer MediaWiki versions automatically place a non-breaking space in front of a percent sign, so it is no longer required to place it manually.

removeUselessSpaces(text: str)str[source]

Cleanup multiple or trailing spaces.

replaceDeprecatedTemplates(text: str)str[source]

Replace deprecated templates.

resolveHtmlEntities(text: str)str[source]

Replace HTML entities with string.

safe_execute(method: collections.abc.Callable[str, str], text: str)str[source]

Execute the method and catch exceptions if enabled.

standardizePageFooter(text: str)str[source]

Standardize page footer.

Makes sure that interwiki links and categories are put into the correct position and into the right order. This combines the old instances of standardizeInterwiki and standardizeCategories.

The page footer consists of the following parts in that sequence: 1. categories 2. additional information depending on the local site policy 3. interwiki

translateAndCapitalizeNamespaces(text: str)str[source]

Use localized namespace names.

translateMagicWords(text: str)str[source]

Use localized magic words.

pywikibot.daemonize module

Module to daemonize the current process on Unix.

pywikibot.daemonize.daemonize(close_fd: bool = True, chdir: bool = True, redirect_std: Optional[str] = None, write_pid=NotImplemented)[source]

Daemonize the current process.

Only works on POSIX compatible operating systems. The process will fork to the background and return control to terminal.

Parameters
  • close_fd – Close the standard streams and replace them by /dev/null

  • chdir – Change the current working directory to /

  • redirect_std – Filename to redirect stdout and stdin to

pywikibot.date module

Date data and manipulation module.

class pywikibot.date.MonthFormat(index: int, format_key: str)[source]

Bases: collections.abc.MutableMapping

A Mapping which creates months formats.

Initializer of MonthFormat mapping.

Parameters
  • index – month number

  • format_key – formats key like Day_January or Year_December

day_formats = {'af': ('%d {}', True), 'ang': ('%d {}', True), 'bg': ('%d {}', False), 'bn': ('{} %%B', None), 'ceb': ('{} %d', True), 'csb': ('%d {}a', False), 'cv': ('{}, %d', True), 'cy': ('%d {}', True), 'de': ('%d. {}', True), 'en': ('{} %d', True), 'eo': ('%d-a de {}', False), 'es': ('%d de {}', False), 'eu': ('{}aren %d', True), 'fi': ('%d. {}ta', False), 'fur': ('%d di {}', True), 'fy': ('%d {}', False), 'gl': ('%d de {}', False), 'gsw': ('%d. {}', True), 'he': ('%d ב{}', None), 'hu': ('{} %d.', True), 'ia': ('%d de {}', False), 'id': ('%d {}', True), 'ie': ('%d {}', False), 'io': ('%d di {}', False), 'it': ('%d {}', False), 'jv': ('%d {}', True), 'ka': ('%d {}', None), 'lb': ('%d. {}', True), 'mhr': ('%d {}', False), 'ml': ('{} %d', None), 'ms': ('%d {}', True), 'nap': ("%d 'e {}", False), 'nds': ('%d. {}', True), 'pt': ('%d de {}', True), 'ro': ('%d {}', False), 'scn': ('%d di {}', False), 'sco': ('%d {}', True), 'su': ('%d {}', True), 'sv': ('%d {}', False), 'ta': ('{} %d', None), 'te': ('{} %d', None), 'th': ('%d {}', None), 'tl': ('{} %d', None), 'tr': ('%d {}', True), 'tt': ('%d. {}', True), 'vec': ('%d de {}', False), 'vo': ('{} %d', False)}
year_formats = {'ar': ('{} %d', None), 'cs': ('{} %d', None), 'eo': ('{} de %d', None), 'es': ('{} de %d', True), 'it': ('Attualità/Anno %d - {}', True), 'ka': ('{}, %d', None), 'sk': ('{} %d', None), 'th': ('{} พ.ศ. %%T', None), 'tl': ('{} %d', None)}
class pywikibot.date.MonthNames[source]

Bases: collections.abc.Mapping

A Mapping which reads month names from MediaWiki messages.

months = {'br': <function MonthNames.<lambda>>, 'en': <function MonthNames.<lambda>>, 'ja': <function MonthNames.<lambda>>, 'ko': <function MonthNames.<lambda>>, 'wa': <function MonthNames.<lambda>>, 'zh': <function MonthNames.<lambda>>}
pywikibot.date.addFmt1(lang: str, isMnthOfYear: bool, patterns: collections.abc.Sequence[Optional[str]])None[source]

Add 12 month formats for a specific type (‘January’, ‘Feb.’).

The function must accept one parameter for the ->int or ->string conversions, just like everywhere else in the formats map. The patterns parameter is a list of 12 elements to be used for each month.

Parameters

lang – language code

pywikibot.date.alwaysTrue(x: Any)bool[source]

Return True, always.

Used for multiple value selection function to accept all other values.

Parameters

x – not used

Returns

True

pywikibot.date.apply_month_delta(date: datetime.date, month_delta: int = 1, add_overlap: bool = False)datetime.date[source]

Add or subtract months from the date.

By default if the new month has less days then the day of the date it chooses the last day in the new month. For example a date in the March 31st added by one month will result in April 30th.

When the overlap is enabled, and there is overlap, then the new_date will be one month off and get_month_delta will report a number one higher.

It does only work on calendars with 12 months per year, and where the months are numbered consecutively beginning by 1.

Parameters
  • date – The starting date

  • month_delta – The amount of months added or subtracted.

  • add_overlap – Add any missing days to the date, increasing the month once more.

Returns

The end date

pywikibot.date.decSinglVal(v: collections.abc.Sequence[Any])Any[source]

Return first item in list v.

pywikibot.date.dh(value: int, pattern: str, encf: collections.abc.Callable[int, Union[int, collections.abc.Sequence[int]]], decf: collections.abc.Callable[collections.abc.Sequence[int], int], filter: Optional[collections.abc.Callable[int, bool]] = None)str[source]
pywikibot.date.dh(value: str, pattern: str, encf: collections.abc.Callable[int, Union[int, collections.abc.Sequence[int]]], decf: collections.abc.Callable[collections.abc.Sequence[int], int], filter: Optional[collections.abc.Callable[int, bool]] = None)int

Function to help with year parsing.

Usually it will be used as a lambda call in a map:

lambda v: dh(v, 'pattern string', encf, decf)
Parameters
  • encf

    Converts from an integer parameter to another integer or a tuple of integers. Depending on the pattern, each integer will be converted to a proper string representation, and will be passed as a format argument to the pattern:

    pattern % encf(value)
    

    This function is a complement of decf.

  • decf – Converts a tuple/list of non-negative integers found in the original value string into a normalized value. The normalized value can be passed right back into dh() to produce the original string. This function is a complement of encf. dh() interprets %d as a decimal and %s as a roman numeral number.

pywikibot.date.dh_centuryAD(value: int, pattern: str)str[source]

Helper for decoding an AD century.

pywikibot.date.dh_centuryBC(value: int, pattern: str)str[source]

Helper for decoding an BC century.

pywikibot.date.dh_constVal(value: int, ind: int, match: str)str[source]
pywikibot.date.dh_constVal(value: str, ind: int, match: str)int

Helper function to match a single value to a constant.

formats[‘CurrEvents’][‘en’](ind) => ‘Current Events’ formats[‘CurrEvents’][‘en’](‘Current Events’) => ind

pywikibot.date.dh_dayOfMnth(value: int, pattern: str)str[source]

Helper for decoding a single integer value.

The single integer should be <=31, no conversion, no rounding (used in days of month).

pywikibot.date.dh_decAD(value: int, pattern: str)str[source]

Helper for decoding a single integer value.

It should be no conversion, round to decimals (used in decades)

pywikibot.date.dh_decBC(value: int, pattern: str)str[source]

Helper for decoding a single integer value.

It should be no conversion, round to decimals (used in decades)

pywikibot.date.dh_millenniumAD(value: int, pattern: str)str[source]

Helper for decoding an AD millennium.

pywikibot.date.dh_millenniumBC(value: int, pattern: str)str[source]

Helper for decoding an BC millennium.

pywikibot.date.dh_mnthOfYear(value: int, pattern: str)str[source]

Helper for decoding a single integer value.

The value should be >=1000, no conversion, no rounding (used in month of the year)

pywikibot.date.dh_noConv(value: int, pattern: str, limit: collections.abc.Callable[int, bool])str[source]

Helper for decoding an integer value, no conversion, no rounding.

pywikibot.date.dh_number(value: int, pattern: str)str[source]

Helper for decoding a number.

pywikibot.date.dh_simpleYearAD(value: int)str[source]

Helper for decoding a single integer value.

This value should be representing a year with no extra symbols.

pywikibot.date.dh_singVal(value: int, match: str)str[source]
pywikibot.date.dh_singVal(value: str, match: str)int

Helper function to match a single value to a constant.

pywikibot.date.dh_yearAD(value: int, pattern: str)str[source]

Helper for decoding a year value.

The value should have no conversion, no rounding, limits to 3000.

pywikibot.date.dh_yearBC(value: int, pattern: str)str[source]

Helper for decoding a year value.

The value should have no conversion, no rounding, limits to 3000.

pywikibot.date.encDec0(i: int)int[source]

Round to the nearest decade, decade starts with a ‘0’-ending year.

pywikibot.date.encDec1(i: int)int[source]

Round to the nearest decade, decade starts with a ‘1’-ending year.

pywikibot.date.escapePattern2(pattern: str)tuple[source]

Convert a string pattern into a regex expression and cache.

Allows matching of any _digitDecoders inside the string. Returns a compiled regex object and a list of digit decoders.

pywikibot.date.formatYear(lang: str, year: int)str[source]

Return year name in a language.

pywikibot.date.format_date(month: int, day: int, lang: Union[None, str, pywikibot.site._basesite.BaseSite] = None, year: int = 2000)str[source]

Format a date localized to given lang.

Parameters
  • month – month in range of 1..12

  • day – day of month in range of 1..31

  • lang – a site object or language key. Defaults to current site.

  • year – year for which the date is to be formatted. always 29 will be given For February except the year is given. Default is leap year 2000.

Returns

localized date like “January 11”

Raises
  • ValueError – Wrong day value; must be 1-28/29/30/31

  • IllegalMonthError – bad month number; must be 1-12

pywikibot.date.getAutoFormat(lang: str, title: str, ignoreFirstLetterCase: bool = True)tuple[source]

Return first matching formatted date value.

Parameters
  • lang – language code

  • title – value to format

Returns

dictName (‘YearBC’, ‘December’, …) and value (a year, date, …)

pywikibot.date.get_month_delta(date1: datetime.date, date2: datetime.date)int[source]

Return the difference between two dates in months.

It does only work on calendars with 12 months per year, and where the months are consecutive and non-negative numbers.

pywikibot.date.intToLocalDigitsStr(value: int, digitsToLocalDict: collections.abc.Mapping[int, str])str[source]

Encode an integer value into a textual form.

pywikibot.date.intToRomanNum(i: int)str[source]

Convert integer to roman numeral.

pywikibot.date.localDigitsStrToInt(value: str, digitsToLocalDict: collections.abc.Mapping[int, str], localToDigitsDict: collections.abc.Mapping[int, str])int[source]

Convert digits to integer.

pywikibot.date.makeMonthList(pattern: str)list[source]

Return a list of 12 elements based on the number of the month.

pywikibot.date.makeMonthNamedList(lang: str, pattern: str, makeUpperCase: Optional[bool] = None)list[source]

Create a list of 12 elements based on the name of the month.

The language-dependent month name is used as a formatting argument to the pattern. The pattern must be have one %s that will be replaced by the localized month name. Use %%d for any other parameters that should be preserved.

pywikibot.date.monthName(lang: str, ind: int)str[source]

Return the month name for a language.

pywikibot.date.multi(value: int, tuplst: list)Any[source]
pywikibot.date.multi(value: str, tuplst: list)Any

Run multiple pattern checks for the same entry.

For example: 1st century, 2nd century, etc.

The tuplst is a list of tuples. Each tuple must contain two functions: first to encode/decode a single value (e.g. simpleInt), second is a predicate function with an integer parameter that returns true or false. When the 2nd function evaluates to true, the 1st function is used.

pywikibot.date.romanNumToInt(v: str)int[source]

Convert roman numeral to integer.

pywikibot.date.slh(value: int, lst: collections.abc.Sequence[str])str[source]
pywikibot.date.slh(value: str, lst: collections.abc.Sequence[str])int

Helper function for simple list value matching.

!!!!! The index starts at 1, so 1st element has index 1, not 0 !!!!!

Usually it will be used as a lambda call in a map:

lambda v: slh(v, ['January','February',...])

Usage scenarios:

formats['MonthName']['en'](1) => 'January'
formats['MonthName']['en']('January') => 1
formats['MonthName']['en']('anything else') => raise ValueError

pywikibot.diff module

Diff module.

class pywikibot.diff.Hunk(a: Union[str, collections.abc.Sequence[str]], b: Union[str, collections.abc.Sequence[str]], grouped_opcode: collections.abc.Sequence[tuple])[source]

Bases: object

One change hunk between a and b.

Note: parts of this code are taken from by difflib.get_grouped_opcodes().

Parameters
  • a – sequence of lines

  • b – sequence of lines

  • grouped_opcode – list of 5-tuples describing how to turn a into b. It has the same format as returned by difflib.get_opcodes().

APPR = 1
NOT_APPR = -1
PENDING = 0
apply()collections.abc.Sequence[str][source]

Turn a into b for this hunk.

color_line(line: str, line_ref: Optional[str] = None)str[source]

Color line characters.

If line_ref is None, the whole line is colored. If line_ref[i] is not blank, line[i] is colored. Color depends if line starts with +/-.

line_ref: string.

create_diff()collections.abc.Iterable[str][source]

Generator of diff text for this hunk, without formatting.

format_diff()collections.abc.Iterable[str][source]

Color diff lines.

get_header()str[source]

Provide header of unified diff.

static get_header_text(a_rng: tuple, b_rng: tuple, affix: str = '@@')str[source]

Provide header for any ranges.

class pywikibot.diff.PatchManager(text_a: str, text_b: str, context: int = 0, by_letter: bool = False, replace_invisible: bool = False, n='[deprecated name of context]')[source]

Bases: object

Apply patches to text_a to obtain a new text.

If all hunks are approved, text_b will be obtained.

Parameters
  • text_a – base text

  • text_b – target text

  • context – number of lines which are context

  • by_letter – if text_a and text_b are single lines, comparison can be done letter by letter.

  • replace_invisible – Replace invisible characters like U+200e with the charnumber in brackets (e.g. <200e>).

apply()list[source]

Apply changes. If there are undecided changes, ask to review.

get_blocks()list[source]

Return list with blocks of indexes.

Format of each block:

[-1, (i1, i2), (-1, -1)] -> block a[i1:i2] does not change from
    a to b then is there is no corresponding hunk.
[hunk index, (i1, i2), (j1, j2)] -> block a[i1:i2] becomes b[j1:j2]
print_hunks()None[source]

Print the headers and diff texts of all hunks to the output.

review_hunks()None[source]

Review hunks.

pywikibot.diff.cherry_pick(oldtext: str, newtext: str, n: int = 0, by_letter: bool = False)str[source]

Propose a list of changes for approval.

Text with approved changes will be returned. n: int, line of context as defined in difflib.get_grouped_opcodes(). by_letter: if text_a and text_b are single lines, comparison can be done

pywikibot.diff.html_comparator(compare_string: str)dict[source]

List of added and deleted contexts from ‘action=compare’ html string.

This function is useful when combineds with site.py’s “compare” method. Site.compare() returns HTML that is useful for displaying on a page. Here we use BeautifulSoup to get the un-HTML-ify the context of changes. Finally we present the added and deleted contexts. :param compare_string: HTML string from MediaWiki API :return: deleted and added list of contexts

pywikibot.echo module

Classes and functions for working with the Echo extension.

class pywikibot.echo.Notification(site: pywikibot.site._basesite.BaseSite)[source]

Bases: object

A notification issued by the Echo extension.

Initialize an empty Notification object.

classmethod fromJSON(site: pywikibot.site._basesite.BaseSite, data: dict)pywikibot.echo.Notification[source]

Construct a Notification object from our API’s JSON data.

mark_as_read()bool[source]

Mark the notification as read.

pywikibot.editor module

Text editor class for your favourite editor.

class pywikibot.editor.TextEditor[source]

Bases: object

Text editor.

edit(text: str, jumpIndex: Optional[int] = None, highlight: Optional[str] = None)Optional[str][source]

Call the editor and thus allows the user to change the text.

Halts the thread’s operation until the editor is closed.

Parameters
  • text – the text to be edited

  • jumpIndex – position at which to put the caret

  • highlight – each occurrence of this substring will be highlighted

Returns

the modified text, or None if the user didn’t save the text file in his text editor

pywikibot.exceptions module

Pywikibot Exceptions and warning classes.

This module contains all exception and warning classes used throughout the framework:

Exception
 +-- Error
      +-- APIError
      |    +-- APIMWError
      |    +-- UploadError
      +-- AutoblockUserError
      +-- CaptchaError
      +-- InvalidTitleError
      +-- NoUsernameError
      +-- PageInUseError
      +-- PageRelatedError
      |    +-- CircularRedirectError
      |    +-- InterwikiRedirectPageError
      |    +-- IsNotRedirectPageError
      |    +-- IsRedirectPageError
      |    +-- NoMoveTargetError
      |    +-- NoPageError
      |    +-- NotEmailableError
      |    +-- PageLoadRelatedError
      |    |    +-- InconsistentTitleError
      |    |    +-- InvalidPageError
      |    +-- PageSaveRelatedError
      |    |    +-- EditConflictError
      |    |    |    +-- ArticleExistsConflictError
      |    |    |    +-- PageCreatedConflictError
      |    |    |    +-- PageDeletedConflictError
      |    |    +-- LockedPageError
      |    |    |    +-- LockedNoPageError
      |    |    |    +-- CascadeLockedPageError
      |    |    +-- NoCreateError
      |    |    +-- OtherPageSaveError
      |    |    +-- SpamblacklistError
      |    |    +-- TitleblacklistError
      |    |    +-- AbuseFilterDisallowedError
      |    +-- UnsupportedPageError
      +-- SectionError
      +-- ServerError
      |    +-- FatalServerError
      |    +-- Server414Error
      |    +-- Server504Error
      +-- SiteDefinitionError
      |    +-- UnknownFamilyError
      |    +-- UnknownSiteError
      +-- TimeoutError
      |    +-- MaxlagTimeoutError
      +-- TranslationError
      +-- UserRightsError
      |    +-- HiddenKeyError (KeyError)
      +-- UnknownExtensionError (NotImplementedError)
      +-- VersionParseError
      +-- WikiBaseError
           +-- CoordinateGlobeUnknownError (NotImplementedError)
           +-- EntityTypeUnknownError
           +-- NoWikibaseEntityError

UserWarning
 +-- ArgumentDeprecationWarning (FutureWarning)
 +-- FamilyMaintenanceWarning

RuntimeWarning
 +-- NotImplementedWarning

Error: Base class, all exceptions should the subclass of this class.

  • NoUsernameError: Username is not in user-config.py, or it is invalid.

  • AutoblockUserError: requested action on a virtual autoblock user not valid

  • TranslationError: no language translation found

  • UserRightsError: insufficient rights for requested action

  • InvalidTitleError: Invalid page title

  • CaptchaError: Captcha is asked and config.solve_captcha == False

  • i18n.TranslationError: i18n/l10n message not available

  • PageInUseError: Page cannot be reserved due to a lock

  • UnknownExtensionError: Extension is not defined for this site

  • VersionParseError: failed to parse version information

  • SectionError: The section specified by # does not exist

APIError: wiki API returned an error

  • APIMWError: MediaWiki internal exception

  • UploadError: upload failed

SiteDefinitionError: Site loading problem

  • UnknownSiteError: Site does not exist in Family

  • UnknownFamilyError: Family is not registered

PageRelatedError: any exception which is caused by an operation on a Page.

  • NoPageError: Page does not exist

  • UnsupportedPageError: Page is not supported due to a namespace restriction

  • IsRedirectPageError: Page is a redirect page

  • IsNotRedirectPageError: Page is not a redirect page

  • CircularRedirectError: Page is a circular redirect

  • InterwikiRedirectPageError: Page is a redirect to another site

  • InvalidPageError: Page is invalid e.g. without history

  • NotEmailableError: The target user has disabled email

  • NoMoveTargetError: An expected move target page does not exist

PageLoadRelatedError: any exception which happens while loading a Page.
  • InconsistentTitleError: Page receives a title inconsistent with query

PageSaveRelatedError: page exceptions within the save operation on a Page

  • AbuseFilterDisallowedError: AbuseFilter disallowed

  • SpamblacklistError: MediaWiki spam filter detected a blacklisted URL

  • TitleblacklistError: MediaWiki detected a blacklisted page title

  • OtherPageSaveError: misc. other save related exception.

  • LockedPageError: Page is locked
    • LockedNoPageError: Title is locked against creation

    • CascadeLockedPageError: Page is locked due to cascading protection

  • EditConflictError: Edit conflict while uploading the page
    • PageDeletedConflictError: Page was deleted since being retrieved

    • PageCreatedConflictError: Page was created by another user

    • ArticleExistsConflictError: Page article already exists

  • NoCreateError: parameter nocreate not allow page creation

ServerError: a problem with the server.

  • FatalServerError: A fatal/non-recoverable server error

  • Server414Error: Server timed out with HTTP 414 code

  • Server504Error: Server timed out with HTTP 504 code

WikiBaseError: any issue specific to Wikibase.

  • NoWikibaseEntityError: entity doesn’t exist

  • CoordinateGlobeUnknownError: globe is not implemented yet.

  • EntityTypeUnknownError: entity type is not available on the site.

TimeoutError: request failed with a timeout

  • MaxlagTimeoutError: request failed with a maxlag timeout

DeprecationWarning: old functionality replaced by new functionality

PendingDeprecationWarning: problematic code which has not yet been fully deprecated, possibly because a replacement is not available

RuntimeWarning: problems developers should have fixed, and users need to be aware of its status.

  • NotImplementedWarning: functionality not implemented

UserWarning: warnings targeted at users

  • config._ConfigurationDeprecationWarning: user configuration file problems

  • login._PasswordFileWarning: password file problems

  • ArgumentDeprecationWarning: command line argument problems

  • FamilyMaintenanceWarning: missing information in family definition

Changed in version 6.0: exceptions were renamed and are ending with “Error”.

exception pywikibot.exceptions.APIError(code: str, info: str, **kwargs: Any)[source]

Bases: pywikibot.exceptions.Error

The wiki site returned an error message.

Save error dict returned by MW API.

exception pywikibot.exceptions.APIMWError(mediawiki_exception_class_name: str, info: str, **kwargs: Any)[source]

Bases: pywikibot.exceptions.APIError

The API site returned an error about a MediaWiki internal exception.

Save error dict returned by MW API.

exception pywikibot.exceptions.AbuseFilterDisallowedError(page: pywikibot.page.Page, info: str, other: str)[source]

Bases: pywikibot.exceptions.PageSaveRelatedError

Page save failed because the AbuseFilter disallowed it.

message = 'Edit to page %(title)s disallowed by the AbuseFilter.\n%(info)s'
exception pywikibot.exceptions.ArgumentDeprecationWarning[source]

Bases: UserWarning, FutureWarning

Command line argument that is no longer supported.

exception pywikibot.exceptions.ArticleExistsConflictError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.EditConflictError

Page already exists.

Parameters

page – Page that caused the exception

message = 'Destination article {} already exists and is not a redirect to the source article'
exception pywikibot.exceptions.AutoblockUserError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Requested action on a virtual autoblock user not valid.

The class AutoblockUserError is an exception that is raised whenever an action is requested on a virtual autoblock user that’s not available for him (i.e. roughly everything except unblock).

exception pywikibot.exceptions.CaptchaError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Captcha is asked and config.solve_captcha == False.

exception pywikibot.exceptions.CascadeLockedPageError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.LockedPageError

Page is locked due to cascading protection.

Parameters

page – Page that caused the exception

message = 'Page {} is locked due to cascading protection.'
exception pywikibot.exceptions.CircularRedirectError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageRelatedError

Page is a circular redirect.

Exception argument is the redirect target; this may be the same title as this page or a different title (in which case the target page directly or indirectly redirects back to this one)

Parameters

page – Page that caused the exception

message = 'Page {} is a circular redirect.'
exception pywikibot.exceptions.CoordinateGlobeUnknownError(arg: str)[source]

Bases: pywikibot.exceptions.WikiBaseError, NotImplementedError

This globe is not implemented yet in either WikiBase or pywikibot.

exception pywikibot.exceptions.EditConflictError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageSaveRelatedError

There has been an edit conflict while uploading the page.

Parameters

page – Page that caused the exception

message = 'Page {} could not be saved due to an edit conflict'
exception pywikibot.exceptions.EntityTypeUnknownError(arg: str)[source]

Bases: pywikibot.exceptions.WikiBaseError

The requested entity type is not recognised on this site.

exception pywikibot.exceptions.Error(arg: str)[source]

Bases: Exception

Pywikibot error.

exception pywikibot.exceptions.FamilyMaintenanceWarning[source]

Bases: UserWarning

Family class is missing definitions.

exception pywikibot.exceptions.FatalServerError(arg: str)[source]

Bases: pywikibot.exceptions.ServerError

A fatal server error will not be corrected by resending the request.

exception pywikibot.exceptions.HiddenKeyError(arg: str)[source]

Bases: pywikibot.exceptions.UserRightsError, KeyError

Insufficient user rights to view the hidden key.

exception pywikibot.exceptions.InconsistentTitleError(page: pywikibot.page.Page, actual: str)[source]

Bases: pywikibot.exceptions.PageLoadRelatedError

Page receives a title inconsistent with query.

Parameters
  • page – Page that caused the exception

  • actual – title obtained by query

exception pywikibot.exceptions.InterwikiRedirectPageError(page: pywikibot.page.Page, target_page: pywikibot.page.Page)[source]

Bases: pywikibot.exceptions.PageRelatedError

Page is a redirect to another site.

This is considered invalid in Pywikibot. See bug T75184.

Parameters

target_page – Target page of the redirect.

message = 'Page redirects to a page on another Site.\nPage: {page}\nTarget page: {target_page} on {target_site}.'
exception pywikibot.exceptions.InvalidPageError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageLoadRelatedError

Missing page history.

New in version 6.2.

Parameters

page – Page that caused the exception

message = 'Page %s is invalid.'
exception pywikibot.exceptions.InvalidTitleError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Invalid page title.

exception pywikibot.exceptions.IsNotRedirectPageError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageRelatedError

Page is not a redirect page.

Parameters

page – Page that caused the exception

message = 'Page {} is not a redirect page.'
exception pywikibot.exceptions.IsRedirectPageError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageRelatedError

Page is a redirect page.

Parameters

page – Page that caused the exception

message = 'Page {} is a redirect page.'
exception pywikibot.exceptions.LockedNoPageError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.LockedPageError

Title is locked against creation.

Parameters

page – Page that caused the exception

message = 'Page {} does not exist and is locked preventing creation.'
exception pywikibot.exceptions.LockedPageError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageSaveRelatedError

Page is locked.

Parameters

page – Page that caused the exception

message = 'Page {} is locked.'
exception pywikibot.exceptions.MaxlagTimeoutError(arg: str)[source]

Bases: pywikibot.exceptions.TimeoutError

Request failed with a maxlag timeout error.

exception pywikibot.exceptions.NoCreateError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageSaveRelatedError

Parameter nocreate doesn’t allow page creation.

Parameters

page – Page that caused the exception

message = 'Page {} could not be created due to parameter nocreate'
exception pywikibot.exceptions.NoMoveTargetError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageRelatedError

Expected move target page not found.

Parameters

page – Page that caused the exception

message = 'Move target page of {} not found.'
exception pywikibot.exceptions.NoPageError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageRelatedError

Page does not exist.

Parameters

page – Page that caused the exception

message = "Page {} doesn't exist."
exception pywikibot.exceptions.NoUsernameError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Username is not in user-config.py.

exception pywikibot.exceptions.NoWikibaseEntityError(entity: pywikibot.page.WikibaseEntity)[source]

Bases: pywikibot.exceptions.WikiBaseError

This entity doesn’t exist.

Parameters

entity – Wikibase entity

exception pywikibot.exceptions.NotEmailableError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageRelatedError

This user is not emailable.

Parameters

page – Page that caused the exception

message = '{} is not emailable.'
exception pywikibot.exceptions.NotImplementedWarning[source]

Bases: pywikibot.tools._deprecate._NotImplementedWarning

Feature that is no longer implemented.

exception pywikibot.exceptions.OtherPageSaveError(page: pywikibot.page.Page, reason: Union[str, Exception])[source]

Bases: pywikibot.exceptions.PageSaveRelatedError

Saving the page has failed due to uncatchable error.

Parameters

reason – Details of the problem

property args

Expose args.

message = 'Edit to page {title} failed:\n{reason}'
exception pywikibot.exceptions.PageCreatedConflictError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.EditConflictError

Page was created by another user.

Parameters

page – Page that caused the exception

message = 'Page {} has been created since last retrieved.'
exception pywikibot.exceptions.PageDeletedConflictError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.EditConflictError

Page was deleted since being retrieved.

Parameters

page – Page that caused the exception

message = 'Page {} has been deleted since last retrieved.'
exception pywikibot.exceptions.PageInUseError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Page cannot be reserved for writing due to existing lock.

exception pywikibot.exceptions.PageLoadRelatedError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageRelatedError

Loading the contents of a Page object has failed.

Parameters

page – Page that caused the exception

message = 'Page {} was not loaded.'
exception pywikibot.exceptions.PageRelatedError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.Error

Abstract Exception, used when the exception concerns a particular Page.

This class should be used when the Exception concerns a particular Page, and when a generic message can be written once for all.

Parameters

page – Page that caused the exception

message = ''
exception pywikibot.exceptions.PageSaveRelatedError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageRelatedError

Saving the page has failed.

Parameters

page – Page that caused the exception

message = 'Page {} was not saved.'
exception pywikibot.exceptions.SectionError(arg: str)[source]

Bases: pywikibot.exceptions.Error

The section specified by # does not exist.

exception pywikibot.exceptions.Server414Error(arg: str)[source]

Bases: pywikibot.exceptions.ServerError

Server returned with HTTP 414 code.

exception pywikibot.exceptions.Server504Error(arg: str)[source]

Bases: pywikibot.exceptions.ServerError

Server timed out with HTTP 504 code.

exception pywikibot.exceptions.ServerError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Got unexpected server response.

exception pywikibot.exceptions.SiteDefinitionError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Site does not exist.

exception pywikibot.exceptions.SpamblacklistError(page: pywikibot.page.Page, url: str)[source]

Bases: pywikibot.exceptions.PageSaveRelatedError

Page save failed because MediaWiki detected a blacklisted spam URL.

message = 'Edit to page {title} rejected by spam filter due to content:\n{url}'
exception pywikibot.exceptions.TimeoutError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Request failed with a timeout error.

exception pywikibot.exceptions.TitleblacklistError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageSaveRelatedError

Page save failed because MediaWiki detected a blacklisted page title.

Parameters

page – Page that caused the exception

message = 'Page {} is title-blacklisted.'
exception pywikibot.exceptions.TranslationError(arg: str)[source]

Bases: pywikibot.exceptions.Error, ImportError

Raised when no correct translation could be found.

Inherits from ImportError, as this exception is now used where previously an ImportError would have been raised, and may have been caught by scripts as such.

exception pywikibot.exceptions.UnknownExtensionError(arg: str)[source]

Bases: pywikibot.exceptions.Error, NotImplementedError

Extension is not defined.

exception pywikibot.exceptions.UnknownFamilyError(arg: str)[source]

Bases: pywikibot.exceptions.SiteDefinitionError

Family is not registered.

exception pywikibot.exceptions.UnknownSiteError(arg: str)[source]

Bases: pywikibot.exceptions.SiteDefinitionError

Site does not exist in Family.

exception pywikibot.exceptions.UnsupportedPageError(page: pywikibot.page.Page, message: Optional[str] = None)[source]

Bases: pywikibot.exceptions.PageRelatedError

Unsupported page due to namespace restriction.

Parameters

page – Page that caused the exception

message = 'Page {} is not supported due to namespace restriction.'
exception pywikibot.exceptions.UploadError(code: str, message: str, file_key: Optional[str] = None, offset: Union[int, bool] = 0)[source]

Bases: pywikibot.exceptions.APIError

Upload failed with a warning message (passed as the argument).

Create a new UploadError instance.

Parameters
  • file_key – The file_key of the uploaded file to reuse it later. If no key is known or it is an incomplete file it may be None.

  • offset – The starting offset for a chunked upload. Is False when there is no offset.

property message

Return warning message.

exception pywikibot.exceptions.UserRightsError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Insufficient user rights to perform an action.

exception pywikibot.exceptions.VersionParseError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Failed to parse version information.

exception pywikibot.exceptions.WikiBaseError(arg: str)[source]

Bases: pywikibot.exceptions.Error

Wikibase related error.

pywikibot.family module

Objects representing MediaWiki families.

pywikibot.family.AutoFamily(name: str, url: str, site=NotImplemented)[source]

Family that automatically loads the site configuration.

Parameters
  • name – Name for the family

  • url – API endpoint URL of the wiki

Returns

Generated family class

Return type

SingleSiteFamily

class pywikibot.family.Family[source]

Bases: object

Parent singleton class for all wiki families.

Allocator.

alphabetic = ['ace', 'kbd', 'ady', 'af', 'ak', 'als', 'alt', 'am', 'smn', 'ang', 'ab', 'ar', 'an', 'arc', 'roa-rup', 'frp', 'as', 'ast', 'atj', 'awa', 'gn', 'av', 'ay', 'az', 'ban', 'bm', 'bn', 'bjn', 'zh-min-nan', 'nan', 'map-bms', 'ba', 'be', 'be-tarask', 'mnw', 'bh', 'bcl', 'bi', 'bg', 'bar', 'bo', 'bs', 'br', 'bxr', 'ca', 'cv', 'ceb', 'cs', 'ch', 'cbk-zam', 'ny', 'sn', 'tum', 'cho', 'co', 'cy', 'dag', 'da', 'dk', 'ary', 'pdc', 'de', 'dv', 'nv', 'dsb', 'dty', 'dz', 'mh', 'et', 'el', 'eml', 'en', 'myv', 'es', 'eo', 'ext', 'eu', 'ee', 'fa', 'hif', 'fo', 'fr', 'fy', 'ff', 'fur', 'ga', 'gv', 'gag', 'gd', 'gl', 'gan', 'ki', 'glk', 'gu', 'gor', 'got', 'hak', 'xal', 'ko', 'ha', 'haw', 'hy', 'hi', 'ho', 'hsb', 'hr', 'hyw', 'io', 'ig', 'ilo', 'inh', 'bpy', 'id', 'ia', 'ie', 'iu', 'ik', 'os', 'xh', 'zu', 'is', 'it', 'he', 'jv', 'kbp', 'kl', 'kn', 'kr', 'pam', 'krc', 'ka', 'ks', 'csb', 'kk', 'kw', 'rw', 'rn', 'sw', 'kv', 'kg', 'gom', 'avk', 'ht', 'gcr', 'ku', 'kj', 'ky', 'mrj', 'lld', 'lad', 'lbe', 'lo', 'ltg', 'la', 'lv', 'lb', 'lez', 'lfn', 'lt', 'lij', 'li', 'ln', 'olo', 'jbo', 'lg', 'lmo', 'lrc', 'mad', 'hu', 'mai', 'mk', 'mg', 'ml', 'mt', 'mi', 'mr', 'xmf', 'arz', 'mzn', 'mni', 'ms', 'min', 'cdo', 'mwl', 'mdf', 'mo', 'mn', 'mus', 'my', 'nah', 'na', 'fj', 'nl', 'nds-nl', 'cr', 'ne', 'new', 'nia', 'ja', 'nqo', 'nap', 'ce', 'frr', 'pih', 'no', 'nb', 'nn', 'nrm', 'nov', 'ii', 'oc', 'mhr', 'or', 'om', 'ng', 'hz', 'uz', 'pa', 'pi', 'pfl', 'pag', 'pnb', 'pap', 'ps', 'jam', 'koi', 'km', 'pcd', 'pms', 'tpi', 'nds', 'pl', 'pnt', 'pt', 'aa', 'kaa', 'crh', 'ty', 'ksh', 'ro', 'rmy', 'rm', 'qu', 'rue', 'ru', 'sah', 'szy', 'se', 'sm', 'sa', 'sg', 'sat', 'skr', 'sc', 'sco', 'trv', 'stq', 'st', 'nso', 'tn', 'sq', 'scn', 'si', 'simple', 'sd', 'ss', 'sk', 'sl', 'cu', 'szl', 'so', 'ckb', 'srn', 'sr', 'sh', 'su', 'fi', 'sv', 'shi', 'tl', 'shn', 'ta', 'kab', 'roa-tara', 'tt', 'tay', 'te', 'tet', 'th', 'ti', 'tg', 'to', 'chr', 'chy', 've', 'tcy', 'tr', 'azb', 'tk', 'tw', 'tyv', 'din', 'udm', 'bug', 'uk', 'ur', 'ug', 'za', 'vec', 'vep', 'vi', 'vo', 'fiu-vro', 'wa', 'zh-classical', 'vls', 'war', 'wo', 'wuu', 'ts', 'yi', 'yo', 'zh-yue', 'diq', 'zea', 'bat-smg', 'zh', 'zh-tw', 'zh-cn']
alphabetic_revised = ['ace', 'ady', 'kbd', 'af', 'ak', 'als', 'alt', 'am', 'smn', 'ang', 'ab', 'ar', 'an', 'arc', 'roa-rup', 'frp', 'as', 'ast', 'atj', 'awa', 'gn', 'av', 'ay', 'az', 'bjn', 'id', 'ms', 'ban', 'bm', 'bn', 'zh-min-nan', 'nan', 'map-bms', 'jv', 'su', 'ba', 'min', 'be', 'be-tarask', 'mnw', 'mad', 'bh', 'bcl', 'bi', 'bar', 'bo', 'bs', 'br', 'bug', 'bg', 'bxr', 'ca', 'ceb', 'cv', 'cs', 'ch', 'cbk-zam', 'ny', 'sn', 'tum', 'cho', 'co', 'cy', 'dag', 'da', 'dk', 'ary', 'pdc', 'de', 'dv', 'nv', 'dsb', 'na', 'dty', 'dz', 'mh', 'et', 'el', 'eml', 'en', 'myv', 'es', 'eo', 'ext', 'eu', 'ee', 'fa', 'hif', 'fo', 'fr', 'fy', 'ff', 'fur', 'ga', 'gv', 'sm', 'gag', 'gd', 'gl', 'gan', 'ki', 'glk', 'gu', 'got', 'hak', 'xal', 'ko', 'ha', 'haw', 'hy', 'hi', 'ho', 'hsb', 'hr', 'hyw', 'io', 'ig', 'ilo', 'inh', 'bpy', 'ia', 'ie', 'iu', 'ik', 'os', 'xh', 'zu', 'is', 'it', 'he', 'kl', 'kn', 'kr', 'pam', 'ka', 'ks', 'csb', 'kk', 'kw', 'rw', 'ky', 'rn', 'mrj', 'sw', 'kv', 'kg', 'gom', 'avk', 'gor', 'ht', 'gcr', 'ku', 'shn', 'kj', 'lld', 'lad', 'lbe', 'lez', 'lfn', 'lo', 'la', 'ltg', 'lv', 'to', 'lb', 'lt', 'lij', 'li', 'ln', 'nia', 'olo', 'jbo', 'lg', 'lmo', 'lrc', 'hu', 'mai', 'mk', 'mg', 'ml', 'krc', 'mt', 'mi', 'mr', 'xmf', 'arz', 'mzn', 'mni', 'cdo', 'mwl', 'koi', 'mdf', 'mo', 'mn', 'mus', 'my', 'nah', 'fj', 'nl', 'nds-nl', 'cr', 'ne', 'new', 'ja', 'nqo', 'nap', 'ce', 'frr', 'pih', 'no', 'nb', 'nn', 'nrm', 'nov', 'ii', 'oc', 'mhr', 'or', 'om', 'ng', 'hz', 'uz', 'pa', 'pi', 'pfl', 'pag', 'pnb', 'pap', 'ps', 'jam', 'km', 'pcd', 'pms', 'nds', 'pl', 'pnt', 'pt', 'aa', 'kaa', 'crh', 'ty', 'ksh', 'ro', 'rmy', 'rm', 'qu', 'ru', 'rue', 'sah', 'szy', 'se', 'sa', 'sg', 'sat', 'skr', 'sc', 'sco', 'trv', 'stq', 'st', 'nso', 'tn', 'sq', 'scn', 'si', 'simple', 'sd', 'ss', 'sk', 'sl', 'cu', 'szl', 'so', 'ckb', 'srn', 'sr', 'sh', 'fi', 'sv', 'shi', 'tl', 'ta', 'kab', 'kbp', 'roa-tara', 'tt', 'tay', 'te', 'tet', 'th', 'vi', 'ti', 'tg', 'tpi', 'chr', 'chy', 've', 'tcy', 'tr', 'azb', 'tk', 'tw', 'tyv', 'din', 'udm', 'uk', 'ur', 'ug', 'za', 'vec', 'vep', 'vo', 'fiu-vro', 'wa', 'zh-classical', 'vls', 'war', 'wo', 'wuu', 'ts', 'yi', 'yo', 'zh-yue', 'diq', 'zea', 'bat-smg', 'zh', 'zh-tw', 'zh-cn']
apipath(code)[source]

Return path to api.php.

archived_page_templates: dict = {}
base_url(code: str, uri: str, protocol=None)str[source]

Prefix uri with port and hostname.

Parameters
  • code – The site code

  • uri – The absolute path after the hostname

  • protocol – The protocol which is used. If None it’ll determine the protocol from the code.

Returns

The full URL ending with uri

categories_last: list = []
category_attop: list = []
category_on_one_line: list = []
category_redirect_templates = {'_default': []}
category_redirects(code, fallback='_default')[source]

Return list of category redirect templates.

category_text_separator = '\n\n'
codes

Get list of codes used by this family.

Return type

set of str

cross_allowed: list = []
cross_projects = []
cross_projects_cookies = ['centralauth_Session', 'centralauth_Token', 'centralauth_User']
crossnamespace = {}
dbName(code)[source]

Return the name of the MySQL database.

disambcatname: dict = {}
disambig(code, fallback='_default')[source]

Return list of disambiguation templates.

disambiguationTemplates = {'_default': []}
domains

Get list of unique domain names included in this family.

These domains may also exist in another family.

Return type

set of str

edit_restricted_templates: dict = {}
encoding(code)[source]

Return the encoding for a specific language wiki.

encodings(code)[source]

Return list of historical encodings for a specific language wiki.

eventstreams_host(code)[source]

Hostname for EventStreams.

New in version 3.0.

eventstreams_path(code)[source]

Return path for EventStreams.

New in version 3.0.

from_url(url: str)Optional[str][source]

Return whether this family matches the given url.

It is first checking if a domain of this family is in the domain of the URL. If that is the case it’s checking all codes and verifies that a path generated via APISite.article_path and Family.path matches the path of the URL together with the hostname for that code.

It is using Family.domains to first check if a domain applies and then iterates over Family.codes to actually determine which code applies.

Parameters

url – the URL which may contain a $1. If it’s missing it is assumed to be at the end and if it’s present nothing is allowed after it.

Returns

The language code of the url. None if that url is not from this family.

Raises
  • RuntimeError – When there are multiple languages in this family which would work with the given URL.

  • ValueError – When text is present after $1.

fyinterwiki = ['aa', 'ab', 'ace', 'ady', 'af', 'ay', 'ak', 'als', 'alt', 'am', 'an', 'ang', 'ar', 'arc', 'ary', 'arz', 'as', 'ast', 'atj', 'av', 'avk', 'awa', 'az', 'azb', 'ba', 'ban', 'bar', 'bat-smg', 'bcl', 'be', 'be-tarask', 'bg', 'bh', 'bi', 'bjn', 'bm', 'bn', 'bo', 'bpy', 'br', 'bs', 'bug', 'bxr', 'ca', 'cbk-zam', 'cdo', 'ce', 'ceb', 'ch', 'chy', 'cho', 'chr', 'cy', 'ckb', 'co', 'cr', 'crh', 'cs', 'csb', 'cu', 'cv', 'da', 'dag', 'de', 'din', 'diq', 'dk', 'dsb', 'dty', 'dv', 'dz', 'ee', 'el', 'eml', 'en', 'eo', 'es', 'et', 'eu', 'ext', 'fa', 'ff', 'fi', 'fy', 'fiu-vro', 'fj', 'fo', 'fr', 'frp', 'frr', 'fur', 'ga', 'gag', 'gan', 'gcr', 'gd', 'gl', 'glk', 'gn', 'gom', 'gor', 'got', 'gu', 'gv', 'ha', 'hak', 'haw', 'he', 'hi', 'hy', 'hif', 'hyw', 'ho', 'hr', 'hsb', 'ht', 'hu', 'hz', 'ia', 'id', 'ie', 'ig', 'ii', 'yi', 'ik', 'ilo', 'inh', 'io', 'yo', 'is', 'it', 'iu', 'ja', 'jam', 'jbo', 'jv', 'ka', 'kaa', 'kab', 'kbd', 'kbp', 'kg', 'ki', 'ky', 'kj', 'kk', 'kl', 'km', 'kn', 'ko', 'koi', 'kr', 'krc', 'ks', 'ksh', 'ku', 'kv', 'kw', 'la', 'lad', 'lb', 'lbe', 'lez', 'lfn', 'lg', 'li', 'lij', 'lld', 'lmo', 'ln', 'lo', 'lrc', 'lt', 'ltg', 'lv', 'mad', 'mai', 'map-bms', 'mdf', 'mg', 'mh', 'mhr', 'mi', 'my', 'min', 'myv', 'mk', 'ml', 'mn', 'mni', 'mnw', 'mo', 'mr', 'mrj', 'ms', 'mt', 'mus', 'mwl', 'mzn', 'na', 'nah', 'nan', 'nap', 'nds', 'nds-nl', 'ne', 'new', 'ng', 'ny', 'nia', 'nl', 'nn', 'no', 'nov', 'nqo', 'nrm', 'nso', 'nv', 'oc', 'olo', 'om', 'or', 'os', 'pa', 'pag', 'pam', 'pap', 'pcd', 'pdc', 'pfl', 'pi', 'pih', 'pl', 'pms', 'pnb', 'pnt', 'ps', 'pt', 'qu', 'rm', 'rmy', 'rn', 'ro', 'roa-rup', 'roa-tara', 'ru', 'rue', 'rw', 'sa', 'sah', 'sat', 'sc', 'scn', 'sco', 'sd', 'se', 'sg', 'sh', 'shi', 'shn', 'si', 'simple', 'sk', 'skr', 'sl', 'sm', 'smn', 'sn', 'so', 'sq', 'sr', 'srn', 'ss', 'st', 'stq', 'su', 'sv', 'sw', 'szy', 'szl', 'ta', 'tay', 'tcy', 'te', 'tet', 'tg', 'th', 'ti', 'ty', 'tyv', 'tk', 'tl', 'tn', 'to', 'tpi', 'tr', 'trv', 'ts', 'tt', 'tum', 'tw', 'udm', 'ug', 'uk', 'ur', 'uz', 've', 'vec', 'vep', 'vi', 'vls', 'vo', 'wa', 'war', 'wo', 'wuu', 'xal', 'xh', 'xmf', 'za', 'zea', 'zh', 'zh-classical', 'zh-cn', 'zh-yue', 'zh-min-nan', 'zh-tw', 'zu']
get_address(code, title, name='[deprecated name of title]')[source]

Return the path to title using index.php with redirects disabled.

get_archived_page_templates(code)[source]

Return tuple of archived page templates.

New in version 3.0.

get_edit_restricted_templates(code)[source]

Return tuple of edit restricted templates.

New in version 3.0.

hostname(code)[source]

The hostname to use for standard http connections.

instance

Get the singleton instance.

interface(code)[source]

Return interface to use for code.

Return type

str or subclass of BaseSite

interwiki_attop: list = []
interwiki_forward = None
interwiki_on_one_line: list = []
interwiki_putfirst = {}
interwiki_removals: list = []
interwiki_replacements: dict = {}
interwiki_text_separator = '\n\n'
isPublic(code)[source]

Check the wiki require logging in before viewing it.

langs: dict = {}
language_groups = {'arab': ['ar', 'ary', 'arz', 'azb', 'ckb', 'fa', 'glk', 'ks', 'lrc', 'mzn', 'ps', 'sd', 'ur', 'ha', 'kk', 'ku', 'pnb', 'ug'], 'chinese': ['wuu', 'zh', 'zh-classical', 'zh-yue', 'gan', 'ii', 'ja', 'za'], 'cyril': ['ab', 'av', 'ba', 'be', 'be-tarask', 'bg', 'bxr', 'ce', 'cu', 'cv', 'kbd', 'koi', 'kv', 'ky', 'mk', 'lbe', 'mdf', 'mn', 'mo', 'myv', 'mhr', 'mrj', 'os', 'ru', 'rue', 'sah', 'tg', 'tk', 'udm', 'uk', 'xal', 'ha', 'kk', 'sh', 'sr', 'tt'], 'grec': ['el', 'grc', 'pnt'], 'latin': ['aa', 'ace', 'af', 'ak', 'als', 'an', 'ang', 'ast', 'ay', 'bar', 'bat-smg', 'bcl', 'bi', 'bm', 'br', 'bs', 'ca', 'cbk-zam', 'cdo', 'ceb', 'ch', 'cho', 'chy', 'co', 'crh', 'cs', 'csb', 'cy', 'da', 'de', 'diq', 'dsb', 'ee', 'eml', 'en', 'eo', 'es', 'et', 'eu', 'ext', 'ff', 'fi', 'fiu-vro', 'fj', 'fo', 'fr', 'frp', 'frr', 'fur', 'fy', 'ga', 'gag', 'gd', 'gl', 'gn', 'gv', 'hak', 'haw', 'hif', 'ho', 'hr', 'hsb', 'ht', 'hu', 'hz', 'ia', 'id', 'ie', 'ig', 'ik', 'ilo', 'io', 'is', 'it', 'jbo', 'jv', 'kaa', 'kab', 'kg', 'ki', 'kj', 'kl', 'kr', 'ksh', 'kw', 'la', 'lad', 'lb', 'lg', 'li', 'lij', 'lmo', 'ln', 'lt', 'ltg', 'lv', 'map-bms', 'mg', 'mh', 'mi', 'ms', 'mt', 'mus', 'mwl', 'na', 'nah', 'nap', 'nds', 'nds-nl', 'ng', 'nl', 'nn', 'no', 'nov', 'nrm', 'nv', 'ny', 'oc', 'om', 'pag', 'pam', 'pap', 'pcd', 'pdc', 'pfl', 'pih', 'pl', 'pms', 'pt', 'qu', 'rm', 'rn', 'ro', 'roa-rup', 'roa-tara', 'rw', 'sc', 'scn', 'sco', 'se', 'sg', 'simple', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'srn', 'ss', 'st', 'stq', 'su', 'sv', 'sw', 'szl', 'tet', 'tl', 'tn', 'to', 'tpi', 'tr', 'ts', 'tum', 'tw', 'ty', 'uz', 've', 'vec', 'vi', 'vls', 'vo', 'wa', 'war', 'wo', 'xh', 'yo', 'zea', 'zh-min-nan', 'zu', 'az', 'chr', 'ckb', 'ha', 'iu', 'kk', 'ku', 'rmy', 'sh', 'sr', 'tt', 'ug', 'za'], 'scand': ['da', 'fo', 'is', 'nb', 'nn', 'no', 'sv']}
languages_by_size: list = []
ldapDomain = ()
linktrail(code, fallback='_default')[source]

Return regex for trailing chars displayed as part of a link.

Returns a string, not a compiled regular expression object.

linktrails = {'_default': '[a-z]*', 'ab': '[a-zабвгҕдежзӡикқҟлмнопҧрстҭуфхҳцҵчҷҽҿшыҩџьә]*', 'als': '[äöüßa-z]*', 'alt': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'an': '[a-záéíóúñ]*', 'ar': '[a-zء-يؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ۭ]*', 'ary': '[a-zء-يؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ۭ]*', 'arz': '[a-zء-يؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ۭ]*', 'ast': '[a-záéíóúñ]*', 'atj': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'av': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'avk': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'awa': '[a-zऀ-ॣ०-꣠-ꣿ]*', 'ay': '[a-záéíóúñ]*', 'az': '[a-zçəğıöşü]*', 'azb': '[ابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهیآأئؤة\u200c]*', 'ba': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюяәөүғҡңҙҫһ“»]*', 'bar': '[äöüßa-z]*', 'bat-smg': '[a-ząčęėįšųūž]*', 'be': '[абвгґджзеёжзійклмнопрстуўфхцчшыьэюяćčłńśšŭźža-z]*', 'be-tarask': '[абвгґджзеёжзійклмнопрстуўфхцчшыьэюяćčłńśšŭźža-z]*', 'bg': '[a-zабвгдежзийклмнопрстуфхцчшщъыьэюя]*', 'bm': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'bn': '[ঀ-\u09ff]*', 'bpy': '[ঀ-\u09ff]*', 'bs': '[a-zćčžšđž]*', 'bxr': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'ca': '[a-zàèéíòóúç·ïü]*', 'cbk-zam': '[a-záéíóúñ]*', 'ce': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'ckb': '[ئابپتجچحخدرڕزژسشعغفڤقکگلڵمنوۆهھەیێ\u200c]*', 'co': '[a-zàéèíîìóòúù]*', 'crh': '[a-zâçğıñöşüа-яёʺʹ“»]*', 'cs': '[a-záčďéěíňóřšťúůýž]*', 'csb': '[a-zęóąśłżźćńĘÓĄŚŁŻŹĆŃ]*', 'cu': '[a-zабвгдеєжѕзїіıићклмнопсстѹфхѡѿцчшщъыьѣюѥѧѩѫѭѯѱѳѷѵґѓђёјйљњќуўџэ҄я\uf011“»]*', 'cv': '[a-zа-яĕçăӳ"»]*', 'cy': '[àáâèéêìíîïòóôûŵŷa-z]*', 'da': '[a-zæøå]*', 'dag': '[ɛɣŋɔʒƐƔŊƆƷa-z]*', 'de': '[äöüßa-z]*', 'din': '[äëɛɛ̈éɣïŋöɔɔ̈óa-z]*', 'dsb': '[äöüßa-z]*', 'el': '[a-zαβγδεζηθικλμνξοπρστυφχψωςΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩάέήίόύώϊϋΐΰΆΈΉΊΌΎΏΪΫ]*', 'eml': '[a-zàéèíîìóòúù]*', 'es': '[a-záéíóúñ]*', 'et': '[äöõšüža-z]*', 'ext': '[a-záéíóúñ]*', 'fa': '[ابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهیآأئؤة\u200c]*', 'ff': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'fi': '[a-zäö]*', 'fiu-vro': '[äöõšüža-z]*', 'fo': '[áðíóúýæøa-z]*', 'fr': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'frp': '[a-zàâçéèêîœôû·’æäåāăëēïīòöōùü‘]*', 'frr': '[a-zäöüßåāđē]*', 'fur': '[a-zàéèíîìóòúù]*', 'fy': '[a-zàáèéìíòóùúâêîôûäëïöü]*', 'gag': '[a-zÇĞçğİıÖöŞşÜüÂâÎîÛû]*', 'gan': '', 'gcr': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'gl': '[áâãàéêẽçíòóôõq̃úüűũa-z]*', 'glk': '[ابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهیآأئؤة\u200c]*', 'gn': '[a-záéíóúñ]*', 'gu': '[\u0a80-૿]*', 'he': '[a-zא-ת]*', 'hi': '[a-zऀ-ॣ०-꣠-ꣿ]*', 'hr': '[čšžćđßa-z]*', 'hsb': '[äöüßa-z]*', 'ht': '[a-zàèòÀÈÒ]*', 'hu': '[a-záéíóúöüőűÁÉÍÓÚÖÜŐŰ]*', 'hy': '[a-zաբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆև«»]*', 'hyw': '[a-zաբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆև«»]*', 'ii': '', 'inh': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'is': '[áðéíóúýþæöa-z-–]*', 'it': '[a-zàéèíîìóòúù]*', 'ka': '[a-zაბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰ“»]*', 'kab': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'kbp': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'kk': '[a-zäçéğıïñöşüýʺʹа-яёәғіқңөұүһٴابپتجحدرزسشعفقكلمنڭەوۇۋۆىيچھ“»]*', 'kl': '[a-zæøå]*', 'koi': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'krc': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'ksh': '[äöüėëijßəğåůæœça-z]*', 'ku': '[a-zçêîşûẍḧÇÊÎŞÛẌḦ]*', 'kv': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'lad': '[a-záéíóúñ]*', 'lb': '[äöüßa-z]*', 'lbe': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюяӀ1“»]*', 'lez': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'li': '[a-zäöüïëéèà]*', 'lij': '[a-zàéèíîìóòúù]*', 'lld': '[a-zàéèíîìóòúù]*', 'lmo': '[a-zàéèíîìóòúù]*', 'ln': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'lrc': '[ابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهیآأئؤة\u200c]*', 'lt': '[a-ząčęėįšųūž]*', 'ltg': '[a-zA-ZĀāČčĒēĢģĪīĶķĻļŅņŠšŪūŽž]*', 'lv': '[a-zA-ZĀāČčĒēĢģĪīĶķĻļŅņŠšŪūŽž]*', 'mai': '[a-zऀ-ॣ०-꣠-ꣿ]*', 'mdf': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'mg': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'mhr': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'mk': '[a-zабвгдѓежзѕијклљмнњопрстќуфхцчџш]*', 'ml': '[a-zം-ൿ]*', 'mn': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя“»]*', 'mr': '[ऀ-ॣॱ-ॿ\ufeff\u200d]*', 'mrj': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'mwl': '[áâãàéêẽçíòóôõq̃úüűũa-z]*', 'myv': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'mzn': '[ابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهیآأئؤة\u200c]*', 'nah': '[a-záéíóúñ]*', 'nap': '[a-zàéèíîìóòúù]*', 'nds': '[äöüßa-z]*', 'nds-nl': '[a-zäöüïëéèà]*', 'nl': '[a-zäöüïëéèà]*', 'nn': '[æøåa-z]*', 'no': '[æøåa-z]*', 'nrm': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'oc': '[a-zàâçéèêîôû]*', 'olo': '[a-zčČšŠžŽäÄöÖ]*', 'or': '[a-z\u0b00-\u0b7f]*', 'os': '[a-zаæбвгдеёжзийклмнопрстуфхцчшщъыьэюя“»]*', 'pa': '[ਁਂਃਅਆਇਈਉਊਏਐਓਔਕਖਗਘਙਚਛਜਝਞਟਠਡਢਣਤਥਦਧਨਪਫਬਭਮਯਰਲਲ਼ਵਸ਼ਸਹ਼ਾਿੀੁੂੇੈੋੌ੍ਖ਼ਗ਼ਜ਼ੜਫ਼ੰੱੲੳa-z]*', 'pcd': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'pdc': '[äöüßa-z]*', 'pfl': '[äöüßa-z]*', 'pl': '[a-zęóąśłżźćńĘÓĄŚŁŻŹĆŃ]*', 'pms': '[a-zàéèíîìóòúù]*', 'pnt': '[a-zαβγδεζηθικλμνξοπρστυφχψωςΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩάέήίόύώϊϋΐΰΆΈΉΊΌΎΏΪΫ]*', 'pt': '[áâãàéêẽçíòóôõq̃úüűũa-z]*', 'qu': '[a-záéíóúñ]*', 'rmy': '[a-zăâîşţșțĂÂÎŞŢȘȚ]*', 'ro': '[a-zăâîşţșțĂÂÎŞŢȘȚ]*', 'roa-rup': '[a-zăâîşţșțĂÂÎŞŢȘȚ]*', 'roa-tara': '[a-zàéèíîìóòúù]*', 'ru': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'rue': '[a-zабвгґдеєжзиіїйклмнопрстуфхцчшщьєюяёъы“»]*', 'sa': '[a-zऀ-ॣ०-꣠-ꣿ]*', 'sah': '[a-zабвгҕдеёжзийклмнҥоөпрсһтуүфхцчшщъыьэюя]*', 'scn': '[a-zàéèíîìóòúù]*', 'se': '[a-zàáâçčʒǯđðéèêëǧǥȟíìîïıǩŋñóòôõßšŧúùûýÿüžþæøåäö]*', 'sg': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'sh': '[a-zčćđžš]*', 'shi': '[ⴰ-ⵯa-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙḍḥɛṛɣṣṭẓḌḤƐṚƔṢṬẒʷ]*', 'sk': '[a-záäčďéíľĺňóôŕšťúýž]*', 'skr': '[ابپتٹثجچحخدڈذرڑزژسشصضطظعغفقکگلمنںوؤہھیئےآأءۃٻڄݙڋڰڳݨ]*', 'sl': '[a-zčćđžš]*', 'smn': '[a-zâčđŋšžäá]*', 'sr': '[abvgdđežzijklljmnnjoprstćufhcčdžšабвгдђежзијклљмнњопрстћуфхцчџш]*', 'srn': '[a-zäöüïëéèà]*', 'stq': '[äöüßa-z]*', 'sv': '[a-zåäöéÅÄÖÉ]*', 'szl': '[a-zęóąśłżźćńĘÓĄŚŁŻŹĆŃ]*', 'szy': '', 'ta': '[\u0b80-\u0bff]*', 'tay': '', 'te': '[ఁ-౯]*', 'tet': '[áâãàéêẽçíòóôõq̃úüűũa-z]*', 'tg': '[a-zабвгдеёжзийклмнопрстуфхчшъэюяғӣқўҳҷцщыь]*', 'tk': '[a-zÄäÇçĞğŇňÖöŞşÜüÝýŽž]*', 'tr': '[a-zÇĞçğİıÖöŞşÜüÂâÎîÛû]*', 'trv': '', 'tt': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюяӘәӨөҮүҖҗҢңҺһ]*', 'ty': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'tyv': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'udm': '[a-zа-яёӝӟӥӧӵ]*', 'uk': '[a-zабвгґдеєжзиіїйклмнопрстуфхцчшщьєюяёъы“»]*', 'ur': '[ابپتٹثجچحخدڈذر\u200bڑ\u200bزژسشصضطظعغفقکگل\u200bم\u200bنںوؤہھیئےآأءۃ]*', 'uz': '[a-zʻʼ“»]*', 'vec': '[a-zàéèíîìóòúù]*', 'vep': '[äöõšüža-z]*', 'vi': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'vls': '[a-zäöüïëéèà]*', 'wa': '[a-zåâêîôûçéè]*', 'wo': '[a-zàâçéèêîôûäëïöüùÇÉÂÊÎÔÛÄËÏÖÜÀÈÙ]*', 'wuu': '', 'xal': '[a-zабвгдеёжзийклмнопрстуфхцчшщъыьэюя]*', 'xmf': '[a-zაბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰ“»]*', 'yi': '[a-zא-ת]*', 'za': '', 'zea': '[a-zäöüïëéèà]*', 'zh': ''}
static load(fam: Optional[str] = None, fatal=NotImplemented)[source]

Import the named family.

Parameters

fam – family name (if omitted, uses the configured default)

Returns

a Family instance configured for the named family.

Raises

pywikibot.exceptions.UnknownFamilyError – family not known

maximum_GET_length(code)[source]

Return the maximum URL length for GET instead of POST.

name = None
property obsolete

Old codes that are not part of the family.

Interwiki replacements override removals for the same code.

Returns

mapping of old codes to new codes (or None)

Return type

dict

path(code)[source]

Return path to index.php.

post_get_convert(site, getText)[source]

Do a conversion on the retrieved text from the Wiki.

For example a X-conversion in Esperanto https://en.wikipedia.org/wiki/Esperanto_orthography#X-system.

pre_put_convert(site, putText)[source]

Do a conversion on the text to insert on the Wiki.

For example a X-conversion in Esperanto https://en.wikipedia.org/wiki/Esperanto_orthography#X-system.

protocol(code: str)str[source]

The protocol to use to connect to the site.

May be overridden to return ‘https’. Other protocols are not supported.

Parameters

code – language code

Returns

protocol that this family uses

querypath(code)[source]

Return path to query.php.

scriptpath(code: str)str[source]

The prefix used to locate scripts on this wiki.

This is the value displayed when you enter {{SCRIPTPATH}} on a wiki page (often displayed at [[Help:Variables]] if the wiki has copied the master help page correctly).

The default value is the one used on Wikimedia Foundation wikis, but needs to be overridden in the family file for any wiki that uses a different value.

Parameters

code – Site code

Raises

KeyError – code is not recognised

Returns

URL path without ending ‘/’

shared_image_repository(code)[source]

Return the shared image repository, if any.

shared_urlshortner_wiki: Optional[tuple] = None
ssl_hostname(code)[source]

The hostname to use for SSL connections.

ssl_pathprefix(code)[source]

The path prefix for secure HTTP access.

use_hard_category_redirects = []
verify_SSL_certificate(code: str)bool[source]

Return whether a HTTPS certificate should be verified.

Renamed in version 5.3.

Parameters

code – language code

Returns

flag to verify the SSL certificate; set it to False to allow access if certificate has an error.

class pywikibot.family.FandomFamily[source]

Bases: pywikibot.family.Family

Common features of Fandom families.

Renamed in version 3.0.

Allocator.

langs: dict

Property listing family languages.

protocol(code)[source]

Return ‘https’ as the protocol.

scriptpath(code)[source]

Return the script path for this family.

class pywikibot.family.SingleSiteFamily[source]

Bases: pywikibot.family.Family

Single site family.

domains

Return the full domain name of the site.

hostname(code)[source]

Return the domain as the hostname.

class pywikibot.family.SubdomainFamily[source]

Bases: pywikibot.family.Family

Multi site wikis that are subdomains of the same top level domain.

codes

Property listing family codes.

domains

Return the domain name of the sites in this family.

langs: dict

Property listing family languages.

class pywikibot.family.WikimediaFamily[source]

Bases: pywikibot.family.Family

Class for all wikimedia families.

Allocator.

closed_wikis: list = []
code_aliases = {'be-x-old': 'be-tarask', 'dk': 'da', 'jp': 'ja', 'minnan': 'zh-min-nan', 'mo': 'ro', 'nan': 'zh-min-nan', 'nb': 'no', 'nds_nl': 'nds-nl', 'zh-cn': 'zh', 'zh-tw': 'zh'}
content_families = {'commons', 'incubator', 'lingualibre', 'mediawiki', 'species', 'wikibooks', 'wikidata', 'wikinews', 'wikipedia', 'wikiquote', 'wikisource', 'wikiversity', 'wikivoyage', 'wiktionary'}
cross_projects = {'commons', 'incubator', 'lingualibre', 'mediawiki', 'meta', 'outreach', 'species', 'strategy', 'wikibooks', 'wikidata', 'wikimania', 'wikimediachapter', 'wikinews', 'wikipedia', 'wikiquote', 'wikisource', 'wikiversity', 'wikivoyage', 'wiktionary'}
disambcatname: dict = {'wikidata': 'Q1982926'}
domain

Domain property.

eventstreams_host(code)[source]

Return ‘https://stream.wikimedia.org’ as the stream hostname.

eventstreams_path(code)[source]

Return path for EventStreams.

interwiki_removals: list

Return a list of interwiki codes to be removed from wiki pages.

interwiki_replacements: dict

Return an interwiki code replacement mapping.

multi_language_content_families = ['wikipedia', 'wiktionary', 'wikisource', 'wikibooks', 'wikinews', 'wikiquote', 'wikiversity', 'wikivoyage']
other_content_families = ['wikidata', 'mediawiki', 'lingualibre']
protocol(code)[source]

Return ‘https’ as the protocol.

removed_wikis: list = []
shared_image_repository(code)[source]

Return Wikimedia Commons as the shared image repository.

shared_urlshortner_wiki: Optional[tuple] = ('meta', 'meta')
wikimedia_org_content_families = ['commons', 'incubator', 'species']
wikimedia_org_families = {'commons', 'incubator', 'meta', 'outreach', 'species', 'strategy', 'wikimania', 'wikimediachapter', 'wikitech'}
wikimedia_org_meta_families = ['meta', 'outreach', 'strategy', 'wikimediachapter', 'wikimania']
wikimedia_org_other_families = ['wikitech']
class pywikibot.family.WikimediaOrgFamily[source]

Bases: pywikibot.family.SingleSiteFamily, pywikibot.family.WikimediaFamily

Single site family for sites hosted at *.wikimedia.org.

domain

Return the parents domain with a subdomain prefix.

pywikibot.fixes module

File containing all standard fixes. Currently available predefined fixes are:

  • HTML - Convert HTML tags to wiki syntax, and

    fix XHTML.

  • isbn - Fix badly formatted ISBNs.

  • syntax - Try to fix bad wiki markup. Do not run

    this in automatic mode, as the bot may make mistakes.

  • syntax-safe - Like syntax, but less risky, so you can

    run this in automatic mode.

  • case-de - fix upper/lower case errors in German

  • grammar-de - fix grammar and typography in German

  • vonbis - Ersetze Binde-/Gedankenstrich durch “bis”

    in German

  • music - Links auf Begriffsklärungen in German

  • datum - specific date formats in German

  • correct-ar - Typo corrections for Arabic Wikipedia and any

    Arabic wiki.

  • yu-tld - Fix links to .yu domains because it is

    disabled, see: https://lists.wikimedia.org/pipermail/wikibots-l/2009-February/000290.html

  • fckeditor - Try to convert FCKeditor HTML tags to wiki

    syntax.

pywikibot.flow module

Objects representing Flow entities, like boards, topics, and posts.

class pywikibot.flow.Board(source: Any, title: str = '')[source]

Bases: pywikibot.flow.FlowPage

A Flow discussion board.

Parameters
  • source – A Flow-enabled site or a Link or Page on such a site

  • title – normalized title of the page

Raises
  • TypeError – incorrect use of parameters

  • ValueError – use of non-Flow-enabled Site

new_topic(title: str, content: str, content_format: str = 'wikitext', format='[deprecated name of content_format]')[source]

Create and return a Topic object for a new topic on this Board.

Parameters
  • title – The title of the new topic (must be in plaintext)

  • content – The content of the topic’s initial post

  • content_format – The content format of the supplied content; either ‘wikitext’ or ‘html’

Returns

The new topic

topics(content_format: str = 'wikitext', limit: int = 100, sort_by: str = 'newest', offset: Optional[Union[str, datetime.datetime]] = None, offset_uuid: str = '', reverse: bool = False, include_offset: bool = False, toc_only: bool = False, format='[deprecated name of content_format]')[source]

Load this board’s topics.

Parameters
  • content_format – The content format to request the data in; must be either ‘wikitext’, ‘html’, or ‘fixed-html’

  • limit – The number of topics to fetch in each request.

  • sort_by – Algorithm to sort topics by; must be either ‘newest’ or ‘updated’

  • offset – The timestamp to start at (when sortby is ‘updated’).

  • offset_uuid – The UUID to start at (when sortby is ‘newest’).

  • reverse – Whether to reverse the topic ordering.

  • include_offset – Whether to include the offset topic.

  • toc_only – Whether to only include information for the TOC.

Returns

A generator of this board’s topics.

class pywikibot.flow.FlowPage(source: Any, title: str = '')[source]

Bases: pywikibot.page.BasePage, abc.ABC

The base page meta class for the Flow extension.

It cannot be instantiated directly.

Parameters
  • source – A Flow-enabled site or a Link or Page on such a site

  • title – normalized title of the page

Raises
  • TypeError – incorrect use of parameters

  • ValueError – use of non-Flow-enabled Site

get(force: bool = False, get_redirect: bool = False)dict[source]

Get the page’s content.

property uuid

Return the UUID of the page.

Returns

UUID of the page

class pywikibot.flow.Post(page: pywikibot.flow.Topic, uuid: str)[source]

Bases: object

A post to a Flow discussion topic.

Parameters
  • page – Flow topic

  • uuid – UUID of a Flow post

Raises

TypeError – incorrect types of parameters

property creator

The creator of this post.

delete(reason: str)None[source]

Delete this post through the Flow moderation system.

Parameters

reason – The reason for deleting this post.

classmethod fromJSON(page: pywikibot.flow.Topic, post_uuid: str, data: dict)pywikibot.flow.Post[source]

Create a Post object using the data returned from the API call.

Parameters
  • page – A Flow topic

  • post_uuid – The UUID of the post

  • data – The JSON data returned from the API

Returns

A Post object

Raises
  • TypeError – data is not a dict

  • ValueError – data is missing required entries

get(content_format: str = 'wikitext', force: bool = False, format='[deprecated name of content_format]')[source]

Return the contents of the post in the given format.

Parameters
  • force – Whether to reload from the API instead of using the cache

  • content_format – Content format to return contents in

Returns

The contents of the post in the given content format

hide(reason: str)None[source]

Hide this post.

Parameters

reason – The reason for hiding this post.

property is_moderated

Whether this post is moderated.

property page

Return the page associated with the post.

Returns

Page associated with the post

replies(content_format: str = 'wikitext', force: bool = False, format='[deprecated name of content_format]')[source]

Return this post’s replies.

Parameters
  • content_format – Content format to return contents in; must be ‘wikitext’, ‘html’, or ‘fixed-html’

  • force – Whether to reload from the API instead of using the cache

Returns

This post’s replies

reply(content: str, content_format: str = 'wikitext', format='[deprecated name of content_format]')[source]

Reply to this post.

Parameters
  • content – The content of the new post

  • content_format – The format of the given content; must be ‘wikitext’ or ‘html’

Returns

The new reply post

restore(reason: str)None[source]

Restore this post.

Parameters

reason – The reason for restoring this post.

property site

Return the site associated with the post.

Returns

Site associated with the post

suppress(reason: str)None[source]

Suppress this post.

Parameters

reason – The reason for suppressing this post.

thank()None[source]

Thank the user who made this post.

property uuid

Return the UUID of the post.

Returns

UUID of the post

class pywikibot.flow.Topic(source: Any, title: str = '')[source]

Bases: pywikibot.flow.FlowPage

A Flow discussion topic.

Parameters
  • source – A Flow-enabled site or a Link or Page on such a site

  • title – normalized title of the page

Raises
  • TypeError – incorrect use of parameters

  • ValueError – use of non-Flow-enabled Site

classmethod create_topic(board: Board, title: str, content: str, content_format: str = 'wikitext', format='[deprecated name of content_format]')[source]

Create and return a Topic object for a new topic on a Board.

Parameters
  • board – The topic’s parent board

  • title – The title of the new topic (must be in plaintext)

  • content – The content of the topic’s initial post

  • content_format – The content format of the supplied content; either ‘wikitext’ or ‘html’

Returns

The new topic

delete_mod(reason: str)None[source]

Delete this topic through the Flow moderation system.

Parameters

reason – The reason for deleting this topic.

classmethod from_topiclist_data(board: pywikibot.flow.Board, root_uuid: str, topiclist_data: dict)pywikibot.flow.Topic[source]

Create a Topic object from API data.

Parameters
  • board – The topic’s parent Flow board

  • root_uuid – The UUID of the topic and its root post

  • topiclist_data – The data returned by view-topiclist

Returns

A Topic object derived from the supplied data

Raises
  • TypeError – any passed parameters have wrong types

  • ValueError – the passed topiclist_data is missing required data

hide(reason: str)None[source]

Hide this topic.

Parameters

reason – The reason for hiding this topic.

property is_locked

Whether this topic is locked.

property is_moderated

Whether this topic is moderated.

lock(reason: str)None[source]

Lock this topic.

Parameters

reason – The reason for locking this topic

replies(content_format: str = 'wikitext', force: bool = False, format='[deprecated name of content_format]')[source]

A list of replies to this topic’s root post.

Parameters
  • content_format – Content format to return contents in; must be ‘wikitext’, ‘html’, or ‘fixed-html’

  • force – Whether to reload from the API instead of using the cache

Returns

The replies of this topic’s root post

reply(content: str, content_format: str = 'wikitext', format='[deprecated name of content_format]')[source]

A convenience method to reply to this topic’s root post.

Parameters
  • content – The content of the new post

  • content_format – The format of the given content; must be ‘wikitext’ or ‘html’)

Returns

The new reply to this topic’s root post

restore(reason: str)None[source]

Restore this topic.

Parameters

reason – The reason for restoring this topic.

property root

The root post of this topic.

suppress(reason: str)None[source]

Suppress this topic.

Parameters

reason – The reason for suppressing this topic.

unlock(reason: str)None[source]

Unlock this topic.

Parameters

reason – The reason for unlocking this topic

pywikibot.i18n module

Various i18n functions.

Helper functions for both the internal localization system and for TranslateWiki-based translations.

By default messages are assumed to reside in a package called ‘scripts.i18n’. In pywikibot 3+, that package is not packaged with pywikibot, and pywikibot 3+ does not have a hard dependency on any i18n messages. However, there are three user input questions in pagegenerators which will use i18n messages if they can be loaded.

The default message location may be changed by calling set_message_package with a package name. The package must contain an __init__.py, and a message bundle called ‘pywikibot’ containing messages. See twtranslate for more information on the messages.

pywikibot.i18n.input(twtitle: str, parameters: Optional[collections.abc.Mapping] = None, password: bool = False, fallback_prompt: Optional[str] = None)str[source]

Ask the user a question, return the user’s answer.

The prompt message is retrieved via twtranslate and uses the config variable ‘userinterface_lang’.

Parameters
  • twtitle – The TranslateWiki string title, in <package>-<key> format

  • parameters – The values which will be applied to the translated text

  • password – Hides the user’s input (for password entry)

  • fallback_prompt – The English prompt if i18n is not available.

pywikibot.i18n.messages_available()bool[source]

Return False if there are no i18n messages available.

To determine if messages are available, it looks for the package name set using set_messages_package for a message bundle called ‘pywikibot’ containing messages.

pywikibot.i18n.set_messages_package(package_name: str)[source]

Set the package name where i18n messages are located.

pywikibot.i18n.translate(code, xdict: Union[dict, str], parameters: Optional[collections.abc.Mapping] = None, fallback=False)str[source]

Return the most appropriate localization from a localization dict.

Given a site code and a dictionary, returns the dictionary’s value for key ‘code’ if this key exists; otherwise tries to return a value for an alternative code that is most applicable to use on the wiki in language ‘code’ except fallback is False.

The code itself is always checked first, then these codes that have been defined to be alternatives, and finally English.

If fallback is False and the code is not found in the

For PLURAL support have a look at the twtranslate method.

Parameters
  • code (str or Site object) – The site code as string or Site object. If xdict is an extended dictionary the Site object should be used in favour of the code string. Otherwise localizations from a wrong family might be used.

  • xdict – dictionary with language codes as keys or extended dictionary with family names as keys containing code dictionaries or a single string. May contain PLURAL tags as described in twtranslate

  • parameters – For passing (plural) parameters

  • fallback (boolean or iterable) – Try an alternate language code. If it’s iterable it’ll also try those entries and choose the first match.

Returns

the localized string

Raises
  • IndexError – If the language supports and requires more plurals than defined for the given PLURAL pattern.

  • KeyError – No fallback key found if fallback is not False

pywikibot.i18n.twget_keys(twtitle: str)list[source]

Return all language codes for a special message.

Parameters

twtitle – The TranslateWiki string title, in <package>-<key> format

Raises

OSError – the package i18n cannot be loaded

pywikibot.i18n.twhas_key(source, twtitle: str, code='[deprecated name of source]')[source]

Check if a message has a translation in the specified language code.

The translations are retrieved from i18n.<package>, based on the callers import table.

No code fallback is made.

Parameters
  • source (BaseSite or str) – When it’s a site it’s using the lang attribute and otherwise it is using the value directly.

  • twtitle – The TranslateWiki string title, in <package>-<key> format

pywikibot.i18n.twtranslate(source, twtitle: str, parameters: Optional[collections.abc.Mapping] = None, code='[deprecated name of source]', *, fallback: bool = True, fallback_prompt: Optional[str] = None, only_plural: bool = False)[source]

Translate a message using JSON files in messages_package_name.

fallback parameter must be True for i18n and False for L10N or testing purposes.

Support for plural is implemented like in MediaWiki extension. If the TranslateWiki message contains a plural tag inside which looks like:

{{PLURAL:<number>|<variant1>|<variant2>[|<variantn>]}}

it takes that variant calculated by the plural_rules depending on the number value. Multiple plurals are allowed.

As an examples, if we had several json dictionaries in test folder like:

en.json:

{
   "test-plural": "Bot: Changing %(num)s {{PLURAL:%(num)d|page|pages}}.",
}

fr.json:

{
   "test-plural": \
   "Robot: Changer %(descr)s {{PLURAL:num|une page|quelques pages}}.",
}

and so on.

>>> # this code snippet is running in test environment
>>> # ignore test message "tests: max_retries reduced from 15 to 1"
>>> import os
>>> os.environ['PYWIKIBOT_TEST_QUIET'] = '1'
>>> from pywikibot import i18n
>>> i18n.set_messages_package('tests.i18n')
>>> # use a dictionary
>>> str(i18n.twtranslate('en', 'test-plural', {'num':2}))
'Bot: Changing 2 pages.'
>>> # use additional format strings
>>> str(i18n.twtranslate(
...    'fr', 'test-plural', {'num': 1, 'descr': 'seulement'}))
'Robot: Changer seulement une page.'
>>> # use format strings also outside
>>> str(i18n.twtranslate(
...    'fr', 'test-plural', {'num': 10}, only_plural=True
... ) % {'descr': 'seulement'})
'Robot: Changer seulement quelques pages.'
Parameters
  • source (BaseSite or str) – When it’s a site it’s using the lang attribute and otherwise it is using the value directly.

  • twtitle – The TranslateWiki string title, in <package>-<key> format

  • parameters – For passing parameters. It should be a mapping but for backwards compatibility can also be a list, tuple or a single value. They are also used for plural entries in which case they must be a Mapping and will cause a TypeError otherwise.

  • fallback – Try an alternate language code

  • fallback_prompt – The English message if i18n is not available

  • only_plural – Define whether the parameters should be only applied to plural instances. If this is False it will apply the parameters also to the resulting string. If this is True the placeholders must be manually applied afterwards.

Raises

IndexError – If the language supports and requires more plurals than defined for the given translation template.

pywikibot.interwiki_graph module

Module with the Graphviz drawing calls.

class pywikibot.interwiki_graph.GraphDrawer(subject: pywikibot.interwiki_graph.Subject)[source]

Bases: object

Graphviz (dot) code creator.

Parameters

subject – page data to graph

:raises ImportError if pydot is not installed

addDirectedEdge(page: pywikibot.page.Page, refPage: pywikibot.page.Page)None[source]

Add a directed edge from refPage to page.

addNode(page: pywikibot.page.Page)None[source]

Add a node for page.

createGraph()None[source]

Create graph of the interwiki links.

For more info see https://meta.wikimedia.org/wiki/Interwiki_graphs

getLabel(page: pywikibot.page.Page)str[source]

Get label for page.

saveGraphFile()None[source]

Write graphs to the data directory.

class pywikibot.interwiki_graph.GraphSavingThread(graph: pydot.Dot, origin: pywikibot.page.Page)[source]

Bases: threading.Thread

Threaded graph renderer.

Rendering a graph can take extremely long. We use multithreading because of that.

TODO: Find out if several threads running in parallel can slow down the system too much. Consider adding a mechanism to kill a thread if it takes too long.

run()None[source]

Write graphs to the data directory.

class pywikibot.interwiki_graph.Subject(origin: Optional[Any] = None)[source]

Bases: object

Data about a page with translations on multiple wikis.

Parameters

origin – the page on the ‘origin’ wiki

property origin

Page on the origin wiki.

pywikibot.interwiki_graph.getFilename(page: pywikibot.page.Page, extension: Optional[str] = None)str[source]

Create a filename that is unique for the page.

Parameters
  • page – page used to create the new filename

  • extension – file extension

Returns

filename of <family>-<lang>-<page>.<ext>

pywikibot.logentries module

Objects representing Mediawiki log entries.

class pywikibot.logentries.BlockEntry(apidata, site)[source]

Bases: pywikibot.logentries.LogEntry

Block or unblock log entry.

It might contain a block or unblock depending on the action. The duration, expiry and flags are not available on unblock log entries.

duration()[source]

Return a datetime.timedelta representing the block duration.

Returns

datetime.timedelta, or None if block is indefinite.

expiry()[source]

Return a Timestamp representing the block expiry date.

Return type

pywikibot.Timestamp or None

flags()list[source]

Return a list of (str) flags associated with the block entry.

It raises an Error if the entry is an unblocking log entry.

Returns

list of flags strings

page()[source]

Return the blocked account or IP.

Returns

the Page object of username or IP if this block action targets a username or IP, or the blockid if this log reflects the removal of an autoblock

Return type

pywikibot.Page or int

class pywikibot.logentries.LogEntry(apidata, site)[source]

Bases: collections.UserDict

Generic log entry.

LogEntry parameters may be retrieved by the corresponding method or the LogEntry key. The following statements are equivalent:

action = logentry.action() action = logentry[‘action’] action = logentry.data[‘action’]

Initialize object from a logevent dict returned by MW API.

page()[source]

Page on which action was performed.

Returns

page on action was performed

Return type

pywikibot.Page

timestamp()[source]

Timestamp object corresponding to event timestamp.

class pywikibot.logentries.LogEntryFactory(site, logtype=None)[source]

Bases: object

LogEntry Factory.

Only available method is create()

Parameters
  • site (BaseSite) – The site on which the log entries are created.

  • logtype ((letype) str : move/block/patrol/etc...) – The log type of the log entries, if known in advance. If None, the Factory will fetch the log entry from the data to create each object.

create(logdata)[source]

Instantiate the LogEntry object representing logdata.

Parameters

logdata (dict) – <item> returned by the api

Returns

LogEntry object representing logdata

classmethod get_entry_class(logtype)[source]

Return the class corresponding to the @logtype string parameter.

Returns

specified subclass of LogEntry

Return type

LogEntry

Note

this class method cannot verify whether the given logtype already exits for a given site; to verify use Site.logtypes or use the get_valid_entry_class instance method instead.

get_valid_entry_class(logtype)[source]

Return the class corresponding to the @logtype string parameter.

Returns

specified subclass of LogEntry

Return type

LogEntry

Raises

KeyError – logtype is not valid

class pywikibot.logentries.MoveEntry(apidata, site)[source]

Bases: pywikibot.logentries.LogEntry

Move log entry.

Initialize object from a logevent dict returned by MW API.

suppressedredirect()bool[source]

Return True if no redirect was created during the move.

property target_ns

Return namespace object of target page.

property target_page

Return target page object.

Return type

pywikibot.Page

property target_title

Return the target title.

class pywikibot.logentries.OtherLogEntry(apidata, site)[source]

Bases: pywikibot.logentries.LogEntry

A log entry class for unspecified log events.

Initialize object from a logevent dict returned by MW API.

class pywikibot.logentries.PatrolEntry(apidata, site)[source]

Bases: pywikibot.logentries.LogEntry

Patrol log entry.

Initialize object from a logevent dict returned by MW API.

property auto

Return auto patrolled.

property current_id

Return the current id.

property previous_id

Return the previous id.

class pywikibot.logentries.RightsEntry(apidata, site)[source]

Bases: pywikibot.logentries.LogEntry

Rights log entry.

Initialize object from a logevent dict returned by MW API.

property newgroups

Return new rights groups.

property oldgroups

Return old rights groups.

class pywikibot.logentries.UploadEntry(apidata, site)[source]

Bases: pywikibot.logentries.LogEntry

Upload log entry.

Initialize object from a logevent dict returned by MW API.

page()[source]

Return FilePage on which action was performed.

Return type

pywikibot.FilePage

class pywikibot.logentries.UserTargetLogEntry(apidata, site)[source]

Bases: pywikibot.logentries.LogEntry

A log entry whose target is a user page.

Initialize object from a logevent dict returned by MW API.

page()[source]

Return the target user.

This returns a User object instead of the Page object returned by the superclass method.

Returns

target user

Return type

pywikibot.User

pywikibot.logging module

Logging functions.

pywikibot.logging.add_init_routine(routine: collections.abc.Callable[Any])None[source]

Add a routine to be run as soon as possible.

pywikibot.logging.critical(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a critical record to the user via the userinterface.

Parameters
  • text – the critical message which is to be displayed to the user.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.logging.debug(text: AnyStr, layer: str, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a debug record to the log file.

Parameters
  • text – the message of the debug record to be logged to the log file.

  • layer – logger to record this message upon

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

  • layer – The name of the logger that text will be sent to.

pywikibot.logging.error(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output an error message to the user via the userinterface.

Parameters
  • text – the message containing the error which occurred.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.logging.exception(msg: Optional[Exception] = None, decoder: Optional[str] = None, newline: bool = True, tb: bool = False, **kwargs: Any)None[source]

Output an error traceback to the user via the userinterface.

Use directly after an ‘except’ statement:

...
except Exception:
    pywikibot.exception()
...

or alternatively:

...
except Exception as e:
    pywikibot.exception(e)
...

This function should only be called from an Exception handler.

Parameters
  • msg – If not None, contains the description of the exception that occurred.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

  • tb – Set to True in order to output traceback also.

pywikibot.logging.log(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a record to the log file.

Parameters
  • text – the message which is to be logged to the log file.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.logging.logoutput(text: Any, decoder: Optional[str] = None, newline: bool = True, _level: int = 20, _logger: str = '', **kwargs: Any)None[source]

Format output and send to the logging module.

Helper function used by all the user-output convenience functions.

pywikibot.logging.output(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a message to the user via the userinterface.

Works like print, but uses the encoding used by the user’s console (console_encoding in the configuration file) instead of ASCII.

If decoder is None, text should be a unicode string. Otherwise it should be encoded in the given encoding.

If newline is True, a line feed will be added after printing the text.

text can contain special sequences to create colored output. These consist of the escape character 03 and the color name in curly braces, e. g. 03{lightpurple}. 03{default} resets the color. By using the color_format method from pywikibot.tools.formatter, the escape character may be omitted.

Other keyword arguments are passed unchanged to the logger; so far, the only argument that is useful is “exc_info=True”, which causes the log message to include an exception traceback.

pywikibot.logging.stdout(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output script results to the user via the userinterface.

The text will be sent to standard output, so that it can be piped to another process. All other text will be sent to stderr. See: https://en.wikipedia.org/wiki/Pipeline_%28Unix%29

Parameters
  • text – the message printed via stdout logger to the user.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.logging.warning(text: AnyStr, decoder: Optional[str] = None, newline: bool = True, **kwargs: Any)None[source]

Output a warning message to the user via the userinterface.

Parameters
  • text – the message the user wants to display.

  • decoder – If None, text should be a unicode string else it should be encoded in the given encoding.

  • newline – If True, a line feed will be added after printing the text.

  • kwargs – The keyword arguments can be found in the python doc: https://docs.python.org/3/howto/logging-cookbook.html

pywikibot.login module

Library to log the bot in to a wiki account.

class pywikibot.login.BotPassword(suffix: str, password: str)[source]

Bases: object

BotPassword object for storage in password file.

BotPassword function by using a separate password paired with a suffixed username of the form <username>@<suffix>.

Parameters
  • suffix – Suffix of the login name

  • password – bot password

Raises

_PasswordFileWarning – suffix improperly specified

login_name(username: str)str[source]

Construct the login name from the username and suffix.

Parameters

user – username (without suffix)

class pywikibot.login.LoginManager(password: Optional[str] = None, site: Any = None, user: Optional[str] = None, username='[deprecated name of user]', verbose=NotImplemented, sysop=NotImplemented)[source]

Bases: object

Site login manager.

All parameters default to defaults in user-config.

Parameters
  • site – Site object to log into

  • user – username to use. If user is None, the username is loaded from config.usernames.

  • password – password to use

Raises

pywikibot.exceptions.NoUsernameError – No username is configured for the requested site.

botAllowed()bool[source]

Check whether the bot is listed on a specific page.

This allows bots to comply with the policy on the respective wiki.

check_user_exists()None[source]

Check that the username exists on the site.

See

https://www.mediawiki.org/wiki/API:Users

Raises

pywikibot.exceptions.NoUsernameError – Username doesn’t exist in user list.

login(retry: bool = False, autocreate: bool = False)bool[source]

Attempt to log into the server.

See

https://www.mediawiki.org/wiki/API:Login

Parameters
  • retry – infinitely retry if the API returns an unknown error

  • autocreate – if true, allow auto-creation of the account using unified login

Raises

pywikibot.exceptions.NoUsernameError – Username is not recognised by the site.

login_to_site()None[source]

Login to the site.

readPassword()None[source]

Read passwords from a file.

DO NOT FORGET TO REMOVE READ ACCESS FOR OTHER USERS!!! Use chmod 600 password-file.

All lines below should be valid Python tuples in the form (code, family, username, password), (family, username, password) or (username, password) to set a default password for an username. The last matching entry will be used, so default usernames should occur above specific usernames.

For BotPasswords the password should be given as a BotPassword object.

The file must be either encoded in ASCII or UTF-8.

Example:

('my_username', 'my_default_password')
('wikipedia', 'my_wikipedia_user', 'my_wikipedia_pass')
('en', 'wikipedia', 'my_en_wikipedia_user', 'my_en_wikipedia_pass')
('my_username', BotPassword(
 'my_BotPassword_suffix', 'my_BotPassword_password'))
storecookiedata()None[source]

Store cookie data.

class pywikibot.login.LoginStatus(value)[source]

Bases: enum.IntEnum

Enum for Login statuses.

>>> LoginStatus.NOT_ATTEMPTED
LoginStatus(-3)
>>> LoginStatus.IN_PROGRESS.value
-2
>>> LoginStatus.NOT_LOGGED_IN.name
'NOT_LOGGED_IN'
>>> int(LoginStatus.AS_USER)
0
>>> LoginStatus(-3).name
'NOT_ATTEMPTED'
>>> LoginStatus(0).name
'AS_USER'
AS_USER = 0
IN_PROGRESS = -2
NOT_ATTEMPTED = -3
NOT_LOGGED_IN = -1
class pywikibot.login.OauthLoginManager(password: Optional[str] = None, site: Any = None, user: Optional[str] = None, sysop=NotImplemented)[source]

Bases: pywikibot.login.LoginManager

Site login manager using OAuth.

All parameters default to defaults in user-config.

Parameters
  • site – Site object to log into

  • user – consumer key

  • password – consumer secret

Raises
property access_token

Return OAuth access key token and secret token.

See

https://www.mediawiki.org/wiki/API:Tokens

property consumer_token

Return OAuth consumer key token and secret token.

See

https://www.mediawiki.org/wiki/API:Tokens

property identity

Get identifying information about a user via an authorized token.

login(retry: bool = False, force: bool = False)bool[source]

Attempt to log into the server.

See

https://www.mediawiki.org/wiki/API:Login

Parameters
  • retry – infinitely retry if exception occurs during authentication.

  • force – force to re-authenticate

pywikibot.pagegenerators module

This module offers a wide variety of page generators.

A page generator is an object that is iterable (see https://legacy.python.org/dev/peps/pep-0255/ ) and that yields page objects on which other scripts can then work.

Pagegenerators.py cannot be run as script. For testing purposes listpages.py can be used instead, to print page titles to standard output.

These parameters are supported to specify which pages titles to print:

GENERATOR OPTIONS

-cat

Work on all pages which are in a specific category. Argument can also be given as “-cat:categoryname” or as “-cat:categoryname|fromtitle” (using # instead of | is also allowed in this one and the following)

-catr

Like -cat, but also recursively includes pages in subcategories, sub-subcategories etc. of the given category. Argument can also be given as “-catr:categoryname” or as “-catr:categoryname|fromtitle”.

-subcats

Work on all subcategories of a specific category. Argument can also be given as “-subcats:categoryname” or as “-subcats:categoryname|fromtitle”.

-subcatsr

Like -subcats, but also includes sub-subcategories etc. of the given category. Argument can also be given as “-subcatsr:categoryname” or as “-subcatsr:categoryname|fromtitle”.

-uncat

Work on all pages which are not categorised.

-uncatcat

Work on all categories which are not categorised.

-uncatfiles

Work on all files which are not categorised.

-file

Read a list of pages to treat from the named text file. Page titles in the file may be either enclosed with [[brackets]], or be separated by new lines. Argument can also be given as “-file:filename”.

-filelinks

Work on all pages that use a certain image/media file. Argument can also be given as “-filelinks:filename”.

-search

Work on all pages that are found in a MediaWiki search across all namespaces.

-logevents

Work on articles that were on a specified Special:Log. The value may be a comma separated list of these values:

logevent,username,start,end

or for backward compatibility:

logevent,username,total

Note: ‘start’ is the most recent date and log events are iterated from present to past. If ‘start’’ is not provided, it means ‘now’; if ‘end’ is not provided, it means ‘since the beginning’.

To use the default value, use an empty string. You have options for every type of logs given by the log event parameter which could be one of the following:

spamblacklist, titleblacklist, gblblock, renameuser, globalauth, gblrights, gblrename, abusefilter, massmessage, thanks, usermerge, block, protect, rights, delete, upload, move, import, patrol, merge, suppress, tag, managetags, contentmodel, review, stable, timedmediahandler, newusers

It uses the default number of pages 10.

Examples:

-logevents:move gives pages from move log (usually redirects) -logevents:delete,,20 gives 20 pages from deletion log -logevents:protect,Usr gives pages from protect log by user Usr -logevents:patrol,Usr,20 gives 20 patrolled pages by Usr -logevents:upload,,20121231,20100101 gives upload pages in the 2010s, 2011s, and 2012s -logevents:review,,20121231 gives review pages since the beginning till the 31 Dec 2012 -logevents:review,Usr,20121231 gives review pages by user Usr since the beginning till the 31 Dec 2012

In some cases it must be given as -logevents:”move,Usr,20”

-interwiki

Work on the given page and all equivalent pages in other languages. This can, for example, be used to fight multi-site spamming. Attention: this will cause the bot to modify pages on several wiki sites, this is not well tested, so check your edits!

-links

Work on all pages that are linked from a certain page. Argument can also be given as “-links:linkingpagetitle”.

-liverecentchanges

Work on pages from the live recent changes feed. If used as -liverecentchanges:x, work on x recent changes.

-imagesused

Work on all images that contained on a certain page. Can also be given as “-imagesused:linkingpagetitle”.

-newimages

Work on the most recent new images. If given as -newimages:x, will work on x newest images.

-newpages

Work on the most recent new pages. If given as -newpages:x, will work on x newest pages.

-recentchanges

Work on the pages with the most recent changes. If given as -recentchanges:x, will work on the x most recently changed pages. If given as -recentchanges:offset,duration it will work on pages changed from ‘offset’ minutes with ‘duration’ minutes of timespan. rctags are supported too. The rctag must be the very first parameter part.

Examples:

-recentchanges:20 gives the 20 most recently changed pages -recentchanges:120,70 will give pages with 120 offset minutes and 70 minutes of timespan -recentchanges:visualeditor,10 gives the 10 most recently changed pages marked with ‘visualeditor’ -recentchanges:”mobile edit,60,35” will retrieve pages marked with ‘mobile edit’ for the given offset and timespan

-unconnectedpages

Work on the most recent unconnected pages to the Wikibase repository. Given as -unconnectedpages:x, will work on the x most recent unconnected pages.

-ref

Work on all pages that link to a certain page. Argument can also be given as “-ref:referredpagetitle”.

-start

Specifies that the robot should go alphabetically through all pages on the home wiki, starting at the named page. Argument can also be given as “-start:pagetitle”.

You can also include a namespace. For example, “-start:Template:!” will make the bot work on all pages in the template namespace.

default value is start:!

-prefixindex

Work on pages commencing with a common prefix.

-transcludes

Work on all pages that use a certain template. Argument can also be given as “-transcludes:Title”.

-unusedfiles

Work on all description pages of images/media files that are not used anywhere. Argument can be given as “-unusedfiles:n” where n is the maximum number of articles to work on.

-lonelypages

Work on all articles that are not linked from any other article. Argument can be given as “-lonelypages:n” where n is the maximum number of articles to work on.

-unwatched

Work on all articles that are not watched by anyone. Argument can be given as “-unwatched:n” where n is the maximum number of articles to work on.

-property:name Work on all pages with a given property name from

Special:PagesWithProp.

-usercontribs

Work on all articles that were edited by a certain user. (Example : -usercontribs:DumZiBoT)

-weblink

Work on all articles that contain an external link to a given URL; may be given as “-weblink:url”

-withoutinterwiki

Work on all pages that don’t have interlanguage links. Argument can be given as “-withoutinterwiki:n” where n is the total to fetch.

-mysqlquery

Takes a MySQL query string like “SELECT page_namespace, page_title FROM page WHERE page_namespace = 0” and treats the resulting pages. See https://www.mediawiki.org/wiki/Manual:Pywikibot/MySQL for more details.

-sparql

Takes a SPARQL SELECT query string including ?item and works on the resulting pages.

-sparqlendpoint

Specify SPARQL endpoint URL (optional). (Example : -sparqlendpoint:http://myserver.com/sparql)

-searchitem

Takes a search string and works on Wikibase pages that contain it. Argument can be given as “-searchitem:text”, where text is the string to look for, or “-searchitem:lang:text”, where lang is the language to search items in.

-wantedpages

Work on pages that are linked, but do not exist; may be given as “-wantedpages:n” where n is the maximum number of articles to work on.

-wantedcategories

Work on categories that are used, but do not exist; may be given as “-wantedcategories:n” where n is the maximum number of categories to work on.

-wantedfiles

Work on files that are used, but do not exist; may be given as “-wantedfiles:n” where n is the maximum number of files to work on.

-wantedtemplates

Work on templates that are used, but do not exist; may be given as “-wantedtemplates:n” where n is the maximum number of templates to work on.

-random

Work on random pages returned by [[Special:Random]]. Can also be given as “-random:n” where n is the number of pages to be returned.

-randomredirect

Work on random redirect pages returned by [[Special:RandomRedirect]]. Can also be given as “-randomredirect:n” where n is the number of pages to be returned.

-google

Work on all pages that are found in a Google search. You need a Google Web API license key. Note that Google doesn’t give out license keys anymore. See google_key in config.py for instructions. Argument can also be given as “-google:searchstring”.

-page

Work on a single page. Argument can also be given as “-page:pagetitle”, and supplied multiple times for multiple pages.

-pageid

Work on a single pageid. Argument can also be given as “-pageid:pageid1,pageid2,.” or “-pageid:’pageid1|pageid2|..’” and supplied multiple times for multiple pages.

-linter

Work on pages that contain lint errors. Extension Linter must be available on the site. -linter select all categories. -linter:high, -linter:medium or -linter:low select all categories for that prio. Single categories can be selected with commas as in -linter:cat1,cat2,cat3

Adding ‘/int’ identifies Lint ID to start querying from: e.g. -linter:high/10000

-linter:show just shows available categories.

-querypage:name Work on pages provided by a QueryPage-based special page,

see https://www.mediawiki.org/wiki/API:Querypage. (tip: use -limit:n to fetch only n pages).

-querypage shows special pages available.

-url

Read a list of pages to treat from the provided URL. The URL must return text in the same format as expected for the -file argument, e.g. page titles separated by newlines or enclosed in brackets.

FILTER OPTIONS

-catfilter

Filter the page generator to only yield pages in the specified category. See -cat generator for argument format.

-grep

A regular expression that needs to match the article otherwise the page won’t be returned. Multiple -grep:regexpr can be provided and the page will be returned if content is matched by any of the regexpr provided. Case insensitive regular expressions will be used and dot matches any character, including a newline.

-grepnot

Like -grep, but return the page only if the regular expression does not match.

-intersect

Work on the intersection of all the provided generators.

-limit

When used with any other argument -limit:n specifies a set of pages, work on no more than n pages in total.

-namespaces

Filter the page generator to only yield pages in the

-namespace

specified namespaces. Separate multiple namespace

-ns

numbers or names with commas.

Examples:

-ns:0,2,4 -ns:Help,MediaWiki

You may use a preleading “not” to exclude the namespace.

Examples:

-ns:not:2,3 -ns:not:Help,File

If used with -newpages/-random/-randomredirect/linter generators, -namespace/ns must be provided before -newpages/-random/-randomredirect/linter. If used with -recentchanges generator, efficiency is improved if -namespace is provided before -recentchanges.

If used with -start generator, -namespace/ns shall contain only one value.

-onlyif

A claim the page needs to contain, otherwise the item won’t be returned. The format is property=value,qualifier=value. Multiple (or none) qualifiers can be passed, separated by commas.

Examples:

P1=Q2 (property P1 must contain value Q2), P3=Q4,P5=Q6,P6=Q7 (property P3 with value Q4 and qualifiers: P5 with value Q6 and P6 with value Q7). Value can be page ID, coordinate in format: latitude,longitude[,precision] (all values are in decimal degrees), year, or plain string. The argument can be provided multiple times and the item page will be returned only if all claims are present. Argument can be also given as “-onlyif:expression”.

-onlyifnot

A claim the page must not contain, otherwise the item won’t be returned. For usage and examples, see -onlyif above.

-ql

Filter pages based on page quality. This is only applicable if contentmodel equals ‘proofread-page’, otherwise has no effects. Valid values are in range 0-4. Multiple values can be comma-separated.

-subpage

-subpage:n filters pages to only those that have depth n i.e. a depth of 0 filters out all pages that are subpages, and a depth of 1 filters out all pages that are subpages of subpages.

-titleregex

A regular expression that needs to match the article title otherwise the page won’t be returned. Multiple -titleregex:regexpr can be provided and the page will be returned if title is matched by any of the regexpr provided. Case insensitive regular expressions will be used and dot matches any character.

-titleregexnot

Like -titleregex, but return the page only if the regular expression does not match.

pywikibot.pagegenerators.AncientPagesPageGenerator(total: int = 100, site=None, number='[deprecated name of total]', repeat=NotImplemented)[source]

Ancient page generator.

Parameters
  • total – Maximum number of pages to retrieve in total

  • site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.pagegenerators.CategorizedPageGenerator(category, recurse=False, start=None, total=None, content=False, namespaces=None, step=NotImplemented)[source]

Yield all pages in a specific category.

If recurse is True, pages in subcategories are included as well; if recurse is an int, only subcategories to that depth will be included (e.g., recurse=2 will get pages in subcats and sub-subcats, but will not go any further).

If start is a string value, only pages whose sortkey comes after start alphabetically are included.

If content is True (default is False), the current page text of each retrieved page will be downloaded.

pywikibot.pagegenerators.CategoryFilterPageGenerator(generator, category_list, site=NotImplemented)[source]

Wrap a generator to filter pages by categories specified.

Parameters
  • generator – A generator object

  • category_list (list of category objects) – categories used to filter generated pages

pywikibot.pagegenerators.DayPageGenerator(start_month: int = 1, end_month: int = 12, site=None, year: int = 2000, startMonth='[deprecated name of start_month]', endMonth='[deprecated name of end_month]')[source]

Day page generator.

Parameters
pywikibot.pagegenerators.DequePreloadingGenerator(generator, groupsize=50, step='[deprecated name of groupsize]')[source]

Preload generator of type DequeGenerator.

pywikibot.pagegenerators.EdittimeFilterPageGenerator(generator, last_edit_start=None, last_edit_end=None, first_edit_start=None, first_edit_end=None, show_filtered=False, begintime='[deprecated name of last_edit_start]', endtime='[deprecated name of last_edit_end]')[source]

Wrap a generator to filter pages outside last or first edit range.

Parameters
  • generator – A generator object

  • last_edit_start (datetime) – Only yield pages last edited after this time

  • last_edit_end (datetime) – Only yield pages last edited before this time

  • first_edit_start (datetime) – Only yield pages first edited after this time

  • first_edit_end (datetime) – Only yield pages first edited before this time

  • show_filtered (bool) – Output a message for each page not yielded

class pywikibot.pagegenerators.GeneratorFactory(site=None, positional_arg_name: Optional[str] = None, enabled_options: Optional[collections.abc.Iterable[str]] = None, disabled_options: Optional[collections.abc.Iterable[str]] = None)[source]

Bases: object

Process command line arguments and return appropriate page generator.

This factory is responsible for processing command line arguments that are used by many scripts and that determine which pages to work on.

Note

GeneratorFactory must be instantiated after global arguments are parsed except if site parameter is given.

Parameters
  • site (pywikibot.site.BaseSite) – Site for generator results

  • positional_arg_name – generator to use for positional args, which do not begin with a hyphen

  • enabled_options – only enable options given by this Iterable. This is priorized over disabled_options

  • disabled_options – disable these given options and let them be handled by scripts options handler

getCategory(category: str, arg='[deprecated name of category]')[source]

Return Category and start as defined by category.

Parameters

category – category name with start parameter

getCategoryGen(category: str, recurse: bool = False, content: bool = False, gen_func=None, arg='[deprecated name of category]')[source]

Return generator based on Category defined by category and gen_func.

Parameters

category – category name with start parameter

Return type

generator

getCombinedGenerator(gen=None, preload=False)[source]

Return the combination of all accumulated generators.

Only call this after all arguments have been parsed.

Parameters
  • gen (iterator) – Another generator to be combined with

  • preload (bool) – preload pages using PreloadingGenerator unless self.nopreload is True

handle_arg(arg: str)bool[source]

Parse one argument at a time.

If it is recognized as an argument that specifies a generator, a generator is created and added to the accumulation list, and the function returns true. Otherwise, it returns false, so that caller can try parsing the argument. Call getCombinedGenerator() after all arguments have been parsed to get the final output generator.

Renamed in version 6.0.

Parameters

arg – Pywikibot argument consisting of -name:value

Returns

True if the argument supplied was recognised by the factory

handle_args(args: collections.abc.Iterable[str])list[source]

Handle command line arguments and return the rest as a list.

New in version 6.0.

property namespaces

List of Namespace parameters.

Converts int or string namespaces to Namespace objects and change the storage to immutable once it has been accessed.

The resolving and validation of namespace command line arguments is performed in this method, as it depends on the site property which is lazy loaded to avoid being cached before the global arguments are handled.

Returns

namespaces selected using arguments

Return type

list of Namespace

Raises
  • KeyError – a namespace identifier was not resolved

  • TypeError – a namespace identifier has an inappropriate type such as NoneType or bool

property site

Generator site.

The generator site should not be accessed until after the global arguments have been handled, otherwise the default Site may be changed by global arguments, which will cause this cached value to be stale.

Returns

Site given to initializer, otherwise the default Site at the time this property is first accessed.

Return type

pywikibot.site.BaseSite

class pywikibot.pagegenerators.GoogleSearchPageGenerator(query=None, site=None)[source]

Bases: object

Page generator using Google search results.

To use this generator, you need to install the package ‘google’:

https://pypi.org/project/google

This package has been available since 2010, hosted on GitHub since 2012, and provided by PyPI since 2013.

As there are concerns about Google’s Terms of Service, this generator prints a warning for each query.

Parameters

site (pywikibot.site.BaseSite) – Site for generator results.

queryGoogle(query)[source]

Perform a query using python package ‘google’.

The terms of service as at June 2014 give two conditions that may apply to use of search:

  1. Don’t access [Google Services] using a method other than the interface and the instructions that [they] provide.

  2. Don’t remove, obscure, or alter any legal notices displayed in or along with [Google] Services.

Both of those issues should be managed by the package ‘google’, however Pywikibot will at least ensure the user sees the TOS in order to comply with the second condition.

pywikibot.pagegenerators.InterwikiPageGenerator(page)[source]

Iterate over all interwiki (non-language) links on a page.

class pywikibot.pagegenerators.ItemClaimFilter[source]

Bases: object

Item claim filter.

classmethod filter(generator, prop: str, claim, qualifiers: Optional[dict] = None, negate: bool = False)[source]

Yield all ItemPages which contain certain claim in a property.

Parameters
  • prop – property id to check

  • claim – value of the property to check. Can be exact value (for instance, ItemPage instance) or a string (e.g. ‘Q37470’).

  • qualifiers – dict of qualifiers that must be present, or None if qualifiers are irrelevant

  • negate – true if pages that do not contain specified claim should be yielded, false otherwise

page_classes = {False: <class 'pywikibot.page.ItemPage'>, True: <class 'pywikibot.page.PropertyPage'>}
pywikibot.pagegenerators.ItemClaimFilterPageGenerator(generator, prop: str, claim, qualifiers: Optional[dict] = None, negate: bool = False)

Yield all ItemPages which contain certain claim in a property.

Parameters
  • prop – property id to check

  • claim – value of the property to check. Can be exact value (for instance, ItemPage instance) or a string (e.g. ‘Q37470’).

  • qualifiers – dict of qualifiers that must be present, or None if qualifiers are irrelevant

  • negate – true if pages that do not contain specified claim should be yielded, false otherwise

pywikibot.pagegenerators.LanguageLinksPageGenerator(page, total=None, step=NotImplemented)[source]

Iterate over all interwiki language links on a page.

pywikibot.pagegenerators.LiveRCPageGenerator(site=None, total: Optional[int] = None)[source]

Yield pages from a socket.io RC stream.

Generates pages based on the EventStreams Server-Sent-Event (SSE) recent changes stream. The Page objects will have an extra property ._rcinfo containing the literal rc data. This can be used to e.g. filter only new pages. See pywikibot.comms.eventstreams.rc_listener for details on the .rcinfo format.

Parameters
  • site (pywikibot.BaseSite) – site to return recent changes for

  • total – the maximum number of changes to return

pywikibot.pagegenerators.LogeventsPageGenerator(logtype: Optional[str] = None, user: Optional[str] = None, site=None, namespace: Optional[int] = None, total: Optional[int] = None, start=None, end=None, reverse: bool = False, number='[deprecated name of total]', mode='[deprecated name of logtype]', repeat=NotImplemented)[source]

Generate Pages for specified modes of logevents.

Parameters
  • logtype – Mode of logs to retrieve

  • user – User of logs retrieved

  • site (pywikibot.site.BaseSite) – Site for generator results

  • namespace – Namespace to retrieve logs from

  • total – Maximum number of pages to retrieve in total

  • start (pywikibot.Timestamp) – Timestamp to start listing from

  • end (pywikibot.Timestamp) – Timestamp to end listing at

  • reverse – if True, start with oldest changes (default: newest)

pywikibot.pagegenerators.LongPagesPageGenerator(total: int = 100, site=None, number='[deprecated name of total]', repeat=NotImplemented)[source]

Long page generator.

Parameters
  • total – Maximum number of pages to retrieve in total

  • site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.pagegenerators.MySQLPageGenerator(query, site=None, verbose=None)[source]

Yield a list of pages based on a MySQL query.

The query should return two columns, page namespace and page title pairs from some table. An example query that yields all ns0 pages might look like:

SELECT
 page_namespace,
 page_title
FROM page
WHERE page_namespace = 0;

See https://www.mediawiki.org/wiki/Manual:Pywikibot/MySQL

Parameters
  • query – MySQL query to execute

  • site (pywikibot.site.BaseSite) – Site object

  • verbose (None or bool) – if True, print query to be executed; if None, config.verbose_output will be used.

Returns

generator which yields pywikibot.Page

pywikibot.pagegenerators.NamespaceFilterPageGenerator(generator, namespaces, site=None)[source]

A generator yielding pages from another generator in given namespaces.

If a site is provided, the namespaces are validated using the namespaces of that site, otherwise the namespaces are validated using the default site.

NOTE: API-based generators that have a “namespaces” parameter perform namespace filtering more efficiently than this generator.

Parameters
  • namespaces (iterable of str or Namespace key, or a single instance of those types.) – list of namespace identifiers to limit results

  • site (pywikibot.site.BaseSite) – Site for generator results; mandatory if namespaces contains namespace names. Defaults to the default site.

Raises
  • KeyError – a namespace identifier was not resolved

  • TypeError – a namespace identifier has an inappropriate type such as NoneType or bool, or more than one namespace if the API module does not support multiple namespaces

pywikibot.pagegenerators.NewimagesPageGenerator(total: Optional[int] = None, site=None, number='[deprecated name of total]', step=NotImplemented, repeat=NotImplemented)[source]

New file generator.

Parameters
  • total – Maximum number of pages to retrieve in total

  • site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.pagegenerators.NewpagesPageGenerator(site=None, namespaces=(0), total: Optional[int] = None, number='[deprecated name of total]', step=NotImplemented, namespace='[deprecated name of namespaces]', repeat=NotImplemented, get_redirect=NotImplemented)[source]

Iterate Page objects for all new titles in a single namespace.

Parameters
  • total – Maxmium number of pages to retrieve in total

  • site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.pagegenerators.PageClassGenerator(generator)[source]

Yield pages from another generator as Page subclass objects.

The page class type depends on the page namespace. Objects may be Category, FilePage, Userpage or Page.

pywikibot.pagegenerators.PageTitleFilterPageGenerator(generator, ignore_list: dict, ignoreList='[deprecated name of ignore_list]')[source]

Yield only those pages are not listed in the ignore list.

Parameters

ignore_list – family names are mapped to dictionaries in which language codes are mapped to lists of page titles. Each title must be a valid regex as they are compared using re.search.

pywikibot.pagegenerators.PageWithTalkPageGenerator(generator, return_talk_only=False)[source]

Yield pages and associated talk pages from another generator.

Only yields talk pages if the original generator yields a non-talk page, and does not check if the talk page in fact exists.

pywikibot.pagegenerators.PagesFromTitlesGenerator(iterable, site=None)[source]

Generate pages from the titles (strings) yielded by iterable.

Parameters

site (pywikibot.site.BaseSite) – Site for generator results.

class pywikibot.pagegenerators.PetScanPageGenerator(categories, subset_combination=True, namespaces=None, site=None, extra_options=None)[source]

Bases: object

Queries PetScan (https://petscan.wmflabs.org/) to generate pages.

Parameters
  • categories – List of categories to retrieve pages from (as strings)

  • subset_combination – Combination mode. If True, returns the intersection of the results of the categories, else returns the union of the results of the categories

  • namespaces – List of namespaces to search in (default is None, meaning all namespaces)

  • site – Site to operate on (default is the default site from the user config)

  • extra_options – Dictionary of extra options to use (optional)

buildQuery(categories, subset_combination, namespaces, extra_options)[source]

Get the querystring options to query PetScan.

Parameters
  • categories – List of categories (as strings)

  • subset_combination – Combination mode. If True, returns the intersection of the results of the categories, else returns the union of the results of the categories

  • namespaces – List of namespaces to search in

  • extra_options – Dictionary of extra options to use

Returns

Dictionary of querystring parameters to use in the query

query()[source]

Query PetScan.

pywikibot.pagegenerators.PrefixingPageGenerator(prefix: str, namespace=None, includeredirects: Union[None, bool, str] = True, site=None, total: int = None, content: bool = False, step=NotImplemented)[source]

Prefixed Page generator.

Parameters
  • prefix – The prefix of the pages.

  • namespace (Namespace or int) – Namespace to retrieve pages from

  • includeredirects – If includeredirects is None, False or an empty string, redirects will not be found. If includeredirects equals the string ‘only’, only redirects will be found. Otherwise redirects will be included.

  • site (pywikibot.site.BaseSite) – Site for generator results.

  • total – Maximum number of pages to retrieve in total

  • content – If True, load current version of each page (default False)

Returns

a generator that yields Page objects

Return type

generator

pywikibot.pagegenerators.PreloadingEntityGenerator(generator, groupsize: int = 50, step='[deprecated name of groupsize]')[source]

Yield preloaded pages taken from another generator.

Function basically is copied from above, but for Wikibase entities.

Parameters
  • generator (Iterable) – pages to iterate over

  • groupsize – how many pages to preload at once

pywikibot.pagegenerators.PreloadingGenerator(generator, groupsize: int = 50, pageNumber='[deprecated name of groupsize]', step='[deprecated name of groupsize]', lookahead=NotImplemented)[source]

Yield preloaded pages taken from another generator.

Parameters
  • generator – pages to iterate over

  • groupsize – how many pages to preload at once

pywikibot.pagegenerators.QualityFilterPageGenerator(generator, quality: list)[source]

Wrap a generator to filter pages according to quality levels.

This is possible only for pages with content_model ‘proofread-page’. In all the other cases, no filter is applied.

Parameters
  • generator – A generator object

  • quality – proofread-page quality levels (valid range 0-4)

pywikibot.pagegenerators.RecentChangesPageGenerator(site=None, _filter_unique=None, **kwargs)[source]

Generate pages that are in the recent changes list, including duplicates.

For parameters refer pywikibot.site.recentchanges

Parameters

site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.pagegenerators.RedirectFilterPageGenerator(generator, no_redirects: bool = True, show_filtered: bool = False)[source]

Yield pages from another generator that are redirects or not.

Parameters
  • no_redirects – Exclude redirects if True, else only include redirects.

  • show_filtered – Output a message for each page not yielded

pywikibot.pagegenerators.RegexBodyFilterPageGenerator(generator, regex, quantifier='any')

Yield pages from another generator whose body matches regex.

Uses regex option re.IGNORECASE depending on the quantifier parameter.

For parameters see titlefilter above.

class pywikibot.pagegenerators.RegexFilter[source]

Bases: object

Regex filter.

classmethod contentfilter(generator, regex, quantifier='any')[source]

Yield pages from another generator whose body matches regex.

Uses regex option re.IGNORECASE depending on the quantifier parameter.

For parameters see titlefilter above.

classmethod titlefilter(generator, regex, quantifier='any', ignore_namespace=True, inverse='[deprecated name of quantifier]')[source]

Yield pages from another generator whose title matches regex.

Uses regex option re.IGNORECASE depending on the quantifier parameter.

If ignore_namespace is False, the whole page title is compared. NOTE: if you want to check for a match at the beginning of the title, you have to start the regex with “^”

Parameters
  • generator (any generator or iterator) – another generator

  • regex (a single regex string or a list of regex strings or a compiled regex or a list of compiled regexes) – a regex which should match the page title

  • quantifier (str of ('all', 'any', 'none')) – must be one of the following values: ‘all’ - yields page if title is matched by all regexes ‘any’ - yields page if title is matched by any regexes ‘none’ - yields page if title is NOT matched by any regexes

  • ignore_namespace (bool) – ignore the namespace when matching the title

Returns

return a page depending on the matching parameters

pywikibot.pagegenerators.RegexFilterPageGenerator(generator, regex, quantifier='any', ignore_namespace=True, inverse='[deprecated name of quantifier]')

Yield pages from another generator whose title matches regex.

Uses regex option re.IGNORECASE depending on the quantifier parameter.

If ignore_namespace is False, the whole page title is compared. NOTE: if you want to check for a match at the beginning of the title, you have to start the regex with “^”

Parameters
  • generator (any generator or iterator) – another generator

  • regex (a single regex string or a list of regex strings or a compiled regex or a list of compiled regexes) – a regex which should match the page title

  • quantifier (str of ('all', 'any', 'none')) – must be one of the following values: ‘all’ - yields page if title is matched by all regexes ‘any’ - yields page if title is matched by any regexes ‘none’ - yields page if title is NOT matched by any regexes

  • ignore_namespace (bool) – ignore the namespace when matching the title

Returns

return a page depending on the matching parameters

pywikibot.pagegenerators.ShortPagesPageGenerator(total: int = 100, site=None, number='[deprecated name of total]', repeat=NotImplemented)[source]

Short page generator.

Parameters
  • total – Maximum number of pages to retrieve in total

  • site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.pagegenerators.SubCategoriesPageGenerator(category, recurse=False, start=None, total=None, content=False, step=NotImplemented)[source]

Yield all subcategories in a specific category.

If recurse is True, pages in subcategories are included as well; if recurse is an int, only subcategories to that depth will be included (e.g., recurse=2 will get pages in subcats and sub-subcats, but will not go any further).

If start is a string value, only categories whose sortkey comes after start alphabetically are included.

If content is True (default is False), the current page text of each category description page will be downloaded.

pywikibot.pagegenerators.SubpageFilterGenerator(generator, max_depth: int = 0, show_filtered: bool = False)[source]

Generator which filters out subpages based on depth.

It looks at the namespace of each page and checks if that namespace has subpages enabled. If so, pages with forward slashes (‘/’) are excluded.

Parameters
  • generator (any generator or iterator) – A generator object

  • max_depth – Max depth of subpages to yield, at least zero

  • show_filtered – Output a message for each page not yielded

pywikibot.pagegenerators.TextIOPageGenerator(source: Optional[str] = None, site: Optional[pywikibot.site._basesite.BaseSite] = None)[source]

Iterate pages from a list in a text file or on a webpage.

The text source must contain page links between double-square-brackets or, alternatively, separated by newlines. The generator will yield each corresponding Page object.

Parameters
  • source – the file path or URL that should be read. If no name is given, the generator prompts the user.

  • site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.pagegenerators.UserContributionsGenerator(username, namespaces: list = None, site=None, total: Optional[int] = None, _filter_unique=functools.partial(<function filter_unique>, key=<function <lambda>>), number='[deprecated name of total]', step=NotImplemented)[source]

Yield unique pages edited by user:username.

Parameters
  • total – Maximum number of pages to retrieve in total

  • namespaces – list of namespace numbers to fetch contribs from

  • site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.pagegenerators.UserEditFilterGenerator(generator, username: str, timestamp=None, skip: bool = False, max_revision_depth: Optional[int] = None, show_filtered: bool = False)[source]

Generator which will yield Pages modified by username.

It only looks at the last editors given by max_revision_depth. If timestamp is set in MediaWiki format JJJJMMDDhhmmss, older edits are ignored. If skip is set, pages edited by the given user are ignored otherwise only pages edited by this user are given back.

Parameters
  • generator – A generator object

  • username – user name which edited the page

  • timestamp (datetime or str (MediaWiki format JJJJMMDDhhmmss) or None) – ignore edits which are older than this timestamp

  • skip – Ignore pages edited by the given user

  • max_revision_depth – It only looks at the last editors given by max_revision_depth

  • show_filtered – Output a message for each page not yielded

pywikibot.pagegenerators.WikibaseItemFilterPageGenerator(generator, has_item: bool = True, show_filtered: bool = False)[source]

A wrapper generator used to exclude if page has a Wikibase item or not.

Parameters
  • generator (generator) – Generator to wrap.

  • has_item – Exclude pages without an item if True, or only include pages without an item if False

  • show_filtered – Output a message for each page not yielded

Returns

Wrapped generator

Return type

generator

pywikibot.pagegenerators.WikibaseItemGenerator(gen)[source]

A wrapper generator used to yield Wikibase items of another generator.

Parameters

gen (generator) – Generator to wrap.

Returns

Wrapped generator

Return type

generator

pywikibot.pagegenerators.WikibaseSearchItemPageGenerator(text: str, language: Optional[str] = None, total: Optional[int] = None, site=None)[source]

Generate pages that contain the provided text.

Parameters
  • text – Text to look for.

  • language – Code of the language to search in. If not specified, value from pywikibot.config.data_lang is used.

  • total – Maximum number of pages to retrieve in total, or None in case of no limit.

  • site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.pagegenerators.WikidataPageFromItemGenerator(gen, site)[source]

Generate pages from site based on sitelinks of item pages.

Parameters
pywikibot.pagegenerators.WikidataSPARQLPageGenerator(query, site=None, item_name: str = 'item', endpoint: Optional[str] = None, entity_url: Optional[str] = None, result_type=<class 'set'>)[source]

Generate pages that result from the given SPARQL query.

Parameters
  • query – the SPARQL query string.

  • site (pywikibot.site.BaseSite) – Site for generator results.

  • item_name – name of the item in the SPARQL query

  • endpoint – SPARQL endpoint URL

  • entity_url – URL prefix for any entities returned in a query.

  • result_type (iterable) – type of the iterable in which SPARQL results are stored (default set)

class pywikibot.pagegenerators.XMLDumpOldPageGenerator(filename: str, start: Optional[str] = None, namespaces=None, site=None, text_predicate=None, xmlFilename='[deprecated name of filename]')[source]

Bases: collections.abc.Iterator

Xml generator that yields Page objects with old text loaded.

Parameters
  • filename (str) – filename of XML dump

  • start (str or None) – skip entries below that value

  • namespaces (iterable of str or Namespace key, or a single instance of those types) – namespace filter

  • site (pywikibot.Site or None) – current site for the generator

  • text_predicate (function identifier or None) – a callable with entry.text as parameter and boolean as result to indicate the generator should return the page or not

Variables
  • text_predicate – holds text_predicate function

  • skipping – True if start parameter is given, else False

  • start – holds start parameter

  • namespaces – holds namespaces filter

  • parser – holds the xmlreader.XmlDump parse method

class pywikibot.pagegenerators.XMLDumpPageGenerator(filename: str, start: Optional[str] = None, namespaces=None, site=None, text_predicate=None, xmlFilename='[deprecated name of filename]')[source]

Bases: pywikibot.pagegenerators.XMLDumpOldPageGenerator

Xml generator that yields Page objects without text loaded.

pywikibot.pagegenerators.YearPageGenerator(start=1, end=2050, site=None)[source]

Year page generator.

Parameters

site (pywikibot.site.BaseSite) – Site for generator results.

pywikibot.plural module

Module containing plural rules of various languages.

pywikibot.plural.plural_rule(lang: str)dict[source]

Return the plural rule for a given lang.

New in version 4.3.

pywikibot.proofreadpage module

Objects used with ProofreadPage Extension.

This module includes objects:

  • ProofreadPage(Page)

  • FullHeader

  • IndexPage(Page)

OCR support of page scans via: - https://phetools.toolforge.org/hocr_cgi.py - https://phetools.toolforge.org/ocr.php - inspired by https://en.wikisource.org/wiki/MediaWiki:Gadget-ocr.js

class pywikibot.proofreadpage.FullHeader(text: Optional[str] = None)[source]

Bases: object

Header of a ProofreadPage object.

TEMPLATE_V1 = '<pagequality level="{0.ql}" user="{0.user}" /><div class="pagetext">{0.header}\n\n\n'
TEMPLATE_V2 = '<pagequality level="{0.ql}" user="{0.user}" />{0.header}'
p_header = re.compile('<pagequality level="(?P<ql>\\d)" user="(?P<user>.*?)" />(?P<has_div><div class="pagetext">)?(?P<header>.*)', re.DOTALL)
class pywikibot.proofreadpage.IndexPage(source, title: str = '')[source]

Bases: pywikibot.page.Page

Index Page page used in Mediawiki ProofreadPage extension.

Instantiate an IndexPage object.

In this class: page number is the number in the page title in the Page namespace, if the wikisource site adopts this convention (e.g. page_number is 12 for Page:Popular Science Monthly Volume 1.djvu/12) or the sequential number of the pages linked from the index section in the Index page if the index is built via transclusion of a list of pages (e.g. like on de wikisource). page label is the label associated with a page in the Index page.

This class provides methods to get pages contained in Index page, and relative page numbers and labels by means of several helper functions.

It also provides a generator to pages contained in Index page, with possibility to define range, filter by quality levels and page existence.

Raises
  • UnknownExtensionError – source Site has no ProofreadPage Extension.

  • ImportError – bs4 is not installed.

INDEX_TEMPLATE = ':MediaWiki:Proofreadpage_index_template'
get_label_from_page(*args: Any, **kwargs: Any)Any[source]
get_label_from_page_number(*args: Any, **kwargs: Any)Any[source]
get_number(*args: Any, **kwargs: Any)Any[source]
get_page(*args: Any, **kwargs: Any)Any[source]
get_page_from_label(*args: Any, **kwargs: Any)Any[source]
get_page_number_from_label(*args: Any, **kwargs: Any)Any[source]
has_valid_content()bool[source]

Test page only contains a single call to the index template.

property num_pages
page_gen(start: int = 1, end: Optional[int] = None, filter_ql: Optional[collections.abc.Sequence[int]] = None, only_existing: bool = False, content: bool = True)collections.abc.Iterable[pywikibot.page.Page][source]

Return a page generator which yields pages contained in Index page.

Range is [start … end], extremes included.

Parameters
  • start – first page, defaults to 1

  • end – num_pages if end is None

  • filter_ql – filters quality levels if None: all but ‘Without Text’.

  • only_existing – yields only existing pages.

  • content – preload content.

pages(*args: Any, **kwargs: Any)Any[source]
purge()None[source]

Overwrite purge method.

Instead of a proper purge action, use PurgeRequest, which skips the check on write rights.

save(*args: Any, **kwargs: Any)None[source]

Save page after validating the content.

Trying to save any other content fails silently with a parameterless INDEX_TEMPLATE being saved.

class pywikibot.proofreadpage.ProofreadPage(source, title: str = '')[source]

Bases: pywikibot.page.Page

ProofreadPage page used in Mediawiki ProofreadPage extension.

Instantiate a ProofreadPage object.

Raises

UnknownExtensionError – source Site has no ProofreadPage Extension.

NOT_PROOFREAD = 1
PROBLEMATIC = 2
PROOFREAD = 3
PROOFREAD_LEVELS = [0, 1, 2, 3, 4]
VALIDATED = 4
WITHOUT_TEXT = 0
property body
close_tag = '</noinclude>'
property footer
property header
property index

Get the Index page which contains ProofreadPage.

If there are many Index pages link to this ProofreadPage, and the ProofreadPage is titled Page:<index title>/<page number>, the Index page with the same title will be returned. Otherwise None is returned in the case of multiple linked Index pages.

To force reload, delete index and call it again.

Returns

the Index page for this ProofreadPage

not_proofread()None[source]

Set Page QL to “Not Proofread”.

ocr(ocr_tool: Optional[str] = None)str[source]

Do OCR of ProofreadPage scan.

The text returned by this function shall be assigned to self.body, otherwise the ProofreadPage format will not be maintained.

It is the user’s responsibility to reset quality level accordingly.

Parameters

ocr_tool – ‘phetools’ or ‘googleOCR’, default is ‘phetools’

Returns

OCR text for the page.

Raises
  • TypeError – wrong ocr_tool keyword arg.

  • ValueError – something went wrong with OCR process.

open_tag = '<noinclude>'
p_close = re.compile('(</div>|\\n\\n\\n)?</noinclude>')
p_close_no_div = re.compile('</noinclude>')
p_open = re.compile('<noinclude>')
property pre_summary

Return trailing part of edit summary.

The edit summary shall be appended to pre_summary to highlight Status in the edit summary on wiki.

problematic()None[source]

Set Page QL to “Problematic”.

proofread()None[source]

Set Page QL to “Proofread”.

property ql
property quality_level

Return the quality level of this page when it is retrieved from API.

This is only applicable if contentmodel equals ‘proofread-page’. None is returned otherwise.

This property is read-only and is applicable only when page is loaded. If quality level is overwritten during page processing, this property is no longer necessarily aligned with the new value.

In this way, no text parsing is necessary to check quality level when fetching a page.

save(*args: Any, **kwargs: Any)None[source]

Save page content after recomposing the page.

property status
property text

Override text property.

Preload text returned by EditFormPreloadText to preload non-existing pages.

property url_image

Get the file url of the scan of ProofreadPage.

Returns

file url of the scan ProofreadPage or None.

Raises
  • Exception – in case of http errors

  • ImportError – if bs4 is not installed, _bs4_soup() will raise

  • ValueError – in case of no prp_page_image src found for scan

property user
validate()None[source]

Set Page QL to “Validated”.

without_text()None[source]

Set Page QL to “Without text”.

class pywikibot.proofreadpage.PurgeRequest(**kwargs: Any)[source]

Bases: pywikibot.data.api.Request

Subclass of Request which skips the check on write rights.

Workaround for T128994. # TODO: remove once bug is fixed.

Monkeypatch action in Request initializer.

pywikibot.proofreadpage.check_if_cached(fn: collections.abc.Callable)collections.abc.Callable[source]

Decorator for IndexPage to ensure data is cached.

pywikibot.proofreadpage.decompose(fn: collections.abc.Callable)collections.abc.Callable[source]

Decorator for ProofreadPage.

Decompose text if needed and recompose text.

pywikibot.site_detect module

Classes for detecting a MediaWiki site.

class pywikibot.site_detect.MWSite(fromurl, **kwargs)[source]

Bases: object

Minimal wiki site class.

Raises
  • pywikibot.exceptions.ServerError – a server error occurred while loading the site

  • Timeout – a timeout occurred while loading the site

  • RuntimeError – Version not found or version less than 1.23

property api

Get api URL.

property iwpath

Get article path URL.

property langs

Build interwikimap.

class pywikibot.site_detect.WikiHTMLPageParser(url)[source]

Bases: html.parser.HTMLParser

Wiki HTML page parser.

handle_starttag(tag, attrs)[source]

Handle an opening tag.

set_api_url(url)[source]

Set api_url.

set_version(value)[source]

Set highest version.

pywikibot.site_detect.check_response(response)[source]

Raise ServerError if the response indicates a server error.

New in version 3.0.

pywikibot.textlib module

Functions for manipulating wiki-text.

Unless otherwise noted, all functions take a unicode string as the argument and return a unicode string.

class pywikibot.textlib.MultiTemplateMatchBuilder(site)[source]

Bases: object

Build template matcher.

pattern(template, flags=re.DOTALL)[source]

Return a compiled regex to match template.

search_any_predicate(templates)[source]

Return a predicate that matches any template.

class pywikibot.textlib.TimeStripper(site=None)[source]

Bases: object

Find timestamp in page and return it as pywikibot.Timestamp object.

fix_digits(line)[source]

Make non-latin digits like Persian to latin to parse.

timestripper(line)[source]

Find timestamp in line and convert it to time zone aware datetime.

All the following items must be matched, otherwise None is returned: -. year, month, hour, time, day, minute, tzinfo :return: A timestamp found on the given line :rtype: pywikibot.Timestamp

pywikibot.textlib.add_text(text: str, add: str, *, site=None)str[source]

Add text to a page content above categories and interwiki.

New in version 6.4.

Parameters
  • text – The page content to add text to.

  • add – Text to add.

  • site (pywikibot.Site) – The site that the text is coming from. Required for reorder of categories and interlanguage links. Te default site is used otherwise.

pywikibot.textlib.categoryFormat(categories, insite=None)str[source]

Return a string containing links to all categories in a list.

Parameters
  • categories (iterable) – A list of Category or Page objects or strings which can be either the raw name, [[Category:..]] or [[cat_localised_ns:…]].

  • insite (pywikibot.Site) – Used to to localise the category namespace.

Returns

String of categories

pywikibot.textlib.compileLinkR(withoutBracketed=False, onlyBracketed: bool = False)[source]

Return a regex that matches external links.

pywikibot.textlib.does_text_contain_section(pagetext: str, section: str)bool[source]

Determine whether the page text contains the given section title.

It does not care whether a section string may contain spaces or underlines. Both will match.

If a section parameter contains an internal link, it will match the section with or without a preceding colon which is required for a text link e.g. for categories and files.

Parameters
  • pagetext – The wikitext of a page

  • section – a section of a page including wikitext markups

pywikibot.textlib.expandmarker(text: str, marker: str = '', separator: str = '')str[source]

Return a marker expanded whitespace and the separator.

It searches for the first occurrence of the marker and gets the combination of the separator and whitespace directly before it.

Parameters
  • text – the text which will be searched.

  • marker – the marker to be searched.

  • separator – the separator string allowed before the marker. If empty it won’t include whitespace too.

Returns

the marker with the separator and whitespace from the text in front of it. It’ll be just the marker if the separator is empty.

pywikibot.textlib.extract_sections(text: str, site=None)pywikibot.textlib._Content[source]

Return section headings and contents found in text.

Returns

The returned namedtuple contains the text parsed into header, contents and footer parts: The header part is a string containing text part above the first heading. The footer part is also a string containing text part after the last section. The section part is a list of tuples, each tuple containing a string with section heading and a string with section content. Example article:

'''A''' is a thing.

== History of A ==
Some history...

== Usage of A ==
Some usage...

[[Category:Things starting with A]]

…is parsed into the following namedtuple:

result = extract_sections(text, site)
result.header = "'''A''' is a thing."
result.body = [('== History of A ==', 'Some history...'),
               ('== Usage of A ==', 'Some usage...')]
result.footer = '[[Category:Things starting with A]]'

New in version 3.0.

pywikibot.textlib.extract_templates_and_params(text: str, remove_disabled_parts: bool = False, strip: bool = False)list[source]

Return a list of templates found in text.

Return value is a list of tuples. There is one tuple for each use of a template in the page, with the template title as the first entry and a dict of parameters as the second entry. Parameters are indexed by strings; as in MediaWiki, an unnamed parameter is given a parameter name with an integer value corresponding to its position among the unnamed parameters, and if this results multiple parameters with the same name only the last value provided will be returned.

This uses the package mwparserfromhell or wikitextparser as MediaWiki markup parser. It is mandatory that one of them is installed.

There are minor differences between the two implementations.

The parser packages preserves whitespace in parameter names and values.

If there are multiple numbered parameters in the wikitext for the same position, MediaWiki will only use the last parameter value. e.g. {{a| foo | 2 <!-- --> = bar | baz }} is {{a|1=foo|2=baz}} To replicate that behaviour, enable both remove_disabled_parts and strip parameters.

Parameters
  • text – The wikitext from which templates are extracted

  • remove_disabled_parts – If enabled, remove disabled wikitext such as comments and pre.

  • strip – If enabled, strip arguments and values of templates.

Returns

list of template name and params

New in version 6.1: wikitextparser package is supported; either wikitextparser or mwparserfromhell is strictly recommended.

pywikibot.textlib.extract_templates_and_params_regex_simple(text: str)[source]

Extract top-level templates with params using only a simple regex.

This function uses only a single regex, and returns an entry for each template called at the top-level of the wikitext. Nested templates are included in the argument values of the top-level template.

This method will incorrectly split arguments when an argument value contains a ‘|’, such as {{template|a={{b|c}} }}.

Parameters

text – The wikitext from which templates are extracted

Returns

list of template name and params

Return type

list of tuple of name and OrderedDict

pywikibot.textlib.findmarker(text: str, startwith: str = '@@', append: Optional[str] = None)str[source]

Find a string which is not part of text.

Return a list of category links found in text.

Parameters

include – list of tags which should not be removed by removeDisabledParts() and where CategoryLinks can be searched.

Returns

all category links found

Return type

list of Category objects

Return a dict of inter-language links found in text.

The returned dict uses the site as keys and Page objects as values. It does not contain its own site.

Do not call this routine directly, use Page.interwiki() method instead.

pywikibot.textlib.glue_template_and_params(template_and_params)str[source]

Return wiki text of template glued from params.

You can use items from extract_templates_and_params here to get an equivalent template wiki text (it may happen that the order of the params changes).

pywikibot.textlib.interwikiFormat(links: dict, insite=None)str[source]

Convert interwiki link dict into a wikitext string.

Parameters
  • links (dict with the Site objects as keys, and Page or Link objects as values.) – interwiki links to be formatted

  • insite (BaseSite) – site the interwiki links will be formatted for (defaulting to the current site).

Returns

string including wiki links formatted for inclusion in insite

pywikibot.textlib.interwikiSort(sites, insite=None)[source]

Sort sites according to local interwiki sort logic.

pywikibot.textlib.isDisabled(text: str, index: int, tags=None)bool[source]

Return True if text[index] is disabled, e.g. by a comment or nowiki tags.

For the tags parameter, see removeDisabledParts.

pywikibot.textlib.reformat_ISBNs(text: str, match_func)str[source]

Reformat ISBNs.

Parameters
  • text – text containing ISBNs

  • match_func (callable) – function to reformat matched ISBNs

Returns

reformatted text

Return text with all category links removed.

Parameters
  • text – The text that needs to be modified.

  • site (pywikibot.Site) – The site that the text is coming from.

  • marker – If defined, marker is placed after the last category link, or at the end of text if there are no category links.

Returns

The modified text.

pywikibot.textlib.removeCategoryLinksAndSeparator(text: str, site=None, marker: str = '', separator: str = '')str[source]

Return text with category links and preceding separators removed.

Parameters
  • text – The text that needs to be modified.

  • site (pywikibot.Site) – The site that the text is coming from.

  • marker – If defined, marker is placed after the last category link, or at the end of text if there are no category links.

  • separator – The separator string that will be removed if followed by the category links.

Returns

The modified text

pywikibot.textlib.removeDisabledParts(text: str, tags=None, include=None, site=None)str[source]

Return text without portions where wiki markup is disabled.

Parts that will be removed by default are * HTML comments * nowiki tags * pre tags * includeonly tags * source and syntaxhighlight tags

Parameters
  • tags (list, set, tuple or None) – The exact set of parts which should be removed using keywords from textlib._get_regexes().

  • include (list, set, tuple or None) – Or, in alternative, default parts that shall not be removed.

  • site (pywikibot.Site) – Site to be used for site-dependent regexes. Default disabled parts listed above do not need it.

Returns

text stripped from disabled parts.

pywikibot.textlib.removeHTMLParts(text: str, keeptags: Optional[list] = None)str[source]

Return text without portions where HTML markup is disabled.

Parts that can/will be removed are – * HTML and all wiki tags

The exact set of parts which should NOT be removed can be passed as the ‘keeptags’ parameter, which defaults to [‘tt’, ‘nowiki’, ‘small’, ‘sup’].

Return text with all inter-language links removed.

If a link to an unknown language is encountered, a warning is printed.

Parameters
  • text – The text that needs to be modified.

  • site (pywikibot.Site) – The site that the text is coming from.

  • marker – If defined, marker is placed after the last language link, or at the end of text if there are no language links.

Returns

The modified text.

pywikibot.textlib.removeLanguageLinksAndSeparator(text: str, site=None, marker: str = '', separator: str = '')str[source]

Return text with inter-language links and preceding separators removed.

If a link to an unknown language is encountered, a warning is printed.

Parameters
  • text – The text that needs to be modified.

  • site (pywikibot.Site) – The site that the text is coming from.

  • marker – If defined, marker is placed after the last language link, or at the end of text if there are no language links.

  • separator – The separator string that will be removed if followed by the language links.

Returns

The modified text

pywikibot.textlib.replaceCategoryInPlace(oldtext, oldcat, newcat, site=None, add_only: bool = False)str[source]

Replace old category with new one and return the modified text.

Parameters
  • oldtext – Content of the old category

  • oldcat – pywikibot.Category object of the old category

  • newcat – pywikibot.Category object of the new category

  • add_only – If add_only is True, the old category won’t be replaced and the category given will be added after it.

Returns

the modified text

Replace all existing category links with new category links.

Parameters
  • oldtext – The text that needs to be replaced.

  • new (iterable) – Should be a list of Category objects or strings which can be either the raw name or [[Category:..]].

  • site (pywikibot.Site) – The site that the text is from.

  • addOnly – If addOnly is True, the old category won’t be deleted and the category(s) given will be added (and they won’t replace anything).

Returns

The modified text.

pywikibot.textlib.replaceExcept(text: str, old, new, exceptions: list, caseInsensitive: bool = False, allowoverlap: bool = False, marker: str = '', site=None, count: int = 0)str[source]

Return text with ‘old’ replaced by ‘new’, ignoring specified types of text.

Skips occurrences of ‘old’ within exceptions; e.g., within nowiki tags or HTML comments. If caseInsensitive is true, then use case insensitive regex matching. If allowoverlap is true, overlapping occurrences are all replaced (watch out when using this, it might lead to infinite loops!).

Parameters
  • text – text to be modified

  • old – a compiled or uncompiled regular expression

  • new – a unicode string (which can contain regular expression references), or a function which takes a match object as parameter. See parameter repl of re.sub().

  • exceptions – a list of strings or already compiled regex objects which signal what to leave out. Strings might be like [‘math’, ‘table’, ‘template’] for example.

  • marker – a string that will be added to the last replacement; if nothing is changed, it is added at the end

  • count – how many replacements to do at most. See parameter count of re.sub().

Replace inter-language links in the text with a new set of links.

Parameters
  • oldtext – The text that needs to be modified.

  • new – A dict with the Site objects as keys, and Page or Link objects as values (i.e., just like the dict returned by getLanguageLinks function).

  • site (pywikibot.Site) – The site that the text is from.

  • addOnly – If True, do not remove old language links, only add new ones.

  • template – Indicates if text belongs to a template page or not.

  • template_subpage – Indicates if text belongs to a template sub-page or not.

Returns

The modified text.

Replace wikilinks selectively.

The text is searched for a link and on each link it replaces the text depending on the result for that link. If the result is just None it skips that link. When it’s False it unlinks it and just inserts the label. When it is a Link instance it’ll use the target, section and label from that Link instance. If it’s a Page instance it’ll use just the target from the replacement and the section and label from the original link.

If it’s a string and the replacement was a sequence it converts it into a Page instance. If the replacement is done via a callable it’ll use it like unlinking and directly replace the link with the text itself. It only supports unicode when used by the callable and bytes are not allowed.

If either the section or label should be used the replacement can be a function which returns a Link instance and copies the value which should remaining.

Parameters
  • text – the text in which to replace links

  • replace (sequence of pywikibot.Page/pywikibot.Link/str or callable) – either a callable which reacts like described above. The callable must accept four parameters link, text, groups, rng and allows for user interaction. The groups are a dict containing ‘title’, ‘section’, ‘label’ and ‘linktrail’ and the rng are the start and end position of the link. The ‘label’ in groups contains everything after the first pipe which might contain additional data which is used in File namespace for example. Alternatively it can be a sequence containing two items where the first must be a Link or Page and the second has almost the same meaning as the result by the callable. It’ll convert that into a callable where the first item (the Link or Page) has to be equal to the found link and in that case it will apply the second value from the sequence.

  • site (pywikibot.site.APISite) – a Site object to use. It should match the origin or target site of the text

pywikibot.textlib.to_local_digits(phrase: Union[str, int], lang: str)str[source]

Change Latin digits based on language to localized version.

Be aware that this function only works for several languages, and that it returns an unchanged string if an unsupported language is given.

Parameters
  • phrase – The phrase to convert to localized numerical

  • lang – language code

Returns

The localized version

class pywikibot.textlib.tzoneFixedOffset(offset: int, name: str)[source]

Bases: datetime.tzinfo

Class building tzinfo objects for fixed-offset time zones.

Parameters
  • offset – a number indicating fixed offset in minutes east from UTC

  • name – a string with name of the timezone

dst(dt)[source]

Return no daylight savings time.

tzname(dt)[source]

Return the name of the timezone.

utcoffset(dt)[source]

Return the offset to UTC.

pywikibot.throttle module

Mechanics to slow down wiki read and/or write rate.

class pywikibot.throttle.ProcEntry(module_id, pid, time, site)

Bases: tuple

Create new instance of ProcEntry(module_id, pid, time, site)

module_id

Alias for field number 0

pid

Alias for field number 1

site

Alias for field number 3

time

Alias for field number 2

class pywikibot.throttle.Throttle(site, multiplydelay=NotImplemented, *, mindelay: Optional[int] = None, maxdelay: Optional[int] = None, writedelay: Optional[Union[int, float]] = None)[source]

Bases: object

Control rate of access to wiki server.

Calling this object blocks the calling thread until at least 'delay' seconds have passed since the previous call.

Each Site initiates one Throttle object (site.throttle) to control the rate of access.

checkMultiplicity()[source]

Count running processes for site and set process_multiplicity.

drop()[source]

Remove me from the list of running bot processes.

getDelay(write=False)[source]

Return the actual delay, accounting for multiple processes.

This value is the maximum wait between reads/writes, not taking into account of how much time has elapsed since the last access.

get_pid(module: str)int[source]

Get the global pid if the module is running multiple times.

lag(lagtime: Optional[float] = None)[source]

Seize the throttle lock due to server lag.

Usually the self.retry-after value from response_header of the last request if available which will be used for wait time. Otherwise lagtime from api maxlag is used. If neither self.retry_after nor lagtime is set, fallback to config.retry_wait.

If the lagtime is disproportionately high compared to self.retry_after value, the wait time will be increased.

This method is used by api.request. It will prevent any thread from accessing this site.

Parameters

lagtime – The time to wait for the next request which is the last maxlag time from api warning. This is only used as a fallback if self.retry_after isn’t set.

setDelays(delay=None, writedelay=None, absolute=False)[source]

Set the nominal delays in seconds. Defaults to config values.

wait(seconds)[source]

Wait for seconds seconds.

Announce the delay if it exceeds a preset limit.

waittime(write=False)[source]

Return waiting time in seconds.

The result is for a query that would be made right now.

pywikibot.titletranslate module

Title translate module.

pywikibot.titletranslate.translate(page=None, hints=(), auto=True, removebrackets=False, site=None, family=NotImplemented)[source]

Return a list of links to pages on other sites based on hints.

Entries for single page titles list those pages. Page titles for entries such as “all:” or “xyz:” or “20:” are first built from the page title of ‘page’ and then listed. When ‘removebrackets’ is True, a trailing pair of brackets and the text between them is removed from the page title. If ‘auto’ is true, known year and date page titles are autotranslated to all known target languages and inserted into the list.

pywikibot.version module

Module to determine the pywikibot version (tag, revision and date).

pywikibot.version.get_module_filename(module)Optional[str][source]

Retrieve filename from an imported pywikibot module.

It uses the __file__ attribute of the module. If it’s file extension ends with py and another character the last character is discarded when the py file exist.

Parameters

module (module) – The module instance.

Returns

The filename if it’s a pywikibot module otherwise None.

pywikibot.version.get_module_mtime(module)[source]

Retrieve the modification time from an imported module.

Parameters

module (module) – The module instance.

Returns

The modification time if it’s a pywikibot module otherwise None.

Return type

datetime or None

pywikibot.version.get_toolforge_hostname()Optional[str][source]

Get hostname of the current Toolforge host.

New in version 3.0.

Returns

The hostname of the currently running host, if it is in Wikimedia Toolforge; otherwise return None.

pywikibot.version.getversion(online: bool = True)str[source]

Return a pywikibot version string.

Parameters

online – Include information obtained online

pywikibot.version.getversion_git(path=None)[source]

Get version info for a Git clone.

Parameters

path – directory of the Git checkout

Returns

  • tag (name for the repository),

  • rev (current revision identifier),

  • date (date of current revision),

  • hash (git hash for the current revision)

Return type

tuple of three str and a time.struct_time

pywikibot.version.getversion_nightly(path=None)[source]

Get version info for a nightly release.

Parameters

path – directory of the uncompressed nightly.

Returns

  • tag (name for the repository),

  • rev (current revision identifier),

  • date (date of current revision),

  • hash (git hash for the current revision)

Return type

tuple of three str and a time.struct_time

pywikibot.version.getversion_onlinerepo(path='branches/master')[source]

Retrieve current framework git hash from Gerrit.

pywikibot.version.getversion_package(path=None)[source]

Get version info for an installed package.

Parameters

path – Unused argument

Returns

  • tag: ‘pywikibot/__init__.py’

  • rev: ‘-1 (unknown)’

  • date (date the package was installed locally),

  • hash (git hash for the current revision of ‘pywikibot/__init__.py’)

Return type

tuple of four str

pywikibot.version.getversion_svn(path=None)[source]

Get version info for a Subversion checkout.

Parameters

path – directory of the Subversion checkout

Returns

  • tag (name for the repository),

  • rev (current Subversion revision identifier),

  • date (date of current revision),

  • hash (git hash for the Subversion revision)

Return type

tuple of three str and a time.struct_time

pywikibot.version.getversiondict()[source]

Get version info for the package.

Returns

  • tag (name for the repository),

  • rev (current revision identifier),

  • date (date of current revision),

  • hash (git hash for the current revision)

Return type

dict of four str

pywikibot.version.github_svn_rev2hash(tag: str, rev)[source]

Convert a Subversion revision to a Git hash using Github.

Parameters
  • tag – name of the Subversion repo on Github

  • rev – Subversion revision identifier

Returns

the git hash

pywikibot.version.package_versions(modules=None, builtins=False, standard_lib=None)[source]

Retrieve package version information.

When builtins or standard_lib are None, they will be included only if a version was found in the package.

Parameters
  • modules (list of strings) – Modules to inspect

  • builtins (Boolean, or None for automatic selection) – Include builtins

  • standard_lib (Boolean, or None for automatic selection) – Include standard library packages

pywikibot.version.svn_rev_info(path)[source]

Fetch information about the current revision of a Subversion checkout.

Parameters

path – directory of the Subversion checkout

Returns

  • tag (name for the repository),

  • rev (current Subversion revision identifier),

  • date (date of current revision),

Return type

tuple of two str and a time.struct_time

pywikibot.xmlreader module

XML reading module.

Each XmlEntry object represents a page, as read from an XML source

The XmlDump class reads a pages_current XML dump (like the ones offered on https://dumps.wikimedia.org/backup-index.html) and offers a generator over XmlEntry objects which can be used by other bots.

class pywikibot.xmlreader.XmlDump(filename, allrevisions=False)[source]

Bases: object

Represents an XML dump file.

Reads the local file at initialization, parses it, and offers access to the resulting XmlEntries via a generator.

Parameters

allrevisions – boolean If True, parse all revisions instead of only the latest one. Default: False.

parse()[source]

Generator using ElementTree iterparse function.

class pywikibot.xmlreader.XmlEntry(title, ns, id, text, username, ipedit, timestamp, editRestriction, moveRestriction, revisionid, comment, redirect)[source]

Bases: object

Represent a page.

class pywikibot.xmlreader.XmlParserThread(filename, handler)[source]

Bases: threading.Thread

XML parser that will run as a single thread.

This allows the XmlDump generator to yield pages before the parser has finished reading the entire dump.

There surely are more elegant ways to do this.

run()[source]

Parse the file in a single thread.

pywikibot.xmlreader.parseRestrictions(restrictions)[source]

Parse the characters within a restrictions tag.

Returns strings representing user groups allowed to edit and to move a page, where None means there are no restrictions.