Revision: 6449 Author: russblau Date: 2009-02-27 16:55:46 +0000 (Fri, 27 Feb 2009)
Log Message: ----------- add Category.members() method to generate both subcategories and articles; fix problems with templatesWithParams() methods; default to groups of 50 when preloading; fix output formatting problems and other minor bugs
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-27 16:53:22 UTC (rev 6448) +++ branches/rewrite/pywikibot/__init__.py 2009-02-27 16:55:46 UTC (rev 6449) @@ -33,10 +33,12 @@ classname = f.f_locals['self'].__class__.__name__ funcname = f.f_code.co_name if instead: - logging.warning("%s.%s is DEPRECATED, use %s instead" % \ - (classname, funcname, instead)) + output(u"%s.%s is DEPRECATED, use %s instead." + % (classname, funcname, instead), + level=WARNING) else: - logging.warning("%s.%s is DEPRECATED." % (classname, funcname)) + output(u"%s.%s is DEPRECATED." % (classname, funcname), + level=WARNING)
def deprecate_arg(old_arg, new_arg): """Decorator to declare old_arg deprecated and replace it with new_arg"""
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2009-02-27 16:53:22 UTC (rev 6448) +++ branches/rewrite/pywikibot/page.py 2009-02-27 16:55:46 UTC (rev 6449) @@ -808,11 +808,22 @@ parameters as the second entry.
""" + # WARNING: may not return all templates used in particularly + # intricate cases such as template substitution + titles = list(t.title() for t in self.templates()) templates = pywikibot.extract_templates_and_params(self.text) # backwards-compatibility: convert the dict returned as the second # element into a list in the format used by old scripts result = [] for template in templates: + link = pywikibot.Link(template[0], self.site(), + defaultNamespace=10) + try: + if link.canonical_title() not in titles: + continue + except pywikibot.Error: + # this is a parser function or magic word, not template name + continue args = template[1] positional = [] named = {} @@ -825,10 +836,7 @@ positional.append(args[key]) for name in named: positional.append("%s=%s" % (name, named[name])) - result.append((pywikibot.Page( - pywikibot.Link(template[0], self.site(), - defaultNamespace=10)), - positional)) + result.append((pywikibot.Page(link, self.site()), positional)) return result
@deprecate_arg("nofollow_redirects", None) @@ -1425,6 +1433,17 @@ for article in subcat.articles(recurse): yield article
+ def members(self, recurse=False): + """Yield all category contents (subcats, pages, and files).""" + for member in self.site().categorymembers(self): + yield member + if recurse: + if not isinstance(recurse, bool) and recurse: + recurse = recurse - 1 + for subcat in self.subcategories(): + for article in subcat.members(recurse): + yield article + def isEmptyCategory(self): """Return True if category has no members (including subcategories).""" for member in self.site().categorymembers(self, limit=1): @@ -1802,7 +1821,8 @@ # "empty" local links can only be self-links # with a fragment identifier. if not t and self._site == self._source and self._namespace != 0: - raise ValueError("Invalid link (no page title): '%s'" % self._text) + raise pywikibot.Error("Invalid link (no page title): '%s'" + % self._text)
self._title = t
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2009-02-27 16:53:22 UTC (rev 6448) +++ branches/rewrite/pywikibot/site.py 2009-02-27 16:55:46 UTC (rev 6449) @@ -1,4 +1,4 @@ - # -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- """ Objects representing MediaWiki sites (wikis) and families (groups of wikis on the same topic in different languages). @@ -994,7 +994,7 @@ api.update_page(target, pagedata) page._redir = target
- def preloadpages(self, pagelist, groupsize=60): + def preloadpages(self, pagelist, groupsize=50): """Return a generator to a list of preloaded pages.
Note that [at least in current implementation] pages may be iterated @@ -1401,10 +1401,12 @@ def categoryinfo(self, category): if not hasattr(category, "_catinfo"): self.getcategoryinfo(category) + if not hasattr(category, "_catinfo"): + # a category that exists but has no contents returns no API result + return {'size':0, 'pages':0, 'files':0, 'subcats':0} return category._catinfo
@deprecate_arg("throttle", None) - @deprecate_arg("includeredirects", "filterredir") def allpages(self, start="!", prefix="", namespace=0, filterredir=None, filterlanglinks=None, minsize=None, maxsize=None, protect_type=None, protect_level=None, limit=None,