jenkins-bot merged this change.

View Change

Approvals: D3r1ck01: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] Strip disambiguation parens from articles

Add a parameter to remove disambiguation parentheses from names of
newly created items. It cannot not be used in conjunction with
`as_link`.

Bug: T200399
Change-Id: I77b3ef1a6c6b690ba73f4e43709506adfc9b1ed4
---
M pywikibot/bot.py
M pywikibot/page.py
M tests/page_tests.py
3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index d3fa757..5cb9b06 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -2141,7 +2141,7 @@
data.setdefault('labels', {}).update({
page.site.lang: {
'language': page.site.lang,
- 'value': page.title()
+ 'value': page.title(without_brackets=page.namespace() == 0)
}
})
pywikibot.output('Creating item for %s...' % page)
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 2f78e30..c033a7d 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -294,7 +294,7 @@
def title(self, underscore=False, with_ns=True,
with_section=True, as_url=False, as_link=False,
allow_interwiki=True, force_interwiki=False, textlink=False,
- as_filename=False, insite=None):
+ as_filename=False, insite=None, without_brackets=False):
"""
Return the title of this Page, as a Unicode string.

@@ -318,6 +318,8 @@
@param insite: (only used if as_link is true) a site object where the
title is to be shown. default is the current family/lang given by
-family and -lang option i.e. config.family and config.mylang
+ @param without_brackets: (cannot be used with as_link) if true, remove
+ the last pair of brackets(usually removes disambiguation brackets).
@rtype: unicode
"""
title = self._link.canonical_title()
@@ -357,6 +359,9 @@
title = label + section
else:
title += section
+ if without_brackets:
+ brackets_re = r'\s+\([^()]+?\)$'
+ title = re.sub(brackets_re, '', title)
if underscore or as_url:
title = title.replace(' ', '_')
if as_url:
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 4cc8062..4b99130 100644
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -391,6 +391,18 @@
force_interwiki=True, insite=site),
'[[' + site.code + ':Test page|Test page]]')

+ title1 = 'Test Page (bracketed)'
+ title2 = 'Test Page (bracketed) (bracketed)'
+
+ self.assertEqual(
+ pywikibot.Page(site, title1).title(without_brackets=True),
+ 'Test Page'
+ )
+ self.assertEqual(
+ pywikibot.Page(site, title2).title(without_brackets=True),
+ 'Test Page (bracketed)'
+ )
+
def testSection(self):
"""Test section() method."""
# use same pages as in previous test

To view, visit change 470627. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I77b3ef1a6c6b690ba73f4e43709506adfc9b1ed4
Gerrit-Change-Number: 470627
Gerrit-PatchSet: 24
Gerrit-Owner: Shreyasminocha <shreyasminocha@protonmail.com>
Gerrit-Reviewer: D3r1ck01 <alangiderick@gmail.com>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Shreyasminocha <shreyasminocha@protonmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot (75)
Gerrit-CC: Rafidaslam <rafidteam@gmail.com>