Commit 7feaa34b authored by Jérome Perrin's avatar Jérome Perrin

Added original grep arguments -A -B -i and -r


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@4589 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 7563bc52
...@@ -2,10 +2,11 @@ import re ...@@ -2,10 +2,11 @@ import re
import cgi import cgi
from Acquisition import aq_base from Acquisition import aq_base
def traverse(ob, r, result): def traverse(ob, r, result, command_line_arguments):
if hasattr(aq_base(ob), 'objectValues'): if command_line_arguments['r'] and \
hasattr(aq_base(ob), 'objectValues'):
for sub in ob.objectValues(): for sub in ob.objectValues():
traverse(sub, r, result) traverse(sub, r, result, command_line_arguments)
try: try:
if hasattr(aq_base(ob), 'manage_FTPget'): if hasattr(aq_base(ob), 'manage_FTPget'):
text = ob.manage_FTPget() text = ob.manage_FTPget()
...@@ -14,21 +15,31 @@ def traverse(ob, r, result): ...@@ -14,21 +15,31 @@ def traverse(ob, r, result):
except: except:
text = None text = None
if text: if text:
for l in text.split('\n'): text_lines = text.split('\n')
for i, l in enumerate(text_lines):
if r.search(l) is not None: if r.search(l) is not None:
context = text_lines[i-command_line_arguments['B'] :
i+1+command_line_arguments['A']]
path = '/'.join(ob.getPhysicalPath()) path = '/'.join(ob.getPhysicalPath())
result.append((path, l)) result.append((path, "\n".join(context)))
break break
def grep(self, pattern): def grep(self, pattern, A=0, B=0, r=1, i=0):
command_line_arguments = {} # emulate grep command line args
command_line_arguments['A'] = int(A)
command_line_arguments['B'] = int(B)
command_line_arguments['r'] = int(r)
re_flags = 0
if int(i) :
re_flags = re.IGNORECASE
result = [] result = []
traverse(self, re.compile(pattern), result) traverse(self, re.compile(pattern, re_flags), result, command_line_arguments)
html_element_list = ['<html>', '<body>'] html_element_list = ['<html>', '<body>']
for path, line in result: for path, line in result:
path = cgi.escape(path) path = cgi.escape(path)
line = cgi.escape(line) line = cgi.escape(line)
html_element_list.append('<a href="%s/manage_workspace">%s</a>: %s<br/>' % ( html_element_list.append('<a href="%s/manage_workspace">%s</a>: %s<br/>' % (
path, path, line)) path, path, line.replace('\n', '<br/>')))
html_element_list.extend(['</body>', '</html>']) html_element_list.extend(['</body>', '</html>'])
self.REQUEST.RESPONSE.setHeader('Content-Type', 'text/html') self.REQUEST.RESPONSE.setHeader('Content-Type', 'text/html')
return '\n'.join(html_element_list) return '\n'.join(html_element_list)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment