From 174e98d4df9c51cc8a59510e8c6c5ce6aa2fa8bb Mon Sep 17 00:00:00 2001 From: Mark Kurtz Date: Thu, 25 May 2023 16:25:57 -0400 Subject: [PATCH 1/2] Suppress analytics errors and messages --- src/sparsezoo/analytics.py | 50 +++++++++++++++++---------------- src/sparsezoo/utils/gdpr.py | 28 ++++++++++-------- src/sparsezoo/utils/suppress.py | 45 +++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 36 deletions(-) create mode 100644 src/sparsezoo/utils/suppress.py diff --git a/src/sparsezoo/analytics.py b/src/sparsezoo/analytics.py index 00e0e4a3..03365227 100644 --- a/src/sparsezoo/analytics.py +++ b/src/sparsezoo/analytics.py @@ -24,6 +24,7 @@ import requests from sparsezoo.utils.gdpr import is_gdpr_country +from sparsezoo.utils.suppress import suppress_stdout_stderr from sparsezoo.version import version as sparsezoo_version @@ -128,30 +129,31 @@ def send_event( event_params = {} def _send_request(): - event_params.update(self._package_params) - event_params["package"] = self._package - event_params["version"] = self._version - payload = { - "client_id": self._client_id, - "events": [{"name": event_name, "params": event_params}], - } - headers = { - "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 Exception as err: - if _DEBUG: - print(err) - - if raise_errors: - raise err + with suppress_stdout_stderr(suppress=not raise_errors): + event_params.update(self._package_params) + event_params["package"] = self._package + event_params["version"] = self._version + payload = { + "client_id": self._client_id, + "events": [{"name": event_name, "params": event_params}], + } + headers = { + "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 Exception as err: + if _DEBUG: + print(err) + + if raise_errors: + raise err thread = threading.Thread(target=_send_request) thread.start() diff --git a/src/sparsezoo/utils/gdpr.py b/src/sparsezoo/utils/gdpr.py index 578652d4..93f391fd 100644 --- a/src/sparsezoo/utils/gdpr.py +++ b/src/sparsezoo/utils/gdpr.py @@ -17,6 +17,8 @@ import geocoder import requests +from sparsezoo.utils.suppress import suppress_stdout_stderr + __all__ = ["get_external_ip", "get_country_code", "is_gdpr_country"] @@ -56,26 +58,28 @@ 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") - external_ip = response.text.strip() + with suppress_stdout_stderr(): + try: + response = requests.get("https://ident.me") + external_ip = response.text.strip() - return external_ip - except Exception: - return None + return external_ip + except Exception: + return None def get_country_code() -> Optional[str]: """ :return: the country code of the machine, None if unable to get """ - try: - ip = get_external_ip() - geo = geocoder.ip(ip) + with suppress_stdout_stderr(): + try: + ip = get_external_ip() + geo = geocoder.ip(ip) - return geo.country - except Exception: - return None + return geo.country + except Exception: + return None def is_gdpr_country() -> bool: diff --git a/src/sparsezoo/utils/suppress.py b/src/sparsezoo/utils/suppress.py new file mode 100644 index 00000000..55338d92 --- /dev/null +++ b/src/sparsezoo/utils/suppress.py @@ -0,0 +1,45 @@ +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys +from contextlib import contextmanager + + +__all__ = ["suppress_stdout_stderr", "NullDevice"] + + +class NullDevice: + def write(self, s): + pass + + +@contextmanager +def suppress_stdout_stderr(suppress: bool = True): + """ + Suppresses stdout and stderr for the duration of the context. + """ + original_stdout = sys.stdout + original_stderr = sys.stderr + null_device = NullDevice() + + try: + if suppress: + # Redirect stdout and stderr to the null device + sys.stdout = null_device + sys.stderr = null_device + yield + finally: + # Restore the original stdout and stderr + sys.stdout = original_stdout + sys.stderr = original_stderr From 2f608b303555314e8cded3e98906fcb6db425949 Mon Sep 17 00:00:00 2001 From: Mark Kurtz Date: Thu, 25 May 2023 16:34:55 -0400 Subject: [PATCH 2/2] Update src/sparsezoo/analytics.py --- src/sparsezoo/analytics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sparsezoo/analytics.py b/src/sparsezoo/analytics.py index 03365227..18a80080 100644 --- a/src/sparsezoo/analytics.py +++ b/src/sparsezoo/analytics.py @@ -129,7 +129,7 @@ def send_event( event_params = {} def _send_request(): - with suppress_stdout_stderr(suppress=not raise_errors): + with suppress_stdout_stderr(suppress=not _DEBUG): event_params.update(self._package_params) event_params["package"] = self._package event_params["version"] = self._version