jenkins-bot submitted this change.
[IMPR] Move LoginStatus class from site to login.py
- Move LoginStatus class from site.__init__.py to login.py to prevent
circular imports
- Update LoginStatus usage
- desupport site.LoginStatus with FutureWarning
Change-Id: I84e2baababf769e09ad3058268bedad9393c9613
---
M pywikibot/data/api.py
M pywikibot/login.py
M pywikibot/site/__init__.py
M scripts/maintenance/cache.py
M tests/dry_api_tests.py
M tests/site_tests.py
M tests/utils.py
7 files changed, 61 insertions(+), 52 deletions(-)
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 81175d4..be9f6b2 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -36,6 +36,7 @@
Error, TimeoutError, MaxlagTimeoutError, InvalidTitle, UnsupportedPage
)
from pywikibot.family import SubdomainFamily
+from pywikibot.login import LoginStatus
from pywikibot.tools import (
deprecated, itergroup, PYTHON_VERSION, remove_last_args
)
@@ -1697,8 +1698,7 @@
# case, force a re-login.
username = result['query']['userinfo']['name']
if (self.site.user() is not None and self.site.user() != username
- and self.site._loginstatus
- != pywikibot.site.LoginStatus.IN_PROGRESS):
+ and self.site._loginstatus != LoginStatus.IN_PROGRESS):
message = ("Logged in as '{actual}' instead of '{expected}'."
.format(actual=username, expected=self.site.user()))
self._relogin(message)
@@ -1815,7 +1815,7 @@
if code != 'badtoken': # Other code not handled here
return False
- if self.site._loginstatus == pywikibot.site.LoginStatus.IN_PROGRESS:
+ if self.site._loginstatus == LoginStatus.IN_PROGRESS:
pywikibot.log('Login status: {}'
.format(self.site._loginstatus.name))
return False
@@ -1954,9 +1954,8 @@
# If readapidenied is returned try to login
if code == 'readapidenied' \
- and self.site._loginstatus in (
- pywikibot.site.LoginStatus.NOT_ATTEMPTED,
- pywikibot.site.LoginStatus.NOT_LOGGED_IN):
+ and self.site._loginstatus in (LoginStatus.NOT_ATTEMPTED,
+ LoginStatus.NOT_LOGGED_IN):
self.site.login()
continue
@@ -2075,7 +2074,7 @@
"""
login_status = self.site._loginstatus
- if login_status >= pywikibot.site.LoginStatus.AS_USER:
+ if login_status >= LoginStatus.AS_USER:
# This uses the format of Page.__repr__, without performing
# config.console_encoding as done by Page.__repr__.
# The returned value can't be encoded to anything other than
@@ -2083,8 +2082,7 @@
# tries to encode it as utf-8.
user_key = 'User(User:{})'.format(self.site.userinfo['name'])
else:
- user_key = repr(pywikibot.site.LoginStatus(
- pywikibot.site.LoginStatus.NOT_LOGGED_IN))
+ user_key = repr(LoginStatus(LoginStatus.NOT_LOGGED_IN))
request_key = repr(sorted(self._encoded_items().items()))
return repr(self.site) + user_key + request_key
@@ -3025,7 +3023,7 @@
if self.site.family.ldapDomain:
login_request[self.keyword('ldap')] = self.site.family.ldapDomain
- self.site._loginstatus = pywikibot.site.LoginStatus.IN_PROGRESS
+ self.site._loginstatus = LoginStatus.IN_PROGRESS
while True:
# get token using meta=tokens if supported
if not below_mw_1_27:
diff --git a/pywikibot/login.py b/pywikibot/login.py
index 9a283d0..7b5a105 100644
--- a/pywikibot/login.py
+++ b/pywikibot/login.py
@@ -10,6 +10,7 @@
import os
import webbrowser
+from enum import IntEnum
from warnings import warn
import pywikibot
@@ -54,6 +55,35 @@
}
+class LoginStatus(IntEnum):
+
+ """
+ Enum for Login statuses.
+
+ >>> LoginStatus.NOT_ATTEMPTED
+ LoginStatus(-3)
+ >>> LoginStatus.IN_PROGRESS.value
+ -2
+ >>> LoginStatus.NOT_LOGGED_IN.name
+ NOT_LOGGED_IN
+ >>> int(LoginStatus.AS_USER)
+ 0
+ >>> LoginStatus(-3).name
+ 'NOT_ATTEMPTED'
+ >>> LoginStatus(0).name
+ 'AS_USER'
+ """
+
+ NOT_ATTEMPTED = -3
+ IN_PROGRESS = -2
+ NOT_LOGGED_IN = -1
+ AS_USER = 0
+
+ def __repr__(self):
+ """Return internal representation."""
+ return 'LoginStatus({})'.format(self)
+
+
class LoginManager:
"""Site login manager."""
diff --git a/pywikibot/site/__init__.py b/pywikibot/site/__init__.py
index 7a1fd1c..ec2b345 100644
--- a/pywikibot/site/__init__.py
+++ b/pywikibot/site/__init__.py
@@ -27,8 +27,8 @@
from collections import defaultdict, namedtuple
from collections.abc import Iterable, Container, Mapping
from contextlib import suppress
-from enum import IntEnum
from itertools import zip_longest
+from pywikibot.login import LoginStatus as _LoginStatus
from textwrap import fill
from typing import Optional
from warnings import warn
@@ -82,6 +82,7 @@
itergroup,
MediaWikiVersion,
merge_unique_dicts,
+ ModuleDeprecationWrapper,
normalize_username,
remove_last_args,
SelfCallMixin,
@@ -100,35 +101,6 @@
"""Page cannot be reserved for writing due to existing lock."""
-class LoginStatus(IntEnum):
-
- """
- Enum for Login statuses.
-
- >>> LoginStatus.NOT_ATTEMPTED
- LoginStatus(-3)
- >>> LoginStatus.IN_PROGRESS.value
- -2
- >>> LoginStatus.NOT_LOGGED_IN.name
- NOT_LOGGED_IN
- >>> int(LoginStatus.AS_USER)
- 0
- >>> LoginStatus(-3).name
- 'NOT_ATTEMPTED'
- >>> LoginStatus(0).name
- 'AS_USER'
- """
-
- NOT_ATTEMPTED = -3
- IN_PROGRESS = -2
- NOT_LOGGED_IN = -1
- AS_USER = 0
-
- def __repr__(self):
- """Return internal representation."""
- return 'LoginStatus({})'.format(self)
-
-
class Namespace(Iterable, ComparableMixin):
"""
@@ -1654,7 +1626,7 @@
"""Initializer."""
super().__init__(code, fam, user)
self._msgcache = {}
- self._loginstatus = LoginStatus.NOT_ATTEMPTED
+ self._loginstatus = _LoginStatus.NOT_ATTEMPTED
self._siteinfo = Siteinfo(self)
self._paraminfo = api.ParamInfo(self)
self._interwikimap = _InterwikiMap(self)
@@ -1826,7 +1798,7 @@
# (below) is successful. Instead, log the problem,
# to be increased to 'warning' level once majority
# of issues are resolved.
- if self._loginstatus == LoginStatus.IN_PROGRESS:
+ if self._loginstatus == _LoginStatus.IN_PROGRESS:
pywikibot.log(
'{!r}.login() called when a previous login was in progress.'
.format(self))
@@ -1835,11 +1807,11 @@
# logged_in() is False if _userinfo exists, which means this
# will have no effect for the invocation from api.py
if self.logged_in():
- self._loginstatus = LoginStatus.AS_USER
+ self._loginstatus = _LoginStatus.AS_USER
return
# check whether a login cookie already exists for this user
# or check user identity when OAuth enabled
- self._loginstatus = LoginStatus.IN_PROGRESS
+ self._loginstatus = _LoginStatus.IN_PROGRESS
try:
self.getuserinfo(force=True)
if self.userinfo['name'] == self.user():
@@ -1874,9 +1846,9 @@
if login_manager.login(retry=True, autocreate=autocreate):
self._username = login_manager.username
self.getuserinfo(force=True)
- self._loginstatus = LoginStatus.AS_USER
+ self._loginstatus = _LoginStatus.AS_USER
else:
- self._loginstatus = LoginStatus.NOT_LOGGED_IN # failure
+ self._loginstatus = _LoginStatus.NOT_LOGGED_IN # failure
def _relogin(self):
"""Force a login sequence without logging out, using the current user.
@@ -1886,7 +1858,7 @@
from the site.
"""
del self._userinfo
- self._loginstatus = LoginStatus.NOT_LOGGED_IN
+ self._loginstatus = _LoginStatus.NOT_LOGGED_IN
self.login()
def logout(self):
@@ -1906,7 +1878,7 @@
req_params['token'] = self.tokens['csrf']
uirequest = self._simple_request(**req_params)
uirequest.submit()
- self._loginstatus = LoginStatus.NOT_LOGGED_IN
+ self._loginstatus = _LoginStatus.NOT_LOGGED_IN
# Reset tokens and user properties
del self._userinfo
@@ -8002,3 +7974,10 @@
site=self, parameters=parameters)
gen.set_maximum_items(total)
return gen
+
+
+wrapper = ModuleDeprecationWrapper(__name__)
+# Note: use LoginStatus instead of _LoginStatus
+# after desupport warning is removed
+wrapper._add_deprecated_attr('LoginStatus', _LoginStatus,
+ since='20200919', future_warning=True)
diff --git a/scripts/maintenance/cache.py b/scripts/maintenance/cache.py
index a59aa33..dc158a7 100755
--- a/scripts/maintenance/cache.py
+++ b/scripts/maintenance/cache.py
@@ -79,8 +79,9 @@
from pywikibot.data import api
# The follow attributes are used by eval()
+from pywikibot.login import LoginStatus
from pywikibot.page import User
-from pywikibot.site import APISite, ClosedSite, DataSite, LoginStatus
+from pywikibot.site import APISite, ClosedSite, DataSite
from pywikibot.tools import PYTHON_VERSION
diff --git a/tests/dry_api_tests.py b/tests/dry_api_tests.py
index 019e391..c6a06c6 100644
--- a/tests/dry_api_tests.py
+++ b/tests/dry_api_tests.py
@@ -15,7 +15,7 @@
QueryGenerator,
)
from pywikibot.family import Family
-from pywikibot.site import LoginStatus
+from pywikibot.login import LoginStatus
from pywikibot.tools import suppress_warnings
from tests import join_images_path, patch
diff --git a/tests/site_tests.py b/tests/site_tests.py
index 6286be3..69fa64d 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -3688,7 +3688,7 @@
def test_login_logout(self):
"""Validate login and logout methods by toggling the state."""
site = self.get_site()
- loginstatus = pywikibot.site.LoginStatus
+ loginstatus = pywikibot.login.LoginStatus
self.assertTrue(site.logged_in())
self.assertIn(site._loginstatus, (loginstatus.IN_PROGRESS,
diff --git a/tests/utils.py b/tests/utils.py
index 2dbab7b..b8ab0a7 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -27,6 +27,7 @@
from pywikibot import config
from pywikibot.data.api import CachedRequest, APIError
from pywikibot.data.api import Request as _original_Request
+from pywikibot.login import LoginStatus
from pywikibot.site import Namespace
from tests import _pwb_py, unittest
@@ -335,7 +336,7 @@
"""Dummy class to use instead of L{pywikibot.site.APISite}."""
- _loginstatus = pywikibot.site.LoginStatus.NOT_ATTEMPTED
+ _loginstatus = LoginStatus.NOT_ATTEMPTED
def __init__(self, code, fam, user):
"""Initializer."""
To view, visit change 628482. To unsubscribe, or for help writing mail filters, visit settings.