예제 #1
0
파일: image.py 프로젝트: CCXXXI/pywikibot
    def __init__(self,
                 generator,
                 old_image: str,
                 new_image: str = '',
                 **kwargs) -> None:
        """
        Initializer.

        :param generator: the pages to work on
        :type generator: iterable
        :param old_image: the title of the old image (without namespace)
        :param new_image: the title of the new image (without namespace), or
                          None if you want to remove the image
        """
        self.available_options.update({
            'summary': None,
            'loose': False,
        })

        SingleSiteBot.__init__(self, **kwargs)

        self.old_image = old_image
        self.new_image = new_image
        param = {
            'old': self.old_image,
            'new': self.new_image,
            'file': self.old_image,
        }

        summary = self.opt.summary or i18n.twtranslate(
            self.site, 'image-replace' if self.new_image else 'image-remove',
            param)

        namespace = self.site.namespaces[6]
        escaped = case_escape(namespace.case, self.old_image)

        # Be careful, spaces and _ have been converted to '\ ' and '\_'
        escaped = re.sub('\\\\[_ ]', '[_ ]', escaped)
        if not self.opt.loose or not self.new_image:
            image_regex = re.compile(
                r'\[\[ *(?:{})\s*:\s*{} *(?P<parameters>\|[^\n]+|) *\]\]'.
                format('|'.join(namespace), escaped))
        else:
            image_regex = re.compile(r'' + escaped)

        replacements = []
        if not self.opt.loose and self.new_image:
            replacements.append(
                (image_regex, '[[{}:{}\\g<parameters>]]'.format(
                    self.site.namespaces.FILE.custom_name, self.new_image)))
        else:
            replacements.append((image_regex, self.new_image))

        super().__init__(generator,
                         replacements,
                         always=self.opt.always,
                         site=self.site,
                         summary=summary)
예제 #2
0
 def __init__(self, **kwargs) -> None:
     """Initializer."""
     # handle options first;
     # they are needed for DisambiguationRobot positional arguments
     SingleSiteBot.__init__(self, **kwargs)
     super().__init__(always=self.opt.always, alternatives=[],
                      getAlternatives=True, dnSkip=False, generator=None,
                      primary=False, main_only=self.opt.main,
                      **self.options)  # save options
예제 #3
0
파일: image.py 프로젝트: dsuess/pywikibot
    def __init__(self,
                 generator,
                 old_image: str,
                 new_image: Optional[str] = None,
                 **kwargs):
        """
        Initializer.

        @param generator: the pages to work on
        @type generator: iterable
        @param old_image: the title of the old image (without namespace)
        @param new_image: the title of the new image (without namespace), or
                          None if you want to remove the image
        """
        self.available_options.update({
            'summary': None,
            'loose': False,
        })

        SingleSiteBot.__init__(self, generator=generator, **kwargs)

        self.old_image = old_image
        self.new_image = new_image
        param = {
            'old': self.old_image,
            'new': self.new_image,
            'file': self.old_image,
        }

        summary = self.opt.summary or i18n.twtranslate(
            self.site, 'image-replace' if self.new_image else 'image-remove',
            param)

        namespace = self.site.namespaces[6]
        if namespace.case == 'first-letter':
            case = re.escape(self.old_image[0].upper() +
                             self.old_image[0].lower())
            escaped = '[' + case + ']' + re.escape(self.old_image[1:])
예제 #4
0
    def __init__(self, generator, templates: dict, **kwargs) -> None:
        """
        Initializer.

        :param generator: the pages to work on
        :type generator: iterable
        :param templates: a dictionary which maps old template names to
            their replacements. If remove or subst is True, it maps the
            names of the templates that should be removed/resolved to None.
        """
        SingleSiteBot.__init__(self, **kwargs)

        self.templates = templates

        # get edit summary message if it's empty
        if not self.opt.summary:
            comma = self.site.mediawiki_message('comma-separator')
            params = {'list': comma.join(self.templates.keys()),
                      'num': len(self.templates)}

            if self.opt.remove:
                tw_key = 'template-removing'
            elif self.opt.subst:
                tw_key = 'template-substituting'
            else:
                tw_key = 'template-changing'
            self.opt.summary = i18n.twtranslate(self.site, tw_key, params)

        replacements = []
        exceptions = {}
        builder = textlib.MultiTemplateMatchBuilder(self.site)
        for old, new in self.templates.items():
            template_regex = builder.pattern(old)

            if self.opt.subst and self.opt.remove:
                replacements.append((template_regex,
                                     r'{{subst:%s\g<parameters>}}' % new))
                exceptions['inside-tags'] = ['ref', 'gallery', 'poem',
                                             'pagelist', ]
            elif self.opt.subst:
                replacements.append(
                    (template_regex, r'{{%s:%s\g<parameters>}}' %
                     (self.opt.subst, old)))
                exceptions['inside-tags'] = ['ref', 'gallery', 'poem',
                                             'pagelist', ]
            elif self.opt.remove:
                separate_line_regex = re.compile(
                    r'^[*#:]* *{} *\n'.format(template_regex.pattern),
                    re.DOTALL | re.MULTILINE)
                replacements.append((separate_line_regex, ''))

                spaced_regex = re.compile(
                    r' +{} +'.format(template_regex.pattern),
                    re.DOTALL)
                replacements.append((spaced_regex, ' '))

                replacements.append((template_regex, ''))
            else:
                template = pywikibot.Page(self.site, new, ns=10)
                if not template.exists():
                    pywikibot.warning('Template "{}" does not exist.'
                                      .format(new))
                    if not pywikibot.input_yn('Do you want to proceed anyway?',
                                              default=False,
                                              automatic_quit=False):
                        continue
                replacements.append((template_regex,
                                     r'{{%s\g<parameters>}}' % new))

        super().__init__(
            generator, replacements, exceptions,
            always=self.opt.always,
            addcat=self.opt.addcat,
            summary=self.opt.summary)