Пример #1
0
def read_file(
    para: Parameter, scanner: Scanner, files: Optional[Tuple[str, str, str]] = None
):
    """Read value for a file parameter.

    Parameters
    ----------
    para: flowserv.model.parameter.base.Parameter
        Workflow template parameter declaration
    scanner: flowserv.scanner.Scanner
        Input scanner.
    files: list, default=None
        List of tuples representing uploaded files. Each tuple has three
        elements: file_id, name, timestamp.
    """
    # Distinguish between the case where a list of uploaded files
    # is given or not.
    if files is not None:
        print('\nSelect file identifier from uploaded files:\n')
        table = ResultTable(
            headline=['ID', 'Name', 'Created at'],
            types=[PARA_STRING] * 3
        )
        for file_id, name, created_at in files:
            table.add([file_id, name, created_at])
        for line in table.format():
            print(line)
        print('\n{}'.format(para.prompt()), end='')
        filename = scanner.next_string()
    else:
        filename = scanner.next_file()
    target_path = None
    if para.target is None:
        print('Target Path:', end='')
        target_path = scanner.next_string()
        if target_path == '':
            target_path = para.default
    else:
        target_path = para.target
    # The type of the returned value depends on whether the list of
    # uploaded files is given or not.
    if files is not None:
        return serialize_fh(file_id=filename, target=target_path)
    else:
        return InputFile(FSFile(filename), target_path)
Пример #2
0
def test_read_file():
    """Test reading the next file token."""
    scanner = Scanner(reader=ListReader(tokens=['out.txt', '']))
    assert scanner.next_file(default_value='a.out') == 'out.txt'
    assert scanner.next_file(default_value='a.out') == 'a.out'