def kwgrep( grepdict, afile, lower=True ):
    """ combined grep + getfields:
        "name: ^mp3  version: ^1.2.3"   -- match these
        "home-page: summary:"           -- and get these too
        or a dict, kw -> matcher() / "" for also-gets
    usage:
        lines, nmatch, nre = kwgrep()
        if nmatch == nre:
            ... all REs matched, here both name: and version:
            ... or 0 == 0, just getfields
    """
    if isinstance( grepdict, basestring ):
        grepdict = str_grepdict( grepdict )
    if isinstance( afile, basestring ):
        afile = open( afile )
    lines = []
    hits = {}  # nr diff keywords w RE matches
    for line in afile:
        m = _kw_rest_re.match( line )  # kw: ...
        if not m:
            continue
        kw = m.group( 1 )
        if lower:  kw = kw.lower()
        if kw not in grepdict:
            continue
        matchf = grepdict[kw]
        if not matchf:
            lines.append( (kw, line.rstrip()) )  # also-gets
            continue
        restofline = m.group( 2 ) .rstrip()
        if matchf( restofline ):
            lines.append( (kw, line.rstrip()) )
            hits[kw] = 1
    nre = sum( [bool(re) for re in grepdict.values()] )
    if Test:
        print >>sys.stderr, "test kwgrep:", lines, len(hits), nre
    return (lines, len(hits), nre)
 combined grep + getfields:
        "name: ^mp3  version: ^1.2.3"   -- match these
        "home-page: summary:"           -- and get these too
        or a dict, kw -> matcher() / "" for also-gets
    usage:
        lines, nmatch, nre = kwgrep()
        if nmatch == nre:
            ... all REs matched, here both name: and version:
            ... or 0 == 0, just getfields
    
recipes/Python/576741_uniform_matcher_re_pattern__re__func__dict_/recipe-576741.py