Backuppy/utils.py
Paul S eb8e536617 check if user paths from user-input exists and differs
- CLI-mode: check if user paths from user-input exists
- CLI-mode: prevent identical source and target path
2021-05-11 19:08:56 +02:00

95 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""
project: Backuppy
version: 0.9
file: utils.py
summary: utilities script
"""
# Standard library imports
import time
import os
# 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, wait: int=1):
print("\n" + get_lang_text(message_txt) + "\n")
time.sleep(wait)
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 paths_are_identical(path1: str, path2: str):
if os.path.isdir(path1):
if path1 == path2:
return True
return False
def query_path(question_txt: str):
path = query(question_txt)
while not os.path.isdir(path):
_print("path_not_existing")
#path = input("Please retype an existing directory.\n> ")
path = query(question_txt)
return path
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