jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/981633 )
Change subject: [IMPR] Use enums for Namespaces instead of integers ......................................................................
[IMPR] Use enums for Namespaces instead of integers
Change-Id: If827b5e8e130d54fe6f3363a0fad8ba4aec2e177 --- M pywikibot/site/_namespace.py 1 file changed, 20 insertions(+), 10 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site/_namespace.py b/pywikibot/site/_namespace.py index 44114ec..275e9e5 100644 --- a/pywikibot/site/_namespace.py +++ b/pywikibot/site/_namespace.py @@ -95,9 +95,9 @@
if aliases: self.aliases = aliases - elif id in (6, 7): + elif id in (BuiltinNamespace.FILE, BuiltinNamespace.FILE_TALK): alias = 'Image' - if id == 7: + if id == BuiltinNamespace.FILE_TALK: alias += ' talk' self.aliases = [alias] else: @@ -135,7 +135,7 @@
:param item: name to check """ - if item == '' and self.id == 0: + if item == '' and self.id == BuiltinNamespace.MAIN: return True
name = Namespace.normalize_name(item) @@ -175,10 +175,10 @@ @staticmethod def _colons(id, name): """Return the name with required colons, depending on the ID.""" - if id == 0: + if id == BuiltinNamespace.MAIN: return ':'
- if id in (6, 14): + if id in (BuiltinNamespace.FILE, BuiltinNamespace.CATEGORY): return ':' + name + ':'
return name + ':' @@ -266,9 +266,10 @@ def default_case(id, default_case=None): """Return the default fixed case value for the namespace ID.""" # https://www.mediawiki.org/wiki/Manual:$wgCapitalLinkOverrides#Warning - if id > 0 and id % 2 == 1: # the talk ns has the non-talk ns case - id -= 1 - if id in (-1, 2, 8): + if id in (BuiltinNamespace.SPECIAL, + BuiltinNamespace.USER, BuiltinNamespace.USER_TALK, + BuiltinNamespace.MEDIAWIKI, BuiltinNamespace.MEDIAWIKI_TALK, + ): return 'first-letter'
return default_case @@ -276,8 +277,8 @@ @classmethod def builtin_namespaces(cls, case: str = 'first-letter'): """Return a dict of the builtin namespaces.""" - return {i: cls(i, case=cls.default_case(i, case)) - for i in range(-2, 16)} + return {e.value: cls(e.value, case=cls.default_case(e.value, case)) + for e in BuiltinNamespace}
@staticmethod def normalize_name(name):
pywikibot-commits@lists.wikimedia.org