mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-24 09:49:12 -06:00
add --negate flag to pygrep
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import argparse
|
||||
import re
|
||||
import sys
|
||||
from typing import NamedTuple
|
||||
from typing import Optional
|
||||
from typing import Pattern
|
||||
from typing import Sequence
|
||||
@@ -45,6 +46,46 @@ def _process_filename_at_once(pattern: Pattern[bytes], filename: str) -> int:
|
||||
return retv
|
||||
|
||||
|
||||
def _process_filename_by_line_negated(
|
||||
pattern: Pattern[bytes],
|
||||
filename: str,
|
||||
) -> int:
|
||||
with open(filename, 'rb') as f:
|
||||
for line in f:
|
||||
if pattern.search(line):
|
||||
return 0
|
||||
else:
|
||||
output.write_line(filename)
|
||||
return 1
|
||||
|
||||
|
||||
def _process_filename_at_once_negated(
|
||||
pattern: Pattern[bytes],
|
||||
filename: str,
|
||||
) -> int:
|
||||
with open(filename, 'rb') as f:
|
||||
contents = f.read()
|
||||
match = pattern.search(contents)
|
||||
if match:
|
||||
return 0
|
||||
else:
|
||||
output.write_line(filename)
|
||||
return 1
|
||||
|
||||
|
||||
class Choice(NamedTuple):
|
||||
multiline: bool
|
||||
negate: bool
|
||||
|
||||
|
||||
FNS = {
|
||||
Choice(multiline=True, negate=True): _process_filename_at_once_negated,
|
||||
Choice(multiline=True, negate=False): _process_filename_at_once,
|
||||
Choice(multiline=False, negate=True): _process_filename_by_line_negated,
|
||||
Choice(multiline=False, negate=False): _process_filename_by_line,
|
||||
}
|
||||
|
||||
|
||||
def run_hook(
|
||||
hook: Hook,
|
||||
file_args: Sequence[str],
|
||||
@@ -64,6 +105,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
)
|
||||
parser.add_argument('-i', '--ignore-case', action='store_true')
|
||||
parser.add_argument('--multiline', action='store_true')
|
||||
parser.add_argument('--negate', action='store_true')
|
||||
parser.add_argument('pattern', help='python regex pattern.')
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
args = parser.parse_args(argv)
|
||||
@@ -75,11 +117,9 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
pattern = re.compile(args.pattern.encode(), flags)
|
||||
|
||||
retv = 0
|
||||
process_fn = FNS[Choice(multiline=args.multiline, negate=args.negate)]
|
||||
for filename in args.filenames:
|
||||
if args.multiline:
|
||||
retv |= _process_filename_at_once(pattern, filename)
|
||||
else:
|
||||
retv |= _process_filename_by_line(pattern, filename)
|
||||
retv |= process_fn(pattern, filename)
|
||||
return retv
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user