示例#1
0
def write_input(string):
    """ Write string to input text file | str --> None """
    with open(get_local_path().joinpath('input.txt'), 'w') as input_file:
        if string:
            input_file.write(string)
        else:
            input_file.write('**BLANK_INPUT_RECEIVED**')
示例#2
0
def wait_for_input():
    """ Wait for input file to be written, then returns contents | None --> str """
    clear_input()
    input_received = False
    while not input_received:
        with open(get_local_path().joinpath('input.txt'), 'r') as input_file:
            contents = input_file.read()
            if not contents:
                time.sleep(1)
            else:
                return contents
示例#3
0
def web_input(*args):
    """ Replaces the built-in input function for browser input | str --> str

    Writes input prompt to prompt file to activate browser_input.
    autofront.browser_input gets user input in browser and writes to input file.
    Reads input file and returns contents.
    """
    if args:
        prompt = [*args][0]
    else:
        prompt = 'None'
    clear_input()
    input_received = False
    with open(get_local_path().joinpath('prompt.txt'), 'w') as prompt_file:
        prompt_file.write(prompt)
    while not input_received:
        input_received = wait_for_input()  #Returns contents when received
    if input_received == '**BLANK_INPUT_RECEIVED**':
        return ''
    return input_received
示例#4
0
def wait_for_prompt(timeout=0):
    """ Wait for prompt file to be written | None --> None

        If script does not end after seconds specified in 'timeout' kwarg,
        will write 'timeout reached' to prompt file.
    """
    clear_prompt()
    prompt_received = False
    time_waited = 0
    while not prompt_received or time_waited > timeout:
        with open(get_local_path().joinpath('prompt.txt'), 'r') as prompt_file:
            contents = prompt_file.read()
            if time_waited > timeout:
                write_prompt('timeout reached')
                break
            if not contents:
                print('Waiting_for_prompt')
                time.sleep(1)
                if timeout:  #if timeout 0, time_waited will never increase
                    time_waited += 1
            else:
                print('Prompt received')
                prompt_received = True
示例#5
0
def clear_input():
    """ Clear input text file | None --> None """
    with open(get_local_path().joinpath('input.txt'), 'w'):
        pass
示例#6
0
def get_input():
    """ Get contents of input text file | None --> str """
    with open(get_local_path().joinpath('input.txt'), 'r') as input_file:
        return input_file.read()
示例#7
0
def initialize_input():
    """ Initialize input file on script start | None --> None """
    with open(get_local_path().joinpath('input.txt'), 'w') as input_file:
        input_file.write('waiting for input')
示例#8
0
def clear_prompt():
    """ Clear prompt text file | None --> None """
    with open(get_local_path().joinpath('prompt.txt'), 'w'):
        pass
示例#9
0
def write_prompt(string):
    """ Write string to prompt text file | str --> None """
    with open(get_local_path().joinpath('prompt.txt'), 'w') as prompt_file:
        prompt_file.write(string)
示例#10
0
def get_prompt():
    """ Get contents of prompt text file | None --> str """
    with open(get_local_path().joinpath('prompt.txt'), 'r') as prompt_file:
        return prompt_file.read()
示例#11
0
def initialize_prompt():
    """ Initialize prompt file on script start | None --> None """
    with open(get_local_path().joinpath('prompt.txt'), 'w') as prompt_file:
        prompt_file.write('waiting for prompt')