jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/453338 )
Change subject: Fix usages of site.namespaces.NAMESPACE_NAME ......................................................................
Fix usages of site.namespaces.NAMESPACE_NAME
These erorrs were found while reviewing 7c12604. Use the built-in canonical namespace names instead of site.namespaces.NAMESPACE_NAME to fix the errors.
Fix the pywikibot.output call in Category.copyTo by replacing it with pywikibot.warning. Using the `level` parameter with the output method is not allowed (raises an error).
Add two simple dry tests for the changed Category methods.
Bug: T201969 Change-Id: I9327f51dedc17f2a6e65b6e5d93b6f5d4f9bc56c --- M pywikibot/page.py M scripts/catall.py M scripts/imagetransfer.py M scripts/noreferences.py M tests/category_tests.py 5 files changed, 29 insertions(+), 13 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index e2f2fcc..e20bae5 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -2982,14 +2982,12 @@ # move to category.py? (Although it doesn't seem to be used there, # either) if not isinstance(cat, Category): - cat = self.site.namespaces.CATEGORY + ':' + cat - target_cat = Category(self.site, cat) + target_cat = Category(self.site, 'Category:' + cat) else: target_cat = cat if target_cat.exists(): - pywikibot.output(u'Target page %s already exists!' - % target_cat.title(), - level=pywikibot.logging.WARNING) + pywikibot.warning( + 'Target page %s already exists!' % target_cat.title()) return False else: pywikibot.output('Moving text from %s to %s.' @@ -3025,8 +3023,7 @@ """ # I don't see why we need this as part of the framework either # move to scripts/category.py? - catname = self.site.namespaces.CATEGORY + ':' + catname - target_cat = Category(self.site, catname) + target_cat = Category(self.site, 'Category:' + catname) if target_cat.exists(): pywikibot.warning(u'Target page %s already exists!' % target_cat.title()) diff --git a/scripts/catall.py b/scripts/catall.py index 73ec62f..3a934ce 100755 --- a/scripts/catall.py +++ b/scripts/catall.py @@ -75,8 +75,7 @@ site = pywikibot.Site() pllist = [] for p in list: - cattitle = "%s:%s" % (site.namespaces.CATEGORY, p) - pllist.append(pywikibot.Page(site, cattitle)) + pllist.append(pywikibot.Page(site, 'Category:' + p)) page.put(textlib.replaceCategoryLinks(page.get(), pllist, site=page.site), asynchronous=True, summary=i18n.twtranslate(site, 'catall-changing')) diff --git a/scripts/imagetransfer.py b/scripts/imagetransfer.py index de8384f..bc7db3e 100755 --- a/scripts/imagetransfer.py +++ b/scripts/imagetransfer.py @@ -213,8 +213,7 @@ # to upload anyway, using another name. try: # Maybe the image is on the target site already - targetTitle = '%s:%s' % (self.targetSite.namespaces.FILE, - image.title().split(':', 1)[1]) + targetTitle = 'File:' + image.title().split(':', 1)[1] targetImage = pywikibot.Page(self.targetSite, targetTitle) targetImage.get() pywikibot.output('Image with this name is already on %s.' diff --git a/scripts/noreferences.py b/scripts/noreferences.py index 06dac87..39d67f0 100755 --- a/scripts/noreferences.py +++ b/scripts/noreferences.py @@ -762,8 +762,7 @@ except Exception: pass else: - cat = pywikibot.Category(site, "%s:%s" % ( - site.namespaces.CATEGORY, cat)) + cat = pywikibot.Category(site, 'Category:' + cat) gen = cat.articles(namespaces=genFactory.namespaces or [0]) if gen: bot = NoReferencesBot(gen, **options) diff --git a/tests/category_tests.py b/tests/category_tests.py index ae2bd4f..9fe5659 100644 --- a/tests/category_tests.py +++ b/tests/category_tests.py @@ -10,6 +10,7 @@ import pywikibot import pywikibot.page
+from tests import patch from tests.aspects import unittest, TestCase
@@ -165,6 +166,27 @@
dry = True
+ def test_copy_to(self): + """Test the copyTo method.""" + site = self.get_site() + foo_cat = pywikibot.Category(site, 'Category:Foo') + with patch.object(pywikibot.Category, 'exists', return_value=True), \ + patch.object(pywikibot, 'warning') as w: + self.assertFalse(foo_cat.copyTo('cattitle', 'message')) + w.assert_called_once_with( + 'Target page Category:Cattitle already exists!') + + def test_copy_and_keep(self): + """Test the copyAndKeep method.""" + site = self.get_site() + foo_cat = pywikibot.Category(site, 'Category:Foo') + with patch.object(pywikibot.Category, 'exists', return_value=True), \ + patch.object(pywikibot, 'warning') as w: + self.assertFalse( + foo_cat.copyAndKeep('cattitle', [], 'message')) + w.assert_called_once_with( + 'Target page Category:Cattitle already exists!') + def test_init_dry(self): """Test the category's __init__.""" site = self.get_site()