33import platformdirs
44from pathlib import Path
55import pybamm
6- import select
76import sys
7+ import threading
8+ import time
89
910
1011def is_running_tests (): # pragma: no cover
@@ -14,8 +15,6 @@ def is_running_tests(): # pragma: no cover
1415 Returns:
1516 bool: True if running tests or building docs, False otherwise.
1617 """
17- import sys
18-
1918 # Check if pytest or unittest is running
2019 if any (
2120 test_module in sys .modules for test_module in ["pytest" , "unittest" , "nose" ]
@@ -33,11 +32,14 @@ def is_running_tests(): # pragma: no cover
3332
3433 # Check for common test runner names in command-line arguments
3534 test_runners = ["pytest" , "unittest" , "nose" , "trial" , "nox" , "tox" ]
36- if any (runner in sys . argv [ 0 ]. lower () for runner in test_runners ):
35+ if any (runner in arg . lower () for arg in sys . argv for runner in test_runners ):
3736 return True
3837
3938 # Check if building docs with Sphinx
40- if "sphinx" in sys .modules :
39+ if any (mod == "sphinx" or mod .startswith ("sphinx." ) for mod in sys .modules ):
40+ print (
41+ f"Found Sphinx module: { [mod for mod in sys .modules if mod .startswith ('sphinx' )]} "
42+ )
4143 return True
4244
4345 return False
@@ -67,24 +69,47 @@ def ask_user_opt_in(timeout=10):
6769 "For more information, see https://docs.pybamm.org/en/latest/source/user_guide/index.html#telemetry"
6870 )
6971
72+ def get_input ():
73+ try :
74+ user_input = (
75+ input ("Do you want to enable telemetry? (Y/n): " ).strip ().lower ()
76+ )
77+ answer .append (user_input )
78+ except Exception :
79+ # Handle any input errors
80+ pass
81+
82+ time_start = time .time ()
83+
7084 while True :
71- print ("Do you want to enable telemetry? (Y/n): " , end = "" , flush = True )
85+ if time .time () - time_start > timeout :
86+ print ("\n Timeout reached. Defaulting to not enabling telemetry." )
87+ return False
88+
89+ answer = []
90+ # Create and start input thread
91+ input_thread = threading .Thread (target = get_input )
92+ input_thread .daemon = True
93+ input_thread .start ()
94+
95+ # Wait for either timeout or input
96+ input_thread .join (timeout )
7297
73- rlist , _ , _ = select .select ([sys .stdin ], [], [], timeout )
74- if rlist :
75- user_input = sys .stdin .readline ().strip ().lower ()
76- if user_input in ["yes" , "y" , "" ]:
98+ if answer :
99+ if answer [0 ] in ["yes" , "y" , "" ]:
100+ print ("\n Telemetry enabled.\n " )
77101 return True
78- elif user_input in ["no" , "n" ]:
102+ elif answer [0 ] in ["no" , "n" ]:
103+ print ("\n Telemetry disabled.\n " )
79104 return False
80105 else :
81- print ("Invalid input. Please enter 'yes/y' for yes or 'no/n' for no." )
106+ print ("\n Invalid input. Please enter 'yes/y' for yes or 'no/n' for no." )
82107 else :
83108 print ("\n Timeout reached. Defaulting to not enabling telemetry." )
84109 return False
85110
86111
87- def generate (): # pragma: no cover
112+ def generate ():
88113 if is_running_tests ():
89114 return
90115
@@ -101,7 +126,7 @@ def generate(): # pragma: no cover
101126 pybamm .telemetry .capture ("user-opted-in" )
102127
103128
104- def read (): # pragma: no cover
129+ def read ():
105130 config_file = Path (platformdirs .user_config_dir ("pybamm" )) / "config.yml"
106131 return read_uuid_from_file (config_file )
107132
@@ -121,7 +146,7 @@ def write_uuid_to_file(config_file, opt_in):
121146
122147def read_uuid_from_file (config_file ):
123148 # Check if the config file exists
124- if not config_file .exists (): # pragma: no cover
149+ if not config_file .exists ():
125150 return None
126151
127152 # Read the UUID from the config file
@@ -134,5 +159,5 @@ def read_uuid_from_file(config_file):
134159
135160 config = yaml .safe_load (content )
136161 return config ["pybamm" ]
137- except (yaml .YAMLError , ValueError ): # pragma: no cover
162+ except (yaml .YAMLError , ValueError ):
138163 return None
0 commit comments