image.py :  » Network » Python-Wikipedia-Robot-Framework » pywikipedia » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Python Wikipedia Robot Framework 
Python Wikipedia Robot Framework » pywikipedia » image.py
# -*- coding: utf-8 -*-
"""
This script can be used to change one image to another or remove an image entirely.

Syntax: python image.py image_name [new_image_name]

If only one command-line parameter is provided then that image will be removed; if
two are provided, then the first image will be replaced by the second one on all pages.

Command line options:

-summary:  Provide a custom edit summary.  If the summary includes spaces, surround
           it with single quotes, such as:  -summary:'My edit summary'
-always    Don't prompt to make changes, just do them.
-loose     Do loose replacements.  This will replace all occurences of the name of the
           image (and not just explicit image syntax).  This should work to catch all
           instances of the image, including where it is used as a template parameter
           or in image galleries.  However, it can also make more mistakes.  This only
           works with image replacement, not image removal.

Examples:

The image "FlagrantCopyvio.jpg" is about to be deleted, so let's first remove it from
everything that displays it:

    python image.py FlagrantCopyvio.jpg

The image "Flag.svg" has been uploaded, making the old "Flag.jpg" obselete:

    python image.py Flag.jpg Flag.svg

"""
__version__ = '$Id: image.py 7301 2009-09-24 23:44:01Z siebrand $'
#
# Distributed under the terms of the MIT license.
#
import wikipedia
import replace, pagegenerators
import re

class ImageRobot:
    """
    This robot will load all pages yielded by a file links image page generator and
    replace or remove all occurences of the old image.
    """
    # Summary messages for replacing images
    msg_replace={
        'ar': u' -   %s  %s',
        'de': u'Bot: Ersetze Bild %s durch %s',
        'en': u'Robot - Replacing image %s with %s',
        'es': u'Robot - Reemplazando imagen %s por %s',
        'fr': u'Bot: Remplace image %s par %s',
        'he': u':    %s  %s',
        'it': u"Bot: Sostituisco l'immagine %s con %s",
        'ja': u' %s  %s ',
        'ko': u' -  %s %s ',
        'lt': u'robotas: vaizdas %s keiiamas  %s',
        'nn': u'robot: erstatta biletet %s med %s',
        'no': u'robot: erstatter bildet %s med %s',
        'nl': u'Bot: afbeelding %s vervangen door %s',
        'pl': u'Robot zamienia obraz %s na %s',
        'pt': u'Bot: Alterando imagem %s para %s',
        'ru': u':   %s  %s',
        'zh': u' %s  %s',
    }

    # Summary messages for removing images
    msg_remove={
        'ar': u' -   %s',
        'de': u'Bot: Entferne Bild %s',
        'en': u'Robot - Removing image %s',
        'es': u'Robot - Retirando imagen %s',
        'fr': u'Bot: Enleve image %s',
        'he': u':    %s',
        'it': u"Bot: Rimuovo l'immagine %s",
        'ja': u' %s',
        'ko': u' - %s  ',
        'lt': u'robotas: alinamas vaizdas %s',
        'nl': u'Bot: afbeelding %s verwijderd',
        'no': u'robot: fjerner bildet %s',
        'nn': u'robot: fjerna biletet %s',
        'pl': u'Robot usuwa obraz %s',
        'pt': u'Bot: Alterando imagem %s',
        'ru': u':   %s',
        'zh': u' %s',
    }

    def __init__(self, generator, oldImage, newImage = None, summary = '', always = False, loose = False):
        """
        Arguments:
            * generator - A page generator.
            * oldImage  - The title of the old image (without namespace)
            * newImage  - The title of the new image (without namespace), or
                          None if you want to remove the image.
        """
        self.generator = generator
        self.oldImage = oldImage
        self.newImage = newImage
        self.editSummary = summary
        self.summary = summary
        self.always = always
        self.loose = loose

        # get edit summary message
        mysite = wikipedia.getSite()
        if summary:
            self.editSummary = summary
        elif self.newImage:
            self.editSummary = wikipedia.translate(mysite, self.msg_replace) % (self.oldImage, self.newImage)
        else:
            self.editSummary = wikipedia.translate(mysite, self.msg_remove) % self.oldImage

    def run(self):
        """
        Starts the robot's action.
        """
        # regular expression to find the original template.
        # {{vfd}} does the same thing as {{Vfd}}, so both will be found.
        # The old syntax, {{msg:vfd}}, will also be found.
        # The group 'parameters' will either match the parameters, or an
        # empty string if there are none.

        replacements = []
        site = wikipedia.getSite()

        if not site.nocapitalize:
            case = re.escape( self.oldImage[0].upper() + self.oldImage[0].lower() )
            escaped = '[' + case + ']' + re.escape(self.oldImage[1:])
        else:
            escaped = re.escape( self.oldImage )

        # Be careful, spaces and _ have been converted to '\ ' and '\_'
        escaped = re.sub('\\\\[_ ]', '[_ ]', escaped)
        if not self.loose or not self.newImage:
            ImageRegex = re.compile(r'\[\[ *(?:' + '|'.join(site.namespace(6, all = True)) + ')\s*:\s*' + escaped + ' *(?P<parameters>\|[^\n]+|) *\]\]')
        else:
            ImageRegex = re.compile(r'' + escaped)

        if self.newImage:
            if not self.loose:
                replacements.append((ImageRegex, '[[' + site.image_namespace() + ':' + self.newImage + '\g<parameters>]]'))
            else:
                replacements.append((ImageRegex, self.newImage))
        else:
            replacements.append((ImageRegex, ''))

        replaceBot = replace.ReplaceRobot(self.generator, replacements, acceptall = self.always, editSummary = self.editSummary)
        replaceBot.run()

def main():
    oldImage = None
    newImage = None
    summary = ''
    always = False
    loose = False
    # read command line parameters
    for arg in wikipedia.handleArgs():
        if arg == '-always':
            always = True
        elif arg == '-loose':
            loose = True
        elif arg.startswith('-summary'):
            if len(arg) == len('-summary'):
                summary = wikipedia.input(u'Choose an edit summary: ')
            else:
                summary = arg[len('-summary:'):]
        else:
            if oldImage:
                newImage = arg
            else:
                oldImage = arg

    if not oldImage:
        wikipedia.showHelp('image')
    else:
        mysite = wikipedia.getSite()
        ns = mysite.image_namespace()

        oldImagePage = wikipedia.ImagePage(mysite, ns + ':' + oldImage)

        gen = pagegenerators.FileLinksGenerator(oldImagePage)
        preloadingGen = pagegenerators.PreloadingGenerator(gen)

        bot = ImageRobot(preloadingGen, oldImage, newImage, summary, always, loose)
        bot.run()

if __name__ == "__main__":
    try:
        main()
    finally:
        wikipedia.stopme()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.