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

View file

@ -1414,7 +1414,7 @@ def select_path(skip_device=None):
elif path_options[index]['Name'] == 'Enter manually': elif path_options[index]['Name'] == 'Enter manually':
# Manual entry # Manual entry
while not selected_path: 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(): if manual_path and pathlib.Path(manual_path).is_dir():
selected_path = DirObj(manual_path) selected_path = DirObj(manual_path)
elif manual_path and pathlib.Path(manual_path).is_file(): elif manual_path and pathlib.Path(manual_path).is_file():

View file

@ -434,7 +434,7 @@ class osTicket():
# Main loop # Main loop
while ticket_number is None: while ticket_number is None:
print_standard(' ') 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() _ticket_id = _ticket_id.strip()
# No ticket ID entered # No ticket ID entered