Revision: 5926 Author: russblau Date: 2008-10-01 21:00:43 +0000 (Wed, 01 Oct 2008)
Log Message: ----------- more site tests; add handy decorator for methods with deprecated arguments
Modified Paths: -------------- branches/rewrite/pywikibot/site.py branches/rewrite/pywikibot/tests/site_tests.py
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2008-10-01 13:36:59 UTC (rev 5925) +++ branches/rewrite/pywikibot/site.py 2008-10-01 21:00:43 UTC (rev 5926) @@ -30,6 +30,32 @@ logger = logging.getLogger("wiki")
+def deprecate_arg(old_arg, new_arg): + """Decorator to declare old_arg deprecated and replace it with new_arg""" + def decorator(method): + def wrapper(*__args, **__kw): + meth_name = method.__name__ + if old_arg in __kw: + if new_arg: + if new_arg in __kw: + logger.warn( +"%(new_arg)s argument of %(meth_name)s replaces %(old_arg)s; cannot use both." + % locals()) + else: + logger.debug( +"%(old_arg)s argument of %(meth_name)s is deprecated; use %(new_arg)s instead." + % locals()) + __kw[new_arg] = __kw[old_arg] + else: + logger.debug( + "%(old_arg)s argument of %(meth_name)s is deprecated." + % locals()) + del __kw[old_arg] + return method(*__args, **__kw) + return wrapper + return decorator + + class PageInUse(pywikibot.Error): """Page cannot be reserved for writing due to existing lock."""
@@ -527,8 +553,9 @@ return 'blockinfo' in self._userinfo
def isBlocked(self, sysop=False): - """Deprecated; retained for backwards-compatibility""" - logger.debug("Site.isBlocked() method is deprecated; use is_blocked()") + """Deprecated synonym for is_blocked""" + logger.debug( + "Site method 'isBlocked' should be changed to 'is_blocked'") return self.is_blocked(sysop)
def has_right(self, right, sysop=False): @@ -917,7 +944,8 @@ namespaces=namespaces) )
- def pagelinks(self, page, namespaces=None, follow_redirects=False): + def pagelinks(self, page, namespaces=None, follow_redirects=False, + limit=None): """Iterate internal wikilinks contained (or transcluded) on page.
@param namespaces: Only iterate pages in these namespaces (default: all) @@ -927,6 +955,8 @@
""" plgen = api.PageGenerator("links", site=self) + if isinstance(limit, int): + plgen.limit = limit if hasattr(page, "_pageid"): plgen.request['pageids'] = str(page._pageid) else: @@ -939,13 +969,10 @@ for ns in namespaces) return plgen
+ @deprecate_arg("withSortKey", None) # Sortkey doesn't work with generator def pagecategories(self, page, withSortKey=None): """Iterate categories to which page belongs."""
- # Sortkey doesn't work with generator; deprecate - if withSortKey is not None: - logger.debug( - "site.pagecategories(): withSortKey option is deprecated") clgen = api.CategoryPageGenerator("categories", site=self) if hasattr(page, "_pageid"): clgen.request['pageids'] = str(page._pageid) @@ -1157,12 +1184,11 @@ for linkdata in pageitem['extlinks']: yield linkdata['*']
- def allpages(self, start="!", prefix="", namespace=0, - filterredir=None, filterlanglinks=None, - minsize=None, maxsize=None, - protect_type=None, protect_level=None, - limit=None, reverse=False, includeRedirects=None, - throttle=None): + @deprecate_arg("throttle", None) + def allpages(self, start="!", prefix="", namespace=0, filterredir=None, + filterlanglinks=None, minsize=None, maxsize=None, + protect_type=None, protect_level=None, limit=None, + reverse=False, includeRedirects=None): """Iterate pages in a single namespace.
Note: parameters includeRedirects and throttle are deprecated and @@ -1194,11 +1220,9 @@ """ if not isinstance(namespace, int): raise Error("allpages: only one namespace permitted.") - if throttle is not None: - logger.debug("allpages: the 'throttle' parameter is deprecated.") if includeRedirects is not None: logger.debug( - "allpages: the 'includeRedirects' parameter is deprecated.") +"allpages: 'includeRedirects' argument is deprecated; use 'filterredirs'.") if includeRedirects: if includeRedirects == "only": filterredirs = True @@ -1261,7 +1285,7 @@ @param limit: maximum number of pages to iterate (default: iterate all pages in namespace) @param fromids: if True, include the pageid of the page containing - each link (default: False) as the 'fromid' attribute of the Page; + each link (default: False) as the '_fromid' attribute of the Page; cannot be combined with unique
""" @@ -1282,7 +1306,7 @@ for link in algen: p = pywikibot.Page(self, link['title'], link['ns']) if fromids: - p.fromid = link['fromid'] + p._fromid = link['fromid'] yield p
def allcategories(self, start="!", prefix="", limit=None, @@ -1585,7 +1609,8 @@ rcgen.request["rcshow"] = "|".join(rcshow) return rcgen
- def search(self, searchstring, number=None, namespaces=[0], where="text", + @deprecate_arg("number", "limit") + def search(self, searchstring, namespaces=[0], where="text", getredirects=False, limit=None): """Iterate Pages that contain the searchstring.
@@ -1603,9 +1628,6 @@ @param number: deprecated, synonym for 'limit'
""" - if number is not None: - logger.debug("search: number parameter is deprecated; use limit") - limit = number if not searchstring: raise Error("search: searchstring cannot be empty") if where not in ("text", "titles"):
Modified: branches/rewrite/pywikibot/tests/site_tests.py =================================================================== --- branches/rewrite/pywikibot/tests/site_tests.py 2008-10-01 13:36:59 UTC (rev 5925) +++ branches/rewrite/pywikibot/tests/site_tests.py 2008-10-01 21:00:43 UTC (rev 5926) @@ -165,7 +165,7 @@ """Test that preloading works"""
count = 0 - for page in mysite.preloadpages(mysite.pagelinks(mainpage)): + for page in mysite.preloadpages(mysite.pagelinks(mainpage, limit=10)): self.assertType(page, pywikibot.Page) self.assertType(page.exists(), bool) if page.exists(): @@ -219,7 +219,8 @@ self.assertTrue(links.issuperset( set(mysite.pagelinks(mainpage, namespaces=[0, 1])))) for target in mysite.preloadpages( - mysite.pagelinks(mainpage, follow_redirects=True)): + mysite.pagelinks(mainpage, follow_redirects=True, + limit=10)): self.assertType(target, pywikibot.Page) self.assertFalse(target.isRedirectPage()) # test pagecategories @@ -254,71 +255,95 @@ def testAllPages(self): """Test the site.allpages() method"""
- global ap, ap6, ap7 - ap = list(mysite.allpages(limit=10)) - self.assertTrue(len(ap) <= 10) - for page in ap: + fwd = list(mysite.allpages(limit=10)) + self.assertTrue(len(fwd) <= 10) + for page in fwd: self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertEqual(page.namespace(), 0) - # TODO: test various optional arguments to allpages - ap1 = mysite.allpages(start="Py", limit=10) - for page in ap1: + rev = list(mysite.allpages(reverse=True, start="Aa", limit=12)) + self.assertTrue(len(rev) <= 12) + for page in rev: self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertEqual(page.namespace(), 0) + self.assertTrue(page.title() <= "Aa") + for page in mysite.allpages(start="Py", limit=10): + self.assertType(page, pywikibot.Page) + self.assertTrue(mysite.page_exists(page)) + self.assertEqual(page.namespace(), 0) self.assertTrue(page.title() >= "Py") - ap2 = mysite.allpages(prefix="Pre", limit=10) - for page in ap2: + for page in mysite.allpages(prefix="Pre", limit=10): self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertEqual(page.namespace(), 0) self.assertTrue(page.title().startswith("Pre")) - ap3 = mysite.allpages(namespace=1, limit=10) - for page in ap3: + for page in mysite.allpages(namespace=1, limit=10): self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertEqual(page.namespace(), 1) - ap4= mysite.allpages(filterredir=True, limit=10) - for page in ap4: + for page in mysite.allpages(filterredir=True, limit=10): self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertEqual(page.namespace(), 0) self.assertTrue(page.isRedirectPage()) - ap5= mysite.allpages(filterredir=False, limit=10) - for page in ap5: + for page in mysite.allpages(filterredir=False, limit=10): self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertEqual(page.namespace(), 0) self.assertFalse(page.isRedirectPage()) - ap6= list(mysite.allpages(filterlanglinks=True, limit=10)) - for page in ap6: + for page in mysite.allpages(filterlanglinks=True, limit=10): self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertEqual(page.namespace(), 0) - ap7= list(mysite.allpages(filterlanglinks=False, limit=10)) - for page in ap7: + for page in mysite.allpages(filterlanglinks=False, limit=10): self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertEqual(page.namespace(), 0) - ap8 = mysite.allpages(minsize=100, limit=10) - for page in ap8: + for page in mysite.allpages(minsize=100, limit=10): self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertTrue(len(page.text) >= 100) - ap9 = mysite.allpages(maxsize=200, limit=10) - for page in ap9: + for page in mysite.allpages(maxsize=200, limit=10): self.assertType(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) self.assertTrue(len(page.text) <= 200) + for page in mysite.allpages(protect_type="edit", limit=5): + self.assertType(page, pywikibot.Page) + self.assertTrue(mysite.page_exists(page)) + self.assertTrue("edit" in page._protection) + for page in mysite.allpages(protect_type="edit", + protect_level="sysop", limit=5): + self.assertType(page, pywikibot.Page) + self.assertTrue(mysite.page_exists(page)) + self.assertTrue("edit" in page._protection) + self.assertTrue("sysop" in page._protection["edit"])
def testAllLinks(self): """Test the site.alllinks() method"""
- al = list(mysite.alllinks(limit=10)) - self.assertTrue(len(al) <= 10) - self.assertTrue(all(isinstance(link, pywikibot.Page) for link in al)) + fwd = list(mysite.alllinks(limit=10)) + self.assertTrue(len(fwd) <= 10) + self.assertTrue(all(isinstance(link, pywikibot.Page) for link in fwd)) + uniq = list(mysite.alllinks(limit=10, unique=True)) + self.assertTrue(all(link in uniq for link in fwd)) # TODO: test various optional arguments to alllinks + for page in mysite.alllinks(start="Link", limit=10): + self.assertType(page, pywikibot.Page) + self.assertEqual(page.namespace(), 0) + self.assertTrue(page.title() >= "Link") + for page in mysite.alllinks(prefix="Fix", limit=10): + self.assertType(page, pywikibot.Page) + self.assertEqual(page.namespace(), 0) + self.assertTrue(page.title().startswith("Fix")) + for page in mysite.alllinks(namespace=1, limit=10): + self.assertType(page, pywikibot.Page) + self.assertEqual(page.namespace(), 1) + for page in mysite.alllinks(start="From", namespace=4, fromids=True, + limit=10): + self.assertType(page, pywikibot.Page) + self.assertTrue(page.title(withNamespace=False) >= "From") + self.assertTrue(hasattr(page, "_fromid"))
def testAllCategories(self): """Test the site.allcategories() method"""
pywikipedia-l@lists.wikimedia.org