Skip to content

Commit

Permalink
implement timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Oct 5, 2024
1 parent b8f588b commit 2e222c6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies = [
"pandas>=1.5.0",
"pooch>=1.8.1",
"posthog",
"inputimeout",
]

[project.urls]
Expand Down
24 changes: 21 additions & 3 deletions src/pybamm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import platformdirs
from pathlib import Path
import pybamm
from inputimeout import inputimeout, TimeoutOccurred


def is_running_tests(): # pragma: no cover
Expand Down Expand Up @@ -41,10 +42,15 @@ def is_running_tests(): # pragma: no cover
return False


def ask_user_opt_in():
def ask_user_opt_in(timeout=10):
"""
Ask the user if they want to opt in to telemetry.
Parameters
----------
timeout: float, optional
The timeout for the user to respond to the prompt. Default is 20 seconds.
Returns
-------
bool
Expand All @@ -59,9 +65,21 @@ def ask_user_opt_in():
"This is entirely optional and does not impact the functionality of PyBaMM.\n"
"For more information, see https://docs.pybamm.org/en/latest/source/user_guide/index.html#telemetry"
)

while True:
user_input = input("Do you want to enable telemetry? (Y/n): ").strip().lower()
if user_input in ["yes", "y"]:
try:
user_input = (
inputimeout(
prompt="Do you want to enable telemetry? (Y/n): ", timeout=timeout
)
.strip()
.lower()
)
except TimeoutOccurred:
print("\nTimeout reached. Defaulting to not enabling telemetry.")
return False

if user_input in ["yes", "y", ""]:
return True
elif user_input in ["no", "n"]:
return False
Expand Down
18 changes: 16 additions & 2 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from inputimeout import TimeoutOccurred

import pybamm
import uuid
Expand Down Expand Up @@ -33,10 +34,23 @@ def test_write_read_uuid(self, tmp_path, write_opt_in):

@pytest.mark.parametrize("user_opted_in, user_input", [(True, "y"), (False, "n")])
def test_ask_user_opt_in(self, monkeypatch, user_opted_in, user_input):
# Mock the input function to return invalid input first, then valid input
# Mock the inputimeout function to return invalid input first, then valid input
inputs = iter(["invalid", user_input])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
monkeypatch.setattr(
"pybamm.config.inputimeout", lambda prompt, timeout: next(inputs)
)

# Call the function to ask the user if they want to opt in
opt_in = pybamm.config.ask_user_opt_in()
assert opt_in is user_opted_in

def test_ask_user_opt_in_timeout(self, monkeypatch):
# Mock the inputimeout function to raise a TimeoutOccurred exception
def mock_inputimeout(*args, **kwargs):
raise TimeoutOccurred

monkeypatch.setattr("pybamm.config.inputimeout", mock_inputimeout)

# Call the function to ask the user if they want to opt in
opt_in = pybamm.config.ask_user_opt_in()
assert opt_in is False

0 comments on commit 2e222c6

Please sign in to comment.