From 93080f2107a9a8ed1f37b29bbd3f1c87680cb006 Mon Sep 17 00:00:00 2001 From: Dimitris Date: Tue, 19 Mar 2024 20:05:53 +0000 Subject: [PATCH] creating the credentials.toml file on the fly, hoping to skip the streamlit welcome msg! --- pciSeq/src/diagnostics/launch_diagnostics.py | 30 +++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/pciSeq/src/diagnostics/launch_diagnostics.py b/pciSeq/src/diagnostics/launch_diagnostics.py index 1fe33d6d..54187305 100644 --- a/pciSeq/src/diagnostics/launch_diagnostics.py +++ b/pciSeq/src/diagnostics/launch_diagnostics.py @@ -1,4 +1,6 @@ import os +from pathlib import Path +import tomlkit import subprocess import pciSeq.src.diagnostics.utils as utils import logging @@ -11,9 +13,35 @@ def launch_dashboard(): filename = os.path.join(dirname, 'diagnostics.py') code_1, code_2 = utils.validate_redis() if code_1 == 0 and code_2 == 0: + make_credentials() p = subprocess.Popen(["streamlit", "run", filename, " --server.headless true"]) # TODO: you need to kill the process on exit - # logger.info('Starting process with pid: %d to run the diagnostics' % p.pid) + # launch_diagnostics_logger.info('Starting process with pid: %d to run the diagnostics' % p.pid) else: launch_diagnostics_logger.info("Skipping diagnostics, cannot run them. Either redis not installed or not running.") + +def make_credentials(): + """ + creates a credentials.toml file in the .streamlit folder under the user's home dir and + in the [general] section it creates an email key with value an empty string. + If an email key/value pair exists already, the file remains intact + The only purpose of this is to skip the annoying streamlit welcome message so that + the diagnostics will be launched without any user interaction. (otherwise the welcome msf + might pause the program flow) + """ + credentials = os.path.join(Path.home(), '.streamlit', 'credentials.toml') + mode = 'rt' if os.path.exists(credentials) else 'w+' + with open(credentials, mode) as fp: + doc = tomlkit.load(fp) + append_email(doc, credentials) + + +def append_email(doc, fname): + if doc.get('general') is None: + general = tomlkit.table() + doc.add("general", general) + if doc.get('general').get('email') is None: + doc.get('general').add("email", "") + with open(fname, 'w') as outfile: + tomlkit.dump(doc, outfile)