Prianka has uploaded a new change for review. https://gerrit.wikimedia.org/r/194825
Change subject: Porting piper.py from pywikibot-compat to core. ...................................................................... Porting piper.py from pywikibot-compat to core. Change-Id: I7578faa86371ee60faaa2a71ec7d11c37c847885 --- A scripts/piper.py 1 file changed, 203 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/25/194825/1 diff --git a/scripts/piper.py b/scripts/piper.py new file mode 100644 index 0000000..611e76c --- /dev/null +++ b/scripts/piper.py @@ -0,0 +1,203 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +""" +This is a bot that uses external filtering programs to munge the +article text, for example: + + python pwb.py piper.py -filter:'tr A-Z a-z' Wikipedia:Sandbox + +Would lower case the article with tr(1). + +Muliple -filter commands can be specified: + + python pwb.py piper.py -filter:cat -filter:'tr A-Z a-z' -filter:'tr a-z A-Z' Wikipedia:Sandbox + + +Would pipe the article text through cat(1) (NOOP) and then lower case +it with tr(1) and upper case it again with tr(1) + +The following parameters are supported: + +¶ms; + + -always Always commit changes without asking you to accept them + + -filter: Filter the article text through this program, can be + given multiple times to filter through multiple programs in + the order which they are given + +In addition all command-line parameters are passed to +genFactory.handleArg() which means pagegenerators.py arguments are +supported. + +""" +# +# (C) Pywikibot team, 2008-2015 +# +# Distributed under the terms of the MIT license. +# +__version__ = '$Id$' +# + +import os +import pipes +import tempfile +import pywikibot +from pywikibot import pagegenerators +from pywikibot import i18n + +# This is required for the text that is shown when you run this script +# with the parameter -help. +docuReplacements = { + '¶ms;': pagegenerators.parameterHelp +} + + +class PiperBot(pywikibot.Bot): + # Edit summary message that should be used. + # NOTE: Put a good description here, and add translations, if possible! + + def __init__(self, generator, filters, always): + """ + Constructor. Parameters: + * generator - The page generator that determines on which pages + to work on. + * always - If True, don't prompt for changes + """ + self.generator = generator + self.always = always + self.filters = filters + self.comment = None + + def run(self): + # Set the edit summary message + s = ', '.join(self.filters) + m = i18n.twtranslate(pywikibot.Site().lang, 'piper') + if m: + self.comment = m.format(s) + for page in self.generator: + self.treat(page) + + def pipe(self, program, text): + """ + Pipes a given text through a given program and returns it + """ + + text = text.encode('utf-8') + + pipe = pipes.Template() + pipe.append(program.encode("ascii"), '--') + + # Create a temporary filename to save the piped stuff to + tempFilename = '%s.%s' % (tempfile.mktemp(), 'txt') + file = pipe.open(tempFilename, 'w') + file.write(text) + file.close() + + # Now retrieve the munged text + mungedText = open(tempFilename, 'r').read() + # clean up + os.unlink(tempFilename) + + unicode_text = unicode(mungedText, 'utf-8') + + return unicode_text + + def treat(self, page): + """ + Loads the given page, does some changes, and saves it. + """ + try: + # Load the page + text = page.get() + except pywikibot.NoPage: + pywikibot.output(u"Page %s does not exist; skipping." + % page.title(asLink=True)) + return + except pywikibot.IsRedirectPage: + pywikibot.output(u"Page %s is a redirect; skipping." + % page.title(asLink=True)) + return + + # Munge! + for program in self.filters: + text = self.pipe(program, text) + + # only save if something was changed + if text != page.get(): + # Show the title of the page we're working on. + # Highlight the title in purple. + pywikibot.output(u"\n\n>>> %s <<<" % page.title()) + # show what was changed + pywikibot.showDiff(page.get(), text) + if not self.always: + choice = pywikibot.inputChoice( + u'Do you want to accept these changes?', + ['Yes', 'No'], ['y', 'N'], 'N') + else: + choice = 'y' + if choice == 'y': + try: + # Save the page + page.put(text, self.comment) + except pywikibot.LockedPage: + pywikibot.output(u"Page %s is locked; skipping." + % page.title(asLink=True)) + except pywikibot.EditConflict: + pywikibot.output( + u'Skipping %s because of edit conflict' + % (page.title())) + except pywikibot.SpamfilterError, error: + pywikibot.output( + u'Cannot change %s because of spam blacklist entry %s' + % (page.title(), error.url)) + + +def main(*args): + # This factory is responsible for processing command line arguments + # that are also used by other scripts and that determine on which pages + # to work on. + genFactory = pagegenerators.GeneratorFactory() + # The generator gives the pages that should be worked upon. + gen = None + # This temporary array is used to read the page title if one single + # page to work on is specified by the arguments. + pageTitleParts = [] + # will become True when the user uses the -always flag. + always = False + # The program to pipe stuff through + filters = [] + + # Parse command line arguments + for arg in pywikibot.handle_args(args): + if arg.startswith("-filter:"): + prog = arg[8:] + filters.append(prog) + elif arg.startswith("-always"): + always = True + else: + # check if a standard argument like + # -start:XYZ or -ref:Asdf was given. + if not genFactory.handleArg(arg): + pageTitleParts.append(arg) + + if pageTitleParts != []: + # We will only work on a single page. + pageTitle = ' '.join(pageTitleParts) + page = pywikibot.Page(pywikibot.Site(), pageTitle) + gen = iter([page]) + + if not gen: + gen = genFactory.getCombinedGenerator() + if gen: + # The preloading generator is responsible for downloading multiple + # pages from the wiki simultaneously. + gen = pagegenerators.PreloadingGenerator(gen) + bot = PiperBot(gen, filters, always) + bot.run() + else: + pywikibot.showHelp() + + +if __name__ == "__main__": + main() -- To view, visit https://gerrit.wikimedia.org/r/194825 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I7578faa86371ee60faaa2a71ec7d11c37c847885 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Prianka <priyankajayaswal...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits