예제 #1
0
파일: template.py 프로젝트: moleculea/ess
    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 = []
        exceptions = {}
        site = pywikibot.getSite()
        for old, new in self.templates.iteritems():
            namespaces = list(site.namespace(10, all=True))
            if not site.nocapitalize:
                pattern = '[' + re.escape(old[0].upper()) + re.escape(old[0].lower()) + ']' + re.escape(old[1:])
            else:
                pattern = re.escape(old)
            pattern = re.sub(r'_|\\ ', r'[_ ]', pattern)
            templateRegex = re.compile(r'\{\{ *(' + ':|'.join(namespaces) + \
                                       r':|[mM][sS][gG]:)?' + pattern + \
                                       r'(?P<parameters>\s*\|.+?|) *}}',
                                       re.DOTALL)

            if self.remove:
                replacements.append((templateRegex, ''))
            elif self.subst:
                replacements.append((templateRegex, '{{subst:' + old + '\g<parameters>}}'))
                exceptions['inside-tags']=['ref']
            else:
                replacements.append((templateRegex, '{{' + new + '\g<parameters>}}'))

        replaceBot = replace.ReplaceRobot(self.generator, replacements, exceptions, acceptall = self.acceptAll, addedCat=self.addedCat, editSummary=self.editSummary)
        replaceBot.run()
예제 #2
0
                                    ' *(?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':
예제 #3
0
        else:
            image_regex = re.compile(r'' + escaped)

        if self.new_image:
            if not self.getOption('loose'):
                replacements.append((image_regex,
                                     u'[[%s:%s\\g<parameters>]]'
                                     % (self.site.image_namespace(),
                                        self.new_image)))
            else:
                replacements.append((image_regex, self.new_image))
        else:
            replacements.append((image_regex, ''))

        replaceBot = replace.ReplaceRobot(self.generator, replacements,
                                          acceptall=self.getOption('always'),
                                          summary=self.getOption('summary'))
        replaceBot.run()


def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    old_image = None
    new_image = None
예제 #4
0
 def run(self):
     replaceBot = replace.ReplaceRobot(self.generator, replacements)
     replaceBot.run()