John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/236257

Change subject: BuiltinNamespace IntEnum
......................................................................

BuiltinNamespace IntEnum

Move the hardcoded namespace identifiers to an IntEnum.

Change-Id: I5576def03c834c0bc180ab4c3d290da746417201
---
M pywikibot/enums.py
M pywikibot/site.py
2 files changed, 41 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/57/236257/1

diff --git a/pywikibot/enums.py b/pywikibot/enums.py
index 53ae1ee..1df21a7 100644
--- a/pywikibot/enums.py
+++ b/pywikibot/enums.py
@@ -79,3 +79,27 @@
             return LoginStatus[name], value[:end + 1]
 
         raise RuntimeError('Value {0} is not a LoginStatus repr'.format(value))
+
+
+class BuiltinNamespace(IntEnum):
+
+    """Builtin namespace enum."""
+
+    MEDIA = -2
+    SPECIAL = -1
+    MAIN = 0
+    TALK = 1
+    USER = 2
+    USER_TALK = 3
+    PROJECT = 4
+    PROJECT_TALK = 5
+    FILE = 6
+    FILE_TALK = 7
+    MEDIAWIKI = 8
+    MEDIAWIKI_TALK = 9
+    TEMPLATE = 10
+    TEMPLATE_TALK = 11
+    HELP = 12
+    HELP_TALK = 13
+    CATEGORY = 14
+    CATEGORY_TALK = 15
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 859c4f5..1278846 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -39,7 +39,7 @@
     merge_unique_dicts,
 )
 from pywikibot.comms.http import get_authentication
-from pywikibot.enums import LoginStatus
+from pywikibot.enums import BuiltinNamespace, LoginStatus
 from pywikibot.tools.ip import is_IP
 from pywikibot.throttle import Throttle
 from pywikibot.data import api
@@ -114,25 +114,6 @@
     If only one of canonical_name and custom_name are available, both
     properties will have the same value.
     """
-
-    MEDIA = -2
-    SPECIAL = -1
-    MAIN = 0
-    TALK = 1
-    USER = 2
-    USER_TALK = 3
-    PROJECT = 4
-    PROJECT_TALK = 5
-    FILE = 6
-    FILE_TALK = 7
-    MEDIAWIKI = 8
-    MEDIAWIKI_TALK = 9
-    TEMPLATE = 10
-    TEMPLATE_TALK = 11
-    HELP = 12
-    HELP_TALK = 13
-    CATEGORY = 14
-    CATEGORY_TALK = 15
 
     # These are the MediaWiki built-in names for MW 1.14+.
     # Namespace prefixes are always case-insensitive, but the
@@ -256,6 +237,22 @@
         """Return an iterator."""
         return iter(self._distinct())
 
+    def __getattr__(self, attr):
+        """
+        Get the namespace constant from BuiltinNamespace.
+
+        @param key: uppercase canonical namespace name, or MAIN
+        @type key: str
+        @rtype: BuiltinNamespace
+        """
+        if attr.isupper():
+            try:
+                return BuiltinNamespace(attr)
+            except KeyError:
+                pass
+
+        return self.__getattribute__(attr)
+
     def __getitem__(self, index):
         """Obtain an item from the iterable."""
         if self.custom_name != self.canonical_name:

-- 
To view, visit https://gerrit.wikimedia.org/r/236257
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5576def03c834c0bc180ab4c3d290da746417201
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to