jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] NamespacesDict: Lookup names from dict ......................................................................
[IMPROV] NamespacesDict: Lookup names from dict
Instead of iterating over all namespaces, the NamespacesDict can cache a dictionary of names mapping to the namespace instance.
This cache is now used by `validLanguageLinks` which also avoids to iterate over all namespaces for each langlink and to avoid any magic provided by the `Namespace` class.
Also add a possibility to lookup the namespace via an already normalized name.
Change-Id: Ice7f51aef1673724c87ae2aa7a86f35f36d01692 --- M pywikibot/site.py M tests/api_tests.py M tests/namespace_tests.py M tests/site_tests.py 4 files changed, 47 insertions(+), 3 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 72d69a3..d9d1f60 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -492,6 +492,10 @@ """Create new dict using the given namespaces.""" super(NamespacesDict, self).__init__() self._namespaces = namespaces + self._namespace_names = {} + for namespace in self._namespaces.values(): + for name in namespace: + self._namespace_names[name.lower()] = namespace
def __iter__(self): """Iterate over all namespaces.""" @@ -545,7 +549,22 @@ @type name: basestring @return: Namespace or None """ - return self._lookup_name(name, self._namespaces) + name = Namespace.normalize_name(name) + if name is False: + return None + return self.lookup_normalized_name(name.lower()) + + def lookup_normalized_name(self, name): + """ + Find the Namespace for a name also checking aliases. + + The name has to be normalized and must be lower case. + + @param name: Name of the namespace. + @type name: basestring + @return: Namespace or None + """ + return self._namespace_names.get(name)
# Temporary until Namespace.lookup_name can be removed @staticmethod @@ -892,9 +911,8 @@
def validLanguageLinks(self): """Return list of language codes that can be used in interwiki links.""" - nsnames = [name for name in self.namespaces.values()] return [lang for lang in self.languages() - if first_upper(lang) not in nsnames] + if self.namespaces.lookup_normalized_name(lang) is None]
def _interwiki_urls(self): site_paths = [self.path()] * 3 diff --git a/tests/api_tests.py b/tests/api_tests.py index 6cb0c83..9ad71d5 100644 --- a/tests/api_tests.py +++ b/tests/api_tests.py @@ -580,6 +580,7 @@ # Add custom_name for this site namespace, to match the live site. if 'Wikipedia' not in self.site.namespaces: self.site.namespaces[4].custom_name = 'Wikipedia' + self.site.namespaces._namespace_names['wikipedia'] = self.site.namespaces[4]
def test_results(self): """Test that PageGenerator yields pages with expected attributes.""" diff --git a/tests/namespace_tests.py b/tests/namespace_tests.py index 9d9c6a6..c021b4d 100644 --- a/tests/namespace_tests.py +++ b/tests/namespace_tests.py @@ -336,6 +336,30 @@ len(positive_namespaces) + len(excluded_namespaces))
+class TestNamespacesDictLookupName(TestCase): + + """Test NamespacesDict.lookup_name and lookup_normalized_name.""" + + net = False + + def test_lookup_name(self): + """Test lookup_name.""" + namespaces = builtin_NamespacesDict() + self.assertIs(namespaces.lookup_name('project'), namespaces[4]) + self.assertIs(namespaces.lookup_name('PROJECT'), namespaces[4]) + self.assertIs(namespaces.lookup_name('Project'), namespaces[4]) + self.assertIs(namespaces.lookup_name('Project:'), namespaces[4]) + + def test_lookup_normalized_name(self): + """Test lookup_normalized_name.""" + namespaces = builtin_NamespacesDict() + self.assertIs(namespaces.lookup_normalized_name('project'), + namespaces[4]) + self.assertIsNone(namespaces.lookup_normalized_name('PROJECT')) + self.assertIsNone(namespaces.lookup_normalized_name('Project')) + self.assertIsNone(namespaces.lookup_normalized_name('Project:')) + + class TestNamespacesDictGetItem(TestCase):
"""Test NamespacesDict.__getitem__.""" diff --git a/tests/site_tests.py b/tests/site_tests.py index 576b790..2439b8d 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -302,6 +302,7 @@
for item in mysite.validLanguageLinks(): self.assertIn(item, langs) + self.assertIsNone(self.site.namespaces.lookup_name(item))
def testNamespaceMethods(self): """Test cases for methods manipulating namespace names."""
pywikibot-commits@lists.wikimedia.org