From b2fdc9c41079d21b907839fad51f671b6ab07e35 Mon Sep 17 00:00:00 2001 From: Fabien Morin <fabien@nexedi.com> Date: Wed, 11 Mar 2009 16:23:17 +0000 Subject: [PATCH] add a new option to display all the occurences of the word (if the word is three times in a file, display 3 lines) This behavior is enable by default but can be disable by passing parameter first_occurence=1 to the external method git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25987 20353a03-c40f-0410-a6d1-a30d3c3de9de --- product/ERP5/Extensions/Grep.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/product/ERP5/Extensions/Grep.py b/product/ERP5/Extensions/Grep.py index d8ff2039d2..1afe92e9a5 100644 --- a/product/ERP5/Extensions/Grep.py +++ b/product/ERP5/Extensions/Grep.py @@ -12,11 +12,11 @@ except: skip_meta_types = ('Image', 'File') -def traverse(ob, r, result, command_line_arguments): +def traverse(ob, r, result, command_line_arguments, first_occurence): if command_line_arguments['r'] and \ hasattr(aq_base(ob), 'objectValues'): for sub in [o for o in ob.objectValues() if o.meta_type not in skip_meta_types]: - traverse(sub, r, result, command_line_arguments) + traverse(sub, r, result, command_line_arguments, first_occurence) try: if hasattr(aq_base(ob), 'manage_FTPget'): text = ob.manage_FTPget() @@ -32,9 +32,10 @@ def traverse(ob, r, result, command_line_arguments): i+1+command_line_arguments['A']] path = '/'.join(ob.getPhysicalPath()) result.append((ob.absolute_url(), path, "\n".join(context))) - break + if first_occurence: + break -def grep(self, pattern, A=0, B=0, r=1, i=0, highlight=1): +def grep(self, pattern, A=0, B=0, r=1, i=0, highlight=1, first_occurence=0): if not _checkPermission(Permissions.ManagePortal, self): raise Unauthorized(self) command_line_arguments = {} # emulate grep command line args @@ -42,12 +43,13 @@ def grep(self, pattern, A=0, B=0, r=1, i=0, highlight=1): command_line_arguments['B'] = int(B) command_line_arguments['r'] = int(r) highlight = int(highlight) + first_occurence = int(first_occurence) re_flags = 0 if int(i) : re_flags = re.IGNORECASE result = [] rx = re.compile(pattern, re_flags) - traverse(self, rx, result, command_line_arguments) + traverse(self, rx, result, command_line_arguments, first_occurence) doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' html = '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">' -- 2.30.9