Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 0a9ed9b

Browse files
rahul-tulimarkurtz
andauthored
[Cherry Pick] Suppress analytics errors and messages (#319)
* Suppress analytics errors and messages * Update src/sparsezoo/analytics.py --------- Co-authored-by: Mark Kurtz <[email protected]>
1 parent de164a0 commit 0a9ed9b

File tree

3 files changed

+87
-36
lines changed

3 files changed

+87
-36
lines changed

src/sparsezoo/analytics.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import requests
2525

2626
from sparsezoo.utils.gdpr import is_gdpr_country
27+
from sparsezoo.utils.suppress import suppress_stdout_stderr
2728
from sparsezoo.version import version as sparsezoo_version
2829

2930

@@ -121,30 +122,31 @@ def send_event(
121122
event_params = {}
122123

123124
def _send_request():
124-
event_params.update(self._package_params)
125-
event_params["package"] = self._package
126-
event_params["version"] = self._version
127-
payload = {
128-
"client_id": self._client_id,
129-
"events": [{"name": event_name, "params": event_params}],
130-
}
131-
headers = {
132-
"Content-Type": "application/json",
133-
}
134-
data = json.dumps(payload)
135-
136-
try:
137-
response = requests.post(self._url, headers=headers, data=data)
138-
response.raise_for_status()
139-
body = response.content
140-
if _DEBUG:
141-
print(body)
142-
except Exception as err:
143-
if _DEBUG:
144-
print(err)
145-
146-
if raise_errors:
147-
raise err
125+
with suppress_stdout_stderr(suppress=not _DEBUG):
126+
event_params.update(self._package_params)
127+
event_params["package"] = self._package
128+
event_params["version"] = self._version
129+
payload = {
130+
"client_id": self._client_id,
131+
"events": [{"name": event_name, "params": event_params}],
132+
}
133+
headers = {
134+
"Content-Type": "application/json",
135+
}
136+
data = json.dumps(payload)
137+
138+
try:
139+
response = requests.post(self._url, headers=headers, data=data)
140+
response.raise_for_status()
141+
body = response.content
142+
if _DEBUG:
143+
print(body)
144+
except Exception as err:
145+
if _DEBUG:
146+
print(err)
147+
148+
if raise_errors:
149+
raise err
148150

149151
thread = threading.Thread(target=_send_request)
150152
thread.start()

src/sparsezoo/utils/gdpr.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import geocoder
1818
import requests
1919

20+
from sparsezoo.utils.suppress import suppress_stdout_stderr
21+
2022

2123
__all__ = ["get_external_ip", "get_country_code", "is_gdpr_country"]
2224

@@ -56,26 +58,28 @@ def get_external_ip() -> Optional[str]:
5658
"""
5759
:return: the external ip of the machine, None if unable to get
5860
"""
59-
try:
60-
response = requests.get("https://ident.me")
61-
external_ip = response.text.strip()
61+
with suppress_stdout_stderr():
62+
try:
63+
response = requests.get("https://ident.me")
64+
external_ip = response.text.strip()
6265

63-
return external_ip
64-
except Exception:
65-
return None
66+
return external_ip
67+
except Exception:
68+
return None
6669

6770

6871
def get_country_code() -> Optional[str]:
6972
"""
7073
:return: the country code of the machine, None if unable to get
7174
"""
72-
try:
73-
ip = get_external_ip()
74-
geo = geocoder.ip(ip)
75+
with suppress_stdout_stderr():
76+
try:
77+
ip = get_external_ip()
78+
geo = geocoder.ip(ip)
7579

76-
return geo.country
77-
except Exception:
78-
return None
80+
return geo.country
81+
except Exception:
82+
return None
7983

8084

8185
def is_gdpr_country() -> bool:

src/sparsezoo/utils/suppress.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import sys
16+
from contextlib import contextmanager
17+
18+
19+
__all__ = ["suppress_stdout_stderr", "NullDevice"]
20+
21+
22+
class NullDevice:
23+
def write(self, s):
24+
pass
25+
26+
27+
@contextmanager
28+
def suppress_stdout_stderr(suppress: bool = True):
29+
"""
30+
Suppresses stdout and stderr for the duration of the context.
31+
"""
32+
original_stdout = sys.stdout
33+
original_stderr = sys.stderr
34+
null_device = NullDevice()
35+
36+
try:
37+
if suppress:
38+
# Redirect stdout and stderr to the null device
39+
sys.stdout = null_device
40+
sys.stderr = null_device
41+
yield
42+
finally:
43+
# Restore the original stdout and stderr
44+
sys.stdout = original_stdout
45+
sys.stderr = original_stderr

0 commit comments

Comments
 (0)