Source code for scripts.piper

# -*- coding: utf-8 -*-
This bot uses external filtering programs for munging text.

For example:

    python piper -filter:"tr A-Z a-z" -page:Wikipedia:Sandbox

Would lower case the article with tr(1).

Muliple -filter commands can be specified:

    python piper -filter:cat -filter:"tr A-Z a-z" -filter:"tr a-z A-Z" \

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:

    -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

The following generators and filters are supported:

# (C) Pywikibot team, 2008-2020
# Distributed under the terms of the MIT license.
import os
import pipes
import tempfile

import pywikibot

from pywikibot import pagegenerators
from import (MultipleSitesBot, ExistingPageBot,
                           NoRedirectPageBot, AutomaticTWSummaryBot)

# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {'&params;': pagegenerators.parameterHelp}  # noqa: N816

[docs]class PiperBot(MultipleSitesBot, ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot): """Bot for munging text using external filtering programs.""" summary_key = 'piper-edit-summary'
[docs] def __init__(self, generator, **kwargs): """ Initializer. @param generator: The page generator that determines on which pages to work on. @type generator: generator """ self.availableOptions.update({ 'filters': [], }) super().__init__(generator=generator, **kwargs)
@property def summary_parameters(self) -> dict: """Return the filter parameter.""" return {'filters': ', '.join(self.getOption('filters'))}
[docs] def pipe(self, program: str, text: str) -> str: """Pipe a given text through a given program. @return: processed text after piping """ pipe = pipes.Template() pipe.append(program, '--') # Create a temporary filename to save the piped stuff to file, temp_filename = tempfile.mkstemp(suffix='.txt') file.close() with, 'w') as file: file.write(text) # Now retrieve the munged text with open(temp_filename, 'r') as file: text = # clean up os.unlink(temp_filename) return text
[docs] def treat_page(self): """Load the given page, do some changes, and save it.""" # Load the page text = self.current_page.text # Munge! for program in self.getOption('filters'): text = self.pipe(program, text) # only save if something was changed self.put_current(text)
[docs]def main(*args): """Create and run a PiperBot instance from the given command arguments.""" local_args = pywikibot.handle_args(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. gen_factory = pagegenerators.GeneratorFactory() # The program to pipe stuff through filters = [] options = {} # Parse command line arguments for arg in local_args: option, sep, value = arg.partition(':') if option == '-filter': filters.append(value) elif option == '-always': options['always'] = True else: # check if a standard argument like # -start:XYZ or -ref:Asdf was given. gen_factory.handleArg(arg) options['filters'] = filters gen = gen_factory.getCombinedGenerator(preload=True) if gen: # The preloading generator is responsible for downloading multiple # pages from the wiki simultaneously. bot = PiperBot(gen, **options) else:
if __name__ == '__main__': main()