jenkins-bot has submitted this change and it was merged.
Change subject: Site: add canonical namespaces to site._namespaces ......................................................................
Site: add canonical namespaces to site._namespaces
Needed to identify which namespaces can be accessed by an Extension. E.g. Proofread Page works on Page and Index namespaces, which keep the same name as 'canonical' namespaces but not as 'actual' namespaces.
Code improvements as well
Preparation for ProofreadPage class.
Change-Id: I58d67501e00ff9f1afdbdde97ac43bd7aeb40963 --- M pywikibot/site.py M tests/site_tests.py 2 files changed, 19 insertions(+), 7 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 5f5ba7e..03928e5 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -1133,16 +1133,26 @@ assert 'namespaces' in sidata, \ "API siteinfo response lacks 'namespaces' key" self._siteinfo = sidata['general'] + nsdata = sidata['namespaces'] for nskey in nsdata: ns = int(nskey) - if ns in self._namespaces: - if nsdata[nskey]["*"] in self._namespaces[ns]: + # this is the preferred form so it goes at front of list + self._namespaces.setdefault(ns, []).insert(0, nsdata[nskey]["*"]) + + if LV(self.version()) >= LV("1.14"): + # nsdata["0"] has no canonical key. + # canonical ns -2 to 15 are hard coded in self._namespaces + # do not get them from API result to avoid canonical duplicates + if -2 <= ns <= 15: continue - # this is the preferred form so it goes at front of list - self._namespaces[ns].insert(0, nsdata[nskey]["*"]) - else: - self._namespaces[ns] = [nsdata[nskey]["*"]] + if 'canonical' not in nsdata[nskey]: + pywikibot.warning( + u'namespace %s without a canonical name. Misconfigured?' + % self._namespaces[ns][0]) + continue + self._namespaces.setdefault(ns, []).append(nsdata[nskey]["canonical"]) + if 'namespacealiases' in sidata: aliasdata = sidata['namespacealiases'] for item in aliasdata: @@ -1150,6 +1160,7 @@ continue # this is a less preferred form so it goes at the end self._namespaces[int(item['id'])].append(item["*"]) + if 'extensions' in sidata: self._extensions = sidata['extensions'] else: diff --git a/tests/site_tests.py b/tests/site_tests.py index cbce977..1f169b0 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -91,7 +91,8 @@ """Test cases for methods manipulating namespace names"""
builtins = { - 'Talk': 1, # these should work in any MW wiki + '': 0, # these should work in any MW wiki + 'Talk': 1, 'User': 2, 'User talk': 3, 'Project': 4,
pywikibot-commits@lists.wikimedia.org