Skip to content

Commit

Permalink
Makes namespace union of int and enum
Browse files Browse the repository at this point in the history
Issue #29
  • Loading branch information
martin-majlis committed Jan 26, 2020
1 parent faa1542 commit 1b677a2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
5 changes: 5 additions & 0 deletions tests/wikipedia_page_test.py
Expand Up @@ -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)
36 changes: 23 additions & 13 deletions wikipediaapi/__init__.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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':
"""
Expand All @@ -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`
"""
Expand All @@ -205,7 +215,7 @@ def page(
def article(
self,
title: str,
ns: Namespace = Namespace.MAIN,
ns: WikiNamespace = Namespace.MAIN,
unquote: bool = False
) -> 'WikipediaPage':
"""
Expand All @@ -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`
"""
Expand Down Expand Up @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -932,7 +942,7 @@ def __init__(

self._attributes = {
'title': title,
'ns': ns.value,
'ns': namespace2int(ns),
'language': language
} # type: Dict[str, Any]

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1b677a2

Please sign in to comment.