#!/usr/bin/env python """Filesystem Utilities Convenience functions for dealing with paths and filesystem operations that are somewhat complex and common to different utilities. """ import os # Extra Path utilities def normalize_path(path): """Takes a path, and returns a Unicode normalized path (env vars and ~ expanded)""" path = os.path.expandvars(path) path = os.path.expanduser(path) path = unicode(os.path.abspath(path)) return path def extension_of(path): """Returns the extension of the given path.""" spl = path.split('.') if spl[-1] in ('gz', 'bz2'): if spl[-2] == 'tar': return '.'.join(spl[-2:]) return spl[-1] def paths_to_list(paths, recursive=False): """Turns a list of paths (each which can contain env vars, ~user, and directories) into a list of unicode absolute paths.""" if not isinstance(paths, list): paths = [paths] l = [] for path in paths: if os.path.isdir(path): l += directory_to_list(path, recursive) else: l.append(normalize_path(path)) return l def directory_to_list(path, recursive=False): """Takes a path that is a directory and returns the filenames in a list. If recursive is True, it returns a recursive list of _file_ paths """ paths = [] path = normalize_path(path) entries = os.listdir(path) opj = os.path.join files = [opj(path, entry) for entry in entries if os.path.isfile(opj(path, entry)) and not entry.startswith('.')] if recursive: directories = [opj(path, entry) for entry in entries if os.path.isdir(opj(path, entry)) and not entry.startswith('.')] for directory in directories: files += directory_to_list(opj(path, directory), recursive=True) return files def filter_by_types(pathlist, types): """Takes a list of paths and filters out the extensions in 'types'""" return [path for path in pathlist if extension_of(path) in types]