From bca9c19053352fe53c12e3328370c9af15782961 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Sat, 29 Jun 2019 19:54:02 -0600 Subject: [PATCH] New safer text input function * Avoids EOFError exceptions and just repeats the prompt --- scripts/wk/__init__.py | 11 ++++++++++ scripts/wk/io.py | 6 ++++++ scripts/wk/std.py | 49 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 scripts/wk/__init__.py diff --git a/scripts/wk/__init__.py b/scripts/wk/__init__.py new file mode 100644 index 00000000..df1d92ac --- /dev/null +++ b/scripts/wk/__init__.py @@ -0,0 +1,11 @@ +'''WizardKit: wk module init''' + +import wk.cfg +import wk.exe +import wk.hw +import wk.io +import wk.kit +import wk.net +import wk.os +import wk.std +import wk.sw diff --git a/scripts/wk/io.py b/scripts/wk/io.py index e69de29b..469408c3 100644 --- a/scripts/wk/io.py +++ b/scripts/wk/io.py @@ -0,0 +1,6 @@ +'''WizardKit: I/O Functions''' +# vim: sts=2 sw=2 ts=2 + + +if __name__ == '__main__': + print("This file is not meant to be called directly.") diff --git a/scripts/wk/std.py b/scripts/wk/std.py index e69de29b..e71eaa27 100644 --- a/scripts/wk/std.py +++ b/scripts/wk/std.py @@ -0,0 +1,49 @@ +'''WizardKit: Standard Functions''' +# vim: sts=2 sw=2 ts=2 + +import os +import sys + +try: + from termios import tcflush, TCIOFLUSH +except ImportError: + if os.name == 'posix': + raise + + +# STATIC VARIABLES +COLORS = { + 'CLEAR': '\033[0m', + 'RED': '\033[31m', + 'ORANGE': '\033[31;1m', + 'GREEN': '\033[32m', + 'YELLOW': '\033[33m', + 'BLUE': '\033[34m', + 'PURPLE': '\033[35m', + 'CYAN': '\033[36m', + } + + +# Functions +def input_text(prompt='Enter text'): + """Get text from user, returns string.""" + prompt = str(prompt) + response = None + if prompt[-1:] != ' ': + prompt += ' ' + + while response is None: + if os.name == 'posix': + # Flush input to (hopefully) avoid EOFError + tcflush(sys.stdin, TCIOFLUSH) + try: + response = input(prompt) + except EOFError: + # Ignore and try again + print('') + + return response + + +if __name__ == '__main__': + print("This file is not meant to be called directly.")