jenkins-bot has submitted this change and it was merged.
Change subject: textlib.py: categories with no indexing in categoryFormat
......................................................................
textlib.py: categories with no indexing in categoryFormat
Avoid the need for 'categories' to support indexing.
In this way, any iterable can be provided as parameter.
Change-Id: I1d1b83fd09f4c1f87bd2756b640aa4df34dc34d5
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 21 insertions(+), 11 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
XZise: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 4c74cef..d21ddf1 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -854,24 +854,34 @@
def categoryFormat(categories, insite=None):
"""Return a string containing links to all categories in a list.
- 'categories' should be a list of Category objects or strings
- which can be either the raw name or [[Category:..]].
+ 'categories' should be a list of Category or Page objects or strings
+ which can be either the raw name, [[Category:..]] or [[cat_localised_ns:...]].
The string is formatted for inclusion in insite.
+ Category namespace is converted to localised namespace.
"""
if not categories:
return ''
if insite is None:
insite = pywikibot.Site()
- if isinstance(categories[0], basestring):
- if categories[0][0] == '[':
- catLinks = categories
- else:
- catLinks = ['[[Category:%s]]' % category for category in categories]
- else:
- catLinks = [pywikibot.Category(category).aslink()
- for category in categories]
+ catLinks = []
+ for category in categories:
+ if isinstance(category, basestring):
+ category, separator, sortKey =
category.strip('[]').partition('|')
+ sortKey = sortKey if separator else None
+ prefix = category.split(":", 1)[0] # whole word if no
":" is present
+ if prefix not in insite.namespaces()[14]:
+ category = u'{0}:{1}'.format(insite.namespace(14), category)
+ category = pywikibot.Category(pywikibot.Link(category,
+ insite,
+ defaultNamespace=14),
+ sortKey=sortKey)
+ # Make sure a category is casted from Page to Category.
+ elif not isinstance(category, pywikibot.Category):
+ category = pywikibot.Category(category)
+ link = category.aslink()
+ catLinks.append(link)
if insite.category_on_one_line():
sep = ' '
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index cafa378..bc28612 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -172,7 +172,7 @@
sep = config.LS
config.line_separator = '' # use an empty separator temporarily
new = textlib.replaceCategoryLinks(old, cats, site=self.site)
- # restore the default separator
+ # Restore the default separator.
config.line_separator = sep
self.assertEqual(old, new)
--
To view, visit
https://gerrit.wikimedia.org/r/165109
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I1d1b83fd09f4c1f87bd2756b640aa4df34dc34d5
Gerrit-PatchSet: 9
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Ricordisamoa <ricordisamoa(a)openmailbox.org>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>