コード例 #1
0
    def testBasePageConstructor(self):
        """Test BasePage constructor."""
        site = self.get_site()

        # Should not raise an error as the constructor only requires
        # the site parameter.
        # Empty string or None as title raises error.
        page = pywikibot.page.BasePage(site)
        with self.assertRaisesRegex(InvalidTitle, INVALID_TITLE_RE):
            page.title()
        page = pywikibot.page.BasePage(site, title='')
        with self.assertRaisesRegex(InvalidTitle, INVALID_TITLE_RE):
            page.title()
        with self.assertRaisesRegex(ValueError, 'Title cannot be None.'):
            pywikibot.page.BasePage(site, title=None)
コード例 #2
0
ファイル: mrmetadata.py プロジェクト: gpaumier/MrMetadata
def format_files(files_to_print, current_site):

    MR_MISSING = '<td class="danger">missing</td>'
    MR_OK = '<td class="success">ok</td>'

    formatted_files_to_print = []
    item_index = 1

    for item in files_to_print:

        file_title = item[0]
        page = pywikibot.Page(source=current_site, title=file_title, ns=6)
        url = 'https://' + current_site.hostname() + '/wiki/' + page.title(asUrl=True)

        formatted_files_to_print.append({'index': item_index,
                                         'title': file_title,
                                         'url': url,
                                         'description': MR_MISSING if item[1] else MR_OK,
                                         'author': MR_MISSING if item[2] else MR_OK,
                                         'source': MR_MISSING if item[3] else MR_OK,
                                         'license_short': MR_MISSING if item[4] else MR_OK,
                                         'license_url': MR_MISSING if item[5] else MR_OK})

        item_index = item_index + 1

    return formatted_files_to_print
コード例 #3
0
    def test_redirect(self):
        """Test that the redirect option is set correctly."""
        site = self.get_site()
        for page in site.allpages(filterredir=True, total=1):
            break
        else:
            raise unittest.SkipTest('No redirect pages on site {0!r}'.format(site))
        # This page is already initialised
        self.assertTrue(hasattr(page, '_isredir'))
        # call api.update_page without prop=info
        del page._isredir
        page.isDisambig()
        self.assertTrue(page.isRedirectPage())

        page_copy = pywikibot.Page(site, page.title())
        self.assertFalse(hasattr(page_copy, '_isredir'))
        page_copy.isDisambig()
        self.assertTrue(page_copy.isRedirectPage())
コード例 #4
0
ファイル: mrmetadata.py プロジェクト: gpaumier/MrMetadata
def format_files(files_to_print, current_site):

    MR_MISSING = '<td class="danger">missing</td>'
    MR_OK = '<td class="success">ok</td>'

    formatted_files_to_print = []
    item_index = 1

    for item in files_to_print:

        file_title = item[0]
        page = pywikibot.Page(source=current_site, title=file_title, ns=6)
        url = 'https://' + current_site.hostname() + '/wiki/' + page.title(
            asUrl=True)

        formatted_files_to_print.append({
            'index':
            item_index,
            'title':
            file_title,
            'url':
            url,
            'description':
            MR_MISSING if item[1] else MR_OK,
            'author':
            MR_MISSING if item[2] else MR_OK,
            'source':
            MR_MISSING if item[3] else MR_OK,
            'license_short':
            MR_MISSING if item[4] else MR_OK,
            'license_url':
            MR_MISSING if item[5] else MR_OK
        })

        item_index = item_index + 1

    return formatted_files_to_print
コード例 #5
0
def item(page):
    try:
        item = page.data_item().title()
        return [True, item]
    except pywikibot.NoPage:
        if page.isImage():
            json = requests.get(commonsedge + page.title()[5:]).json()
            if json["status"] == "ERROR":
                if "Artwork" in json["error"]:
                    if u'wikidata' in json["error_data"][0]["params"]:
                        return [
                            True,
                            json["error_data"][0]["params"]["wikidata"][0][0]
                        ]
                    else:
                        return [False, "No wikidata item"]
                else:
                    return [False, "No Artwork template"]
            else:
                return [False, "Non-Artwork related error"]
        else:
            return [False, "Not a file"]
    except pywikibot.data.api.APIError:
        return [False, "API Error"]
コード例 #6
0
    def test_bot_may_edit_general(self):
        """Test that bot is allowed to edit."""
        site = self.get_site()
        user = site.user()

        page = pywikibot.Page(site, 'not_existent_page_for_pywikibot_tests')
        if page.exists():
            raise unittest.SkipTest(
                "Page %s exists! Change page name in tests/page_tests.py" %
                page.title())

        # Ban all compliant bots (shortcut).
        page.text = '{{nobots}}'
        page._templates = [pywikibot.Page(site, 'Template:Nobots')]
        self.assertFalse(page.botMayEdit())

        # Ban all compliant bots not in the list, syntax for de wp.
        page.text = '{{nobots|HagermanBot,Werdnabot}}'
        self.assertTrue(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots not in the list, syntax for de wp.
        page.text = '{{nobots|%s, HagermanBot,Werdnabot}}' % user
        self.assertFalse(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Ban all bots, syntax for de wp.
        page.text = '{{nobots|all}}'
        self.assertFalse(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Allow all bots (shortcut).
        page.text = '{{bots}}'
        page._templates = [pywikibot.Page(site, 'Template:Bots')]
        self.assertTrue(page.botMayEdit())

        # Ban all compliant bots not in the list.
        page.text = '{{bots|allow=HagermanBot,Werdnabot}}'
        self.assertFalse(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots in the list.
        page.text = '{{bots|deny=HagermanBot,Werdnabot}}'
        self.assertTrue(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots not in the list.
        page.text = '{{bots|allow=%s, HagermanBot}}' % user
        self.assertTrue(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots in the list.
        page.text = '{{bots|deny=%s, HagermanBot}}' % user
        self.assertFalse(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Allow all bots.
        page.text = '{{bots|allow=all}}'
        self.assertTrue(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots.
        page.text = '{{bots|allow=none}}'
        self.assertFalse(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots.
        page.text = '{{bots|deny=all}}'
        self.assertFalse(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))

        # Allow all bots.
        page.text = '{{bots|deny=none}}'
        self.assertTrue(
            page.botMayEdit(),
            u'%s: %s but user=%s' % (page.text, page.botMayEdit(), user))
コード例 #7
0
    def test_bot_may_edit_general(self):
        """Test that bot is allowed to edit."""
        site = self.get_site()
        user = site.user()

        page = pywikibot.Page(site, 'not_existent_page_for_pywikibot_tests')
        if page.exists():
            raise unittest.SkipTest(
                "Page %s exists! Change page name in tests/page_tests.py"
                % page.title())

        # Ban all compliant bots (shortcut).
        page.text = '{{nobots}}'
        page._templates = [pywikibot.Page(site, 'Template:Nobots')]
        self.assertFalse(page.botMayEdit())

        # Ban all compliant bots not in the list, syntax for de wp.
        page.text = '{{nobots|HagermanBot,Werdnabot}}'
        self.assertTrue(page.botMayEdit(),
                        u'%s: %s but user=%s'
                        % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots not in the list, syntax for de wp.
        page.text = '{{nobots|%s, HagermanBot,Werdnabot}}' % user
        self.assertFalse(page.botMayEdit(),
                         u'%s: %s but user=%s'
                         % (page.text, page.botMayEdit(), user))

        # Ban all bots, syntax for de wp.
        page.text = '{{nobots|all}}'
        self.assertFalse(page.botMayEdit(),
                         u'%s: %s but user=%s'
                         % (page.text, page.botMayEdit(), user))

        # Allow all bots (shortcut).
        page.text = '{{bots}}'
        page._templates = [pywikibot.Page(site, 'Template:Bots')]
        self.assertTrue(page.botMayEdit())

        # Ban all compliant bots not in the list.
        page.text = '{{bots|allow=HagermanBot,Werdnabot}}'
        self.assertFalse(page.botMayEdit(),
                         u'%s: %s but user=%s'
                         % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots in the list.
        page.text = '{{bots|deny=HagermanBot,Werdnabot}}'
        self.assertTrue(page.botMayEdit(),
                        u'%s: %s but user=%s'
                        % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots not in the list.
        page.text = '{{bots|allow=%s, HagermanBot}}' % user
        self.assertTrue(page.botMayEdit(),
                        u'%s: %s but user=%s'
                        % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots in the list.
        page.text = '{{bots|deny=%s, HagermanBot}}' % user
        self.assertFalse(page.botMayEdit(),
                         u'%s: %s but user=%s'
                         % (page.text, page.botMayEdit(), user))

        # Allow all bots.
        page.text = '{{bots|allow=all}}'
        self.assertTrue(page.botMayEdit(),
                        u'%s: %s but user=%s'
                        % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots.
        page.text = '{{bots|allow=none}}'
        self.assertFalse(page.botMayEdit(),
                         u'%s: %s but user=%s'
                         % (page.text, page.botMayEdit(), user))

        # Ban all compliant bots.
        page.text = '{{bots|deny=all}}'
        self.assertFalse(page.botMayEdit(),
                         u'%s: %s but user=%s'
                         % (page.text, page.botMayEdit(), user))

        # Allow all bots.
        page.text = '{{bots|deny=none}}'
        self.assertTrue(page.botMayEdit(),
                        u'%s: %s but user=%s'
                        % (page.text, page.botMayEdit(), user))