jenkins-bot submitted this change.

View Change


Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] Use BuiltinNamespace enum instead of int

Change-Id: I80a9c638562edbbccd5cc5308361e7d15a49c664
---
M pywikibot/site/__init__.py
M pywikibot/page/_basepage.py
M pywikibot/cosmetic_changes.py
M pywikibot/site/_apisite.py
M pywikibot/data/api/_generators.py
M scripts/patrol.py
6 files changed, 46 insertions(+), 27 deletions(-)

diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index 845d6ce..27650ef 100644
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -66,6 +66,7 @@
import pywikibot
from pywikibot import exceptions, textlib
from pywikibot.backports import Callable, Match, Pattern
+from pywikibot.site import Namespace
from pywikibot.textlib import (
FILE_LINK_REGEX,
MultiTemplateMatchBuilder,
@@ -247,7 +248,7 @@
self.namespace = page.namespace()

self.show_diff = show_diff
- self.template = (self.namespace == 10)
+ self.template = (self.namespace == Namespace.TEMPLATE)
self.talkpage = self.namespace >= 0 and self.namespace % 2 == 1
self.ignore = ignore

@@ -402,12 +403,13 @@
exceptions = ['nowiki', 'comment', 'math', 'pre']

for namespace in self.site.namespaces.values():
- if namespace == 0:
+ if namespace == Namespace.MAIN:
# skip main (article) namespace
continue
# a clone is needed. Won't change the namespace dict
namespaces = list(namespace)
- if namespace == 6 and self.site.family.name == 'wikipedia':
+ if (namespace == Namespace.FILE
+ and self.site.family.name == 'wikipedia'):
if self.site.code in ('en', 'fr'):
# do not change "Image" on en-wiki and fr-wiki
with suppress(ValueError):
@@ -423,9 +425,11 @@
0, namespaces.pop(namespaces.index('Imagem')))
# final namespace variant
final_ns = namespaces.pop(0)
- if namespace in (2, 3):
+ if namespace in (Namespace.USER, Namespace.USER_TALK):
# skip localized user namespace, maybe gender is used
- namespaces = ['User' if namespace == 2 else 'User talk']
+ namespaces = ['User'
+ if namespace == Namespace.USER
+ else 'User talk']
# lowerspaced and underscored namespaces
for i, item in enumerate(namespaces):
item = item.replace(' ', '[ _]')
@@ -433,7 +437,8 @@
namespaces[i] = item
namespaces.append(first_lower(final_ns))
if final_ns and namespaces:
- if self.site.sitename == 'wikipedia:pt' and namespace == 6:
+ if (self.site.sitename == 'wikipedia:pt'
+ and namespace == Namespace.FILE):
# only change on these file extensions (per T57242)
extensions = ('png', 'gif', 'jpg', 'jpeg', 'svg', 'tiff',
'tif')
@@ -544,7 +549,7 @@
page = pywikibot.Page(pywikibot.Link(titleWithSection, self.site))
in_main_namespace = None
with suppress(exceptions.InvalidTitleError):
- in_main_namespace = page.namespace() == 0
+ in_main_namespace = page.namespace() == Namespace.MAIN
if not in_main_namespace:
return oldlink

@@ -682,7 +687,7 @@
def removeEmptySections(self, text: str) -> str:
"""Cleanup empty sections."""
# userspace contains article stubs without nobots/in use templates
- if self.namespace == 2:
+ if self.namespace == Namespace.USER:
return text

skippings = ['comment', 'category']
@@ -818,10 +823,11 @@
def replace_link(match: Match[str]) -> str:
"""Create a string to replace a single link."""
replacement = '[['
- if re.match(r'(?:{}):'
- .format('|'.join((*self.site.namespaces[6],
- *self.site.namespaces[14]))),
- match['link']):
+ if re.match(
+ r'(?:{}):'.format('|'.join(
+ (*self.site.namespaces[Namespace.FILE],
+ *self.site.namespaces[Namespace.CATEGORY]))),
+ match['link']):
replacement += ':'

replacement += match['link']
@@ -1045,7 +1051,8 @@
[1]:
https://commons.wikimedia.org/wiki/Commons:Tools/pywiki_file_description_cleanup
"""
- if self.site.sitename != 'commons:commons' or self.namespace == 6:
+ if (self.site.sitename != 'commons:commons'
+ or self.namespace == Namespace.FILE):
return text

# section headers to {{int:}} versions
diff --git a/pywikibot/data/api/_generators.py b/pywikibot/data/api/_generators.py
index af10237..4b0d1f0 100644
--- a/pywikibot/data/api/_generators.py
+++ b/pywikibot/data/api/_generators.py
@@ -19,6 +19,7 @@
import pywikibot
from pywikibot import config
from pywikibot.exceptions import Error, InvalidTitleError, UnsupportedPageError
+from pywikibot.site import Namespace
from pywikibot.tools import deprecated
from pywikibot.tools.collections import GeneratorWrapper

@@ -718,12 +719,12 @@
p = pywikibot.Page(self.site, pagedata['title'], pagedata['ns'])
ns = pagedata['ns']
# Upcast to proper Page subclass.
- if ns == 2:
+ if ns == Namespace.USER:
p = pywikibot.User(p)
- elif ns == 6:
+ elif ns == Namespace.FILE:
with suppress(ValueError):
p = pywikibot.FilePage(p)
- elif ns == 14:
+ elif ns == Namespace.CATEGORY:
p = pywikibot.Category(p)
update_page(p, pagedata, self.props)
return p
diff --git a/pywikibot/page/_basepage.py b/pywikibot/page/_basepage.py
index b6b7255..9ff008c 100644
--- a/pywikibot/page/_basepage.py
+++ b/pywikibot/page/_basepage.py
@@ -267,12 +267,12 @@
title = f'{self.site.code}:{title}'
elif textlink and (self.is_filepage() or self.is_categorypage()):
title = f':{title}'
- elif self.namespace() == 0 and not section:
+ elif self.namespace() == Namespace.MAIN and not section:
with_ns = True
if with_ns:
return f'[[{title}{section}]]'
return f'[[{title}{section}|{label}]]'
- if not with_ns and self.namespace() != 0:
+ if not with_ns and self.namespace() != Namespace.MAIN:
title = label + section
else:
title += section
@@ -845,11 +845,11 @@

def is_categorypage(self):
"""Return True if the page is a Category, False otherwise."""
- return self.namespace() == 14
+ return self.namespace() == Namespace.CATEGORY

def is_filepage(self):
"""Return True if this is a file description page, False otherwise."""
- return self.namespace() == 6
+ return self.namespace() == Namespace.FILE

def isDisambig(self) -> bool:
"""
diff --git a/pywikibot/site/__init__.py b/pywikibot/site/__init__.py
index 146ee74..8a7c5d4 100644
--- a/pywikibot/site/__init__.py
+++ b/pywikibot/site/__init__.py
@@ -6,11 +6,13 @@
#
from __future__ import annotations

+# to prevent ImportError exception due to circular imports
+from pywikibot.site._namespace import Namespace, NamespacesDict
+
from pywikibot.site._apisite import APISite
from pywikibot.site._basesite import BaseSite
from pywikibot.site._datasite import DataSite
from pywikibot.site._namespace import NamespaceArgType # noqa: F401
-from pywikibot.site._namespace import Namespace, NamespacesDict
from pywikibot.site._obsoletesites import ClosedSite, RemovedSite
from pywikibot.site._siteinfo import Siteinfo
from pywikibot.site._tokenwallet import TokenWallet
diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index 1b4b1e6..defdd83 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -1565,14 +1565,13 @@

# Upcast to proper Page subclass.
ns = target.namespace()
- if ns == 2:
+ if ns == Namespace.USER:
target = pywikibot.User(target)
- elif ns == 6:
+ elif ns == Namespace.FILE:
target = pywikibot.FilePage(target)
- elif ns == 14:
+ elif ns == Namespace.CATEGORY:
target = pywikibot.Category(target)
page._redirtarget = target
-
return page._redirtarget

@deprecated(since='8.0.0')
diff --git a/scripts/patrol.py b/scripts/patrol.py
index cf3df12..c0d422f 100755
--- a/scripts/patrol.py
+++ b/scripts/patrol.py
@@ -58,6 +58,7 @@
from pywikibot import pagegenerators
from pywikibot.backports import Container, removeprefix
from pywikibot.bot import BaseBot, suggest_help
+from pywikibot.site import Namespace


def verbose_output(string) -> None:
@@ -216,7 +217,7 @@
continue

obj = pywikibot.Link(node.title, self.site)
- if obj.namespace == -1:
+ if obj.namespace == Namespace.SPECIAL:
# the parser accepts 'special:prefixindex/' as a wildcard
# this allows a prefix that doesn't match an existing page
# to be a blue link, and can be clicked to see what pages
@@ -231,7 +232,7 @@
verbose_output('Whitelist prefixindex hack for: '
+ page)

- elif obj.namespace == 2 and not current_user:
+ elif obj.namespace == Namespace.USER and not current_user:
# if a target user hasn't been found yet, and the link is
# 'user:'
# the user will be the target of subsequent rules

To view, visit change 981672. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I80a9c638562edbbccd5cc5308361e7d15a49c664
Gerrit-Change-Number: 981672
Gerrit-PatchSet: 7
Gerrit-Owner: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged