예제 #1
0
def find_file(re_filter=None, branch=".", recurse=True, depth=0, get_file=True, get_dir=True, case_i=True):
    """Search a branch directory for files and / or directories whose name matches one or more of the filter patterns

    Args:
        branch (str): (optional) Directory to search. Suggestion: use absolute path. Defaults to current working directory.
        re_filter (list): (optional) List of strings of wildcard patterns. Defaults to None, which skips any filtering.
        recurse (bool): (optional) Recursive search flag. Defaults to True.
        depth (int): (optional) Set the recursion depth. Defaults to 0, which has a max of Python's recursion limit.
        get_file (bool): (optional) Return entry for file. Defaults to True.
        get_dir (bool): (optional) Return entry for directory. Defaults to True.
        case_i (bool): (optional) Case insensitive search. Defaults to True.

    Returns:
        list: List of strings with paths of files.

    """
    if not is_str_not_empty(branch):
        raise ValueError("branch must be a non empty string")
    if not os.path.isdir(branch):
        raise ValueError("branch must be a valid directory")
    if not is_bool(recurse):
        raise ValueError("recurse must be True or False")
    if is_int_neg(depth):
        raise ValueError("depth must be >= 0")
    if not is_bool(get_file):
        raise ValueError("get_file must be True or False")
    if not is_bool(get_dir):
        raise ValueError("get_dir must be True or False")
    if not is_bool(case_i):
        raise ValueError("case_i must be True or False")

    search_results = list_dir(branch, recurse, depth, get_file, get_dir)

    if re_filter is None or len(re_filter) < 1:
        return search_results
    elif re_filter == "*":
        return search_results
    elif is_list(re_filter) and "*" in re_filter:
        return search_results

    if is_str(re_filter):
        re_filter = [re_filter]

    re_flag = 0
    if case_i:
        re_flag = re.IGNORECASE

    bre = partial(wildcard_re, wildcard="*", escape="\\")
    return list_filter(map(bre, re_filter), search_results, re_flag)
예제 #2
0
def find_file_re(re_filter=None, branch=".", recurse=True, depth=0, get_file=True, get_dir=True, re_flag=0):
    """Search a branch directory for files and / or directories whose name matches one or more of the regex filter patterns

    Args:
        branch (str): (optional) Directory to search. Suggestion: use absolute path. Defaults to current working directory.
        re_filter (list): (optional) List of strings of regular expression patterns. Defaults to None, which skips any filtering.
        recurse (bool): (optional) Recursive search flag. Defaults to True.
        depth (int): (optional) Set the recursion depth. Defaults to 0, which has a max of Python's recursion limit.
        get_file (bool): (optional) Return entry for file. Defaults to True.
        get_dir (bool): (optional) Return entry for directory. Defaults to True.
        re_flag (int): (optional) See :py:mod:`re`

    Returns:
        list: List of strings with paths of files.

    """
    if not is_str_not_empty(branch):
        raise ValueError("branch must be a non empty string")
    if not os.path.isdir(branch):
        raise ValueError("branch must be a valid directory")
    if not is_bool(recurse):
        raise TypeError("recurse must be True or False")
    if is_int_neg(depth):
        raise ValueError("depth must be >= 0")
    if not is_bool(get_file):
        raise ValueError("get_file must be True or False")
    if not is_bool(get_dir):
        raise ValueError("get_dir must be True or False")

    search_results = list_dir(branch, recurse, depth, get_file, get_dir)

    if re_filter is None or len(re_filter) < 1:
        return search_results

    if is_str(re_filter):
        re_filter = [re_filter]

    return list_filter(re_filter, search_results, re_flag)