Skip to content

Commit

Permalink
[IMPR] Use enums for Namespaces instead of integers
Browse files Browse the repository at this point in the history
Change-Id: If827b5e8e130d54fe6f3363a0fad8ba4aec2e177
  • Loading branch information
Mpaa committed Dec 9, 2023
1 parent 1c4cd9c commit 0ae4a70
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions pywikibot/site/_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def __init__(self, id,

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:
Expand Down Expand Up @@ -135,7 +135,7 @@ def __contains__(self, item: str) -> bool:
: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)
Expand Down Expand Up @@ -175,10 +175,10 @@ def __getitem__(self, index):
@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 + ':'
Expand Down Expand Up @@ -266,18 +266,19 @@ def __repr__(self) -> str:
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

@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):
Expand Down

0 comments on commit 0ae4a70

Please sign in to comment.