From 0ecd734fd19becc6fa227a8f28b26fb857d20a32 Mon Sep 17 00:00:00 2001 From: allegroai <> Date: Mon, 13 Jan 2020 17:16:25 +0200 Subject: [PATCH] Support multi-line paste of credentials in configuration wizard --- trains/config/default/__main__.py | 35 +++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/trains/config/default/__main__.py b/trains/config/default/__main__.py index 8e245b84..c09ee1dc 100644 --- a/trains/config/default/__main__.py +++ b/trains/config/default/__main__.py @@ -1,5 +1,7 @@ from __future__ import print_function +import os + from six.moves import input from pathlib2 import Path from six.moves.urllib.parse import urlparse @@ -32,6 +34,36 @@ Enter the url of the trains-server's Web service, for example: {HOST} ) +def get_user_input(): + """ + Hack: Supporting two forms of input: + - one line credentials, e.g. + 'credentials {"access_key" = "", "secret_key" = ""}' + - line break with all the info, e.g. + api { + web_server: https://demoapp.trains.allegro.ai + api_server: https://demoapi.trains.allegro.ai + credentials { + "access_key" = "" + "secret_key" = "" + } + } + :return: str The paste data + """ + brackets_counter = 0 # check that the parse field is valid + input_list = [] + while True: + user_input = input() + if not user_input: + return + brackets_counter += user_input.count("{") - user_input.count("}") + input_list.append(user_input) + if user_input.strip() == '}' and brackets_counter == 0: # For break line input + return os.linesep.join(input_list) + elif user_input.strip().startswith("credentials") and len(input_list) == 1: # For one line input + return os.linesep.join(input_list) + + def main(): print('TRAINS SDK setup process') conf_file = Path(LOCAL_CONFIG_FILES[0]).absolute() @@ -41,8 +73,7 @@ def main(): return print(description, end='') - sentinel = '' - parse_input = '\n'.join(iter(input, sentinel)) + parse_input = get_user_input() credentials = None api_host = None web_server = None