jenkins-bot has submitted this change and it was merged.
Change subject: Move deprecator methods to tools module ......................................................................
Move deprecator methods to tools module
The methods deprecated and deprecate_arg are not needed by all pywikibot usage, so do not belong in __init__.
As they are quite generic, except for the need to present a message to the user, they can reside in the tools module. The methods have been moved there without modification, and two variables (warning and debug) added to the tools module so they behave the same. There is a slight improvement, that they no longer have a hard dependency on the bot logging layer and therefore may be used earlier in the initialisation process or in custom code without importing the pywikibot package.
Change-Id: I1a7520617d11ed18e541b0e4bce28a775c3f131a --- M pywikibot/__init__.py M pywikibot/bot.py M pywikibot/compat/query.py M pywikibot/family.py M pywikibot/login.py M pywikibot/page.py M pywikibot/pagegenerators.py M pywikibot/site.py M pywikibot/tools.py M scripts/category.py 10 files changed, 79 insertions(+), 74 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved XZise: Looks good to me, but someone else must approve jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index 6d6cb6c..59bb683 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -50,11 +50,11 @@ removeCategoryLinks, removeCategoryLinksAndSeparator, replaceCategoryInPlace, compileLinkR, extract_templates_and_params, ) -from pywikibot.tools import UnicodeMixin +from pywikibot.tools import UnicodeMixin, deprecated, deprecate_arg from pywikibot.i18n import translate
__all__ = ( - 'config', 'ui', 'UnicodeMixin', 'translate', + 'config', 'ui', 'UnicodeMixin', 'translate', 'deprecated', 'deprecate_arg', 'Page', 'FilePage', 'ImagePage', 'Category', 'Link', 'User', 'ItemPage', 'PropertyPage', 'Claim', 'TimeStripper', 'html2unicode', 'url2unicode', 'unicode2html', @@ -428,57 +428,6 @@ def __repr__(self): return (u"WbQuantity(amount=%(amount)s, upperBound=%(upperBound)s, " u"lowerBound=%(lowerBound)s, unit=%(unit)s)" % self.__dict__) - - -def deprecated(instead=None): - """Decorator to output a method deprecation warning. - - @param instead: if provided, will be used to specify the replacement - @type instead: string - """ - def decorator(method): - def wrapper(*args, **kwargs): - funcname = method.__name__ - classname = args[0].__class__.__name__ - if instead: - warning(u"%s.%s is DEPRECATED, use %s instead." - % (classname, funcname, instead)) - else: - warning(u"%s.%s is DEPRECATED." % (classname, funcname)) - return method(*args, **kwargs) - wrapper.__name__ = method.__name__ - return wrapper - return decorator - - -def deprecate_arg(old_arg, new_arg): - """Decorator to declare old_arg deprecated and replace it with new_arg.""" - _logger = "" - - def decorator(method): - def wrapper(*__args, **__kw): - meth_name = method.__name__ - if old_arg in __kw: - if new_arg: - if new_arg in __kw: - warning( -u"%(new_arg)s argument of %(meth_name)s replaces %(old_arg)s; cannot use both." - % locals()) - else: - warning( -u"%(old_arg)s argument of %(meth_name)s is deprecated; use %(new_arg)s instead." - % locals()) - __kw[new_arg] = __kw[old_arg] - else: - debug( -u"%(old_arg)s argument of %(meth_name)s is deprecated." - % locals(), _logger) - del __kw[old_arg] - return method(*__args, **__kw) - wrapper.__doc__ = method.__doc__ - wrapper.__name__ = method.__name__ - return wrapper - return decorator
_sites = {} diff --git a/pywikibot/bot.py b/pywikibot/bot.py index b5474eb..1011d85 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -242,6 +242,9 @@
_handlers_initialized = True
+ pywikibot.tools.debug = debug + pywikibot.tools.warning = warning + writelogheader()
diff --git a/pywikibot/compat/query.py b/pywikibot/compat/query.py index 5564226..a23025a 100644 --- a/pywikibot/compat/query.py +++ b/pywikibot/compat/query.py @@ -1,7 +1,6 @@ import pywikibot from pywikibot.data import api -from pywikibot import deprecated -from pywikibot import deprecate_arg +from pywikibot.tools import deprecated, deprecate_arg
@deprecated("pywikibot.data.api.Request") diff --git a/pywikibot/family.py b/pywikibot/family.py index 1f9b14a..7ea793f 100644 --- a/pywikibot/family.py +++ b/pywikibot/family.py @@ -12,8 +12,9 @@ import re import collections
-from . import config2 as config import pywikibot +from pywikibot import config2 as config +from pywikibot.tools import deprecated
logger = logging.getLogger("pywiki.wiki.family")
@@ -976,7 +977,7 @@ # Here we return the latest mw release for downloading return '1.23.2'
- @pywikibot.deprecated("version()") + @deprecated("version()") def versionnumber(self, code): """ DEPRECATED, use version() instead and use distutils.version.LooseVersion to compare version strings. @@ -1046,7 +1047,7 @@ """Return the shared Wikibase repository, if any.""" return (None, None)
- @pywikibot.deprecated("Site.getcurrenttime()") + @deprecated("Site.getcurrenttime()") def server_time(self, code): """ DEPRECATED, use Site.getcurrenttime() instead diff --git a/pywikibot/login.py b/pywikibot/login.py index aa23d9d..46afba3 100644 --- a/pywikibot/login.py +++ b/pywikibot/login.py @@ -13,7 +13,8 @@ #
import pywikibot -from pywikibot import config, deprecate_arg +from pywikibot import config +from pywikibot.tools import deprecate_arg from pywikibot.exceptions import NoUsername
_logger = "wiki.login" diff --git a/pywikibot/page.py b/pywikibot/page.py index 22adbcf..8c7afe8 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -18,12 +18,10 @@ #
import pywikibot -from pywikibot import deprecate_arg -from pywikibot import deprecated from pywikibot import config import pywikibot.site from pywikibot.exceptions import AutoblockUser, UserActionRefuse -from pywikibot.tools import ComparableMixin +from pywikibot.tools import ComparableMixin, deprecated, deprecate_arg from pywikibot import textlib import hashlib
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index 22d3b3c..a46e27c 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -23,9 +23,8 @@ import itertools import re import pywikibot -from pywikibot import date -from pywikibot import config -from pywikibot import deprecate_arg, i18n +from pywikibot import date, config, i18n +from pywikibot.tools import deprecate_arg from pywikibot.comms import http import pywikibot.data.wikidataquery as wdquery
diff --git a/pywikibot/site.py b/pywikibot/site.py index 5132dfc..c6681b8 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -28,10 +28,8 @@ import copy
import pywikibot -import pywikibot.tools as tools -from pywikibot import deprecate_arg from pywikibot import config -from pywikibot import deprecated +from pywikibot.tools import itergroup, deprecated, deprecate_arg from pywikibot.throttle import Throttle from pywikibot.data import api from pywikibot.exceptions import ( @@ -919,12 +917,12 @@ 'semiprotectedlevels': 'autoconfirmed' 'levels': '' (everybody), 'autoconfirmed', 'sysop' 'types': 'create', 'edit', 'move', 'upload' - Otherwise it returns L{tools.EMPTY_DEFAULT}. + Otherwise it returns L{pywikibot.tools.EMPTY_DEFAULT}.
@param key: The property name @type key: str @return: The default value - @rtype: dict or L{tools.EmptyDefault} + @rtype: dict or L{pywikibot.tools.EmptyDefault} """ if key == 'restrictions': # implemented in b73b5883d486db0e9278ef16733551f28d9e096d @@ -935,7 +933,7 @@ 'types': ['create', 'edit', 'move', 'upload'] } else: - return tools.EMPTY_DEFAULT + return pywikibot.tools.EMPTY_DEFAULT
def _get_siteinfo(self, prop, force=False): """ @@ -2020,7 +2018,6 @@ @param langlinks: preload list of language links found in the pages
""" - from pywikibot.tools import itergroup for sublist in itergroup(pagelist, groupsize): pageids = [str(p._pageid) for p in sublist if hasattr(p, "_pageid") and p._pageid > 0] @@ -4239,7 +4236,6 @@ @param groupsize: how many pages to query at a time @type groupsize: int """ - from pywikibot.tools import itergroup for sublist in itergroup(pagelist, groupsize): req = {'ids': [], 'titles': [], 'sites': []} for p in sublist: diff --git a/pywikibot/tools.py b/pywikibot/tools.py index bf3dbc9..462b5ef 100644 --- a/pywikibot/tools.py +++ b/pywikibot/tools.py @@ -5,6 +5,7 @@ # # Distributed under the terms of the MIT license. # +from __future__ import print_function __version__ = '$Id$'
import sys @@ -16,6 +17,13 @@ import queue as Queue else: import Queue + + +# These variables are functions debug(str) and warning(str) +# which are initially the builtin print function. +# They exist here as the deprecators in this module rely only on them. +# pywikibot updates these function variables in bot.init_handlers() +debug = warning = print
class UnicodeMixin(object): @@ -255,6 +263,57 @@ EMPTY_DEFAULT = EmptyDefault()
+def deprecated(instead=None): + """Decorator to output a method deprecation warning. + + @param instead: if provided, will be used to specify the replacement + @type instead: string + """ + def decorator(method): + def wrapper(*args, **kwargs): + funcname = method.__name__ + classname = args[0].__class__.__name__ + if instead: + warning(u"%s.%s is DEPRECATED, use %s instead." + % (classname, funcname, instead)) + else: + warning(u"%s.%s is DEPRECATED." % (classname, funcname)) + return method(*args, **kwargs) + wrapper.__name__ = method.__name__ + return wrapper + return decorator + + +def deprecate_arg(old_arg, new_arg): + """Decorator to declare old_arg deprecated and replace it with new_arg.""" + _logger = "" + + def decorator(method): + def wrapper(*__args, **__kw): + meth_name = method.__name__ + if old_arg in __kw: + if new_arg: + if new_arg in __kw: + warning( +u"%(new_arg)s argument of %(meth_name)s replaces %(old_arg)s; cannot use both." + % locals()) + else: + warning( +u"%(old_arg)s argument of %(meth_name)s is deprecated; use %(new_arg)s instead." + % locals()) + __kw[new_arg] = __kw[old_arg] + else: + debug( +u"%(old_arg)s argument of %(meth_name)s is deprecated." + % locals(), _logger) + del __kw[old_arg] + return method(*__args, **__kw) + wrapper.__doc__ = method.__doc__ + wrapper.__name__ = method.__name__ + return wrapper + return decorator + + if __name__ == "__main__": def _test(): import doctest diff --git a/scripts/category.py b/scripts/category.py index 82edeb6..96d5e3e 100755 --- a/scripts/category.py +++ b/scripts/category.py @@ -106,7 +106,7 @@ import pywikibot from pywikibot import config, pagegenerators from pywikibot import i18n, textlib -from pywikibot import deprecate_arg, deprecated +from pywikibot.tools import deprecate_arg, deprecated
# This is required for the text that is shown when you run this script # with the parameter -help.
pywikibot-commits@lists.wikimedia.org