diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..ce6df85 --- /dev/null +++ b/utils.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +""" +project: Backuppy +version: 0.9 +file: utils.py +summary: utilities script +""" + +# Standard library imports +import time + +# local imports +from languages import english +from languages import german + +# local globals +LANG_EN = "EN" +LANG_DE = "DE" +LANGUAGE = LANG_EN +class bcolors: + """ Define colors for the std. output to console. """ + + OK = '\033[92m' #GREEN + WARNING = '\033[93m' #YELLOW + ERROR = '\033[91m' #RED + RESET = '\033[0m' #RESET COLOR + + +def trace(message_txt, prefix_crlf=False, err=False): + """ Print a formatted message to std out. + + :param "message_txt" [in] The message text that should be displayed. + :param "prefix_crlf" [in] If True, a carriage return/line feed will be done prior to the message text. + + """ + + if prefix_crlf: + print("\n") + if err: + print("[ " + bcolors.ERROR + "ERR" + bcolors.RESET + " ] " + message_txt) + else: + print("[ " + bcolors.OK + "OK" + bcolors.RESET + " ] " + message_txt) + +def get_lang_text(search_str: str): + """ Returns a string from the appropriate language file. """ + return_str: str = eval("english." + search_str) + global LANGUAGE + if LANGUAGE == LANG_DE: + return_str = eval("german." + search_str) + return return_str + +def _print(message_txt: str): + print("\n" + get_lang_text(message_txt) + "\n") + time.sleep(1) + +def set_language(language): + """ Set global constant "LANGUAGE" to given language ("EN"/"DE"). """ + global LANGUAGE, LANG_DE, LANG_EN + + if str(language) and language.upper() == LANG_DE: + LANGUAGE = LANG_DE + else: + LANGUAGE = LANG_EN + +def query(question_txt: str): + try: + answer_txt = input(get_lang_text(question_txt) + "\n> ") + + if not answer_txt: + answer_txt = "''" + + if answer_txt.upper() == ":Q" or answer_txt == chr(27): + trace("Program exited by user-input.") + return None + + except KeyboardInterrupt: + trace("Program interrupted by user.", prefix_crlf=True, err=True) + return None + + return answer_txt