diff --git a/src/sparsezoo/analytics.py b/src/sparsezoo/analytics.py index 7190ab8c..6814b139 100644 --- a/src/sparsezoo/analytics.py +++ b/src/sparsezoo/analytics.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import asyncio import json import os import threading @@ -25,13 +24,13 @@ from requests import HTTPError from sparsezoo.utils.gdpr import is_gdpr_country +from sparsezoo.utils.helpers import disable_request_logs from sparsezoo.version import version as sparsezoo_version __all__ = ["GoogleAnalytics", "analytics_disabled", "sparsezoo_analytics"] -_LOOP = asyncio.get_event_loop() _DEBUG = os.getenv("NM_DEBUG_ANALYTICS") @@ -133,19 +132,19 @@ def _send_request(): "Content-Type": "application/json", } data = json.dumps(payload) - - try: - response = requests.post(self._url, headers=headers, data=data) - response.raise_for_status() - body = response.content - if _DEBUG: - print(body) - except HTTPError as http_error: - if _DEBUG: - print(http_error) - - if raise_errors: - raise http_error + with disable_request_logs(): + try: + response = requests.post(self._url, headers=headers, data=data) + response.raise_for_status() + body = response.content + if _DEBUG: + print(body) + except HTTPError as http_error: + if _DEBUG: + print(http_error) + + if raise_errors: + raise http_error thread = threading.Thread(target=_send_request) thread.start() diff --git a/src/sparsezoo/utils/gdpr.py b/src/sparsezoo/utils/gdpr.py index 2b83f4df..52a1ba5d 100644 --- a/src/sparsezoo/utils/gdpr.py +++ b/src/sparsezoo/utils/gdpr.py @@ -12,12 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +import contextlib from typing import Optional import geocoder import requests from requests import HTTPError +from sparsezoo.utils.helpers import disable_request_logs + __all__ = ["get_external_ip", "get_country_code", "is_gdpr_country"] @@ -58,7 +61,8 @@ def get_external_ip() -> Optional[str]: :return: the external ip of the machine, None if unable to get """ try: - response = requests.get("https://ident.me") + with disable_request_logs(): + response = requests.get("https://ident.me") external_ip = response.text.strip() return external_ip @@ -84,6 +88,8 @@ def is_gdpr_country() -> bool: :return: True if the country code of the machine is in the GDPR list, False otherwise """ - country_code = get_country_code() + with contextlib.redirect_stderr(None): + # suppress geocoder error logging + country_code = get_country_code() return country_code is None or country_code in _GDPR_COUNTRY_CODES diff --git a/src/sparsezoo/utils/helpers.py b/src/sparsezoo/utils/helpers.py index 041ede77..56bc9ca9 100644 --- a/src/sparsezoo/utils/helpers.py +++ b/src/sparsezoo/utils/helpers.py @@ -13,7 +13,9 @@ # limitations under the License. import glob +import logging import os +from contextlib import contextmanager from typing import Any @@ -23,6 +25,7 @@ "clean_path", "remove_tar_duplicates", "convert_to_bool", + "disable_request_logs", ] @@ -87,3 +90,20 @@ def clean_path(path: str) -> str: :return: a cleaned version that expands the user path and creates an absolute path """ return os.path.abspath(os.path.expanduser(path)) + + +@contextmanager +def disable_request_logs(): + """ + Context manager for disabling logs for a requests session + """ + loggers = [logging.getLogger("requests"), logging.getLogger("urllib3")] + + original_disabled_states = [logger.disabled for logger in loggers] + for logger in loggers: + logger.disabled = True + + yield + + for logger, original_disabled_state in zip(loggers, original_disabled_states): + logger.disabled = original_disabled_state