Avoid EOFErrors

This commit is contained in:
2Shirt 2019-11-18 17:40:15 -07:00
parent f2373edd8e
commit 990618bfeb
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
3 changed files with 35 additions and 8 deletions

View file

@ -18,6 +18,12 @@ from settings.main import *
from settings.tools import *
from settings.windows_builds import *
from subprocess import CalledProcessError
try:
from termios import tcflush, TCIOFLUSH
except ImportError:
if os.name == 'posix':
# Not worried about this under Windows
raise
# Global variables
@ -98,6 +104,27 @@ class WindowsUnsupportedError(Exception):
pass
# Backported functions
def input_text(prompt):
"""Get input and avoid EOFErrors, returns str."""
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('', flush=True)
return response
# General functions
def abort(show_prompt=True):
"""Abort script."""
@ -113,7 +140,7 @@ def ask(prompt='Kotaero!'):
answer = None
prompt = '{} [Y/N]: '.format(prompt)
while answer is None:
tmp = input(prompt)
tmp = input_text(prompt)
if re.search(r'^y(es|)$', tmp, re.IGNORECASE):
answer = True
elif re.search(r'^n(o|ope|)$', tmp, re.IGNORECASE):
@ -145,7 +172,7 @@ def choice(choices, prompt='Kotaero!'):
# Get user's choice
while answer is None:
tmp = input(prompt)
tmp = input_text(prompt)
if re.search(regex, tmp, re.IGNORECASE):
answer = tmp
@ -264,7 +291,7 @@ def get_simple_string(prompt='Enter string'):
"""Get string from user (restricted character set), returns str."""
simple_string = None
while simple_string is None:
_input = input('{}: '.format(prompt))
_input = input_text('{}: '.format(prompt))
if re.match(r"^(\w|-| |\.|')+$", _input, re.ASCII):
simple_string = _input.strip()
return simple_string
@ -276,7 +303,7 @@ def get_ticket_number():
return None
ticket_number = None
while ticket_number is None:
_input = input('Enter ticket number: ')
_input = input_text('Enter ticket number: ')
if re.match(r'^([0-9]+([-_]?\w+|))$', _input):
ticket_number = _input
out_file = r'{}\TicketNumber'.format(global_vars['LogDir'])
@ -421,7 +448,7 @@ def menu_select(
while (answer.upper() not in valid_answers):
clear_screen()
print(menu_splash)
answer = input('{}: '.format(prompt))
answer = input_text('{}: '.format(prompt))
return answer.upper()
@ -441,7 +468,7 @@ def pause(prompt='Press Enter to continue... '):
"""Simple pause implementation."""
if prompt[-1] != ' ':
prompt += ' '
input(prompt)
input_text(prompt)
def ping(addr='google.com'):

View file

@ -1414,7 +1414,7 @@ def select_path(skip_device=None):
elif path_options[index]['Name'] == 'Enter manually':
# Manual entry
while not selected_path:
manual_path = input('Please enter path: ').strip()
manual_path = input_text('Please enter path: ').strip()
if manual_path and pathlib.Path(manual_path).is_dir():
selected_path = DirObj(manual_path)
elif manual_path and pathlib.Path(manual_path).is_file():

View file

@ -434,7 +434,7 @@ class osTicket():
# Main loop
while ticket_number is None:
print_standard(' ')
_ticket_id = input('Enter ticket number (or leave blank to disable): ')
_ticket_id = input_text('Enter ticket number (or leave blank to disable): ')
_ticket_id = _ticket_id.strip()
# No ticket ID entered