def test_combine_path_no_right(self): result = suite.combine_path("/path/to/left", None) assert result == "/path/to/left"
def test_combine_path(self): result = suite.combine_path("/path/to/left", "right/side") assert result == "/path/to/left/right/side"
def get_combinations(suite_dir, fields, subset, limit, filter_in, filter_out, include_facet, _isdir=os.path.isdir, _open=open, _isfile=os.path.isfile, _listdir=os.listdir): """ Describes the combinations of a suite, optionally limiting or filtering output based on the given parameters. Includes columns for the subsuite and facets when include_facet is True. Returns a tuple of (headers, rows) where both elements are lists of strings. """ configs = [(combine_path(suite_dir, item[0]), item[1]) for item in build_matrix(suite_dir, _isfile, _isdir, _listdir, subset)] num_listed = 0 rows = [] facet_headers = set() dirs = {} max_dir_depth = 0 for _, fragment_paths in configs: if limit > 0 and num_listed >= limit: break if filter_in and not any([f in path for f in filter_in for path in fragment_paths]): continue if filter_out and any([f in path for f in filter_out for path in fragment_paths]): continue fragment_fields = [extract_info(path, fields, _isdir, _open) for path in fragment_paths] # merge fields from multiple fragments by joining their values with \n metadata = {} for fragment_meta in fragment_fields: for field, value in fragment_meta.items(): if value == '': continue if field in metadata: metadata[field] += '\n' + str(value) else: metadata[field] = str(value) if include_facet: # map final dir (facet) -> filename without the .yaml suffix for path in fragment_paths: facet_dir = os.path.dirname(path) facet = os.path.basename(facet_dir) metadata[facet] = os.path.basename(path)[:-5] facet_headers.add(facet) facet_dirs = facet_dir.split('/')[:-1] for i, dir_ in enumerate(facet_dirs): if i not in dirs: dirs[i] = set() dirs[i].add(dir_) metadata['_dir_' + str(i)] = os.path.basename(dir_) max_dir_depth = max(max_dir_depth, i) rows.append(metadata) num_listed += 1 subsuite_headers = [] if include_facet: first_subsuite_depth = max_dir_depth for i in range(max_dir_depth): if len(dirs[i]) > 1: first_subsuite_depth = i break subsuite_headers = ['subsuite depth ' + str(i) for i in range(0, max_dir_depth - first_subsuite_depth + 1)] for row in rows: for i in range(first_subsuite_depth, max_dir_depth + 1): row[subsuite_headers[i - first_subsuite_depth]] = \ row.get('_dir_' + str(i), '') headers = subsuite_headers + sorted(facet_headers) + fields return headers, sorted([[row.get(field, '') for field in headers] for row in rows])
def get_combinations(suite_dir, fields, subset, limit, filter_in, filter_out, include_facet, _isdir=os.path.isdir, _open=open, _isfile=os.path.isfile, _listdir=os.listdir): """ Describes the combinations of a suite, optionally limiting or filtering output based on the given parameters. Includes columns for the subsuite and facets when include_facet is True. Returns a tuple of (headers, rows) where both elements are lists of strings. """ configs = [ (combine_path(suite_dir, item[0]), item[1]) for item in build_matrix(suite_dir, _isfile, _isdir, _listdir, subset) ] num_listed = 0 rows = [] facet_headers = set() dirs = {} max_dir_depth = 0 for _, fragment_paths in configs: if limit > 0 and num_listed >= limit: break if filter_in and not any( [f in path for f in filter_in for path in fragment_paths]): continue if filter_out and any( [f in path for f in filter_out for path in fragment_paths]): continue fragment_fields = [ extract_info(path, fields, _isdir, _open) for path in fragment_paths ] # merge fields from multiple fragments by joining their values with \n metadata = {} for fragment_meta in fragment_fields: for field, value in fragment_meta.items(): if value == '': continue if field in metadata: metadata[field] += '\n' + str(value) else: metadata[field] = str(value) if include_facet: # map final dir (facet) -> filename without the .yaml suffix for path in fragment_paths: facet_dir = os.path.dirname(path) facet = os.path.basename(facet_dir) metadata[facet] = os.path.basename(path)[:-5] facet_headers.add(facet) facet_dirs = facet_dir.split('/')[:-1] for i, dir_ in enumerate(facet_dirs): if i not in dirs: dirs[i] = set() dirs[i].add(dir_) metadata['_dir_' + str(i)] = os.path.basename(dir_) max_dir_depth = max(max_dir_depth, i) rows.append(metadata) num_listed += 1 subsuite_headers = [] if include_facet: first_subsuite_depth = max_dir_depth for i in range(max_dir_depth): if len(dirs[i]) > 1: first_subsuite_depth = i break subsuite_headers = [ 'subsuite depth ' + str(i) for i in range(0, max_dir_depth - first_subsuite_depth + 1) ] for row in rows: for i in range(first_subsuite_depth, max_dir_depth + 1): row[subsuite_headers[i - first_subsuite_depth]] = \ row.get('_dir_' + str(i), '') headers = subsuite_headers + sorted(facet_headers) + fields return headers, sorted([[row.get(field, '') for field in headers] for row in rows])