本文整理汇总了Python中pywikibot.textlib.getCategoryLinks函数的典型用法代码示例。如果您正苦于以下问题:Python getCategoryLinks函数的具体用法?Python getCategoryLinks怎么用?Python getCategoryLinks使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getCategoryLinks函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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
示例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
示例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())
示例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)
示例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)
示例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
示例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))
示例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
示例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"))