jenkins-bot has submitted this change and it was merged.
Change subject: new APISite.list_to_text() method
......................................................................
new APISite.list_to_text() method
to join two or more strings using localized MediaWiki messages
added some testcases accordingly
Change-Id: I84f2346f6f445089fd87b15c9c2c6612496bfa2a
---
M pywikibot/site.py
M tests/site_tests.py
2 files changed, 34 insertions(+), 0 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 9599d4c..6e71404 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -952,6 +952,37 @@
return self._months_names
+ def list_to_text(self, args):
+ """Join a list of strings together into a human-readable
+ list. The MediaWiki message 'and' is used as separator
+ between the last two arguments.
+ If present, other arguments are joined using a comma.
+
+ @param args: text to be expanded
+ @type args: iterable
+ @return: unicode
+
+ """
+ if not args:
+ return u''
+ args = [unicode(e) for e in args]
+ msgs = {
+ 'and': ',',
+ 'comma-separator': ', ',
+ 'word-separator': ' '
+ }
+ try:
+ self.mediawiki_messages(list(msgs.keys()))
+ except KeyError:
+ pass
+ for msg in msgs:
+ try:
+ msgs[msg] = self.mediawiki_message(msg)
+ except KeyError:
+ pass
+ concat = msgs['and'] + msgs['word-separator']
+ return msgs['comma-separator'].join(args[:-2] +
[concat.join(args[-2:])])
+
def expand_text(self, text, title=None, includecomments=None):
""" Parses the given text for preprocessing and rendering
e.g expand templates and strip comments if includecomments
diff --git a/tests/site_tests.py b/tests/site_tests.py
index b9e8d34..644b782 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -180,6 +180,9 @@
self.assertType(ver[2], basestring)
self.assertType(mysite.months_names, list)
self.assertEqual(mysite.months_names[4], (u'May', u'May'))
+ self.assertEqual(mysite.list_to_text(('pywikibot',)),
'pywikibot')
+ self.assertEqual(mysite.list_to_text(('Pride', 'Prejudice')),
'Pride and Prejudice')
+ self.assertEqual(mysite.list_to_text(('This', 'that', 'the
other')), 'This, that and the other')
def testPageMethods(self):
"""Test ApiSite methods for getting page-specific
info"""
--
To view, visit
https://gerrit.wikimedia.org/r/135187
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I84f2346f6f445089fd87b15c9c2c6612496bfa2a
Gerrit-PatchSet: 8
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <ricordisamoa(a)openmailbox.org>
Gerrit-Reviewer: Ricordisamoa <ricordisamoa(a)openmailbox.org>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>