def test_ls_l_formatting(self): file_describe = { "state": "closed", "folder": "/", "name": "foo", "class": "file", "modified": 1482430288965, "project": "project-111122223333444455556666", "id": "file-222233334444555566667777" } # Make sure that for a variety of sizes, the formatting of the size # column is consistent (and doesn't cause subsequent columns to be # misaligned) # Strip ANSI escape sequences so this test "sees" the column positions # as they would be displayed on the screen header = strip_ansi_escape_sequences(describe.get_ls_l_header()) name_col = header.index("Name (ID)") self.assertTrue(name_col > 0) for i in range(0, 25): line = strip_ansi_escape_sequences( describe.get_ls_l_desc(dict(file_describe, size=4**i))) self.assertEqual(name_col, line.index("foo (file-"))
def test_ls_l_formatting(self): file_describe = { "state": "closed", "folder": "/", "name": "foo", "class": "file", "modified": 1482430288965, "project": "project-111122223333444455556666", "id": "file-222233334444555566667777" } # Make sure that for a variety of sizes, the formatting of the size # column is consistent (and doesn't cause subsequent columns to be # misaligned) # Strip ANSI escape sequences so this test "sees" the column positions # as they would be displayed on the screen header = strip_ansi_escape_sequences(describe.get_ls_l_header()) name_col = header.index("Name (ID)") self.assertTrue(name_col > 0) for i in range(0, 25): line = strip_ansi_escape_sequences(describe.get_ls_l_desc(dict(file_describe, size=4 ** i))) self.assertEqual(name_col, line.index("foo (file-"))
def interactive_help(in_class, param_desc, prompt): is_array = param_desc['class'].startswith("array:") print_param_help(param_desc) print array_help_str = ', or <ENTER> to finish the list of inputs' if in_class in dx_data_classes: # Class is some sort of data object if dxpy.WORKSPACE_ID is not None: proj_name = None try: proj_name = dxpy.DXHTTPRequest( '/' + dxpy.WORKSPACE_ID + '/describe', {})['name'] except: pass if proj_name is not None: print 'Your current working directory is ' + proj_name + ':' + os.environ.get( 'DX_CLI_WD', '/') while True: print 'Pick an option to find input data:' try: opt_num = pick([ 'List and choose from available data in the current project', 'List and choose from available data in the DNAnexus Reference Genomes project', 'Select another project to list and choose available data', 'Select an output from a previously-run job (current project only)', 'Return to original prompt (specify an ID or path directly)' ]) except KeyboardInterrupt: opt_num = 4 if opt_num == 0: query_project = dxpy.WORKSPACE_ID elif opt_num == 1: query_project = dxpy.find_one_project(name="Reference Genomes", public=True, level="VIEW")['id'] elif opt_num == 2: project_generator = dxpy.find_projects(level='VIEW', describe=True, explicit_perms=True) print '\nProjects to choose from:' query_project = paginate_and_pick( project_generator, (lambda result: result['describe']['name']))['id'] if opt_num in range(3): result_generator = dxpy.find_data_objects( classname=in_class, typename=param_desc.get('type'), describe=True, project=query_project) print '\nAvailable data:' result_choice = paginate_and_pick( result_generator, (lambda result: get_ls_l_desc(result['describe']))) if result_choice == 'none found': print 'No compatible data found' continue elif result_choice == 'none picked': continue else: return [ result_choice['project'] + ':' + result_choice['id'] ] elif opt_num == 3: # Select from previous jobs in current project result_generator = dxpy.find_jobs(project=dxpy.WORKSPACE_ID, describe=True, parent_job="none") print print 'Previously-run jobs to choose from:' result_choice = paginate_and_pick( result_generator, (lambda result: get_find_jobs_string(result['describe'], has_children=False, single_result=True)), filter_fn=( lambda result: result['describe']['state'] not in [ 'unresponsive', 'terminating', 'terminated', 'failed' ])) if result_choice == 'none found': print 'No jobs found' continue elif result_choice == 'none picked': continue else: if 'output' in result_choice['describe'] and result_choice[ 'describe']['output'] != None: keys = result_choice['describe']['output'].keys() else: exec_handler = dxpy.get_handler( result_choice.get('app', result_choice['applet'])) exec_desc = exec_handler.describe() if 'outputSpec' not in exec_desc: # This if block will either continue, return, or raise print 'No output spec found for the executable' try: field = raw_input( 'Output field to use (^C or <ENTER> to cancel): ' ) if field == '': continue else: return [result_choice['id'] + ':' + field] except KeyboardInterrupt: continue else: keys = exec_desc['outputSpec'].keys() if len(keys) > 1: print '\nOutput fields to choose from:' field_choice = pick(keys) return [result_choice['id'] + ':' + keys[field_choice]] elif len(keys) == 1: print 'Using the only output field: ' + keys[0] return [result_choice['id'] + ':' + keys[0]] else: print 'No available output fields' else: print fill('Enter an ID or path (<TAB> twice for compatible ' + in_class + 's in current directory)' + (array_help_str if is_array else '')) return shlex.split(raw_input(prompt)) else: if in_class == 'boolean': if is_array: print fill('Enter "true", "false"' + array_help_str) else: print fill('Enter "true" or "false"') elif in_class == 'string' and is_array: print fill('Enter a nonempty string' + array_help_str) elif (in_class == 'float' or in_class == 'int') and is_array: print fill('Enter a number' + array_help_str) elif in_class == 'hash': print fill('Enter a quoted JSON hash') result = raw_input(prompt) if in_class == 'string': return [result] else: return shlex.split(result)
def interactive_help(in_class, param_desc, prompt): is_array = param_desc['class'].startswith("array:") print_param_help(param_desc) print array_help_str = ', or <ENTER> to finish the list of inputs' if in_class in dx_data_classes: # Class is some sort of data object if dxpy.WORKSPACE_ID is not None: proj_name = None try: proj_name = dxpy.DXHTTPRequest('/' + dxpy.WORKSPACE_ID + '/describe', {})['name'] except: pass if proj_name is not None: print 'Your current working directory is ' + proj_name + ':' + os.environ.get('DX_CLI_WD', '/') while True: print 'Pick an option to find input data:' try: opt_num = pick(['List and choose from available data in the current project', 'List and choose from available data in the DNAnexus Reference Genomes project', 'Select another project to list and choose available data', 'Select an output from a previously-run job (current project only)', 'Return to original prompt (specify an ID or path directly)']) except KeyboardInterrupt: opt_num = 4 if opt_num == 0: query_project = dxpy.WORKSPACE_ID elif opt_num == 1: query_project = dxpy.find_one_project(name="Reference Genomes", public=True, level="VIEW")['id'] elif opt_num == 2: project_generator = dxpy.find_projects(level='VIEW', describe=True, explicit_perms=True) print '\nProjects to choose from:' query_project = paginate_and_pick(project_generator, (lambda result: result['describe']['name']))['id'] if opt_num in range(3): result_generator = dxpy.find_data_objects(classname=in_class, typename=param_desc.get('type'), describe=True, project=query_project) print '\nAvailable data:' result_choice = paginate_and_pick(result_generator, (lambda result: get_ls_l_desc(result['describe']))) if result_choice == 'none found': print 'No compatible data found' continue elif result_choice == 'none picked': continue else: return [result_choice['project'] + ':' + result_choice['id']] elif opt_num == 3: # Select from previous jobs in current project result_generator = dxpy.find_jobs(project=dxpy.WORKSPACE_ID, describe=True, parent_job="none") print print 'Previously-run jobs to choose from:' result_choice = paginate_and_pick(result_generator, (lambda result: get_find_jobs_string(result['describe'], has_children=False, single_result=True)), filter_fn=(lambda result: result['describe']['state'] not in ['unresponsive', 'terminating', 'terminated', 'failed'])) if result_choice == 'none found': print 'No jobs found' continue elif result_choice == 'none picked': continue else: if 'output' in result_choice['describe'] and result_choice['describe']['output'] != None: keys = result_choice['describe']['output'].keys() else: exec_handler = dxpy.get_handler(result_choice.get('app', result_choice['applet'])) exec_desc = exec_handler.describe() if 'outputSpec' not in exec_desc: # This if block will either continue, return, or raise print 'No output spec found for the executable' try: field = raw_input('Output field to use (^C or <ENTER> to cancel): ') if field == '': continue else: return [result_choice['id'] + ':' + field] except KeyboardInterrupt: continue else: keys = exec_desc['outputSpec'].keys() if len(keys) > 1: print '\nOutput fields to choose from:' field_choice = pick(keys) return [result_choice['id'] + ':' + keys[field_choice]] elif len(keys) == 1: print 'Using the only output field: ' + keys[0] return [result_choice['id'] + ':' + keys[0]] else: print 'No available output fields' else: print fill('Enter an ID or path (<TAB> twice for compatible ' + in_class + 's in current directory)' + (array_help_str if is_array else '')) return shlex.split(raw_input(prompt)) else: if in_class == 'boolean': if is_array: print fill('Enter "true", "false"' + array_help_str) else: print fill('Enter "true" or "false"') elif in_class == 'string' and is_array: print fill('Enter a nonempty string' + array_help_str) elif (in_class == 'float' or in_class == 'int') and is_array: print fill('Enter a number' + array_help_str) elif in_class == 'hash': print fill('Enter a quoted JSON hash') result = raw_input(prompt) if in_class == 'string': return [result] else: return shlex.split(result)