def test_read_bool(): """Test reading Boolean values.""" # Test reading True values. scanner = Scanner(reader=ListReader(tokens=['true', 'yes', 'y', 't', 1])) for i in range(5): assert scanner.next_bool() # Test reading False values. scanner = Scanner(reader=ListReader(tokens=['false', 'no', 'n', 'f', 0])) for i in range(5): assert not scanner.next_bool() # Test default value. scanner = Scanner(reader=ListReader(tokens=['', ''])) assert scanner.next_bool(default_value=True) assert not scanner.next_bool(default_value=False)
def read( parameters: List[Parameter], scanner: Optional[Scanner] = None, files: Optional[Tuple[str, str, str]] = None ) -> Dict: """Read values for each of the template parameters using a given input scanner. If no scanner is given, values are read from standard input. The optional list of file handles is used for convenience when the user is asked to input the identifier of an uploaded file. It allows to display the identifier of available files for easy copy and paste. Returns a dictionary of argument values that can be passed to the workflow execution engine to run a parameterized workflow. Parameters ---------- parameters: list(flowserv.model.parameter.base.Parameter) List of workflow template parameter declarations scanner: flowserv.scanner.Scanner Input scanner to read parameter values files: list, default=None List of tuples representing uploaded files. Each tuple has three elements: file_id, name, timestamp. Returns ------- dict """ sc = scanner if scanner is not None else Scanner() arguments = dict() for para in parameters: arguments[para.name] = read_parameter(para, sc, files=files) return arguments
def test_read_file_parameter_with_uploads(tmpdir): """Test reading a file parameter with a given list of upload files.""" parameters = [File(name='A', index=0, target='target1')] sc = Scanner(reader=ListReader(['f1'])) arguments = cli.read(parameters, sc, files=[('f1', 'F', '123')]) assert len(arguments) == 1 assert arguments['A'] == serialize_fh('f1', target='target1')
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 test_read_file_parameters(tmpdir): """Test reading lists of file parameters.""" parameters = [ File(name='A', index=0, target='target1'), File(name='B', index=1, default='target2'), File(name='C', index=2), ] sc = Scanner(reader=ListReader([tmpdir, tmpdir, '', tmpdir, 'target3'])) arguments = cli.read(parameters, sc) assert len(arguments) == 3
def test_read_string_parameters(): """Test reading lists of string parameters.""" parameters = [ String(name='A', index=0), String(name='B', index=1, default='ABC') ] sc = Scanner(reader=ListReader(['true', ''])) arguments = cli.read(parameters, sc) assert len(arguments) == 2 assert arguments['A'] == 'true' assert arguments['B'] == 'ABC'
def test_read_boolean_parameters(): """Test reading lists of boolean parameters.""" parameters = [ Bool(name='A', index=0), Bool(name='B', index=1, default=False), Bool(name='C', index=2, default=False), ] sc = Scanner(reader=ListReader(['true', 'xyz', '', 'True'])) arguments = cli.read(parameters, sc) assert len(arguments) == 3 assert arguments['A'] assert not arguments['B'] assert arguments['C']
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_numeric_parameters(): """Test reading lists of numeric parameters.""" parameters = [ Int(name='A', index=0), Int(name='B', index=1, default=10), Float(name='C', index=2), Float(name='D', index=3, default=1.23) ] sc = Scanner(reader=ListReader(['1', 'xyz', '', 'True', '3.4', ''])) arguments = cli.read(parameters, sc) assert len(arguments) == 4 assert arguments['A'] == 1 assert arguments['B'] == 10 assert arguments['C'] == 3.4 assert arguments['D'] == 1.23
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'
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'