Revision: 6806 Author: nicdumz Date: 2009-05-03 14:34:33 +0000 (Sun, 03 May 2009)
Log Message: ----------- * preloadpages : add the possibility to preload templates and langlinks too. * Page templates & langlinks methods are changed to be able to returned the preloaded values: they now return lists instead of iterators. * Introducing itertemplates and iterlanglinks, when getting the full list is not important.
Modified Paths: -------------- branches/rewrite/pywikibot/data/api.py branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/data/api.py =================================================================== --- branches/rewrite/pywikibot/data/api.py 2009-05-03 11:53:49 UTC (rev 6805) +++ branches/rewrite/pywikibot/data/api.py 2009-05-03 14:34:33 UTC (rev 6806) @@ -716,14 +716,29 @@ text=rev.get('*', None) ) page._revisions[revision.revid] = revision + if 'lastrevid' in pagedict: page._revid = pagedict['lastrevid'] if page._revid in page._revisions: page._text = page._revisions[page._revid].text + if "categoryinfo" in pagedict: page._catinfo = pagedict["categoryinfo"]
+ if "templates" in pagedict: + page._templates = [ pywikibot.Page(page.site(), tl['title']) + for tl in pagedict['templates'] ]
+ if "langlinks" in pagedict: + links = [] + for ll in pagedict["langlinks"]: + link = pywikibot.Link.langlinkUnsafe(ll['lang'], + ll['*'], + source=page.site()) + links.append(link) + page._langlinks = links + + if __name__ == "__main__": from pywikibot import Site logger.setLevel(pywikibot.logging.DEBUG)
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2009-05-03 11:53:49 UTC (rev 6805) +++ branches/rewrite/pywikibot/page.py 2009-05-03 14:34:33 UTC (rev 6806) @@ -793,24 +793,44 @@ continue
def langlinks(self): + """Returns a list of all interlanguage Links on this page. + + """ + # Data might have been preloaded + if not hasattr(self, '_langlinks'): + self._langlinks = list(self.iterlanglinks()) + + return self._langlinks + + def iterlanglinks(self): """Iterate all interlanguage links on this page.
@return: a generator that yields Link objects.
""" + if hasattr(self, '_langlinks'): + return iter(self._langlinks) + # XXX We might want to fill _langlinks when the Site + # method is called. If we do this, we'll have to think + # about what will happen if the generator is not completely + # iterated upon. return self.site().pagelanglinks(self)
- @deprecate_arg("followRedirects", None) - @deprecate_arg("loose", None) - def imagelinks(self, followRedirects=None, loose=None): - """Iterate ImagePage objects for images displayed on this Page. + def templates(self): + """Returns a list of Page objects for templates used on this Page.
- @return: a generator that yields ImagePage objects. + Template parameters are ignored. This method only returns embedded + templates, not template pages that happen to be referenced through + a normal link.
""" - return self.site().pageimages(self) + # Data might have been preloaded + if not hasattr(self, '_templates'): + self._templates = list(self.itertemplates())
- def templates(self): + return self._templates + + def itertemplates(self): """Iterate Page objects for templates used on this Page.
Template parameters are ignored. This method only returns embedded @@ -818,8 +838,20 @@ a normal link.
""" + if hasattr(self, '_templates'): + return iter(self._templates) return self.site().pagetemplates(self)
+ @deprecate_arg("followRedirects", None) + @deprecate_arg("loose", None) + def imagelinks(self, followRedirects=None, loose=None): + """Iterate ImagePage objects for images displayed on this Page. + + @return: a generator that yields ImagePage objects. + + """ + return self.site().pageimages(self) + def templatesWithParams(self): """Iterate templates used on this Page.
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2009-05-03 11:53:49 UTC (rev 6805) +++ branches/rewrite/pywikibot/site.py 2009-05-03 14:34:33 UTC (rev 6806) @@ -1010,7 +1010,8 @@ page._redirtarget = target return page._redirtarget
- def preloadpages(self, pagelist, groupsize=50): + def preloadpages(self, pagelist, groupsize=50, templates=False, + langlinks=False): """Return a generator to a list of preloaded pages.
Note that [at least in current implementation] pages may be iterated @@ -1019,6 +1020,8 @@ @param pagelist: an iterable that returns Page objects @param groupsize: how many Pages to query at a time @type groupsize: int + @param templates: preload list of templates in the pages + @param langlinks: preload list of language links found in the pages
""" from pywikibot.tools import itergroup @@ -1027,8 +1030,13 @@ if hasattr(p, "_pageid") and p._pageid > 0] cache = dict((p.title(withSection=False), p) for p in sublist) - rvgen = api.PropertyGenerator("revisions|info|categoryinfo", - site=self) + + props = "revisions|info|categoryinfo" + if templates: + props += '|templates' + if langlinks: + props += '|langlinks' + rvgen = api.PropertyGenerator(props, site=self) rvgen.set_maximum_items(-1) # suppress use of "rvlimit" parameter if len(pageids) == len(sublist): # only use pageids if all pages have them
pywikipedia-svn@lists.wikimedia.org