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)
def read_parameter( para: Parameter, scanner: Scanner, files: Optional[Tuple[str, str, str]] = None ) -> Any: """Read value for a given template parameter declaration. Prompts the user to enter a value for the given parameter and returns the converted value that was entered by the user. 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. Returns ------- bool or float or int or string or tuple(string, string) """ while True: if not para.is_file(): print(para.prompt(), end='') try: if para.is_bool(): return scanner.next_bool(default_value=para.default) elif para.is_file(): return read_file(para=para, scanner=scanner, files=files) elif para.is_float(): return scanner.next_float(default_value=para.default) elif para.is_int(): return scanner.next_int(default_value=para.default) return scanner.next_string(default_value=para.default) except ValueError as ex: print(ex)
def test_read_string(): """Test reading the next file token.""" scanner = Scanner(reader=ListReader(tokens=['a', ''])) assert scanner.next_string(default_value='b') == 'a' assert scanner.next_string(default_value='b') == 'b' assert scanner.next_string(default_value='b') == 'b'