From 4fc9ebc38a79872dfa2054a9ac81f63997ffc5b9 Mon Sep 17 00:00:00 2001 From: Davte Date: Sun, 12 Apr 2020 11:35:44 +0200 Subject: [PATCH] Offer to store new settings in config file --- src/client.py | 28 ++++++++++++++++++++++++++++ src/utilities.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/utilities.py diff --git a/src/client.py b/src/client.py index be0d87c..87adce3 100644 --- a/src/client.py +++ b/src/client.py @@ -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, diff --git a/src/utilities.py b/src/utilities.py new file mode 100644 index 0000000..cefdcf4 --- /dev/null +++ b/src/utilities.py @@ -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)