User:Stabbot/skill beautify.py

""" Skill beautification script
 * 1) -*- coding: utf-8  -*-

Run as:

% python2.4 skill_beautify.py -pages:PAGESFILE

Where PAGESFILE is the name of a text file containing a list of skills, one skill on each line, comments starting with '# ' (minus quotes), no blank lines.

Calls: ./skill_rewrite.pl """

from __future__ import generators import sys, re, time import wikipedia, pagegenerators, config import os import subprocess

def cleanup (text): p = subprocess.Popen ("./skill_rewrite.pl", shell = True, stdin = subprocess.PIPE, stdout = subprocess.PIPE) output = p.communicate (input = text)[0] return output

def main : pages = [] for arg in wikipedia.handleArgs : if arg.startswith ('-pages'): f = file (arg[7:], 'r') pages = [page.strip for page in f.readlines  if not page.startswith ('# ')] f.close else: pages.append (arg)

pages = [wikipedia.Page (wikipedia.getSite, title) for title in pages] wikipedia.setAction(wikipedia.translate(wikipedia.getSite, {'en': u'Robot: skill beautification project'}))

doAll = False choice = 'n'

gen = iter(pages) pregen = pagegenerators.PreloadingGenerator (gen, pageNumber = 5)

for page in pregen: try: text = page.get if not page.canBeEdited : wikipedia.output (u'Skipping locked page %s' % page.title ) continue except wikipedia.NoPage: wikipedia.output (u'Page %s not found' % page.title ) continue except wikipedia.IsRedirectPage: text = page.get(get_redirect=True)

repText = cleanup (text)

if repText == text: print "Nothing to be done for: " + page.title else: print repText

if not doAll: choice = wikipedia.inputChoice (u'Commit?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice in ['a', 'A']: doAll = True if doAll or choice in ['y', 'Y']: destPage = page destPage.put (repText)

if __name__ == "__main__": try: main finally: wikipedia.stopme