Offer to store new settings in config file
This commit is contained in:
parent
a95d0aaa91
commit
4fc9ebc38a
@ -8,6 +8,8 @@ import ssl
|
||||
import string
|
||||
import sys
|
||||
|
||||
import utilities
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, host='localhost', port=3001,
|
||||
@ -425,14 +427,17 @@ def main():
|
||||
token = None
|
||||
|
||||
# If import fails, prompt user for host or port
|
||||
new_settings = {} # After getting these settings, offer to store them
|
||||
while host is None:
|
||||
host = input("Enter host:\t\t\t\t\t\t")
|
||||
new_settings['host'] = host
|
||||
while port is None:
|
||||
try:
|
||||
port = int(input("Enter port:\t\t\t\t\t\t"))
|
||||
except ValueError:
|
||||
logging.info("Invalid port. Enter a valid port number!")
|
||||
port = None
|
||||
new_settings['port'] = port
|
||||
while action is None:
|
||||
action = get_action(
|
||||
input("Do you want to (R)eceive or (S)end a file?\t\t")
|
||||
@ -459,6 +464,7 @@ def main():
|
||||
)
|
||||
if file_path and not os.path.isdir(os.path.abspath(file_path)):
|
||||
file_path = None
|
||||
new_settings['file_path'] = file_path
|
||||
if password is None:
|
||||
logging.warning(
|
||||
"You have provided no password for file encryption.\n"
|
||||
@ -482,6 +488,28 @@ def main():
|
||||
)
|
||||
while token is None or not (6 <= len(token) <= 10):
|
||||
token = input("Please enter a 6-10 chars token.\t\t\t\t")
|
||||
if new_settings:
|
||||
answer = utilities.timed_input(
|
||||
"Do you want to store the following configuration values in "
|
||||
"`config.py`?\n\n" + '\n'.join(
|
||||
'\t\t'.join(map(str, item))
|
||||
for item in new_settings.items()
|
||||
),
|
||||
timeout=3
|
||||
)
|
||||
if answer:
|
||||
with open('config.py', 'a') as configuration_file:
|
||||
configuration_file.writelines(
|
||||
[
|
||||
f'{name} = "{value}"'
|
||||
if type(value) is str
|
||||
else f'{name} = {value}'
|
||||
for name, value in new_settings.items()
|
||||
]
|
||||
)
|
||||
logging.info("Configuration values stored.")
|
||||
else:
|
||||
logging.info("Proceeding without storing values...")
|
||||
loop = asyncio.get_event_loop()
|
||||
client = Client(
|
||||
host=host,
|
||||
|
36
src/utilities.py
Normal file
36
src/utilities.py
Normal file
@ -0,0 +1,36 @@
|
||||
"""Useful functions."""
|
||||
import logging
|
||||
import signal
|
||||
|
||||
|
||||
def timed_input(message: str = None,
|
||||
timeout: int = 5):
|
||||
|
||||
class TimeoutExpired(Exception):
|
||||
pass
|
||||
|
||||
def interrupted(signal_number, stack_frame):
|
||||
"""Called when read times out."""
|
||||
raise TimeoutExpired
|
||||
|
||||
if message is None:
|
||||
message = f"Enter something within {timeout} seconds"
|
||||
|
||||
signal.alarm(timeout)
|
||||
signal.signal(signal.SIGALRM, interrupted)
|
||||
try:
|
||||
given_input = input(message)
|
||||
except TimeoutExpired:
|
||||
given_input = None
|
||||
print() # Print end of line
|
||||
logging.info("Timeout!")
|
||||
signal.alarm(0)
|
||||
if given_input:
|
||||
logging.info(f"You typed: {given_input}")
|
||||
return
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(logging.INFO)
|
||||
timed_input("Inserisci qualcosa (entro 3 secondi)\t\t", timeout=3)
|
Loading…
x
Reference in New Issue
Block a user