jenkins-bot has submitted this change and it was merged.
Change subject: Change BaseSite.namespaces from method to property
......................................................................
Change BaseSite.namespaces from method to property
Use new SelfCallDict to allow either syntax:
- site.namespaces()
- site.namespaces
Change-Id: I46d33f9c6041f9ba32748c62ad3dae3f48efdb10
---
M pywikibot/site.py
M pywikibot/tools.py
M tests/utils.py
M tests/wikibase_tests.py
4 files changed, 35 insertions(+), 9 deletions(-)
Approvals:
XZise: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 9cadefe..28a16a2 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -29,7 +29,8 @@
import pywikibot.family
from pywikibot.tools import (
itergroup, deprecated, deprecate_arg, UnicodeMixin, ComparableMixin,
- redirect_func, add_decorated_full_name, deprecated_args
+ redirect_func, add_decorated_full_name, deprecated_args,
+ SelfCallDict, SelfCallString,
)
from pywikibot.tools import MediaWikiVersion as LV
from pywikibot.throttle import Throttle
@@ -555,11 +556,14 @@
raise AttributeError("%s instance has no attribute '%s'"
% (self.__class__.__name__, attr))
- def sitename(self):
+ def __str__(self):
"""Return string representing this Site's name and
code."""
return self.family.name + ':' + self.code
- __str__ = sitename
+ @property
+ def sitename(self):
+ """String representing this Site's name and
code."""
+ return SelfCallString(self.__str__())
def __repr__(self):
return 'Site("%s", "%s")' % (self.code,
self.family.name)
@@ -637,11 +641,13 @@
getNamespaceIndex = redirect_func(ns_index, old_name='getNamespaceIndex',
class_name='BaseSite')
+ @property
def namespaces(self):
"""Return dict of valid namespaces on this
wiki."""
if not hasattr(self, '_namespaces'):
use_image_name = LV(self.version()) < LV("1.14")
- self._namespaces = Namespace.builtin_namespaces(use_image_name)
+ self._namespaces = SelfCallDict(
+ Namespace.builtin_namespaces(use_image_name))
return self._namespaces
def ns_normalize(self, value):
@@ -2000,8 +2006,7 @@
return self.getmagicwords("pagenamee")
def _build_namespaces(self):
-
- self._namespaces = {}
+ self._namespaces = SelfCallDict()
# In MW 1.14, API siprop 'namespaces' added 'canonical',
# and Image became File with Image as an alias.
@@ -2143,6 +2148,7 @@
# 'title' is expected to be URL-encoded already
return self.siteinfo["articlepath"].replace("$1", title)
+ @property
def namespaces(self):
"""Return dict of valid namespaces on this
wiki."""
if not hasattr(self, '_namespaces'):
diff --git a/pywikibot/tools.py b/pywikibot/tools.py
index 39f31ca..bd1e109 100644
--- a/pywikibot/tools.py
+++ b/pywikibot/tools.py
@@ -321,6 +321,24 @@
EMPTY_DEFAULT = EmptyDefault()
+class SelfCallMixin(object):
+
+ """Return self when called."""
+
+ def __call__(self):
+ return self
+
+
+class SelfCallDict(SelfCallMixin, dict):
+
+ """Dict with SelfCallMixin."""
+
+
+class SelfCallString(SelfCallMixin, str):
+
+ """Unicode string with SelfCallMixin."""
+
+
class DequeGenerator(deque):
"""A generator that allows items to be added during
generating."""
diff --git a/tests/utils.py b/tests/utils.py
index 05eecd4..c932e59 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -9,6 +9,7 @@
__version__ = '$Id$'
#
import pywikibot
+from pywikibot.tools import SelfCallDict
from pywikibot.site import Namespace
from pywikibot.data.api import CachedRequest
from pywikibot.data.api import Request as _original_Request
@@ -109,7 +110,7 @@
self._userinfo = pywikibot.tools.EMPTY_DEFAULT
self._siteinfo = DummySiteinfo({})
self._siteinfo._cache['lang'] = (code, True)
- self._namespaces = Namespace.builtin_namespaces()
+ self._namespaces = SelfCallDict(Namespace.builtin_namespaces())
@property
def userinfo(self):
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 05f47c5..c9ec516 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -12,6 +12,7 @@
import sys
import pywikibot
from pywikibot import pagegenerators
+from pywikibot.tools import SelfCallDict
from pywikibot.page import WikibasePage
from pywikibot.site import Namespace
import json
@@ -790,7 +791,7 @@
def setUpClass(cls):
super(TestAlternateNamespaces, cls).setUpClass()
- cls.get_repo()._namespaces = {
+ cls.get_repo()._namespaces = SelfCallDict({
90: Namespace(id=90,
case='first-letter',
canonical_name='Item',
@@ -799,7 +800,7 @@
case='first-letter',
canonical_name='Prop',
defaultcontentmodel='wikibase-property')
- }
+ })
def test_alternate_item_namespace(self):
item = pywikibot.ItemPage(self.repo, 'Q60')
--
To view, visit
https://gerrit.wikimedia.org/r/170055
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I46d33f9c6041f9ba32748c62ad3dae3f48efdb10
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>