XZise has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/188809

Change subject: [FEAT] tools: redirect_method to deprecate methods
......................................................................

[FEAT] tools: redirect_method to deprecate methods

The redirect_method function wraps around redirect_func and makes it
easier to deprecate methods (functions of a class). So instead of the
template

  A = redirect_func(B, old_name='A', class_name='D')

it's reducing the duplication of A and makes sure that both are the
same:

  redirect_method(D.B, 'A', D)

As the class name is known after creation of it, this call needs to be
executed afterwards instead of inside, which is why B must be defined
absolutely. It is possible to use the string name for and avoid the
duplication of D:

  redirect_method('B', 'A', D)

Change-Id: Ic3ded7eba1844276b2ea28533ceb1e7bdcb71d19
---
M pywikibot/__init__.py
M pywikibot/site.py
M pywikibot/tools.py
3 files changed, 33 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/09/188809/1

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index b93f53f..36425da 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -45,7 +45,7 @@
     CaptchaError, SpamfilterError, CircularRedirect, InterwikiRedirectPage,
     WikiBaseError, CoordinateGlobeUnknownException,
 )
-from pywikibot.tools import UnicodeMixin, redirect_func
+from pywikibot.tools import UnicodeMixin, redirect_func, redirect_method
 from pywikibot.i18n import translate
 from pywikibot.data.api import UploadWarning
 from pywikibot.diff import PatchManager
@@ -158,9 +158,6 @@
         """
         return self.strftime(self.ISO8601Format)
 
-    toISOformat = redirect_func(isoformat, old_name='toISOformat',
-                                class_name='Timestamp')
-
     def totimestampformat(self):
         """Convert object to a MediaWiki internal timestamp."""
         return self.strftime(self.mediawikiTSFormat)
@@ -190,6 +187,9 @@
             return newdt
 
 
+redirect_method('isoformat', 'toISOformat', Timestamp)
+
+
 class Coordinate(object):
 
     """
diff --git a/pywikibot/site.py b/pywikibot/site.py
index f86e430..ae54196 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -32,7 +32,7 @@
 from pywikibot.tools import (
     itergroup, UnicodeMixin, ComparableMixin, SelfCallDict, SelfCallString,
     deprecated, deprecate_arg, deprecated_args, remove_last_args,
-    redirect_func, manage_wrapping,
+    redirect_func, redirect_method, manage_wrapping,
 )
 from pywikibot.tools import MediaWikiVersion
 from pywikibot.throttle import Throttle
@@ -751,10 +751,6 @@
         """
         return Namespace.lookup_name(namespace, self.namespaces)
 
-    # for backwards-compatibility
-    getNamespaceIndex = redirect_func(ns_index, old_name='getNamespaceIndex',
-                                      class_name='BaseSite')
-
     @property
     def namespaces(self):
         """Return dict of valid namespaces on this wiki."""
@@ -773,11 +769,6 @@
         """
         index = self.ns_index(value)
         return self.namespace(index)
-
-    # for backwards-compatibility
-    normalizeNamespace = redirect_func(ns_normalize,
-                                       old_name='normalizeNamespace',
-                                       class_name='BaseSite')
 
     @remove_last_args(('default', ))
     def redirect(self):
@@ -1078,6 +1069,11 @@
 
         return callee
     return decorator
+
+
+# for backwards-compatibility
+redirect_method('ns_index', 'getNamespaceIndex', BaseSite)
+redirect_method('ns_normalize', 'normalizeNamespace', BaseSite)
 
 
 class Siteinfo(Container):
@@ -1672,10 +1668,6 @@
                                  if sysop else LoginStatus.AS_USER)
         else:
             self._loginstatus = LoginStatus.NOT_LOGGED_IN  # failure
-
-    # alias for backward-compatibility
-    forceLogin = redirect_func(login, old_name='forceLogin',
-                               class_name='APISite')
 
     def logout(self):
         """ Logout of the site and load details for the logged out user.
@@ -5189,6 +5181,10 @@
         return lrgen
 
 
+# alias for backward-compatibility
+redirect_method('login', 'forceLogin', APISite)
+
+
 class DataSite(APISite):
 
     """Wikibase data capable site."""
diff --git a/pywikibot/tools.py b/pywikibot/tools.py
index 6037195..439b03f 100644
--- a/pywikibot/tools.py
+++ b/pywikibot/tools.py
@@ -930,6 +930,25 @@
     return call
 
 
+def redirect_method(target, old_name, cls):
+    """
+    Create a redirect with a deprecation warning to a method of a class.
+
+    @param target: The target method
+    @type target: str or function
+    @param old_name: The old (deprecated) name of the method.
+    @type old_name: str
+    @param cls: The class to which the method belongs
+    @type cls: class
+    """
+    if isinstance(target, basestring):
+        target = getattr(cls, target)
+    # source_module is '.' because otherwise it'd use _getframe(1) which is 
this
+    setattr(cls, old_name,
+            redirect_func(target, source_module='.',
+                          old_name=old_name, class_name=cls.__name__))
+
+
 class ModuleDeprecationWrapper(types.ModuleType):
 
     """A wrapper for a module to deprecate classes or variables of it."""

-- 
To view, visit https://gerrit.wikimedia.org/r/188809
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic3ded7eba1844276b2ea28533ceb1e7bdcb71d19
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to