• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python textlib.getCategoryLinks函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pywikibot.textlib.getCategoryLinks函数的典型用法代码示例。如果您正苦于以下问题:Python getCategoryLinks函数的具体用法?Python getCategoryLinks怎么用?Python getCategoryLinks使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了getCategoryLinks函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_in_place_replace

    def test_in_place_replace(self):
        """Test in-place category change is reversible."""
        dummy = pywikibot.Category(self.site, 'foo')
        dummy.sortKey = 'bah'

        cats = textlib.getCategoryLinks(self.old, site=self.site)

        # Sanity checking
        temp = textlib.replaceCategoryInPlace(self.old, cats[0], dummy, site=self.site)
        self.assertNotEqual(temp, self.old)
        new = textlib.replaceCategoryInPlace(temp, dummy, cats[0], site=self.site)
        self.assertEqual(self.old, new)

        temp = textlib.replaceCategoryInPlace(self.old, cats[1], dummy, site=self.site)
        self.assertNotEqual(temp, self.old)
        new = textlib.replaceCategoryInPlace(temp, dummy, cats[1], site=self.site)
        self.assertEqual(self.old, new)

        temp = textlib.replaceCategoryInPlace(self.old, cats[2], dummy, site=self.site)
        self.assertNotEqual(temp, self.old)
        new = textlib.replaceCategoryInPlace(temp, dummy, cats[2], site=self.site)
        self.assertEqual(self.old, new)

        temp = textlib.replaceCategoryInPlace(self.old, cats[3], dummy, site=self.site)
        self.assertNotEqual(temp, self.old)
        new = textlib.replaceCategoryInPlace(temp, dummy, cats[3], site=self.site)
        self.assertEqual(self.old, new)

        new_cats = textlib.getCategoryLinks(new, site=self.site)
        self.assertEqual(cats, new_cats)
开发者ID:metakgp,项目名称:batman,代码行数:30,代码来源:textlib_tests.py


示例2: test_adjoining_links

 def test_adjoining_links(self):
     cats_std = textlib.getCategoryLinks(self.old, site=self.site)
     old = self.old.replace(config.LS, "")
     cats = textlib.getCategoryLinks(old, site=self.site)
     self.assertEqual(cats_std, cats)
     sep = config.LS
     config.line_separator = ""  # use an empty separator temporarily
     new = textlib.replaceCategoryLinks(old, cats, site=self.site)
     # Restore the default separator.
     config.line_separator = sep
     self.assertEqual(old, new)
开发者ID:hasteur,项目名称:pywikibot_scripts,代码行数:11,代码来源:textlib_tests.py


示例3: test_adjoining_links

 def test_adjoining_links(self):
     """Test getting and replacing adjacent categories."""
     cats_std = textlib.getCategoryLinks(self.old, site=self.site)
     old = self.old.replace(config.LS, '')
     cats = textlib.getCategoryLinks(old, site=self.site)
     self.assertEqual(cats_std, cats)
     sep = config.LS
     config.line_separator = ''  # use an empty separator temporarily
     new = textlib.replaceCategoryLinks(old, cats, site=self.site)
     # Restore the default separator.
     config.line_separator = sep
     self.assertEqual(old, new)
开发者ID:metakgp,项目名称:batman,代码行数:12,代码来源:textlib_tests.py


示例4: test_in_place_retain_sort

    def test_in_place_retain_sort(self):
        """Test in-place category change does not alter the sortkey."""
        # sort key should be retained when the new cat sortKey is None
        dummy = pywikibot.Category(self.site, 'foo')
        self.assertIsNone(dummy.sortKey)

        cats = textlib.getCategoryLinks(self.old, site=self.site)

        self.assertEqual(cats[3].sortKey, 'key')
        orig_sortkey = cats[3].sortKey
        temp = textlib.replaceCategoryInPlace(self.old, cats[3], dummy, site=self.site)
        self.assertNotEqual(self.old, temp)
        new_dummy = textlib.getCategoryLinks(temp, site=self.site)[3]
        self.assertIsNotNone(new_dummy.sortKey)
        self.assertEqual(orig_sortkey, new_dummy.sortKey)
开发者ID:metakgp,项目名称:batman,代码行数:15,代码来源:textlib_tests.py


示例5: setUpClass

 def setUpClass(cls):
     super(TestCategoryRearrangement, cls).setUpClass()
     cls.site = cls.get_site()
     cls.old = ('[[Category:Cat1]]%(LS)s[[Category:Cat2|]]%(LS)s'
                '[[Category:Cat1| ]]%(LS)s[[Category:Cat2|key]]'
                % {'LS': config.LS})
     cls.cats = textlib.getCategoryLinks(cls.old, site=cls.site)
开发者ID:anrao91,项目名称:pywikibot-core,代码行数:7,代码来源:textlib_tests.py


示例6: get_final_categories

    def get_final_categories(self):
        """
        Return final categories to keep.

        We keep any categories added after the first revision
        plus any categories in the new text which were not also
        present in the first revision and later removed.
        :return: list of pywikibot.Category
        """
        first_cats = set(textlib.getCategoryLinks(self.first_text, self.site))
        # can't use page.categories() since some cats are embedded by templates
        last_cats = set(textlib.getCategoryLinks(self.last_text, self.site))
        new_cats = set(textlib.getCategoryLinks(self.new_text, self.site))

        cats = ((new_cats - (first_cats - last_cats)) |
                (last_cats - first_cats))
        return list(cats)
开发者ID:lokal-profil,项目名称:LSH,代码行数:17,代码来源:replace_descriptions.py


示例7: test_adjoining_links

 def test_adjoining_links(self):
     old = self.old.replace(config.LS, '')
     cats = textlib.getCategoryLinks(old, site=self.site)
     self.assertEqual(self.cats, cats)
     sep = config.LS
     config.line_separator = ''  # use an empty separator temporarily
     new = textlib.replaceCategoryLinks(old, cats, site=self.site)
     self.assertEqual(old, new)
     config.line_separator = sep  # restore the default separator
开发者ID:anrao91,项目名称:pywikibot-core,代码行数:9,代码来源:textlib_tests.py


示例8: get_category_status

def get_category_status(site, page, cat):
    state = False
    old_text = page.text
    cats = textlib.getCategoryLinks(old_text)
    catpl = pywikibot.Category(site, cat)
    for c in cats:
        if c.title() == catpl.title():
            state = True
    return state
开发者ID:WikiToLearn,项目名称:pywikibot,代码行数:9,代码来源:wtlpywikibot.py


示例9: run

    def run(self):
        """Run the bot."""
        if not all((self.getOption('action'), self.generator)):
            return
        catmode = (self.getOption('action') == 'categories')
        for page in self.generator:
            try:
                self.current_page = page
                commons = page.site.image_repository()
                commonspage = getattr(pywikibot,
                                      ('Page', 'Category')[catmode]
                                      )(commons, page.title())
                try:
                    commonspage.get(get_redirect=True)
                    pagetitle = commonspage.title(withNamespace=not catmode)
                    if page.title() == pagetitle:
                        oldText = page.get()
                        text = oldText

                        # for Commons/Commonscat template
                        s = self.findTemplate.search(text)
                        s2 = getattr(self, 'findTemplate%d'
                                           % (2, 3)[catmode]).search(text)
                        if s or s2:
                            pywikibot.output(u'** Already done.')
                        else:
                            cats = textlib.getCategoryLinks(text,
                                                            site=page.site)
                            text = textlib.replaceCategoryLinks(
                                u'%s{{commons%s|%s}}'
                                % (text, ('', 'cat')[catmode], pagetitle),
                                cats, site=page.site)
                            comment = i18n.twtranslate(
                                page.site, 'commons_link%s-template-added'
                                % ('', '-cat')[catmode])
                            try:
                                self.userPut(page, oldText, text,
                                             summary=comment)
                            except pywikibot.EditConflict:
                                pywikibot.output(
                                    u'Skipping %s because of edit conflict'
                                    % page.title())

                except pywikibot.NoPage:
                    pywikibot.output(u'%s does not exist in Commons'
                                     % page.__class__.__name__)

            except pywikibot.NoPage:
                pywikibot.output(u'Page %s does not exist' % page.title())
            except pywikibot.IsRedirectPage:
                pywikibot.output(u'Page %s is a redirect; skipping.'
                                 % page.title())
            except pywikibot.LockedPage:
                pywikibot.output(u'Page %s is locked' % page.title())
开发者ID:magul,项目名称:pywikibot-core,代码行数:54,代码来源:commons_link.py


示例10: test_templates

 def test_templates(self):
     """Test normal templates inside category links."""
     self.site = self.get_site()
     self.assertEqual(textlib.getCategoryLinks(
         '[[Category:{{P1|Foo}}]]', self.site),
         [pywikibot.page.Category(self.site, 'Foo')])
     self.assertEqual(textlib.getCategoryLinks(
         '[[Category:{{P1|Foo}}|bar]]', self.site),
         [pywikibot.page.Category(self.site, 'Foo', sortKey='bar')])
     self.assertEqual(textlib.getCategoryLinks(
         '[[Category:{{P1|{{P2|L33t|Foo}}}}|bar]]', self.site),
         [pywikibot.page.Category(self.site, 'Foo', sortKey='bar')])
     self.assertEqual(textlib.getCategoryLinks(
         '[[Category:Foo{{!}}bar]]', self.site),
         [pywikibot.page.Category(self.site, 'Foo', sortKey='bar')])
     self.assertEqual(textlib.getCategoryLinks(
         '[[Category:Foo{{!}}bar]][[Category:Wiki{{P2||pedia}}]]',
         self.site),
         [pywikibot.page.Category(self.site, 'Foo', sortKey='bar'),
          pywikibot.page.Category(self.site, 'Wikipedia')])
     self.assertEqual(textlib.getCategoryLinks(
         '[[Category:Foo{{!}}and{{!}}bar]]', self.site),
         [pywikibot.page.Category(self.site, 'Foo', sortKey='and|bar')])
     self.assertRaises(pywikibot.InvalidTitle, textlib.getCategoryLinks,
                       '[[Category:nasty{{{!}}]]', self.site)
开发者ID:metakgp,项目名称:batman,代码行数:25,代码来源:textlib_tests.py


示例11: test_templates

 def test_templates(self):
     self.site = self.get_site()
     self.assertEqual(
         textlib.getCategoryLinks("[[Category:{{P1|Foo}}]]", self.site), [pywikibot.page.Category(self.site, "Foo")]
     )
     self.assertEqual(
         textlib.getCategoryLinks("[[Category:{{P1|Foo}}|bar]]", self.site),
         [pywikibot.page.Category(self.site, "Foo", sortKey="bar")],
     )
     self.assertEqual(
         textlib.getCategoryLinks("[[Category:{{P1|{{P2|L33t|Foo}}}}|bar]]", self.site),
         [pywikibot.page.Category(self.site, "Foo", sortKey="bar")],
     )
     self.assertEqual(
         textlib.getCategoryLinks("[[Category:Foo{{!}}bar]]", self.site),
         [pywikibot.page.Category(self.site, "Foo", sortKey="bar")],
     )
     self.assertEqual(
         textlib.getCategoryLinks("[[Category:Foo{{!}}bar]][[Category:Wiki{{P2||pedia}}]]", self.site),
         [pywikibot.page.Category(self.site, "Foo", sortKey="bar"), pywikibot.page.Category(self.site, "Wikipedia")],
     )
     self.assertEqual(
         textlib.getCategoryLinks("[[Category:Foo{{!}}and{{!}}bar]]", self.site),
         [pywikibot.page.Category(self.site, "Foo", sortKey="and|bar")],
     )
     self.assertRaises(pywikibot.InvalidTitle, textlib.getCategoryLinks, "[[Category:nasty{{{!}}]]", self.site)
开发者ID:hasteur,项目名称:pywikibot_scripts,代码行数:26,代码来源:textlib_tests.py


示例12: addCategory

def addCategory(site, page, cat):
    old_text = page.text
    cats = textlib.getCategoryLinks(old_text)

    catpl = pywikibot.Category(site, cat)

    if catpl not in cats:
        print("\t'" + cat + "' not in page categories. Adding") 
        cats.append(catpl)
        text = textlib.replaceCategoryLinks(page.text, cats, site=site)
        userPut(page, old_text, text, minor=True, botflag=True)
        return True
    else:
        print("\t'" + cat + "' already in page categories")
        return False
开发者ID:WikiToLearn,项目名称:PDFCheck,代码行数:15,代码来源:check.py


示例13: treat

 def treat(self, page):
     """Process one page."""
     if page.isRedirectPage():
         # if it's a redirect use the redirect target instead
         redirTarget = page.getRedirectTarget()
         if self.follow_redirects:
             self.current_page = redirTarget
         else:
             pywikibot.warning(
                 "Page %s is a redirect to %s; skipping." % (page.title(asLink=True), redirTarget.title(asLink=True))
             )
             # loading it will throw an error if we don't jump out before
             return
     else:
         self.current_page = page
     if self.current_page.exists():
         # Load the page
         text = self.current_page.text
     elif self.create:
         pywikibot.output("Page %s doesn't exist yet; creating." % (self.current_page.title(asLink=True)))
         text = ""
     else:
         pywikibot.output("Page %s does not exist; skipping." % self.current_page.title(asLink=True))
         return
     # store old text, so we don't have reload it every time
     old_text = text
     cats = textlib.getCategoryLinks(text)
     pywikibot.output("Current categories:")
     for cat in cats:
         pywikibot.output("* %s" % cat.title())
     catpl = pywikibot.Category(self.current_page.site, self.newcat)
     if catpl in cats:
         pywikibot.output("%s is already in %s." % (self.current_page.title(), catpl.title()))
     else:
         if self.sort:
             catpl = self.sorted_by_last_name(catpl, self.current_page)
         pywikibot.output("Adding %s" % catpl.title(asLink=True))
         cats.append(catpl)
         text = textlib.replaceCategoryLinks(text, cats, site=self.current_page.site)
         comment = self.comment
         if not comment:
             comment = i18n.twtranslate(
                 self.current_page.site, "category-adding", {"newcat": catpl.title(withNamespace=False)}
             )
         try:
             self.userPut(self.current_page, old_text, text, summary=comment, minor=True, botflag=True)
         except pywikibot.PageSaveRelatedError as error:
             pywikibot.output("Page %s not saved: %s" % (self.current_page.title(asLink=True), error))
开发者ID:emijrp,项目名称:pywikibot-core,代码行数:48,代码来源:category.py


示例14: set_category_status

def set_category_status(site, page, cat, status):
    old_text = page.text
    cats = textlib.getCategoryLinks(old_text)
    catpl = pywikibot.Category(site, cat)

    if status:
        if catpl not in cats:
            cats.append(catpl)
    else:
        if catpl in cats:
            cats.remove(catpl)
    text = textlib.replaceCategoryLinks(page.text, cats, site=site)
    if old_text != text:
        page.text = text
        page.save(minor=True, botflag=True)
        return True
    return False
开发者ID:WikiToLearn,项目名称:pywikibot,代码行数:17,代码来源:wtlpywikibot.py


示例15: run

 def run(self):
     """Start the bot."""
     # Run the generator which will yield Pages which might need to be
     # changed.
     for page in self.generator:
         if self.isTitleExcepted(page.title()):
             pywikibot.output("Skipping %s because the title is on the exceptions list." % page.title(asLink=True))
             continue
         try:
             # Load the page's text from the wiki
             original_text = page.get(get_redirect=True)
             if not page.canBeEdited():
                 pywikibot.output("You can't edit page %s" % page.title(asLink=True))
                 continue
         except pywikibot.NoPage:
             pywikibot.output("Page %s not found" % page.title(asLink=True))
             continue
         applied = set()
         new_text = original_text
         while True:
             if self.isTextExcepted(new_text):
                 pywikibot.output(
                     "Skipping %s because it contains text "
                     "that is on the exceptions list." % page.title(asLink=True)
                 )
                 break
             last_text = None
             while new_text != last_text:
                 last_text = new_text
                 new_text = self.apply_replacements(last_text, applied, page)
                 if not self.recursive:
                     break
             if new_text == original_text:
                 pywikibot.output("No changes were necessary in %s" % page.title(asLink=True))
                 break
             if hasattr(self, "addedCat"):
                 # Fetch only categories in wikitext, otherwise the others will
                 # be explicitly added.
                 cats = textlib.getCategoryLinks(new_text, site=page.site)
                 if self.addedCat not in cats:
                     cats.append(self.addedCat)
                     new_text = textlib.replaceCategoryLinks(new_text, cats, site=page.site)
             # Show the title of the page we're working on.
             # Highlight the title in purple.
             pywikibot.output(color_format("\n\n>>> {lightpurple}{0}{default} <<<", page.title()))
             pywikibot.showDiff(original_text, new_text)
             if self.getOption("always"):
                 break
             choice = pywikibot.input_choice(
                 "Do you want to accept these changes?",
                 [("Yes", "y"), ("No", "n"), ("Edit", "e"), ("open in Browser", "b"), ("all", "a")],
                 default="N",
             )
             if choice == "e":
                 editor = editarticle.TextEditor()
                 as_edited = editor.edit(original_text)
                 # if user didn't press Cancel
                 if as_edited and as_edited != new_text:
                     new_text = as_edited
                 continue
             if choice == "b":
                 pywikibot.bot.open_webbrowser(page)
                 try:
                     original_text = page.get(get_redirect=True, force=True)
                 except pywikibot.NoPage:
                     pywikibot.output("Page %s has been deleted." % page.title())
                     break
                 new_text = original_text
                 continue
             if choice == "a":
                 self.options["always"] = True
             if choice == "y":
                 page.text = new_text
                 page.save(
                     summary=self.generate_summary(applied), async=True, callback=self._count_changes, quiet=True
                 )
             while not self._pending_processed_titles.empty():
                 proc_title, res = self._pending_processed_titles.get()
                 pywikibot.output("Page %s%s saved" % (proc_title, "" if res else " not"))
             # choice must be 'N'
             break
         if self.getOption("always") and new_text != original_text:
             try:
                 page.text = new_text
                 page.save(summary=self.generate_summary(applied), callback=self._count_changes, quiet=True)
             except pywikibot.EditConflict:
                 pywikibot.output("Skipping %s because of edit conflict" % (page.title(),))
             except pywikibot.SpamfilterError as e:
                 pywikibot.output("Cannot change %s because of blacklist entry %s" % (page.title(), e.url))
             except pywikibot.LockedPage:
                 pywikibot.output("Skipping %s (locked page)" % (page.title(),))
             except pywikibot.PageNotSaved as error:
                 pywikibot.output("Error putting page: %s" % (error.args,))
             if self._pending_processed_titles.qsize() > 50:
                 while not self._pending_processed_titles.empty():
                     proc_title, res = self._pending_processed_titles.get()
                     pywikibot.output("Page %s%s saved" % (proc_title, "" if res else " not"))
开发者ID:PersianWikipedia,项目名称:pywikibot-core,代码行数:97,代码来源:replace.py


示例16: run

 def run(self):
     """Start the bot."""
     # Run the generator which will yield Pages which might need to be
     # changed.
     for page in self.generator:
         if self.isTitleExcepted(page.title()):
             pywikibot.output(
                 u'Skipping {0!s} because the title is on the exceptions list.'.format(page.title(asLink=True)))
             continue
         try:
             # Load the page's text from the wiki
             original_text = page.get(get_redirect=True)
             if not page.canBeEdited():
                 pywikibot.output(u"You can't edit page {0!s}".format(page.title(asLink=True)))
                 continue
         except pywikibot.NoPage:
             pywikibot.output(u'Page {0!s} not found'.format(page.title(asLink=True)))
             continue
         applied = set()
         new_text = original_text
         while True:
             if self.isTextExcepted(new_text):
                 pywikibot.output(u'Skipping %s because it contains text '
                                  u'that is on the exceptions list.'
                                  % page.title(asLink=True))
                 break
             last_text = None
             while new_text != last_text:
                 last_text = new_text
                 new_text = self.apply_replacements(last_text, applied,
                                                    page)
                 if not self.recursive:
                     break
             if new_text == original_text:
                 pywikibot.output(u'No changes were necessary in {0!s}'.format(page.title(asLink=True)))
                 break
             if hasattr(self, 'addedCat'):
                 # Fetch only categories in wikitext, otherwise the others will
                 # be explicitly added.
                 cats = textlib.getCategoryLinks(new_text, site=page.site)
                 if self.addedCat not in cats:
                     cats.append(self.addedCat)
                     new_text = textlib.replaceCategoryLinks(new_text,
                                                             cats,
                                                             site=page.site)
             # Show the title of the page we're working on.
             # Highlight the title in purple.
             pywikibot.output(color_format(
                 '\n\n>>> {lightpurple}{0}{default} <<<', page.title()))
             pywikibot.showDiff(original_text, new_text)
             if self.getOption('always'):
                 break
             choice = pywikibot.input_choice(
                 u'Do you want to accept these changes?',
                 [('Yes', 'y'), ('No', 'n'), ('Edit', 'e'),
                  ('open in Browser', 'b'), ('all', 'a')],
                 default='N')
             if choice == 'e':
                 editor = editarticle.TextEditor()
                 as_edited = editor.edit(original_text)
                 # if user didn't press Cancel
                 if as_edited and as_edited != new_text:
                     new_text = as_edited
                 continue
             if choice == 'b':
                 pywikibot.bot.open_webbrowser(page)
                 try:
                     original_text = page.get(get_redirect=True, force=True)
                 except pywikibot.NoPage:
                     pywikibot.output(u'Page {0!s} has been deleted.'.format(page.title()))
                     break
                 new_text = original_text
                 continue
             if choice == 'a':
                 self.options['always'] = True
             if choice == 'y':
                 page.text = new_text
                 page.save(summary=self.generate_summary(applied), async=True,
                           callback=self._count_changes, quiet=True)
             while not self._pending_processed_titles.empty():
                 proc_title, res = self._pending_processed_titles.get()
                 pywikibot.output('Page {0!s}{1!s} saved'.format(proc_title, '' if res else ' not'))
             # choice must be 'N'
             break
         if self.getOption('always') and new_text != original_text:
             try:
                 page.text = new_text
                 page.save(summary=self.generate_summary(applied),
                           callback=self._count_changes, quiet=True)
             except pywikibot.EditConflict:
                 pywikibot.output(u'Skipping {0!s} because of edit conflict'.format(page.title()))
             except pywikibot.SpamfilterError as e:
                 pywikibot.output(
                     u'Cannot change {0!s} because of blacklist entry {1!s}'.format(page.title(), e.url))
             except pywikibot.LockedPage:
                 pywikibot.output(u'Skipping {0!s} (locked page)'.format(page.title()))
             except pywikibot.PageNotSaved as error:
                 pywikibot.output(u'Error putting page: {0!s}'.format(error.args))
             if self._pending_processed_titles.qsize() > 50:
                 while not self._pending_processed_titles.empty():
#.........这里部分代码省略.........
开发者ID:runt18,项目名称:pywikibot-core,代码行数:101,代码来源:replace.py


示例17: put_cats

def put_cats(page, new_cats, summary=None, always=False):
    line_sep = pywikibot.config.line_separator
    if not summary:
        summary = "Adding categories using catfiles"

    oldtext = page.get()
    old_cats = textlib.getCategoryLinks(oldtext)
    old_templates = textlib.extract_templates_and_params(oldtext)
    old_template_titles = [i[0].lower() for i in old_templates]

    templates, cats = [], []
    for val in new_cats:
        if val.lower().startswith('category:'):
            tmp_cat = pywikibot.Category(pywikibot.Link(val, page.site))
            if tmp_cat not in old_cats:
                cats.append(tmp_cat)
        elif val.lower().startswith('{{'):
            tmp_templates = textlib.extract_templates_and_params(val)
            if len(tmp_templates) != 1:
                logging.warn("There was an error when parsing the template "
                             "'{0}'. Contact the developer, skipping it for "
                             "now.".format(val))
            tmp_template = tmp_templates[0]
            if tmp_template[0].lower() not in old_template_titles:
                templates.append(val)

    # Add templates to the top, and the categories to the bottom.
    newtext = oldtext
    if len(templates) > 0:
        newtext = line_sep.join(templates) + line_sep + newtext
    if len(cats) > 0:
        newtext = (newtext + line_sep +
                   line_sep.join(c.title(asLink=True, underscore=False)
                                 for c in cats))

    if oldtext == newtext:
        pywikibot.output("No changes to the page need to be made.")
        return

    while True:
        # Show the diff that has been created
        pywikibot.output(color_format(
            '\n\n>>> {lightpurple}{0}{default} <<<',
            page.title(underscore=False)))
        pywikibot.showDiff(oldtext, newtext)

        if always:
            choice = 'y'
        else:
            # Ask user whether to accept
            choice = pywikibot.input_choice(
                'Do you want to accept these changes?',
                [('Yes', 'y'), ('No', 'n'), ('Edit', 'e'),
                 ('Open browser', 'b')],
                'n', automatic_quit=False)
        # Apply the choice from above
        if choice == 'n':
            break
        elif choice == 'b':
            pywikibot.bot.open_webbrowser(page)
        elif choice == 'e':
            editor = pywikibot.editor.TextEditor()
            as_edited = editor.edit(newtext)
            if as_edited and as_edited != newtext:
                newtext = as_edited
        elif choice == 'y':
            try:
                page.put_async(newtext, summary)
            except pywikibot.EditConflict:
                pywikibot.output('Edit conflict! Skipping')
            except pywikibot.ServerError:
                pywikibot.output('Server Error! Skipping')
            except pywikibot.SpamfilterError as e:
                pywikibot.output(
                    'Cannot change %s because of blacklist entry %s'
                    % (page.title(), e.url))
            except pywikibot.LockedPage:
                pywikibot.output('Skipping %s (locked page)' % page.title())
            except pywikibot.PageNotSaved as error:
                pywikibot.output('Error putting page: %s' % error.args)
            break
开发者ID:pywikibot-catfiles,项目名称:file-metadata,代码行数:81,代码来源:utilities.py


示例18: apply

 def apply(self, text, page):
     categories = textlib.getCategoryLinks(text)
     if len(categories) > len(set(categories)):
         deduplicate(categories)
         text = textlib.replaceCategoryLinks(text, categories, page.site)
     return text
开发者ID:matejsuchanek,项目名称:pywikibot-scripts,代码行数:6,代码来源:checkwiki_errors.py


示例19: test_standard_links

 def test_standard_links(self):
     """Test getting and replacing categories."""
     cats = textlib.getCategoryLinks(self.old, site=self.site)
     new = textlib.replaceCategoryLinks(self.old, cats, site=self.site)
     self.assertEqual(self.old, new)
开发者ID:metakgp,项目名称:batman,代码行数:5,代码来源:textlib_tests.py


示例20: standardizePageFooter

    def standardizePageFooter(self, text):
        """
        Standardize page footer.

        Makes sure that interwiki links, categories and star templates are
        put to the correct position and into the right order. This combines the
        old instances standardizeInterwiki and standardizeCategories
        The page footer has the following section in that sequence:
        1. categories
        2. ## TODO: template beyond categories ##
        3. additional information depending on local site policy
        4. stars templates for featured and good articles
        5. interwiki links

        """
        starsList = [
            u'bueno',
            u'bom interwiki',
            u'cyswllt[ _]erthygl[ _]ddethol', u'dolen[ _]ed',
            u'destacado', u'destaca[tu]',
            u'enllaç[ _]ad',
            u'enllaz[ _]ad',
            u'leam[ _]vdc',
            u'legătură[ _]a[bcf]',
            u'liamm[ _]pub',
            u'lien[ _]adq',
            u'lien[ _]ba',
            u'liên[ _]kết[ _]bài[ _]chất[ _]lượng[ _]tốt',
            u'liên[ _]kết[ _]chọn[ _]lọc',
            u'ligam[ _]adq',
            u'ligazón[ _]a[bd]',
            u'ligoelstara',
            u'ligoleginda',
            u'link[ _][afgu]a', u'link[ _]adq', u'link[ _]f[lm]', u'link[ _]km',
            u'link[ _]sm', u'linkfa',
            u'na[ _]lotura',
            u'nasc[ _]ar',
            u'tengill[ _][úg]g',
            u'ua',
            u'yüm yg',
            u'רא',
            u'وصلة مقالة جيدة',
            u'وصلة مقالة مختارة',
        ]

        categories = None
        interwikiLinks = None
        allstars = []

        # The PyWikipediaBot is no longer allowed to touch categories on the
        # German Wikipedia. See
        # https://de.wikipedia.org/wiki/Hilfe_Diskussion:Personendaten/Archiv/1#Position_der_Personendaten_am_.22Artikelende.22
        # ignoring nn-wiki of cause of the comment line above iw section
        if not self.template and '{{Personendaten' not in text and \
           '{{SORTIERUNG' not in text and '{{DEFAULTSORT' not in text and \
           self.site.code not in ('et', 'it', 'bg', 'ru'):
            categories = textlib.getCategoryLinks(text, site=self.site)

        if not self.talkpage:  # and pywikibot.calledModuleName() <> 'interwiki':
            subpage = False
            if self.template:
                loc = None
                try:
                    tmpl, loc = moved_links[self.site.code]
                    del tmpl
                except KeyError:
                    pass
                if loc is not None and loc in self.title:
                    subpage = True
            interwikiLinks = textlib.getLanguageLinks(
                text, insite=self.site, template_subpage=subpage)

            # Removing the interwiki
            text = textlib.removeLanguageLinks(text, site=self.site)
            # Removing the stars' issue
            starstext = textlib.removeDisabledParts(text)
            for star in starsList:
                regex = re.compile(r'(\{\{(?:template:|)%s\|.*?\}\}[\s]*)'
                                   % star, re.I)
                found = regex.findall(starstext)
                if found != []:
                    text = regex.sub('', text)
                    allstars += found

        # Adding categories
        if categories:
            # TODO: Sorting categories in alphabetic order.
            # e.g. using categories.sort()

            # TODO: Taking main cats to top
            #   for name in categories:
            #       if re.search(u"(.+?)\|(.{,1}?)",name.title()) or name.title()==name.title().split(":")[0]+title:
            #            categories.remove(name)
            #            categories.insert(0, name)
            text = textlib.replaceCategoryLinks(text, categories,
                                                site=self.site)
        # Adding stars templates
        if allstars:
            text = text.strip() + self.site.family.interwiki_text_separator
            allstars.sort()
#.........这里部分代码省略.........
开发者ID:skamithi,项目名称:pywikibot-core,代码行数:101,代码来源:cosmetic_changes.py



注:本文中的pywikibot.textlib.getCategoryLinks函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python textlib.removeDisabledParts函数代码示例发布时间:2022-05-26
下一篇:
Python site.Namespace类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap