Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9cde31

Browse files
committed
Now uses varargs syntax to grep more than one file.
1 parent b914257 commit e9cde31

2 files changed

Lines changed: 96 additions & 60 deletions

File tree

Lib/grep.py

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,59 @@
44
from regex_syntax import *
55
import string
66

7-
def grep(pat, filename):
8-
return ggrep(RE_SYNTAX_GREP, pat, filename)
7+
opt_show_where = 0
8+
opt_show_filename = 0
9+
opt_show_lineno = 1
910

10-
def egrep(pat, filename):
11-
return ggrep(RE_SYNTAX_EGREP, pat, filename)
11+
def grep(pat, +files):
12+
return ggrep(RE_SYNTAX_GREP, pat, files)
1213

13-
def emgrep(pat, filename):
14-
return ggrep(RE_SYNTAX_EMACS, pat, filename)
14+
def egrep(pat, +files):
15+
return ggrep(RE_SYNTAX_EGREP, pat, files)
1516

16-
def ggrep(syntax, pat, filename):
17+
def emgrep(pat, +files):
18+
return ggrep(RE_SYNTAX_EMACS, pat, files)
19+
20+
def ggrep(syntax, pat, files):
21+
if len(files) == 1 and type(files[0]) == type([]):
22+
files = files[0]
23+
global opt_show_filename
24+
opt_show_filename = (len(files) != 1)
1725
syntax = regex.set_syntax(syntax)
1826
try:
1927
prog = regex.compile(pat)
2028
finally:
2129
syntax = regex.set_syntax(syntax)
22-
fp = open(filename, 'r')
23-
lineno = 0
24-
while 1:
25-
line = fp.readline()
26-
if not line: break
27-
lineno = lineno + 1
28-
if prog.search(line) >= 0:
29-
if line[-1:] == '\n': line = line[:-1]
30-
prefix = string.rjust(`lineno`, 3) + ': '
31-
print prefix + line
32-
if 0: # XXX
33-
start, end = prog.regs[0]
34-
line = line[:start]
35-
if '\t' not in line:
36-
prefix = ' ' * (len(prefix) + start)
37-
else:
38-
prefix = ' ' * len(prefix)
39-
for c in line:
40-
if c <> '\t': c = ' '
41-
prefix = prefix + c
42-
if start == end: prefix = prefix + '\\'
43-
else: prefix = prefix + '^'*(end-start)
44-
print prefix
30+
for filename in files:
31+
fp = open(filename, 'r')
32+
lineno = 0
33+
while 1:
34+
line = fp.readline()
35+
if not line: break
36+
lineno = lineno + 1
37+
if prog.search(line) >= 0:
38+
showline(filename, lineno, line, prog)
39+
fp.close()
40+
41+
def showline(filename, lineno, line, prog):
42+
if line[-1:] == '\n': line = line[:-1]
43+
if opt_show_lineno:
44+
prefix = string.rjust(`lineno`, 3) + ': '
45+
else:
46+
prefix = ''
47+
if opt_show_filename:
48+
prefix = filename + ': ' + prefix
49+
print prefix + line
50+
if opt_show_where:
51+
start, end = prog.regs()[0]
52+
line = line[:start]
53+
if '\t' not in line:
54+
prefix = ' ' * (len(prefix) + start)
55+
else:
56+
prefix = ' ' * len(prefix)
57+
for c in line:
58+
if c <> '\t': c = ' '
59+
prefix = prefix + c
60+
if start == end: prefix = prefix + '\\'
61+
else: prefix = prefix + '^'*(end-start)
62+
print prefix

Lib/lib-old/grep.py

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,59 @@
44
from regex_syntax import *
55
import string
66

7-
def grep(pat, filename):
8-
return ggrep(RE_SYNTAX_GREP, pat, filename)
7+
opt_show_where = 0
8+
opt_show_filename = 0
9+
opt_show_lineno = 1
910

10-
def egrep(pat, filename):
11-
return ggrep(RE_SYNTAX_EGREP, pat, filename)
11+
def grep(pat, +files):
12+
return ggrep(RE_SYNTAX_GREP, pat, files)
1213

13-
def emgrep(pat, filename):
14-
return ggrep(RE_SYNTAX_EMACS, pat, filename)
14+
def egrep(pat, +files):
15+
return ggrep(RE_SYNTAX_EGREP, pat, files)
1516

16-
def ggrep(syntax, pat, filename):
17+
def emgrep(pat, +files):
18+
return ggrep(RE_SYNTAX_EMACS, pat, files)
19+
20+
def ggrep(syntax, pat, files):
21+
if len(files) == 1 and type(files[0]) == type([]):
22+
files = files[0]
23+
global opt_show_filename
24+
opt_show_filename = (len(files) != 1)
1725
syntax = regex.set_syntax(syntax)
1826
try:
1927
prog = regex.compile(pat)
2028
finally:
2129
syntax = regex.set_syntax(syntax)
22-
fp = open(filename, 'r')
23-
lineno = 0
24-
while 1:
25-
line = fp.readline()
26-
if not line: break
27-
lineno = lineno + 1
28-
if prog.search(line) >= 0:
29-
if line[-1:] == '\n': line = line[:-1]
30-
prefix = string.rjust(`lineno`, 3) + ': '
31-
print prefix + line
32-
if 0: # XXX
33-
start, end = prog.regs[0]
34-
line = line[:start]
35-
if '\t' not in line:
36-
prefix = ' ' * (len(prefix) + start)
37-
else:
38-
prefix = ' ' * len(prefix)
39-
for c in line:
40-
if c <> '\t': c = ' '
41-
prefix = prefix + c
42-
if start == end: prefix = prefix + '\\'
43-
else: prefix = prefix + '^'*(end-start)
44-
print prefix
30+
for filename in files:
31+
fp = open(filename, 'r')
32+
lineno = 0
33+
while 1:
34+
line = fp.readline()
35+
if not line: break
36+
lineno = lineno + 1
37+
if prog.search(line) >= 0:
38+
showline(filename, lineno, line, prog)
39+
fp.close()
40+
41+
def showline(filename, lineno, line, prog):
42+
if line[-1:] == '\n': line = line[:-1]
43+
if opt_show_lineno:
44+
prefix = string.rjust(`lineno`, 3) + ': '
45+
else:
46+
prefix = ''
47+
if opt_show_filename:
48+
prefix = filename + ': ' + prefix
49+
print prefix + line
50+
if opt_show_where:
51+
start, end = prog.regs()[0]
52+
line = line[:start]
53+
if '\t' not in line:
54+
prefix = ' ' * (len(prefix) + start)
55+
else:
56+
prefix = ' ' * len(prefix)
57+
for c in line:
58+
if c <> '\t': c = ' '
59+
prefix = prefix + c
60+
if start == end: prefix = prefix + '\\'
61+
else: prefix = prefix + '^'*(end-start)
62+
print prefix

0 commit comments

Comments
 (0)