Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress analytics errors and messages #318

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions src/sparsezoo/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 _DEBUG):
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()
Expand Down
28 changes: 16 additions & 12 deletions src/sparsezoo/utils/gdpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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:
Expand Down
45 changes: 45 additions & 0 deletions src/sparsezoo/utils/suppress.py
Original file line number Diff line number Diff line change
@@ -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