Revision: 6410 Author: nicdumz Date: 2009-02-22 12:31:24 +0000 (Sun, 22 Feb 2009)
Log Message: ----------- Introducing pywikibot.deprecated() to handle easily deprecation warnings
Modified Paths: -------------- branches/rewrite/pywikibot/__init__.py branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/__init__.py =================================================================== --- branches/rewrite/pywikibot/__init__.py 2009-02-22 11:32:12 UTC (rev 6409) +++ branches/rewrite/pywikibot/__init__.py 2009-02-22 12:31:24 UTC (rev 6410) @@ -22,6 +22,22 @@ logging.basicConfig(fmt="%(message)s")
+def deprecated(instead=None): + """Outputs a method deprecation warning. + Uses the stack to determine class and name of calling method + + @param instead: if provided, will be used to specify the replacement + @type instead: string + """ + f = sys._getframe(1) + classname = f.f_locals['self'].__class__.__name__ + funcname = f.f_code.co_name + if new: + logging.warning("%s.%s is DEPRECATED, use %s instead" % \ + (classname, funcname, instead)) + else: + logging.warning("%s.%s is DEPRECATED." % (classname, funcname)) + def deprecate_arg(old_arg, new_arg): """Decorator to declare old_arg deprecated and replace it with new_arg""" logger = logging.getLogger()
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2009-02-22 11:32:12 UTC (rev 6409) +++ branches/rewrite/pywikibot/page.py 2009-02-22 12:31:24 UTC (rev 6410) @@ -11,6 +11,7 @@
import pywikibot from pywikibot import deprecate_arg +from pywikibot import deprecated from pywikibot import config import pywikibot.site
@@ -1172,28 +1173,28 @@
def encoding(self): """DEPRECATED: use Site.encoding() instead""" - logger.debug(u"Page.encoding() is deprecated; use Site.encoding().") + deprecated("Site.encoding()") return self.site().encoding()
def titleWithoutNamespace(self, underscore=False): """DEPRECATED: use self.title(withNamespace=False) instead.""" - logger.debug(u"Page.titleWithoutNamespace() method is deprecated.") + deprecated("Page.title(withNamespace=False)") return self.title(underscore=underscore, withNamespace=False, withSection=False)
def titleForFilename(self): """DEPRECATED: use self.title(as_filename=True) instead.""" - logger.debug(u"Page.titleForFilename() method is deprecated.") + deprecated("Page.title(as_filename=True)") return self.title(as_filename=True)
def sectionFreeTitle(self, underscore=False): """DEPRECATED: use self.title(withSection=False) instead.""" - logger.debug(u"Page.sectionFreeTitle() method is deprecated.") + deprecated("Page.title(withSection=False)") return self.title(underscore=underscore, withSection=False)
def aslink(self, forceInterwiki=False, textlink=False, noInterwiki=False): """DEPRECATED: use self.title(asLink=True) instead.""" - logger.debug(u"Page.aslink() method is deprecated.") + deprecated("Page.title(asLink=True)") return self.title(asLink=True, forceInterwiki=forceInterwiki, allowInterwiki=not noInterwiki, textlink=textlink)
@@ -1203,7 +1204,7 @@ DEPRECATED: use self.title(asUrl=True) instead.
""" - logger.debug(u"Page.urlname() method is deprecated.") + deprecated("Page.title(asUrl=True)") return self.title(asUrl=True)
####### DISABLED METHODS (warnings provided) ###### @@ -1212,14 +1213,12 @@
def removeImage(self, image, put=False, summary=None, safe=True): """Old method to remove all instances of an image from page.""" - pywikibot.output(u"Page.removeImage() is no longer supported.", - level=pywikibot.WARNING) + logger.warning(u"Page.removeImage() is no longer supported.")
def replaceImage(self, image, replacement=None, put=False, summary=None, safe=True): """Old method to replace all instances of an image with another.""" - pywikibot.output(u"Page.replaceImage() is no longer supported.", - level=pywikibot.WARNING) + logger.warning(u"Page.replaceImage() is no longer supported.")
class ImagePage(Page): @@ -1282,8 +1281,7 @@
def getFileMd5Sum(self): """Return image file's MD5 checksum.""" - logger.debug( - u"ImagePage.getFileMd5Sum() is deprecated; use getFileSHA1Sum().") + deprecated("ImagePage.getFileSHA1Sum()") # FIXME: MD5 might be performed on incomplete file due to server disconnection # (see bug #1795683). import md5, urllib @@ -1496,22 +1494,22 @@ #### DEPRECATED METHODS #### def subcategoriesList(self, recurse=False): """DEPRECATED: Equivalent to list(self.subcategories(...))""" - logger.debug(u"Category.subcategoriesList() method is deprecated.") + deprecated("list(Category.subcategories(...))") return sorted(list(set(self.subcategories(recurse))))
def articlesList(self, recurse=False): """DEPRECATED: equivalent to list(self.articles(...))""" - logger.debug(u"Category.articlesList() method is deprecated.") + deprecated("list(Category.articles(...))") return sorted(list(set(self.articles(recurse))))
def supercategories(self): """DEPRECATED: equivalent to self.categories()""" - logger.debug(u"Category.supercategories() method is deprecated.") + deprecated("Category.categories()") return self.categories()
def supercategoriesList(self): """DEPRECATED: equivalent to list(self.categories(...))""" - logger.debug(u"Category.articlesList() method is deprecated.") + deprecated("list(Category.categories(...))") return sorted(list(set(self.categories())))
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2009-02-22 11:32:12 UTC (rev 6409) +++ branches/rewrite/pywikibot/site.py 2009-02-22 12:31:24 UTC (rev 6410) @@ -13,6 +13,7 @@ import pywikibot from pywikibot import deprecate_arg from pywikibot import config +from pywikibot import deprecated from pywikibot.throttle import Throttle from pywikibot.data import api from pywikibot.exceptions import * @@ -313,7 +314,7 @@ Use optional Site argument 'othersite' to generate an interwiki link.
""" - logger.debug("Site.linkto() method is deprecated; use pywikibot.Link") + deprecated("pywikibot.Link") return pywikibot.Link(title, self).astext(othersite)
def isInterwikiLink(self, s): @@ -408,10 +409,12 @@
def fam(self): """Return Family object for this Site.""" + deprecated("family attribute") return self.family
def urlEncode(self, query): """DEPRECATED""" + deprecated("urllib.urlencode()") return urllib.urlencode(query)
def getUrl(self, path, retry=True, sysop=False, data=None, @@ -422,6 +425,7 @@ are ignored.
""" + deprecated("pywikibot.comms.data.request") if data: if not isinstance(data, basestring): data = urllib.urlencode(data) @@ -432,11 +436,13 @@
def postForm(self, address, predata, sysop=False, cookies=None): """DEPRECATED""" + deprecated() return self.getUrl(address, data=predata)
def postData(self, address, data, contentType=None, sysop=False, compress=True, cookies=None): """DEPRECATED""" + deprecated() return self.getUrl(address, data=data)
# unsupported methods from version 1 @@ -635,7 +641,7 @@ DEPRECATED (use .user() method instead)
""" - logger.debug("Site.loggedInAs() method is deprecated.") + deprecated("Site.user()") return self.logged_in(sysop) and self.user()
def login(self, sysop=False): @@ -729,7 +735,7 @@
def isAllowed(self, right, sysop=False): """Deprecated; retained for backwards-compatibility""" - logger.debug("Site.isAllowed() method is deprecated; use has_right()") + deprecated("Site.has_right()") return self.has_right(right, sysop)
def has_group(self, group, sysop=False): @@ -1459,7 +1465,7 @@ Use allpages() with the prefix= parameter instead of this method.
""" - logger.debug("Site.prefixindex() is deprecated; use allpages instead.") + deprecated("Site.allpages()") return self.allpages(prefix=prefix, namespace=namespace, includeredirects=includeredirects)
@@ -1533,8 +1539,7 @@
def categories(self, number=10, repeat=False): """Deprecated; retained for backwards-compatibility""" - logger.debug( - "Site.categories() method is deprecated; use .allcategories()") + deprecated("Site.allcategories()") if repeat: limit = None else: