diff --git a/tests/wikipedia_page_test.py b/tests/wikipedia_page_test.py index f7456e3..e5cd75b 100644 --- a/tests/wikipedia_page_test.py +++ b/tests/wikipedia_page_test.py @@ -63,3 +63,8 @@ def test_page_title_unquote(self): 'पाइथन' ) self.assertEqual(p_encoded.pageid, p_decoded.pageid) + + def test_page_with_int_namespace(self): + page = self.wiki.page('NonExisting', ns=110) + self.assertFalse(page.exists()) + self.assertEqual(110, page.namespace) diff --git a/wikipediaapi/__init__.py b/wikipediaapi/__init__.py index 7d6f75f..cad56b1 100644 --- a/wikipediaapi/__init__.py +++ b/wikipediaapi/__init__.py @@ -10,6 +10,7 @@ import logging import re from enum import IntEnum +from typing import Union import requests from typing import Dict, Any, List, Optional @@ -88,7 +89,6 @@ class Namespace(IntEnum): REFERENCE_TALK = 105 BOOK = 108 BOOK_TALK = 109 - FIX = 110 DRAFT = 118 DRAFT_TALK = 119 EDUCATION_PROGRAM = 446 @@ -103,6 +103,16 @@ class Namespace(IntEnum): GADGET_DEFINITION_TALK = 2303 +WikiNamespace = Union[Namespace, int] + + +def namespace2int(namespace: WikiNamespace) -> int: + if isinstance(namespace, Namespace): + return namespace.value + else: + return namespace + + RE_SECTION = { ExtractFormat.WIKI: re.compile(r'\n\n *(===*) (.*?) (===*) *\n'), ExtractFormat.HTML: re.compile( @@ -162,7 +172,7 @@ def __del__(self) -> None: def page( self, title: str, - ns: Namespace = Namespace.MAIN, + ns: WikiNamespace = Namespace.MAIN, unquote: bool = False, ) -> 'WikipediaPage': """ @@ -187,7 +197,7 @@ def page( # पाइथन :param title: page title as used in Wikipedia URL - :param ns: :class:`Namespace` + :param ns: :class:`WikiNamespace` :param unquote: if true it will unquote title :return: object representing :class:`WikipediaPage` """ @@ -205,7 +215,7 @@ def page( def article( self, title: str, - ns: Namespace = Namespace.MAIN, + ns: WikiNamespace = Namespace.MAIN, unquote: bool = False ) -> 'WikipediaPage': """ @@ -214,7 +224,7 @@ def article( This function is an alias for :func:`page` :param title: page title as used in Wikipedia URL - :param ns: :class:`Namespace` + :param ns: :class:`WikiNamespace` :param unquote: if true it will unquote title :return: object representing :class:`WikipediaPage` """ @@ -692,7 +702,7 @@ def _build_links( page._links[link['title']] = WikipediaPage( wiki=self, title=link['title'], - ns=Namespace(link['ns']), + ns=int(link['ns']), language=page.language ) @@ -711,7 +721,7 @@ def _build_backlinks( page._backlinks[backlink['title']] = WikipediaPage( wiki=self, title=backlink['title'], - ns=Namespace(backlink['ns']), + ns=int(backlink['ns']), language=page.language ) @@ -730,7 +740,7 @@ def _build_categories( page._categories[category['title']] = WikipediaPage( wiki=self, title=category['title'], - ns=Namespace(category['ns']), + ns=int(category['ns']), language=page.language ) @@ -749,7 +759,7 @@ def _build_categorymembers( p = WikipediaPage( wiki=self, title=member['title'], - ns=Namespace(member['ns']), + ns=int(member['ns']), language=page.language ) p.pageid = member['pageid'] # type: ignore @@ -906,7 +916,7 @@ def __init__( self, wiki: Wikipedia, title: str, - ns: Namespace = Namespace.MAIN, + ns: WikiNamespace = Namespace.MAIN, language: str = 'en', url: str = None ) -> None: @@ -932,7 +942,7 @@ def __init__( self._attributes = { 'title': title, - 'ns': ns.value, + 'ns': namespace2int(ns), 'language': language } # type: Dict[str, Any] @@ -970,13 +980,13 @@ def title(self) -> str: return self._attributes['title'] @property - def namespace(self) -> Namespace: + def namespace(self) -> int: """ Returns namespace of the current page. :return: namespace """ - return Namespace(self._attributes['ns']) + return int(self._attributes['ns']) # # @property