Revision: 8631 Author: xqt Date: 2010-10-09 21:01:00 +0000 (Sat, 09 Oct 2010)
Log Message: ----------- import wikipedia as pywikibot for merging to rewrite
Modified Paths: -------------- trunk/pywikipedia/statistics_in_wikitable.py trunk/pywikipedia/table2wiki.py trunk/pywikipedia/templatecount.py trunk/pywikipedia/testfamily.py trunk/pywikipedia/unlink.py trunk/pywikipedia/us-states.py
Modified: trunk/pywikipedia/statistics_in_wikitable.py =================================================================== --- trunk/pywikipedia/statistics_in_wikitable.py 2010-10-09 19:32:57 UTC (rev 8630) +++ trunk/pywikipedia/statistics_in_wikitable.py 2010-10-09 21:01:00 UTC (rev 8631) @@ -1,8 +1,6 @@ -#!/usr/bin/python +#!/usr/bin/python # -*- coding: utf-8 -*- """ - - \03{lightyellow}This bot renders statistics provided by [[Special:Statistics]] in a table on a wiki page.\03{default} Thus it creates and updates a Statistics wikitable.
@@ -15,8 +13,10 @@ If existing, it is updated. """ __version__ = '$Id$' -import wikipedia, pagegenerators, query +# import time +import wikipedia as pywikibot +import pagegenerators, query
# This is the title of the wikipage where to render stats. your_page = "Logstats" @@ -39,16 +39,16 @@ self.screen = screen self.your_page = your_page self.dict = self.getdata() # Try to get data. - self.site = wikipedia.getSite() + self.site = pywikibot.getSite()
def run(self): if self.screen: - wikipedia.output("Bot is running to output stats.") + pywikibot.output("Bot is running to output stats.") self.idle(1) # Run a function to idle self.outputall() if not self.screen: self.outputall() # Output all datas on screen. - wikipedia.output("\nBot is running. Going to treat \03{lightpurple}%s\03{default}..." % self.your_page ) + pywikibot.output("\nBot is running. Going to treat \03{lightpurple}%s\03{default}..." % self.your_page ) self.idle(2) self.treat()
@@ -60,49 +60,49 @@ 'meta' :'siteinfo', 'siprop' :'statistics', } - wikipedia.output("\nQuerying api for json-formatted data...") + pywikibot.output("\nQuerying api for json-formatted data...") try: data = query.GetData(params,self.site, encodeTitle = False) except: url = self.site.protocol() + '://' + self.site.hostname() + self.site.api_address() - wikipedia.output("The query has failed. Have you check the API? Cookies are working?") - wikipedia.output(u"\n>> \03{lightpurple}%s\03{default} <<" % url) + pywikibot.output("The query has failed. Have you check the API? Cookies are working?") + pywikibot.output(u"\n>> \03{lightpurple}%s\03{default} <<" % url) if data != None: - wikipedia.output("Extracting statistics...") + pywikibot.output("Extracting statistics...") data = data['query'] # "query" entry of data. dict = data['statistics'] # "statistics" entry of "query" dict. return dict
def treat(self): - page = wikipedia.Page(self.site, self.your_page) + page = pywikibot.Page(self.site, self.your_page) if page.exists(): - wikipedia.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be completed with:\n' % self.your_page ) + pywikibot.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be completed with:\n' % self.your_page ) text = page.get() newtext = self.newraw() - wikipedia.output(newtext) - choice = wikipedia.inputChoice(u'Do you want to add these on wikitable?', ['Yes', 'No'], ['y', 'N'], 'N') + pywikibot.output(newtext) + choice = pywikibot.inputChoice(u'Do you want to add these on wikitable?', ['Yes', 'No'], ['y', 'N'], 'N') text = text[:-3] + newtext - summ = wikipedia.translate(self.site, summary_update) + summ = pywikibot.translate(self.site, summary_update) if choice == 'y': try: page.put(u''.join(text), summ) except: - wikipedia.output(u'Impossible to edit. It may be an edit conflict... Skipping...') + pywikibot.output(u'Impossible to edit. It may be an edit conflict... Skipping...') else: - wikipedia.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be created with:\n' % self.your_page ) + pywikibot.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be created with:\n' % self.your_page ) newtext = self.newtable()+self.newraw() - wikipedia.output(newtext) - summ = wikipedia.translate(self.site, summary_creation) - choice = wikipedia.inputChoice(u'Do you want to accept this page creation?', ['Yes', 'No'], ['y', 'N'], 'N') + pywikibot.output(newtext) + summ = pywikibot.translate(self.site, summary_creation) + choice = pywikibot.inputChoice(u'Do you want to accept this page creation?', ['Yes', 'No'], ['y', 'N'], 'N') if choice == 'y': try: page.put(newtext, summ) - except wikipedia.LockedPage: - wikipedia.output(u"Page %s is locked; skipping." % title) - except wikipedia.EditConflict: - wikipedia.output(u'Skipping %s because of edit conflict' % title) - except wikipedia.SpamfilterError, error: - wikipedia.output(u'Cannot change %s because of spam blacklist entry %s' % (title, error.url)) + except pywikibot.LockedPage: + pywikibot.output(u"Page %s is locked; skipping." % title) + except pywikibot.EditConflict: + pywikibot.output(u'Skipping %s because of edit conflict' % title) + except pywikibot.SpamfilterError, error: + pywikibot.output(u'Cannot change %s because of spam blacklist entry %s' % (title, error.url))
def newraw(self): newtext = ('\n|----\n!'''+ self.date() +'''') # new raw for date and stats @@ -124,22 +124,22 @@ list = self.dict.keys() list.sort() for name in self.dict: - wikipedia.output("There are "+str(self.dict[name])+" "+name) + pywikibot.output("There are "+str(self.dict[name])+" "+name)
def idle(self, retry_idle_time): time.sleep(retry_idle_time) - wikipedia.output(u"Starting in %i second..." % retry_idle_time) + pywikibot.output(u"Starting in %i second..." % retry_idle_time) time.sleep(retry_idle_time)
def main(your_page): screen = False # If True it would not edit the wiki, only output statistics _page = None
- wikipedia.output("\nBuilding the bot...") - for arg in wikipedia.handleArgs(): # Parse command line arguments + pywikibot.output("\nBuilding the bot...") + for arg in pywikibot.handleArgs(): # Parse command line arguments if arg.startswith('-page'): if len(arg) == 5: - _page = wikipedia.input(u'On what page do you want to add statistics?') + _page = pywikibot.input(u'On what page do you want to add statistics?') else: _page = arg[6:] if arg.startswith("-screen"): @@ -147,7 +147,7 @@ if not _page: _page = your_page if not screen: - wikipedia.output("The bot will add statistics on %s.\n" % _page ) + pywikibot.output("The bot will add statistics on %s.\n" % _page ) bot = StatisticsBot(screen, _page) # Launch the instance of a StatisticsBot bot.run() # Execute the 'run' method
@@ -155,4 +155,4 @@ try: main(your_page) finally: - wikipedia.stopme() + pywikibot.stopme()
Modified: trunk/pywikipedia/table2wiki.py =================================================================== --- trunk/pywikipedia/table2wiki.py 2010-10-09 19:32:57 UTC (rev 8630) +++ trunk/pywikipedia/table2wiki.py 2010-10-09 21:01:00 UTC (rev 8631) @@ -53,9 +53,11 @@ # Distributed under the terms of the MIT license. # __version__='$Id$' +#
import re, sys, time -import wikipedia, config, pagegenerators +import wikipedia as pywikibot +import config, pagegenerators
# This is required for the text that is shown when you run this script # with the parameter -help. @@ -133,7 +135,7 @@ tableTagR = re.compile('<table', re.IGNORECASE) for entry in self.xmldump.parse(): if tableTagR.search(entry.text): - yield wikipedia.Page(wikipedia.getSite(), entry.title) + yield pywikibot.Page(pywikibot.getSite(), entry.title)
class Table2WikiRobot: def __init__(self, generator, quietMode = False): @@ -161,7 +163,7 @@ num = 1 while num != 0: newTable, num = re.subn("([^\r\n]{1})(<[tT]{1}[dDhHrR]{1})", - r"\1\r\n\2", newTable) + r"\1\r\n\2", newTable)
################## # every open-tag gets a new line. @@ -170,25 +172,27 @@ ################## # Note that we added the ## characters in markActiveTables(). # <table> tag with attributes, with more text on the same line - newTable = re.sub("(?i)[\r\n]*?<##table## (?P<attr>[\w\W]*?)>(?P<more>[\w\W]*?)[\r\n ]*", - r"\r\n{| \g<attr>\r\n\g<more>", newTable) + newTable = re.sub( + "(?i)[\r\n]*?<##table## (?P<attr>[\w\W]*?)>(?P<more>[\w\W]*?)[\r\n ]*", + r"\r\n{| \g<attr>\r\n\g<more>", newTable) # <table> tag without attributes, with more text on the same line newTable = re.sub("(?i)[\r\n]*?<##table##>(?P<more>[\w\W]*?)[\r\n ]*", - r"\r\n{|\n\g<more>\r\n", newTable) + r"\r\n{|\n\g<more>\r\n", newTable) # <table> tag with attributes, without more text on the same line newTable = re.sub("(?i)[\r\n]*?<##table## (?P<attr>[\w\W]*?)>[\r\n ]*", - r"\r\n{| \g<attr>\r\n", newTable) + r"\r\n{| \g<attr>\r\n", newTable) # <table> tag without attributes, without more text on the same line newTable = re.sub("(?i)[\r\n]*?<##table##>[\r\n ]*", - "\r\n{|\r\n", newTable) + "\r\n{|\r\n", newTable) # end </table> newTable = re.sub("(?i)[\s]*</##table##>", "\r\n|}", newTable)
################## # caption with attributes - newTable = re.sub("(?i)<caption (?P<attr>[\w\W]*?)>(?P<caption>[\w\W]*?)</caption>", - r"\r\n|+\g<attr> | \g<caption>", newTable) + newTable = re.sub( + "(?i)<caption (?P<attr>[\w\W]*?)>(?P<caption>[\w\W]*?)</caption>", + r"\r\n|+\g<attr> | \g<caption>", newTable) # caption without attributes newTable = re.sub("(?i)<caption>(?P<caption>[\w\W]*?)</caption>", r"\r\n|+ \g<caption>", newTable) @@ -196,56 +200,61 @@ ################## # <th> often people don't write them within <tr>, be warned! # <th> with attributes - newTable = re.sub("(?i)[\r\n]+<th(?P<attr> [^>]*?)>(?P<header>[\w\W]*?)</th>", - r"\r\n!\g<attr> | \g<header>\r\n", newTable) + newTable = re.sub( + "(?i)[\r\n]+<th(?P<attr> [^>]*?)>(?P<header>[\w\W]*?)</th>", + r"\r\n!\g<attr> | \g<header>\r\n", newTable)
# <th> without attributes newTable = re.sub("(?i)[\r\n]+<th>(?P<header>[\w\W]*?)</th>", r"\r\n! \g<header>\r\n", newTable)
- # fail save. sometimes people forget </th> # <th> without attributes, without closing </th> newTable, n = re.subn("(?i)[\r\n]+<th>(?P<header>[\w\W]*?)[\r\n]+", r"\r\n! \g<header>\r\n", newTable) if n>0: - warning_messages.append(u'WARNING: found <th> without </th>. (%d occurences)\n' % n) + warning_messages.append( + u'WARNING: found <th> without </th>. (%d occurences)\n' % n) warnings += n
# <th> with attributes, without closing </th> - newTable, n = re.subn("(?i)[\r\n]+<th(?P<attr> [^>]*?)>(?P<header>[\w\W]*?)[\r\n]+", - r"\n!\g<attr> | \g<header>\r\n", newTable) + newTable, n = re.subn( + "(?i)[\r\n]+<th(?P<attr> [^>]*?)>(?P<header>[\w\W]*?)[\r\n]+", + r"\n!\g<attr> | \g<header>\r\n", newTable) if n>0: - warning_messages.append(u'WARNING: found <th ...> without </th>. (%d occurences\n)' % n) + warning_messages.append( + u'WARNING: found <th ...> without </th>. (%d occurences\n)' % n) warnings += n
################## # <tr> with attributes newTable = re.sub("(?i)[\r\n]*<tr(?P<attr> [^>]*?)>[\r\n]*", - r"\r\n|-\g<attr>\r\n", newTable) + r"\r\n|-\g<attr>\r\n", newTable)
# <tr> without attributes newTable = re.sub("(?i)[\r\n]*<tr>[\r\n]*", - r"\r\n|-\r\n", newTable) + r"\r\n|-\r\n", newTable)
################## # normal <td> without arguments newTable = re.sub("(?i)[\r\n]+<td>(?P<cell>[\w\W]*?)</td>", - r"\r\n| \g<cell>\r\n", newTable) + r"\r\n| \g<cell>\r\n", newTable)
################## # normal <td> with arguments - newTable = re.sub("(?i)[\r\n]+<td(?P<attr> [^>]*?)>(?P<cell>[\w\W]*?)</td>", - r"\r\n|\g<attr> | \g<cell>", newTable) + newTable = re.sub( + "(?i)[\r\n]+<td(?P<attr> [^>]*?)>(?P<cell>[\w\W]*?)</td>", + r"\r\n|\g<attr> | \g<cell>", newTable)
# WARNING: this sub might eat cells of bad HTML, but most likely it # will correct errors # TODO: some more docu please newTable, n = re.subn("(?i)[\r\n]+<td>(?P<cell>[^\r\n]*?)<td>", - r"\r\n| \g<cell>\r\n", newTable) + r"\r\n| \g<cell>\r\n", newTable) if n>0: - warning_messages.append(u'<td> used where </td> was expected. (%d occurences)\n' % n) + warning_messages.append( + u'<td> used where </td> was expected. (%d occurences)\n' % n) warnings += n
# fail save, sometimes it's a <td><td></tr> @@ -261,20 +270,24 @@ newTable, n = re.subn("[\r\n]+<(td|TD)([^>]+?)>([^\r\n]*?)</(td|TD)>", r"\r\n|\2 | \3\r\n", newTable) if n>0: - warning_messages.append(u'WARNING: (sorry, bot code unreadable (1). I don't know why this warning is given.) (%d occurences)\n' % n) + warning_messages.append( + u'WARNING: (sorry, bot code unreadable (1). I don't know why this warning is given.) (%d occurences)\n' % n)
# fail save. sometimes people forget </td> # <td> without arguments, with missing </td> newTable, n = re.subn("(?i)<td>(?P<cell>[^<]*?)[\r\n]+", r"\r\n| \g<cell>\r\n", newTable) if n>0: - warning_messages.append(u'NOTE: Found <td> without </td>. This shouldn't cause problems.\n') + warning_messages.append( + u'NOTE: Found <td> without </td>. This shouldn't cause problems.\n')
# <td> with attributes, with missing </td> - newTable, n = re.subn("(?i)[\r\n]*<td(?P<attr> [^>]*?)>(?P<cell>[\w\W]*?)[\r\n]+", - r"\r\n|\g<attr> | \g<cell>\r\n", newTable) + newTable, n = re.subn( + "(?i)[\r\n]*<td(?P<attr> [^>]*?)>(?P<cell>[\w\W]*?)[\r\n]+", + r"\r\n|\g<attr> | \g<cell>\r\n", newTable) if n > 0: - warning_messages.append(u'NOTE: Found <td> without </td>. This shouldn't cause problems.\n') + warning_messages.append( + u'NOTE: Found <td> without </td>. This shouldn't cause problems.\n')
################## @@ -309,7 +322,7 @@ num = 1 while num != 0: newTable, num = re.subn("({|[\w\W]*?)\n[ \t]+([\w\W]*?|})", - r"\1\r\n\2", newTable) + r"\1\r\n\2", newTable)
################## # kills additional spaces after | or ! or {| @@ -344,8 +357,9 @@ # group 2 is the value of the attribute we want to fix here. # We recognize it by searching for a string of non-whitespace characters # - [^\s]+? - which is not embraced by quotation marks - [^"] - newTable, num = re.subn(r'([\r\n]+(?:|-|{|)[^\r\n|]+) *= *([^"\s>]+)', - r'\1="\2"', newTable, 1) + newTable, num = re.subn( + r'([\r\n]+(?:|-|{|)[^\r\n|]+) *= *([^"\s>]+)', + r'\1="\2"', newTable, 1)
num = 1 while num != 0: @@ -354,16 +368,18 @@ # cell contents which accidentially contain an equal sign. # Group 1 and 2 are anologously to the previous regular expression, # group 3 are the remaining attribute key - value pairs. - newTable, num = re.subn(r'([\r\n]+(?:!||)[^\r\n|]+) *= *([^"\s>]+)([^|\r\n]*)|', - r'\1="\2"\3|', newTable, 1) + newTable, num = re.subn( + r'([\r\n]+(?:!||)[^\r\n|]+) *= *([^"\s>]+)([^|\r\n]*)|', + r'\1="\2"\3|', newTable, 1)
################## # merge two short <td>s num = 1 while num != 0: - newTable, num = re.subn("[\r\n]+(|[^|-}]{1}[^\n\r]{0,35})" + - "[\r\n]+(|[^|-}]{1}[^\r\n]{0,35})[\r\n]+", - r"\r\n\1 |\2\r\n", newTable) + newTable, num = re.subn( + "[\r\n]+(|[^|-}]{1}[^\n\r]{0,35})" + + "[\r\n]+(|[^|-}]{1}[^\r\n]{0,35})[\r\n]+", + r"\r\n\1 |\2\r\n", newTable) #### # add a new line if first is * or # newTable = re.sub("[\r\n]+| ([*#]{1})", @@ -378,15 +394,17 @@ newTable = re.sub("([\r\n]+! +)align="center" +|", r"\1", newTable) # if there are other attributes, simply strip the align="center" - newTable = re.sub("([\r\n]+![^\r\n|]+?)align="center"([^\n\r|]+?|)", - r"\1 \2", newTable) + newTable = re.sub( + "([\r\n]+![^\r\n|]+?)align="center"([^\n\r|]+?|)", + r"\1 \2", newTable)
################## # kill additional spaces within arguments num = 1 while num != 0: - newTable, num = re.subn("[\r\n]+(||!)([^|\r\n]*?)[ \t]{2,}([^\r\n]+?)", - r"\r\n\1\2 \3", newTable) + newTable, num = re.subn( + "[\r\n]+(||!)([^|\r\n]*?)[ \t]{2,}([^\r\n]+?)", + r"\r\n\1\2 \3", newTable)
################## # I hate those long lines because they make a wall of letters @@ -396,8 +414,9 @@ while num != 0: # TODO: how does this work? docu please. # why are only äöüß used, but not other special characters? - newTable, num = re.subn("(\r\n[A-Z]{1}[^\n\r]{200,}?[a-zäöüß].)\ ([A-ZÄÖÜ]{1}[^\n\r]{200,})", - r"\1\r\n\2", newTable) + newTable, num = re.subn( + "(\r\n[A-Z]{1}[^\n\r]{200,}?[a-zäöüß].)\ ([A-ZÄÖÜ]{1}[^\n\r]{200,})", + r"\1\r\n\2", newTable) return newTable, warnings, warning_messages
def markActiveTables(self, text): @@ -410,15 +429,19 @@ tableStartTagR = re.compile("<table", re.IGNORECASE) tableEndTagR = re.compile("</table>", re.IGNORECASE)
- text = wikipedia.replaceExcept(text, tableStartTagR, "<##table##", exceptions = ['comment', 'math', 'nowiki', 'pre', 'source']) - text = wikipedia.replaceExcept(text, tableEndTagR, "</##table##>", exceptions = ['comment', 'math', 'nowiki', 'pre', 'source']) + text = pywikibot.replaceExcept(text, tableStartTagR, "<##table##", + exceptions=['comment', 'math', + 'nowiki', 'pre', 'source']) + text = pywikibot.replaceExcept(text, tableEndTagR, "</##table##>", + exceptions=['comment', 'math', + 'nowiki', 'pre', 'source']) return text
def findTable(self, text): """ Finds the first HTML table (which can contain nested tables) inside a - text. - Returns the table and the start and end position inside the text. + text. Returns the table and the start and end position inside the text. + """ # Note that we added the ## characters in markActiveTables(). markedTableStartTagR = re.compile("<##table##", re.IGNORECASE) @@ -469,49 +492,51 @@ if not table: # no more HTML tables left break - wikipedia.output(">> Table %i <<" % (convertedTables + 1)) + pywikibot.output(">> Table %i <<" % (convertedTables + 1)) # convert the current table newTable, warningsThisTable, warnMsgsThisTable = self.convertTable(table) # show the changes for this table if not self.quietMode: - wikipedia.showDiff(table.replace('##table##', 'table'), newTable) + pywikibot.showDiff(table.replace('##table##', 'table'), + newTable) warningSum += warningsThisTable for msg in warnMsgsThisTable: - warningMessages += 'In table %i: %s' % (convertedTables + 1, msg) + warningMessages += 'In table %i: %s' % (convertedTables + 1, + msg) text = text[:start] + newTable + text[end:] convertedTables += 1
- wikipedia.output(warningMessages) + pywikibot.output(warningMessages)
return text, convertedTables, warningSum
def treat(self, page): ''' Loads a page, converts all HTML tables in its text to wiki syntax, - and saves the converted text. - Returns True if the converted table was successfully saved, otherwise - returns False. + and saves the converted text. Returns True if the converted table was + successfully saved, otherwise returns False. ''' - wikipedia.output(u'\n>>> %s <<<' % page.title()) + pywikibot.output(u'\n>>> %s <<<' % page.title()) site = page.site() try: text = page.get() - except wikipedia.NoPage: - wikipedia.output(u"ERROR: couldn't find %s" % page.title()) + except pywikibot.NoPage: + pywikibot.output(u"ERROR: couldn't find %s" % page.title()) return False - except wikipedia.IsRedirectPage: - wikipedia.output(u'Skipping redirect %s' % page.title()) + except pywikibot.IsRedirectPage: + pywikibot.output(u'Skipping redirect %s' % page.title()) return False newText, convertedTables, warningSum = self.convertAllHTMLTables(text)
# Check if there are any marked tags left markedTableTagR = re.compile("<##table##|</##table##>", re.IGNORECASE) if markedTableTagR.search(newText): - wikipedia.output(u'ERROR: not all marked table start or end tags processed!') + pywikibot.output( + u'ERROR: not all marked table start or end tags processed!') return
if convertedTables == 0: - wikipedia.output(u"No changes were necessary.") + pywikibot.output(u"No changes were necessary.") else: if config.table2wikiAskOnlyWarnings and warningSum == 0: doUpload = True @@ -520,15 +545,21 @@ doUpload = True else: print "There were %i replacement(s) that might lead to bad output." % warningSum - doUpload = (wikipedia.input(u'Do you want to change the page anyway? [y|N]') == "y") + doUpload = (pywikibot.input( + u'Do you want to change the page anyway? [y|N]') == "y") if doUpload: # get edit summary message if warningSum == 0: - wikipedia.setAction(wikipedia.translate(site.lang, msg_no_warnings)) + pywikibot.setAction( + pywikibot.translate(site.lang, msg_no_warnings)) elif warningSum == 1: - wikipedia.setAction(wikipedia.translate(site.lang, msg_one_warning) % warningSum) + pywikibot.setAction( + pywikibot.translate(site.lang, msg_one_warning) + % warningSum) else: - wikipedia.setAction(wikipedia.translate(site.lang, msg_multiple_warnings) % warningSum) + pywikibot.setAction( + pywikibot.translate(site.lang, msg_multiple_warnings) + % warningSum) page.put_async(newText)
def run(self): @@ -555,10 +586,11 @@ # to work on. genFactory = pagegenerators.GeneratorFactory()
- for arg in wikipedia.handleArgs(): + for arg in pywikibot.handleArgs(): if arg.startswith('-xml'): if len(arg) == 4: - xmlfilename = wikipedia.input(u'Please enter the XML dump's filename:') + xmlfilename = pywikibot.input( + u'Please enter the XML dump's filename:') else: xmlfilename = arg[5:] gen = TableXmlDumpPageGenerator(xmlfilename) @@ -590,7 +622,7 @@ # connect the title's parts with spaces if page_title != []: page_title = ' '.join(page_title) - page = wikipedia.Page(wikipedia.getSite(), page_title) + page = pywikibot.Page(pywikibot.getSite(), page_title) gen = iter([page])
if not gen: @@ -603,10 +635,10 @@ bot = Table2WikiRobot(preloadingGen, quietMode) bot.run() else: - wikipedia.showHelp('table2wiki') + pywikibot.showHelp('table2wiki')
if __name__ == "__main__": try: main() finally: - wikipedia.stopme() + pywikibot.stopme()
Modified: trunk/pywikipedia/templatecount.py =================================================================== --- trunk/pywikipedia/templatecount.py 2010-10-09 19:32:57 UTC (rev 8630) +++ trunk/pywikipedia/templatecount.py 2010-10-09 21:01:00 UTC (rev 8631) @@ -1,9 +1,9 @@ #!/usr/bin/python # -*- coding: utf-8 -*- """ -This script will display the list of pages transcluding a given list of templates. -It can also be used to simply count the number of pages (rather than listing each -individually). +This script will display the list of pages transcluding a given list of +templates. It can also be used to simply count the number of pages (rather than +listing each individually).
Syntax: python templatecount.py command [arguments]
@@ -34,10 +34,11 @@ # __version__ = '$Id$'
-import wikipedia, config -import replace, pagegenerators import re, sys, string import datetime +import wikipedia as pywikibot +import config +import replace, pagegenerators
templates = ['ref', 'note', 'ref label', 'note label', 'reflist']
@@ -45,16 +46,20 @@ #def __init__(self): #Nothing def countTemplates(self, templates, namespaces): - mysite = wikipedia.getSite() + mysite = pywikibot.getSite() mytpl = mysite.template_namespace()+':' - finalText = [u'Number of transclusions per template',u'------------------------------------'] + finalText = [u'Number of transclusions per template', u'-' * 36] total = 0 - # The names of the templates are the keys, and the numbers of transclusions are the values. + # The names of the templates are the keys, and the numbers of + # transclusions are the values. templateDict = {} for template in templates: - gen = pagegenerators.ReferringPageGenerator(wikipedia.Page(mysite, mytpl + template), onlyTemplateInclusion = True) + gen = pagegenerators.ReferringPageGenerator( + pywikibot.Page(mysite, mytpl + template), + onlyTemplateInclusion = True) if namespaces: - gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces) + gen = pagegenerators.NamespaceFilterPageGenerator(gen, + namespaces) count = 0 for page in gen: count += 1 @@ -62,25 +67,32 @@ finalText.append(u'%s: %d' % (template, count)) total += count for line in finalText: - wikipedia.output(line, toStdout=True) - wikipedia.output(u'TOTAL: %d' % total, toStdout=True) - wikipedia.output(u'Report generated on %s' % datetime.datetime.utcnow().isoformat(), toStdout=True) + pywikibot.output(line, toStdout=True) + pywikibot.output(u'TOTAL: %d' % total, toStdout=True) + pywikibot.output(u'Report generated on %s' + % datetime.datetime.utcnow().isoformat(), + toStdout=True) return templateDict
def listTemplates(self, templates, namespaces): - mysite = wikipedia.getSite() + mysite = pywikibot.getSite() count = 0 - # The names of the templates are the keys, and lists of pages transcluding templates are the values. + # The names of the templates are the keys, and lists of pages + # transcluding templates are the values. templateDict = {} finalText = [u'List of pages transcluding templates:'] for template in templates: finalText.append(u'* %s' % template) - finalText.append(u'------------------------------------') + finalText.append(u'-' * 36) for template in templates: transcludingArray = [] - gen = pagegenerators.ReferringPageGenerator(wikipedia.Page(mysite, mysite.template_namespace() + ':' + template), onlyTemplateInclusion = True) + gen = pagegenerators.ReferringPageGenerator( + pywikibot.Page(mysite, + mysite.template_namespace() + ':' + template), + onlyTemplateInclusion=True) if namespaces: - gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces) + gen = pagegenerators.NamespaceFilterPageGenerator(gen, + namespaces) for page in gen: finalText.append(u'%s' % page.title()) count += 1 @@ -88,8 +100,10 @@ templateDict[template] = transcludingArray; finalText.append(u'Total page count: %d' % count) for line in finalText: - wikipedia.output(line, toStdout=True) - wikipedia.output(u'Report generated on %s' % datetime.datetime.utcnow().isoformat(), toStdout=True) + pywikibot.output(line, toStdout=True) + pywikibot.output(u'Report generated on %s' + % datetime.datetime.utcnow().isoformat(), + toStdout=True) return templateDict
def main(): @@ -97,7 +111,7 @@ argsList = [] namespaces = []
- for arg in wikipedia.handleArgs(): + for arg in pywikibot.handleArgs(): if arg == '-count': operation = "Count" elif arg == '-list': @@ -111,15 +125,17 @@ argsList.append(arg)
if operation == None: - wikipedia.showHelp('templatecount') + pywikibot.showHelp('templatecount') else: robot = TemplateCountRobot() if not argsList: argsList = templates choice = '' if 'reflist' in argsList: - wikipedia.output(u'NOTE: it will take a long time to count "reflist".') - choice = wikipedia.inputChoice(u'Proceed anyway?', ['yes', 'no', 'skip'], ['y', 'n', 's'], 'y') + pywikibot.output( + u'NOTE: it will take a long time to count "reflist".') + choice = pywikibot.inputChoice( + u'Proceed anyway?', ['yes', 'no', 'skip'], ['y', 'n', 's'], 'y') if choice == 's': argsList.remove('reflist') if choice == 'n': @@ -133,4 +149,4 @@ try: main() finally: - wikipedia.stopme() + pywikibot.stopme()
Modified: trunk/pywikipedia/testfamily.py =================================================================== --- trunk/pywikipedia/testfamily.py 2010-10-09 19:32:57 UTC (rev 8630) +++ trunk/pywikipedia/testfamily.py 2010-10-09 21:01:00 UTC (rev 8631) @@ -23,34 +23,34 @@ """ # # (C) Yuri Astrakhan, 2005 +# (C) Pywikipedia bot team, 2006-2010 # # Distributed under the terms of the MIT license. # __version__ = '$Id$' #
-import sys, wikipedia, traceback +import sys +import wikipedia as pywikibot +import traceback
- -#=========== - def testSite(site): try: - wikipedia.getall(site, [wikipedia.Page(site, 'Any page name')]) + pywikibot.getall(site, [pywikibot.Page(site, 'Any page name')]) except KeyboardInterrupt: raise - except wikipedia.NoSuchSite: - wikipedia.output( u'No such language %s' % site.lang ) + except pywikibot.NoSuchSite: + pywikibot.output( u'No such language %s' % site.lang ) except: - wikipedia.output( u'Error processing language %s' % site.lang ) - wikipedia.output( u''.join(traceback.format_exception(*sys.exc_info()))) + pywikibot.output( u'Error processing language %s' % site.lang ) + pywikibot.output( u''.join(traceback.format_exception(*sys.exc_info())))
def main(): all = False language = None fam = None wikimedia = False - for arg in wikipedia.handleArgs(): + for arg in pywikibot.handleArgs(): if arg == '-all': all = True elif arg[0:7] == '-langs:': @@ -60,11 +60,13 @@ elif arg[0:10] == '-wikimedia': wikimedia = True
- mySite = wikipedia.getSite() + mySite = pywikibot.getSite() if language is None: language = mySite.lang if wikimedia: - families = ['wikipedia', 'wiktionary', 'wikiquote', 'wikisource', 'wikibooks', 'wikinews', 'wikiversity', 'meta', 'commons', 'mediawiki', 'species', 'incubator', 'test'] + families = ['wikipedia', 'wiktionary', 'wikiquote', 'wikisource', + 'wikibooks', 'wikinews', 'wikiversity', 'meta', 'commons', + 'mediawiki', 'species', 'incubator', 'test'] elif fam is not None: families = fam.split(',') else: @@ -72,23 +74,24 @@
for family in families: try: - fam = wikipedia.Family(family) + fam = pywikibot.Family(family) except ValueError: - wikipedia.output(u'No such family %s' % family) + pywikibot.output(u'No such family %s' % family) continue if all: for lang in fam.langs.iterkeys(): - testSite(wikipedia.getSite(lang, family)) + testSite(pywikibot.getSite(lang, family)) else: languages = language.split(',') for lang in languages: try: - testSite(wikipedia.getSite(lang, family)) - except wikipedia.NoSuchSite: - wikipedia.output(u'No such language %s in family %s' % (lang, family)) + testSite(pywikibot.getSite(lang, family)) + except pywikibot.NoSuchSite: + pywikibot.output(u'No such language %s in family %s' + % (lang, family))
if __name__ == "__main__": try: main() finally: - wikipedia.stopme() + pywikibot.stopme()
Modified: trunk/pywikipedia/unlink.py =================================================================== --- trunk/pywikipedia/unlink.py 2010-10-09 19:32:57 UTC (rev 8630) +++ trunk/pywikipedia/unlink.py 2010-10-09 21:01:00 UTC (rev 8631) @@ -24,9 +24,10 @@
__version__='$Id$'
-import wikipedia, pagegenerators +import re +import wikipedia as pywikibot +import pagegenerators import editarticle -import re
# Summary messages in different languages msg = { @@ -44,18 +45,16 @@ 'pt':u'Bot: Retirando link para "%s"', }
+ class UnlinkBot:
def __init__(self, pageToUnlink, namespaces, always): self.pageToUnlink = pageToUnlink - gen = pagegenerators.ReferringPageGenerator(pageToUnlink) - if namespaces != []: gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces) self.generator = pagegenerators.PreloadingGenerator(gen) - - linktrail = wikipedia.getSite().linktrail() + linktrail = pywikibot.getSite().linktrail() # The regular expression which finds links. Results consist of four groups: # group title is the target page title, that is, everything before | or ]. # group section is the page section. It'll include the # to make life easier for us. @@ -65,6 +64,8 @@ self.linkR = re.compile(r'[[(?P<title>[^]|#]*)(?P<section>#[^]|]*)?(|(?P<label>[^]]*))?]](?P<linktrail>' + linktrail + ')') self.always = always self.done = False + self.comment = pywikibot.translate(pywikibot.getSite(), msg) \ + % self.pageToUnlink.title()
def handleNextLink(self, text, match, context = 100): """ @@ -80,8 +81,8 @@ or self.pageToUnlink.site().isInterwikiLink(match.group('title')) \ or match.group('section'): return text, False - - linkedPage = wikipedia.Page(self.pageToUnlink.site(), match.group('title')) + linkedPage = pywikibot.Page(self.pageToUnlink.site(), + match.group('title')) # Check whether the link found is to the current page itself. if linkedPage != self.pageToUnlink: # not a self-link @@ -92,16 +93,16 @@ if self.always: choice = 'a' else: - wikipedia.output( + pywikibot.output( text[max(0, match.start() - context) : match.start()] \ + '\03{lightred}' + text[match.start() : match.end()] \ + '\03{default}' + text[match.end() : match.end() + context]) - choice = wikipedia.inputChoice( + choice = pywikibot.inputChoice( u'\nWhat shall be done with this link?\n', ['unlink', 'skip', 'edit', 'more context', 'unlink all', 'quit'], ['U', 's', 'e', 'm', 'a', 'q'], 'u') - wikipedia.output(u'') + pywikibot.output(u'')
if choice == 's': # skip this link @@ -116,7 +117,8 @@ return text, True elif choice == 'm': # show more context by recursive self-call - return self.handleNextLink(text, match, context = context + 100) + return self.handleNextLink(text, match, + context=context + 100) elif choice == 'a': self.always = True elif choice == 'q': @@ -129,7 +131,8 @@ def treat(self, page): # Show the title of the page we're working on. # Highlight the title in purple. - wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) + pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" + % page.title()) try: oldText = page.get() text = oldText @@ -138,32 +141,32 @@ match = self.linkR.search(text, pos = curpos) if not match: break - # Make sure that next time around we will not find this same hit. + # Make sure that next time around we will not find this same + # hit. curpos = match.start() + 1 text, jumpToBeginning = self.handleNextLink(text, match) if jumpToBeginning: curpos = 0 - if oldText == text: - wikipedia.output(u'No changes necessary.') + pywikibot.output(u'No changes necessary.') else: - wikipedia.showDiff(oldText, text) - page.put(text) - except wikipedia.NoPage: - wikipedia.output(u"Page %s does not exist?!" % page.aslink()) - except wikipedia.IsRedirectPage: - wikipedia.output(u"Page %s is a redirect; skipping." % page.aslink()) - except wikipedia.LockedPage: - wikipedia.output(u"Page %s is locked?!" % page.aslink()) + pywikibot.showDiff(oldText, text) + page.put(text, self.comment) + except pywikibot.NoPage: + pywikibot.output(u"Page %s does not exist?!" + % page.title(asLink=True)) + except pywikibot.IsRedirectPage: + pywikibot.output(u"Page %s is a redirect; skipping." + % page.title(asLink=True)) + except pywikibot.LockedPage: + pywikibot.output(u"Page %s is locked?!" % page.title(asLink=True))
def run(self): - comment = wikipedia.translate(wikipedia.getSite(), msg) % self.pageToUnlink.title() - wikipedia.setAction(comment) - for page in self.generator: if self.done: break self.treat(page)
+ def main(): # This temporary array is used to read the page title if one single # page that should be unlinked. @@ -173,7 +176,7 @@ namespaces = [] always = False
- for arg in wikipedia.handleArgs(): + for arg in pywikibot.handleArgs(): if arg.startswith('-namespace:'): try: namespaces.append(int(arg[11:])) @@ -185,14 +188,14 @@ pageTitle.append(arg)
if pageTitle: - page = wikipedia.Page(wikipedia.getSite(), ' '.join(pageTitle)) + page = pywikibot.Page(pywikibot.getSite(), ' '.join(pageTitle)) bot = UnlinkBot(page, namespaces, always) bot.run() else: - wikipedia.showHelp('unlink') + pywikibot.showHelp('unlink')
if __name__ == "__main__": try: main() finally: - wikipedia.stopme() + pywikibot.stopme()
Modified: trunk/pywikipedia/us-states.py =================================================================== --- trunk/pywikipedia/us-states.py 2010-10-09 19:32:57 UTC (rev 8630) +++ trunk/pywikipedia/us-states.py 2010-10-09 21:01:00 UTC (rev 8631) @@ -14,14 +14,17 @@ -force: Don't ask whether to create pages, just create them.
""" -__version__ = '$Id$' # # (C) Andre Engels, 2004 +# (C) Pywikipedia bot team, 2004-2010 # # Distributed under the terms of the MIT license. # +__version__ = '$Id$' +#
-import re,wikipedia,sys +import re, sys +import wikipedia as pywikibot
def main(): start = '0' @@ -84,48 +87,60 @@ 'Wyoming': 'WY' }
- for arg in wikipedia.handleArgs(): + for arg in pywikibot.handleArgs(): if arg.startswith('-start:'): start = arg[7:] elif arg == '-force': force = True else: - wikipedia.output(u'Warning: argument "%s" not understood; ignoring.'%arg) + pywikibot.output( + u'Warning: argument "%s" not understood; ignoring.' % arg)
- mysite = wikipedia.getSite() + mysite = pywikibot.getSite() for p in mysite.allpages(start = start): for sn in abbrev: R=re.compile('[^[]]*' + '%2C_' + sn) for res in R.findall(p.title()): - pl=wikipedia.Page(mysite, p.title().replace(sn,abbrev[sn])) - # A bit hacking here - the real work is done in the 'except wikipedia.NoPage' - # part rather than the 'try'. + pl=pywikibot.Page(mysite, p.title().replace(sn,abbrev[sn])) + # A bit hacking here - the real work is done in the + # 'except pywikibot.NoPage' part rather than the 'try'. try: goal = pl.getRedirectTarget().title() - if wikipedia.Page(mysite, goal): - wikipedia.output(u"Not creating %s - redirect already exists." % goal) + if pywikibot.Page(mysite, goal): + pywikibot.output( + u"Not creating %s - redirect already exists." + % goal) else: - wikipedia.output(u"WARNING!!! %s already exists but redirects elsewhere!" % goal) - except wikipedia.IsNotRedirectPage: - wikipedia.output(u"WARNING!!! Page %s already exists and is not a redirect. Please check page!" % goal) - except wikipedia.NoPage: + pywikibot.output( + u"WARNING!!! %s already exists but redirects elsewhere!" + % goal) + except pywikibot.IsNotRedirectPage: + pywikibot.output( + u"WARNING!!! Page %s already exists and is not a redirect. Please check page!" + % goal) + except pywikibot.NoPage: change='' if p.isRedirectPage(): p2 = p.getRedirectTarget() - wikipeda.ouput(u'Note: goal page is redirect. Creating redirect to "%s" to avoid double redirect.'%p2.title().replace("%2C",",").replace("_"," ")) + wikipeda.ouput( + u'Note: goal page is redirect. Creating redirect to "%s" to avoid double redirect.' + % p2.title().replace("%2C",",").replace("_"," ")) else: p2 = p if force: change='y' else: while not change in ['y','n']: - wikipedia.output(u"Create redirect %s"%pl.title().replace("%2C",",").replace("_"," ")) + pywikibot.output( + u"Create redirect %s" % + pl.title().replace("%2C",",").replace("_"," ")) change = raw_input("(y/n)? ") if change=='y': text = '#REDIRECT [['+p2.title().replace("%2C",",").replace("_"," ")+']]' - pl.put(text, comment = wikipedia.translate(mysite, msg), minorEdit = '0') + pl.put(text, comment=pywikibot.translate(mysite, msg), + minorEdit = '0')
try: main() finally: - wikipedia.stopme() + pywikibot.stopme()
pywikipedia-svn@lists.wikimedia.org