Source code for pywikibot.exceptions

# -*- coding: utf-8 -*-
"""
Exception and warning classes used throughout the framework.

Error: Base class, all exceptions should the subclass of this class.

  - NoUsername: Username is not in user-config.py, or it is invalid.
  - UserBlocked: Username or IP has been blocked
  - AutoblockUser: requested action on a virtual autoblock user not valid
  - UserRightsError: insufficient rights for requested action
  - BadTitle: Server responded with BadTitle
  - InvalidTitle: Invalid page title
  - CaptchaError: Captcha is asked and config.solve_captcha == False
  - Server504Error: Server timed out with HTTP 504 code
  - i18n.TranslationError: i18n/l10n message not available
  - UnknownExtension: Extension is not defined for this site

SiteDefinitionError: Site loading problem

  - UnknownSite: Site does not exist in Family
  - UnknownFamily: Family is not registered

PageRelatedError: any exception which is caused by an operation on a Page.

  - NoPage: Page does not exist
  - UnsupportedPage: Page is not supported due to a namespace restriction
  - IsRedirectPage: Page is a redirect page
  - IsNotRedirectPage: Page is not a redirect page
  - CircularRedirect: Page is a circular redirect
  - InterwikiRedirectPage: Page is a redirect to another site
  - SectionError: The section specified by # does not exist
  - NotEmailableError: The target user has disabled email
  - NoMoveTarget: An expected move target page does not exist

PageLoadRelatedError: any exception which happens while loading a Page.
  - InconsistentTitleReceived: Page receives a title inconsistent with query

PageSaveRelatedError: page exceptions within the save operation on a Page
(alias: PageNotSaved).

  - SpamfilterError: MediaWiki spam filter detected a blacklisted URL
  - TitleblacklistError: MediaWiki detected a blacklisted page title
  - OtherPageSaveError: misc. other save related exception.
  - LockedPage: Page is locked
      - LockedNoPage: Title is locked against creation
      - CascadeLockedPage: Page is locked due to cascading protection
  - EditConflict: Edit conflict while uploading the page
      - PageDeletedConflict: Page was deleted since being retrieved
      - PageCreatedConflict: Page was created by another user
      - ArticleExistsConflict: Page article already exists
  - NoCreateError: parameter nocreate not allow page creation

ServerError: a problem with the server.

  - FatalServerError: A fatal/non-recoverable server error

WikiBaseError: any issue specific to Wikibase.

  - CoordinateGlobeUnknownException: globe is not implemented yet.
  - EntityTypeUnknownException: entity type is not available on the site.

TimeoutError: request failed with a 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.

  - tools._NotImplementedWarning: do not use
  - NotImplementedWarning: functionality not implemented

UserWarning: warnings targeted at users

  - config2._ConfigurationDeprecationWarning: user configuration file problems
  - login._PasswordFileWarning: password file problems
  - ArgumentDeprecationWarning: command line argument problems
  - FamilyMaintenanceWarning: missing information in family definition
"""
#
# (C) Pywikibot team, 2008-2019
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, division, unicode_literals

from pywikibot.tools import (
    # __ to avoid conflict with ModuleDeprecationWrapper._deprecated
    deprecated as __deprecated,
    ModuleDeprecationWrapper as _ModuleDeprecationWrapper,
    UnicodeMixin,
    UnicodeType,
    _NotImplementedWarning,
)


[docs]class NotImplementedWarning(_NotImplementedWarning): """Feature that is no longer implemented.""" pass
[docs]class ArgumentDeprecationWarning(UserWarning): """Command line argument that is no longer supported.""" pass
[docs]class FamilyMaintenanceWarning(UserWarning): """Family class is missing definitions.""" pass
[docs]class Error(UnicodeMixin, Exception): """Pywikibot error.""" # NOTE: UnicodeMixin must be the first object Error class is derived from.
[docs] def __init__(self, arg): """Initializer.""" self.unicode = arg
[docs] def __unicode__(self): """Return a unicode string representation.""" return self.unicode
[docs]class PageRelatedError(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. """ # Preformatted UNICODE message where the page title will be inserted # Override this in subclasses. # 'Oh noes! Page %s is too funky, we should not delete it ;(' message = None
[docs] def __init__(self, page, message=None): """ Initializer. @param page: Page that caused the exception @type page: Page object """ if message: self.message = message if self.message is None: raise Error("PageRelatedError is abstract. Can't instantiate it!") self.page = page self.title = page.title(as_link=True) self.site = page.site if '%(' in self.message and ')s' in self.message: super(PageRelatedError, self).__init__( self.message % self.__dict__) else: super(PageRelatedError, self).__init__(self.message % page)
[docs] def getPage(self): """Return the page related to the exception.""" return self.page
[docs]class PageSaveRelatedError(PageRelatedError): """Saving the page has failed.""" message = 'Page %s was not saved.' # This property maintains backwards compatibility with # the old PageNotSaved which inherited from Error # (not PageRelatedError) and exposed the normal 'args' # which could be printed @property def args(self): """Expose args.""" return UnicodeType(self)
[docs]class OtherPageSaveError(PageSaveRelatedError): """Saving the page has failed due to uncatchable error.""" message = 'Edit to page %(title)s failed:\n%(reason)s'
[docs] def __init__(self, page, reason): """Initializer. @param reason: Details of the problem @type reason: Exception or basestring """ self.reason = reason super(OtherPageSaveError, self).__init__(page)
@property def args(self): """Expose args.""" return UnicodeType(self.reason)
[docs]class NoUsername(Error): """Username is not in user-config.py.""" pass
[docs]class NoPage(PageRelatedError): """Page does not exist.""" message = "Page %s doesn't exist." pass
[docs]class UnsupportedPage(PageRelatedError): """Unsupported page due to namespace restriction.""" # namespaces < 0 aren't supported (T169213) message = 'Page %s is not supported due to namespace restriction.' pass
[docs]class NoMoveTarget(PageRelatedError): """Expected move target page not found.""" message = 'Move target page of %s not found.' pass
[docs]class PageLoadRelatedError(PageRelatedError): """Loading the contents of a Page object has failed.""" message = 'Page %s was not loaded.'
[docs]class InconsistentTitleReceived(PageLoadRelatedError): """Page receives a title inconsistent with query."""
[docs] def __init__(self, page, actual): """Initializer. @param page: Page that caused the exception @type page: Page object @param actual: title obtained by query @type reason: basestring """ self.message = "Query on %s returned data on '{0}'".format(actual) super(InconsistentTitleReceived, self).__init__(page)
[docs]class SiteDefinitionError(Error): """Site does not exist.""" pass
# The name 'NoSuchSite' was used for all site related issues, # and it used message "Site does not exist". # These are retain for backwards compatibility with scripts. NoSuchSite = SiteDefinitionError
[docs]class UnknownSite(SiteDefinitionError): """Site does not exist in Family.""" pass
[docs]class UnknownFamily(SiteDefinitionError): """Family is not registered.""" pass
[docs]class UnknownExtension(Error, NotImplementedError): """Extension is not defined.""" pass
[docs]class IsRedirectPage(PageRelatedError): """Page is a redirect page.""" message = 'Page %s is a redirect page.' pass
[docs]class IsNotRedirectPage(PageRelatedError): """Page is not a redirect page.""" message = 'Page %s is not a redirect page.' pass
[docs]class CircularRedirect(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) """ message = 'Page %s is a circular redirect.'
[docs]class InterwikiRedirectPage(PageRelatedError): """ Page is a redirect to another site. This is considered invalid in Pywikibot. See bug T75184. """ message = ('Page redirects to a page on another Site.\n' 'Page: %(page)s\n' 'Target page: %(target_page)s on %(target_site)s.')
[docs] def __init__(self, page, target_page): """Initializer. @param target_page: Target page of the redirect. @type reason: Page """ self.target_page = target_page self.target_site = target_page.site super(InterwikiRedirectPage, self).__init__(page)
[docs]class InvalidTitle(Error): """Invalid page title.""" pass
[docs]class LockedPage(PageSaveRelatedError): """Page is locked.""" message = 'Page %s is locked.' pass
[docs]class LockedNoPage(LockedPage): """Title is locked against creation.""" message = 'Page %s does not exist and is locked preventing creation.' pass
[docs]class CascadeLockedPage(LockedPage): """Page is locked due to cascading protection.""" message = 'Page %s is locked due to cascading protection.' pass
[docs]class SectionError(Error): """The section specified by # does not exist.""" pass
PageNotSaved = PageSaveRelatedError
[docs]class NoCreateError(PageSaveRelatedError): """Parameter nocreate doesn't allow page creation.""" message = 'Page %s could not be created due to parameter nocreate' pass
[docs]class EditConflict(PageSaveRelatedError): """There has been an edit conflict while uploading the page.""" message = 'Page %s could not be saved due to an edit conflict' pass
[docs]class PageDeletedConflict(EditConflict): """Page was deleted since being retrieved.""" message = 'Page %s has been deleted since last retrieved.' pass
[docs]class PageCreatedConflict(EditConflict): """Page was created by another user.""" message = 'Page %s has been created since last retrieved.' pass
[docs]class ArticleExistsConflict(EditConflict): """Page already exists.""" message = ('Destination article %s already exists and is not a redirect ' 'to the source article') pass
[docs]class SpamfilterError(PageSaveRelatedError): """Page save failed because MediaWiki detected a blacklisted spam URL.""" message = ('Edit to page %(title)s rejected by spam filter due to ' 'content:\n%(url)s')
[docs] def __init__(self, page, url): """Initializer.""" self.url = url super(SpamfilterError, self).__init__(page)
[docs]class TitleblacklistError(PageSaveRelatedError): """Page save failed because MediaWiki detected a blacklisted page title.""" message = 'Page %s is title-blacklisted.' pass
[docs]class ServerError(Error): """Got unexpected server response.""" pass
[docs]class FatalServerError(ServerError): """A fatal server error will not be corrected by resending the request.""" pass
[docs]class Server504Error(ServerError): """Server timed out with HTTP 504 code.""" pass
[docs]class Server414Error(ServerError): """Server returned with HTTP 414 code.""" pass
[docs]class BadTitle(Error): """Server responded with BadTitle.""" pass
# UserBlocked exceptions should in general not be caught. If the bot has # been blocked, the bot operator should address the reason for the block # before continuing.
[docs]class UserBlocked(Error): """Your username or IP has been blocked.""" pass
[docs]class CaptchaError(Error): """Captcha is asked and config.solve_captcha == False.""" pass
[docs]class AutoblockUser(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). """ pass
[docs]class UserRightsError(Error): """Insufficient user rights to perform an action.""" pass
[docs]class HiddenKeyError(UserRightsError, KeyError): """Insufficient user rights to view the hidden key.""" pass
[docs]class NotEmailableError(PageRelatedError): """This user is not emailable.""" message = '%s is not emailable.' pass
[docs]class WikiBaseError(Error): """Wikibase related error.""" pass
[docs]class CoordinateGlobeUnknownException(WikiBaseError, NotImplementedError): """This globe is not implemented yet in either WikiBase or pywikibot.""" pass
[docs]class EntityTypeUnknownException(WikiBaseError): """The requested entity type is not recognised on this site.""" pass
[docs]class TimeoutError(Error): """Request failed with a timeout error.""" pass
@__deprecated(since='20141214') class DeprecatedPageNotFoundError(Error): """Page not found (deprecated).""" pass @__deprecated(since='20141218') class _EmailUserError(UserRightsError, NotEmailableError): """Email related error.""" pass wrapper = _ModuleDeprecationWrapper(__name__) wrapper._add_deprecated_attr( 'UploadWarning', replacement_name='pywikibot.data.api.UploadWarning', warning_message='pywikibot.exceptions.UploadWarning is deprecated; ' 'use APISite.upload with a warning handler instead.', since='20150921') wrapper._add_deprecated_attr('PageNotFound', DeprecatedPageNotFoundError, warning_message='{0}.{1} is deprecated, and no ' 'longer used by pywikibot; use ' 'http.fetch() instead.', since='20141214') wrapper._add_deprecated_attr( 'UserActionRefuse', _EmailUserError, warning_message='UserActionRefuse is deprecated; ' 'use UserRightsError and/or NotEmailableError', since='20141218')