def grep(regex_list, recursive=True, dpath_list=None, include_patterns=None,
         exclude_dirs=[], greater_exclude_dirs=None, inverse=False,
         exclude_patterns=[], verbose=VERBOSE, fpath_list=None, reflags=0,
         cache=None):
    r"""
    greps for patterns
    Python implementation of grep. NOT FINISHED

    Args:
        regex_list (str or list): one or more patterns to find
        recursive (bool):
        dpath_list (list): directories to search (defaults to cwd)
        include_patterns (list) : defaults to standard file extensions

    Returns:
        (list, list, list): (found_fpaths, found_lines_list, found_lxs_list)

    CommandLine:
        python -m utool.util_path --test-grep
        utprof.py -m utool.util_path --exec-grep
        utprof.py utool/util_path.py --exec-grep

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_path import *  # NOQA
        >>> import utool as ut
        >>> #dpath_list = [ut.truepath('~/code/utool/utool')]
        >>> dpath_list = [ut.truepath(dirname(ut.__file__))]
        >>> include_patterns = ['*.py']
        >>> exclude_dirs = []
        >>> regex_list = ['grepfile']
        >>> verbose = True
        >>> recursive = True
        >>> result = ut.grep(regex_list, recursive, dpath_list, include_patterns,
        >>>                  exclude_dirs)
        >>> (found_fpath_list, found_lines_list, found_lxs_list) = result
        >>> assert 'util_path.py' in list(map(basename, found_fpath_list))
    """
    from utool import util_regex
    # from utool import util_str
    from utool import util_list
    if include_patterns is None:
        include_patterns =  ['*']
        # include_patterns = get_standard_include_patterns()
    if greater_exclude_dirs is None:
        greater_exclude_dirs = []
        # greater_exclude_dirs =  get_standard_exclude_dnames()
    # ensure list input
    if isinstance(include_patterns, six.string_types):
        include_patterns = [include_patterns]
    if dpath_list is None:
        dpath_list = [os.getcwd()]
    if verbose:
        recursive_stat_str = ['flat', 'recursive'][recursive]
        print('[util_path] Greping (%s) %r for %r' % (recursive_stat_str,
                                                      dpath_list, regex_list))
        print('[util_path] regex_list = %s' % (regex_list))
    if isinstance(regex_list, six.string_types):
        regex_list = [regex_list]
    found_fpath_list = []
    found_lines_list = []
    found_lxs_list = []
    # Walk through each directory recursively
    if fpath_list is None:
        fpath_generator = matching_fpaths(
            dpath_list=dpath_list, include_patterns=include_patterns,
            exclude_dirs=exclude_dirs,
            greater_exclude_dirs=greater_exclude_dirs,
            exclude_patterns=exclude_patterns, recursive=recursive)
    else:
        fpath_generator = fpath_list
    #     from utool import util_regex
    #     extended_regex_list, reflags = util_regex.extend_regex3(regex_list, reflags)
    #     if verbose:
    #         print('extended_regex_list = %r' % (extended_regex_list,))
    #         print('reflags = %r' % (reflags,))
    _exprs_flags = [util_regex.extend_regex2(expr, reflags)
                    for expr in regex_list]
    extended_regex_list = util_list.take_column(_exprs_flags, 0)
    reflags_list = util_list.take_column(_exprs_flags, 1)
    # HACK
    reflags = reflags_list[0]

    # For each matching filepath
    for fpath in fpath_generator:
        # For each search pattern
        found_lines, found_lxs = grepfile(fpath, extended_regex_list,
                                          reflags_list, cache=cache)
        if inverse:
            if len(found_lines) == 0:
                # Append files that the pattern was not found in
                found_fpath_list.append(fpath)
                found_lines_list.append([])
                found_lxs_list.append([])
        elif len(found_lines) > 0:
            found_fpath_list.append(fpath)  # regular matching
            found_lines_list.append(found_lines)
            found_lxs_list.append(found_lxs)

    grep_result = (found_fpath_list, found_lines_list, found_lxs_list)
    if verbose:
        print('==========')
        print('==========')
        print('[util_path] found matches in %d files' %
              len(found_fpath_list))
        print(make_grep_resultstr(grep_result, extended_regex_list, reflags))
        # print('[util_path] found matches in %d files' % len(found_fpath_list))

        # pat = util_regex.regex_or(extended_regex_list)

        # for fpath, found, lxs in zip(found_fpath_list, found_lines_list,
        #                              found_lxs_list):
        #     if len(found) > 0:
        #         print('----------------------')
        #         print('Found %d line(s) in %r: ' % (len(found), fpath))
        #         name = split(fpath)[1]
        #         max_line = len(lxs)
        #         ndigits = str(len(str(max_line)))
        #         fmt_str = '%s : %' + ndigits + 'd |%s'
        #         for (lx, line) in zip(lxs, found):
        #             # hack
        #             colored_line = util_str.highlight_regex(
        #                 line.rstrip('\n'), pat, reflags=reflags)
        #             print(fmt_str % (name, lx, colored_line))

        #print('[util_path] found matches in %d files' % len(found_fpath_list))

    return grep_result
def grep ( regex_list , recursive = True , dpath_list = None , include_patterns = None , exclude_dirs = [ ] , greater_exclude_dirs = None , inverse = False , exclude_patterns = [ ] , verbose = VERBOSE , fpath_list = None , reflags = 0 , cache = None ) : from utool import util_regex from utool import util_list if include_patterns is None : include_patterns = [ '*' ] if greater_exclude_dirs is None : greater_exclude_dirs = [ ] if isinstance ( include_patterns , six . string_types ) : include_patterns = [ include_patterns ] if dpath_list is None : dpath_list = [ os . getcwd ( ) ] if verbose : recursive_stat_str = [ 'flat' , 'recursive' ] [ recursive ] print ( '[util_path] Greping (%s) %r for %r' % ( recursive_stat_str , dpath_list , regex_list ) ) print ( '[util_path] regex_list = %s' % ( regex_list ) ) if isinstance ( regex_list , six . string_types ) : regex_list = [ regex_list ] found_fpath_list = [ ] found_lines_list = [ ] found_lxs_list = [ ] if fpath_list is None : fpath_generator = matching_fpaths ( dpath_list = dpath_list , include_patterns = include_patterns , exclude_dirs = exclude_dirs , greater_exclude_dirs = greater_exclude_dirs , exclude_patterns = exclude_patterns , recursive = recursive ) else : fpath_generator = fpath_list _exprs_flags = [ util_regex . extend_regex2 ( expr , reflags ) for expr in regex_list ] extended_regex_list = util_list . take_column ( _exprs_flags , 0 ) reflags_list = util_list . take_column ( _exprs_flags , 1 ) reflags = reflags_list [ 0 ] for fpath in fpath_generator : found_lines , found_lxs = grepfile ( fpath , extended_regex_list , reflags_list , cache = cache ) if inverse : if len ( found_lines ) == 0 : found_fpath_list . append ( fpath ) found_lines_list . append ( [ ] ) found_lxs_list . append ( [ ] ) elif len ( found_lines ) > 0 : found_fpath_list . append ( fpath ) found_lines_list . append ( found_lines ) found_lxs_list . append ( found_lxs ) grep_result = ( found_fpath_list , found_lines_list , found_lxs_list ) if verbose : print ( '==========' ) print ( '==========' ) print ( '[util_path] found matches in %d files' % len ( found_fpath_list ) ) print ( make_grep_resultstr ( grep_result , extended_regex_list , reflags ) ) return grep_result
r"""
    greps for patterns
    Python implementation of grep. NOT FINISHED

    Args:
        regex_list (str or list): one or more patterns to find
        recursive (bool):
        dpath_list (list): directories to search (defaults to cwd)
        include_patterns (list) : defaults to standard file extensions

    Returns:
        (list, list, list): (found_fpaths, found_lines_list, found_lxs_list)

    CommandLine:
        python -m utool.util_path --test-grep
        utprof.py -m utool.util_path --exec-grep
        utprof.py utool/util_path.py --exec-grep

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_path import *  # NOQA
        >>> import utool as ut
        >>> #dpath_list = [ut.truepath('~/code/utool/utool')]
        >>> dpath_list = [ut.truepath(dirname(ut.__file__))]
        >>> include_patterns = ['*.py']
        >>> exclude_dirs = []
        >>> regex_list = ['grepfile']
        >>> verbose = True
        >>> recursive = True
        >>> result = ut.grep(regex_list, recursive, dpath_list, include_patterns,
        >>>                  exclude_dirs)
        >>> (found_fpath_list, found_lines_list, found_lxs_list) = result
        >>> assert 'util_path.py' in list(map(basename, found_fpath_list))