-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevre
executable file
Β·160 lines (148 loc) Β· 5.32 KB
/
evre
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
'''evREwhere is a pattern searcher like grep but enables extended capabilities
evREwhere can be used as a module or a separate utility program
Examples needed here...
'''
import sys
import pathlib
import argparse
import traceback
import collections
from evrewhere import PatternFinder
from evrewhere.printers import FileInfoPrefixPrinter, MatchPrinter, VerbosePrinter
def parse_arguments() -> argparse.Namespace:
'''Parse program arguments'''
def postparse(args: argparse.Namespace) -> argparse.Namespace:
'''Post-parse program arguments'''
if not sys.stdin.isatty():
args.paths.append(sys.stdin)
if args.count_only:
if args.with_lineno:
raise ValueError('-n and -c are mutually exclusive')
if args.full_lines:
raise ValueError('--full-lines and --count are mutually exclusive')
if args.full_lines and args.template is not None:
raise ValueError('--full-lines and --format are mutually exclusive')
if args.with_filename is None:
args.with_filename = args.recursive or len(args.paths) > 1
elif args.verbose and not args.with_filename:
raise ValueError('--verbose does not support -h/--no-filename')
return args
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument(
'--help', action='help', default=argparse.SUPPRESS,
help='show this help message and exit'
)
parser.add_argument(
'pattern',
help='pattern used in search'
)
parser.add_argument(
'paths', nargs='*', type=pathlib.Path,
help='path to a directory (-r required) or file'
)
parser.add_argument(
'-r', action='store_true', dest='recursive', default=False,
help='recursive search'
)
parser.add_argument(
'-m', dest='limit', type=int, default=0,
help='maximum matches (default: no limit)'
)
parser.add_argument(
'-n', '--lineno', dest='with_lineno', action='store_true',
help='whether to display line numbers (may affect performance)'
)
parser.add_argument(
'-f', '--format', dest='template', default=None,
help='display format, {0} means group(0) (default: {0})'
)
parser.add_argument(
'-H', '--with-filename', dest='with_filename', action='store_true', default=None,
help='print file name with output lines'
)
parser.add_argument(
'-h', '--no-filename', dest='with_filename', action='store_false', default=None,
help='suppress the file name prefix on output'
)
parser.add_argument(
'-g', '--full-lines', dest='full_lines', action='store_true',
help='print full lines like grep (may affect performance)'
)
parser.add_argument(
'-i', '--ignore-case', dest='case_insensitive', action='store_true',
help='case insensitive search'
)
parser.add_argument(
'-a', '--dot-all', dest='dot_all', action='store_true',
help='dot (.) includes newline characters'
)
parser.add_argument(
'-q', '--quiet', '--silent', dest='quiet', action='store_true',
help='suppress all normal output'
)
parser.add_argument(
'-c', '--count', dest='count_only', action='store_true',
help='print only a (positive) number of matches per file'
)
parser.add_argument(
'-v', '--verbose', action='store_true', dest='verbose',
help='outputs regex Match object instead of raw text'
)
args = parser.parse_args()
try:
return postparse(args)
except ValueError as error:
parser.error(error)
def main() -> int:
'''Run program as a utility with argument parsing
Returns 0 if a match was found or 1 if none were found'''
args = parse_arguments()
finder = PatternFinder(
args.pattern,
limit=args.limit,
line_numbers=args.with_lineno,
case_insensitive=args.case_insensitive,
dot_all=args.dot_all,
full_lines=args.full_lines,
)
found = finder.search(args.paths, recursive=args.recursive)
exit_code = int(not found)
if args.count_only:
prefix_printer = FileInfoPrefixPrinter(with_filename=args.with_filename)
counts = collections.Counter(result.path for result in found)
for path in counts:
prefix_printer.print(path, 0, counts[path], sep='')
return exit_code
if args.quiet:
return exit_code
if args.verbose:
printer = VerbosePrinter()
else:
printer = MatchPrinter(
args.template,
finder.pattern.groups,
with_filename=args.with_filename,
with_lineno=args.with_lineno,
full_lines=args.full_lines,
)
# Show results
for result in found:
printer.print(result)
return exit_code
if __name__ == '__main__':
EXIT_CODE = 255
try:
EXIT_CODE = main()
except KeyboardInterrupt:
EXIT_CODE = 130
except OSError as error:
print(f'evre: {error.filename}: {error.strerror}')
EXIT_CODE = 2
except SystemExit:
sys.exit(EXIT_CODE)
except: # pylint: disable=bare-except
# This bare except makes sure that all exceptions
# have a defined exit code and are printed properly
traceback.print_exc()
sys.exit(EXIT_CODE)