Skip to content

Commit

Permalink
Replace print() with logging methods
Browse files Browse the repository at this point in the history
Co-Authored-By: Dennis Priskorn <[email protected]>
  • Loading branch information
LeMyst and dpriskorn committed Jun 1, 2022
1 parent 18954ee commit 0dd072f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion wikibaseintegrator/entities/baseentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _write(self, data: Dict = None, summary: str = None, login: _Login = None, a
try:
json_result: dict = mediawiki_api_call_helper(data=payload, login=login, allow_anonymous=allow_anonymous, is_bot=is_bot, **kwargs)
except Exception:
print('Error while writing to the Wikibase instance')
logging.error('Error while writing to the Wikibase instance')
raise
else:
if 'error' in json_result and 'messages' in json_result['error']:
Expand Down
5 changes: 3 additions & 2 deletions wikibaseintegrator/wbi_backoff.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
WikibaseIntegrator implementation of backoff python library.
"""
import logging
import sys
from functools import partial
from json import JSONDecodeError
Expand All @@ -14,8 +15,8 @@
def wbi_backoff_backoff_hdlr(details):
exc_type, exc_value, _ = sys.exc_info()
if exc_type == JSONDecodeError:
print(exc_value.doc) # pragma: no cover
print("Backing off {wait:0.1f} seconds afters {tries} tries calling function with args {args} and kwargs {kwargs}".format(**details)) # pylint: disable=consider-using-f-string
logging.error(exc_value.doc) # pragma: no cover
logging.error("Backing off {wait:0.1f} seconds afters {tries} tries calling function with args {args} and kwargs {kwargs}".format(**details)) # pylint: disable=consider-using-f-string


def wbi_backoff_check_json_decode_error(e) -> bool:
Expand Down
16 changes: 8 additions & 8 deletions wikibaseintegrator/wbi_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Sess
try:
response = session.request(method=method, url=mediawiki_api_url, **kwargs)
except requests.exceptions.ConnectionError as e:
print(f"Connection error: {e}. Sleeping for {retry_after} seconds.")
logging.error(f"Connection error: {e}. Sleeping for {retry_after} seconds.")
sleep(retry_after)
continue
if response.status_code in (500, 502, 503, 504):
print(f"Service unavailable (HTTP Code {response.status_code}). Sleeping for {retry_after} seconds.")
logging.error(f"Service unavailable (HTTP Code {response.status_code}). Sleeping for {retry_after} seconds.")
sleep(retry_after)
continue

Expand All @@ -87,7 +87,7 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Sess
error_msg_names = {x.get('name') for x in json_data['error']['messages']}
if 'actionthrottledtext' in error_msg_names: # pragma: no cover
sleep_sec = int(response.headers.get('retry-after', retry_after))
print(f"{datetime.datetime.utcnow()}: rate limited. sleeping for {sleep_sec} seconds")
logging.error(f"{datetime.datetime.utcnow()}: rate limited. sleeping for {sleep_sec} seconds")
sleep(sleep_sec)
continue

Expand All @@ -100,13 +100,13 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Sess
sleep_sec = max(sleep_sec, 5)
# The number of second can't be more than retry_after
sleep_sec = min(sleep_sec, retry_after)
print(f"{datetime.datetime.utcnow()}: maxlag. sleeping for {sleep_sec} seconds")
logging.error(f"{datetime.datetime.utcnow()}: maxlag. sleeping for {sleep_sec} seconds")
sleep(sleep_sec)
continue

# readonly
if 'code' in json_data['error'] and json_data['error']['code'] == 'readonly': # pragma: no cover
print(f'The Wikibase instance is currently in readonly mode, waiting for {retry_after} seconds')
logging.error(f'The Wikibase instance is currently in readonly mode, waiting for {retry_after} seconds')
sleep(retry_after)
continue

Expand Down Expand Up @@ -248,17 +248,17 @@ def execute_sparql_query(query: str, prefix: str = None, endpoint: str = None, u
try:
response = helpers_session.post(sparql_endpoint_url, params=params, headers=headers)
except requests.exceptions.ConnectionError as e:
print(f"Connection error: {e}. Sleeping for {retry_after} seconds.")
logging.error(f"Connection error: {e}. Sleeping for {retry_after} seconds.")
sleep(retry_after)
continue
if response.status_code in (500, 502, 503, 504):
print(f"Service unavailable (HTTP Code {response.status_code}). Sleeping for {retry_after} seconds.")
logging.error(f"Service unavailable (HTTP Code {response.status_code}). Sleeping for {retry_after} seconds.")
sleep(retry_after)
continue
if response.status_code == 429:
if 'retry-after' in response.headers.keys():
retry_after = int(response.headers['retry-after'])
print(f"Too Many Requests (429). Sleeping for {retry_after} seconds")
logging.error(f"Too Many Requests (429). Sleeping for {retry_after} seconds")
sleep(retry_after)
continue
response.raise_for_status()
Expand Down
8 changes: 4 additions & 4 deletions wikibaseintegrator/wbi_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ def __init__(self, user: str = None, password: str = None, mediawiki_api_url: st
raise LoginError(f"Login failed. Reason: '{login_result['login']['reason']}'")

if 'warnings' in login_result:
print("MediaWiki login warnings messages:")
logging.warning("MediaWiki login warnings messages:")
for message in login_result['warnings']:
print(f"* {message}: {login_result['warnings'][message]['*']}")
logging.warning(f"* {message}: {login_result['warnings'][message]['*']}")

super().__init__(session=session, token_renew_period=token_renew_period, user_agent=user_agent, mediawiki_api_url=mediawiki_api_url)

Expand Down Expand Up @@ -296,9 +296,9 @@ def __init__(self, user: str = None, password: str = None, mediawiki_api_url: st
raise LoginError(f"Login failed ({login_result['error']['code']}). Message: '{login_result['error']['info']}'")

if 'warnings' in login_result:
print("MediaWiki login warnings messages:")
logging.warning("MediaWiki login warnings messages:")
for message in login_result['warnings']:
print(f"* {message}: {login_result['warnings'][message]['*']}")
logging.warning(f"* {message}: {login_result['warnings'][message]['*']}")

super().__init__(session=session, token_renew_period=token_renew_period, user_agent=user_agent, mediawiki_api_url=mediawiki_api_url)

Expand Down

1 comment on commit 0dd072f

@dpriskorn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Thanks. This makes it much easier for me to control the UX downstream.

Please sign in to comment.