コード例 #1
0
    def test_ascii(self):
        # -a/--ascii

        # No match when in ascii mode
        options = grin.Options(regex=r"\d",
                               re_flags=[re.A],
                               before_context=0,
                               after_context=0)
        regex_unicode = grin.GrepText(grin.utils.get_regex(options))
        self.assertEqual(
            regex_unicode.do_grep(BytesIO(unicode_digits)),
            [],
        )
        # [(1, 0, 'an Arabic-Indic digit ٢ on the\n', [(22, 23)])]

        # Unicode (default)
        options = grin.Options(regex=r"\d",
                               re_flags=[],
                               before_context=0,
                               after_context=0)
        regex_unicode = grin.GrepText(grin.utils.get_regex(options))
        self.assertEqual(
            regex_unicode.do_grep(BytesIO(unicode_digits)),
            [(1, 0, "an Arabic-Indic digit ٢ on the\n", [(22, 23)])],
        )
コード例 #2
0
    def test_word_match_option(self):
        # -w/--word-regexp

        # Not a word-match
        options = grin.Options(word_regexp=True,
                               regex="tes",
                               re_flags=[],
                               before_context=0,
                               after_context=0)
        regex_on_word_boundaries = grin.GrepText(grin.utils.get_regex(options))
        self.assertEqual(
            regex_on_word_boundaries.do_grep(BytesIO(word_boundaries)),
            [],
        )

        # Word-match
        options = grin.Options(word_regexp=True,
                               regex="test",
                               re_flags=[],
                               before_context=0,
                               after_context=0)
        regex_on_word_boundaries = grin.GrepText(grin.utils.get_regex(options))
        self.assertEqual(
            regex_on_word_boundaries.do_grep(BytesIO(word_boundaries)),
            [(1, 0, "This is a test.\n", [(10, 14)])],
        )
コード例 #3
0
ファイル: app.py プロジェクト: shenghuatang/logan
def search_for_expression(output, filepaths, validfiles, expression,
                          grepbefore, grepafter):
    """Carry out search for expression (using grep context) on validfiles returning matching files as output"""
    options = grin.Options()
    options['before_context'] = int(grepbefore)
    options['after_context'] = int(grepafter)
    options['use_color'] = False
    options['show_filename'] = False
    options['show_match'] = True
    options['show_emacs'] = False
    options['show_line_numbers'] = True

    anchorcount = 1

    searchregexp = re.compile(expression)
    grindef = grin.GrepText(searchregexp, options)

    for file in validfiles:
        filepath = validfiles.get(file)[0]
        report = grindef.grep_a_file(filepath)
        if report:

            output += '<a name="filename' + str(
                anchorcount) + '"></a><h2>' + filepath + '</h2>'

            filepaths.append(filepath)
            reporttext = report.split("\n")
            for text in reporttext:
                if text:
                    output += "line " + text + "<br>"
            anchorcount += 1

    return output
コード例 #4
0
ファイル: test_grep.py プロジェクト: ksamuel/grin
def test_basic_defaults_with_no_context():

    gt_default = grin.GrepText(re.compile(b"foo"))
    assert gt_default.do_grep(BytesIO(all_foo)) == [
        (0, 0, b"foo\n", [(0, 3)]),
        (1, 0, b"foo\n", [(0, 3)]),
        (2, 0, b"foo\n", [(0, 3)]),
        (3, 0, b"foo\n", [(0, 3)]),
        (4, 0, b"foo\n", [(0, 3)]),
    ]
    assert gt_default.do_grep(BytesIO(first_foo)) == [(0, 0, b"foo\n", [(0, 3)
                                                                        ])]
    assert gt_default.do_grep(BytesIO(last_foo)) == [(4, 0, b"foo\n", [(0, 3)])
                                                     ]
    assert gt_default.do_grep(BytesIO(second_foo)) == [(1, 0, b"foo\n", [(0, 3)
                                                                         ])]
    assert gt_default.do_grep(BytesIO(second_last_foo)) == [(3, 0, b"foo\n",
                                                             [(0, 3)])]
    assert gt_default.do_grep(BytesIO(middle_foo)) == [(2, 0, b"foo\n", [(0, 3)
                                                                         ])]
    assert gt_default.do_grep(BytesIO(small_gap)) == [
        (2, 0, b"foo\n", [(0, 3)]),
        (4, 0, b"foo\n", [(0, 3)]),
    ]
    assert gt_default.do_grep(BytesIO(no_eol)) == [(0, 0, b"foo", [(0, 3)])]
    assert gt_default.do_grep(BytesIO(middle_of_line)) == [
        (2, 0, b"barfoobar\n", [(3, 6)])
    ]
コード例 #5
0
 def test_basic_defaults(self):
     # Test the basic defaults, no context.
     gt_default = grin.GrepText(re.compile("foo"))
     self.assertEqual(
         gt_default.do_grep(BytesIO(all_foo)),
         [
             (0, 0, "foo\n", [(0, 3)]),
             (1, 0, "foo\n", [(0, 3)]),
             (2, 0, "foo\n", [(0, 3)]),
             (3, 0, "foo\n", [(0, 3)]),
             (4, 0, "foo\n", [(0, 3)]),
         ],
     )
     self.assertEqual(gt_default.do_grep(BytesIO(first_foo)),
                      [(0, 0, "foo\n", [(0, 3)])])
     self.assertEqual(gt_default.do_grep(BytesIO(last_foo)),
                      [(4, 0, "foo\n", [(0, 3)])])
     self.assertEqual(gt_default.do_grep(BytesIO(second_foo)),
                      [(1, 0, "foo\n", [(0, 3)])])
     self.assertEqual(gt_default.do_grep(BytesIO(second_last_foo)),
                      [(3, 0, "foo\n", [(0, 3)])])
     self.assertEqual(gt_default.do_grep(BytesIO(middle_foo)),
                      [(2, 0, "foo\n", [(0, 3)])])
     self.assertEqual(
         gt_default.do_grep(BytesIO(small_gap)),
         [(2, 0, "foo\n", [(0, 3)]), (4, 0, "foo\n", [(0, 3)])],
     )
     self.assertEqual(gt_default.do_grep(BytesIO(no_eol)),
                      [(0, 0, "foo", [(0, 3)])])
     self.assertEqual(
         gt_default.do_grep(BytesIO(middle_of_line)),
         [(2, 0, "barfoobar\n", [(3, 6)])],
     )
コード例 #6
0
ファイル: test_grep.py プロジェクト: ksamuel/grin
def test_symmetric_2_line_context():

    gt_context_2 = grin.GrepText(re.compile(b"foo"),
                                 options=grin.Options(before_context=2,
                                                      after_context=2))
    assert gt_context_2.do_grep(BytesIO(all_foo)) == [
        (0, 0, b"foo\n", [(0, 3)]),
        (1, 0, b"foo\n", [(0, 3)]),
        (2, 0, b"foo\n", [(0, 3)]),
        (3, 0, b"foo\n", [(0, 3)]),
        (4, 0, b"foo\n", [(0, 3)]),
    ]
    assert gt_context_2.do_grep(BytesIO(first_foo)) == [
        (0, 0, b"foo\n", [(0, 3)]),
        (1, 1, b"bar\n", None),
        (2, 1, b"bar\n", None),
    ]
    assert gt_context_2.do_grep(BytesIO(last_foo)) == [
        (2, -1, b"bar\n", None),
        (3, -1, b"bar\n", None),
        (4, 0, b"foo\n", [(0, 3)]),
    ]
    assert gt_context_2.do_grep(BytesIO(second_foo)) == [
        (0, -1, b"bar\n", None),
        (1, 0, b"foo\n", [(0, 3)]),
        (2, 1, b"bar\n", None),
        (3, 1, b"bar\n", None),
    ]
    assert gt_context_2.do_grep(BytesIO(second_last_foo)) == [
        (1, -1, b"bar\n", None),
        (2, -1, b"bar\n", None),
        (3, 0, b"foo\n", [(0, 3)]),
        (4, 1, b"bar\n", None),
    ]
    assert gt_context_2.do_grep(BytesIO(middle_foo)) == [
        (0, -1, b"bar\n", None),
        (1, -1, b"bar\n", None),
        (2, 0, b"foo\n", [(0, 3)]),
        (3, 1, b"bar\n", None),
        (4, 1, b"bar\n", None),
    ]
    assert gt_context_2.do_grep(BytesIO(small_gap)) == [
        (0, -1, b"bar\n", None),
        (1, -1, b"bar\n", None),
        (2, 0, b"foo\n", [(0, 3)]),
        (3, 1, b"bar\n", None),
        (4, 0, b"foo\n", [(0, 3)]),
        (5, 1, b"bar\n", None),
        (6, 1, b"bar\n", None),
    ]
    assert gt_context_2.do_grep(BytesIO(no_eol)) == [(0, 0, b"foo", [(0, 3)])]
    assert gt_context_2.do_grep(BytesIO(middle_of_line)) == [
        (0, -1, b"bar\n", None),
        (1, -1, b"bar\n", None),
        (2, 0, b"barfoobar\n", [(3, 6)]),
        (3, 1, b"bar\n", None),
        (4, 1, b"bar\n", None),
    ]
コード例 #7
0
    def test_fixed_string_option(self):
        # -F/--fixed-string works with unescaped regex metachars

        options = grin.Options(fixed_string=True,
                               regex="foo(",
                               re_flags=[],
                               before_context=0,
                               after_context=0)
        regex_with_metachars = grin.GrepText(grin.utils.get_regex(options))
        self.assertEqual(
            regex_with_metachars.do_grep(BytesIO(regex_metachar_foo)),
            [(2, 0, "def foo(...):\n", [(4, 8)])],
        )
コード例 #8
0
    def test_one_line_after_context_no_lines_before(self):
        # 1 line of after-context, no lines before.

        gt_after_context_1 = grin.GrepText(re.compile("foo"),
                                           options=grin.Options(
                                               before_context=0,
                                               after_context=1))
        self.assertEqual(
            gt_after_context_1.do_grep(BytesIO(all_foo)),
            [
                (0, 0, "foo\n", [(0, 3)]),
                (1, 0, "foo\n", [(0, 3)]),
                (2, 0, "foo\n", [(0, 3)]),
                (3, 0, "foo\n", [(0, 3)]),
                (4, 0, "foo\n", [(0, 3)]),
            ],
        )
        self.assertEqual(
            gt_after_context_1.do_grep(BytesIO(first_foo)),
            [(0, 0, "foo\n", [(0, 3)]), (1, 1, "bar\n", None)],
        )
        self.assertEqual(gt_after_context_1.do_grep(BytesIO(last_foo)),
                         [(4, 0, "foo\n", [(0, 3)])])
        self.assertEqual(
            gt_after_context_1.do_grep(BytesIO(second_foo)),
            [(1, 0, "foo\n", [(0, 3)]), (2, 1, "bar\n", None)],
        )
        self.assertEqual(
            gt_after_context_1.do_grep(BytesIO(second_last_foo)),
            [(3, 0, "foo\n", [(0, 3)]), (4, 1, "bar\n", None)],
        )
        self.assertEqual(
            gt_after_context_1.do_grep(BytesIO(middle_foo)),
            [(2, 0, "foo\n", [(0, 3)]), (3, 1, "bar\n", None)],
        )
        self.assertEqual(
            gt_after_context_1.do_grep(BytesIO(small_gap)),
            [
                (2, 0, "foo\n", [(0, 3)]),
                (3, 1, "bar\n", None),
                (4, 0, "foo\n", [(0, 3)]),
                (5, 1, "bar\n", None),
            ],
        )
        self.assertEqual(gt_after_context_1.do_grep(BytesIO(no_eol)),
                         [(0, 0, "foo", [(0, 3)])])
        self.assertEqual(
            gt_after_context_1.do_grep(BytesIO(middle_of_line)),
            [(2, 0, "barfoobar\n", [(3, 6)]), (3, 1, "bar\n", None)],
        )
コード例 #9
0
    def test_non_ascii(self):
        non_ascii = grin.GrepText(re.compile("é"))
        self.assertEqual(
            non_ascii.do_grep(BytesIO(utf_8_foo), encoding="utf8"),
            [(0, 0, "Rémy\n", [(1, 2)])],
        )
        self.assertEqual(
            non_ascii.do_grep(BytesIO(latin_1_foo), encoding="latin1"),
            [(0, 0, "Rémy\n", [(1, 2)])],
        )

        self.assertEqual(
            non_ascii.do_grep(BytesIO(utf_8_foo), encoding="latin1"), [])

        # Fallback to latin1
        self.assertEqual(
            non_ascii.do_grep(BytesIO(latin_1_foo), encoding="utf8"),
            [(0, 0, "Rémy\n", [(1, 2)])],
        )
コード例 #10
0
ファイル: test_grep.py プロジェクト: ksamuel/grin
def test_1_line_of_before_context_no_lines_before():

    gt_after_context_1 = grin.GrepText(re.compile(b"foo"),
                                       options=grin.Options(before_context=0,
                                                            after_context=1))
    assert gt_after_context_1.do_grep(BytesIO(all_foo)) == [
        (0, 0, b"foo\n", [(0, 3)]),
        (1, 0, b"foo\n", [(0, 3)]),
        (2, 0, b"foo\n", [(0, 3)]),
        (3, 0, b"foo\n", [(0, 3)]),
        (4, 0, b"foo\n", [(0, 3)]),
    ]
    assert gt_after_context_1.do_grep(BytesIO(first_foo)) == [
        (0, 0, b"foo\n", [(0, 3)]),
        (1, 1, b"bar\n", None),
    ]
    assert gt_after_context_1.do_grep(BytesIO(last_foo)) == [(4, 0, b"foo\n",
                                                              [(0, 3)])]
    assert gt_after_context_1.do_grep(BytesIO(second_foo)) == [
        (1, 0, b"foo\n", [(0, 3)]),
        (2, 1, b"bar\n", None),
    ]
    assert gt_after_context_1.do_grep(BytesIO(second_last_foo)) == [
        (3, 0, b"foo\n", [(0, 3)]),
        (4, 1, b"bar\n", None),
    ]
    assert gt_after_context_1.do_grep(BytesIO(middle_foo)) == [
        (2, 0, b"foo\n", [(0, 3)]),
        (3, 1, b"bar\n", None),
    ]
    assert gt_after_context_1.do_grep(BytesIO(small_gap)) == [
        (2, 0, b"foo\n", [(0, 3)]),
        (3, 1, b"bar\n", None),
        (4, 0, b"foo\n", [(0, 3)]),
        (5, 1, b"bar\n", None),
    ]
    assert gt_after_context_1.do_grep(BytesIO(no_eol)) == [(0, 0, b"foo",
                                                            [(0, 3)])]
    assert gt_after_context_1.do_grep(BytesIO(middle_of_line)) == [
        (2, 0, b"barfoobar\n", [(3, 6)]),
        (3, 1, b"bar\n", None),
    ]
コード例 #11
0
def grinimports_main(argv=None):
    if argv is None:
        # Look at the GRIN_ARGS environment variable for more arguments.
        env_args = shlex.split(os.getenv("GRIN_ARGS", ""))
        argv = [sys.argv[0]] + env_args + sys.argv[1:]
    parser = get_grinimports_arg_parser()
    args = parser.parse_args(argv[1:])
    if args.context is not None:
        args.before_context = args.context
        args.after_context = args.context
    _isatty = not args.no_color and sys.stdout.isatty() and (
        os.environ.get("TERM") != "dumb")
    args.use_color = args.force_color or _isatty

    regex = grin.get_regex(args)
    g = grin.GrepText(regex, args)
    for filename, kind in grin.get_filenames(args):
        if kind == "text":
            # Ignore gzipped files.
            report = g.grep_a_file(filename, opener=normalize_file)
            sys.stdout.write(report)
コード例 #12
0
ファイル: grinpython.py プロジェクト: suliveevil/grin
def grinpython_main(argv=None):
    if argv is None:
        # Look at the GRIN_ARGS environment variable for more arguments.
        env_args = shlex.split(os.getenv('GRIN_ARGS', ''))
        argv = [sys.argv[0]] + env_args + sys.argv[1:]
    parser = get_grinpython_arg_parser()
    args = parser.parse_args(argv[1:])
    if args.context is not None:
        args.before_context = args.context
        args.after_context = args.context
    args.use_color = args.force_color or (not args.no_color
                                          and sys.stdout.isatty() and
                                          (os.environ.get('TERM') != 'dumb'))

    xform = Transformer(args.python_code, args.comments, args.strings)

    regex = grin.get_regex(args)
    g = grin.GrepText(regex, args)
    for filename, kind in grin.get_filenames(args):
        if kind == 'text':
            # Ignore gzipped files.
            report = g.grep_a_file(filename, opener=xform)
            sys.stdout.write(report)
コード例 #13
0
    def test_symmetric_two_line_context(self):
        # Symmetric 2-line context.

        gt_context_2 = grin.GrepText(re.compile("foo"),
                                     options=grin.Options(before_context=2,
                                                          after_context=2))
        self.assertEqual(
            gt_context_2.do_grep(BytesIO(all_foo)),
            [
                (0, 0, "foo\n", [(0, 3)]),
                (1, 0, "foo\n", [(0, 3)]),
                (2, 0, "foo\n", [(0, 3)]),
                (3, 0, "foo\n", [(0, 3)]),
                (4, 0, "foo\n", [(0, 3)]),
            ],
        )
        self.assertEqual(
            gt_context_2.do_grep(BytesIO(first_foo)),
            [(0, 0, "foo\n", [(0, 3)]), (1, 1, "bar\n", None),
             (2, 1, "bar\n", None)],
        )
        self.assertEqual(
            gt_context_2.do_grep(BytesIO(last_foo)),
            [(2, -1, "bar\n", None), (3, -1, "bar\n", None),
             (4, 0, "foo\n", [(0, 3)])],
        )
        self.assertEqual(
            gt_context_2.do_grep(BytesIO(second_foo)),
            [(0, -1, "bar\n", None), (1, 0, "foo\n", [(0, 3)]),
             (2, 1, "bar\n", None), (3, 1, "bar\n", None)],
        )
        self.assertEqual(
            gt_context_2.do_grep(BytesIO(second_last_foo)),
            [(1, -1, "bar\n", None), (2, -1, "bar\n", None),
             (3, 0, "foo\n", [(0, 3)]), (4, 1, "bar\n", None)],
        )
        self.assertEqual(
            gt_context_2.do_grep(BytesIO(middle_foo)),
            [
                (0, -1, "bar\n", None),
                (1, -1, "bar\n", None),
                (2, 0, "foo\n", [(0, 3)]),
                (3, 1, "bar\n", None),
                (4, 1, "bar\n", None),
            ],
        )
        self.assertEqual(
            gt_context_2.do_grep(BytesIO(small_gap)),
            [
                (0, -1, "bar\n", None),
                (1, -1, "bar\n", None),
                (2, 0, "foo\n", [(0, 3)]),
                (3, 1, "bar\n", None),
                (4, 0, "foo\n", [(0, 3)]),
                (5, 1, "bar\n", None),
                (6, 1, "bar\n", None),
            ],
        )
        self.assertEqual(gt_context_2.do_grep(BytesIO(no_eol)),
                         [(0, 0, "foo", [(0, 3)])])
        self.assertEqual(
            gt_context_2.do_grep(BytesIO(middle_of_line)),
            [
                (0, -1, "bar\n", None),
                (1, -1, "bar\n", None),
                (2, 0, "barfoobar\n", [(3, 6)]),
                (3, 1, "bar\n", None),
                (4, 1, "bar\n", None),
            ],
        )
コード例 #14
0
ファイル: logagent.py プロジェクト: jph98/gridpy
def grep():

    if request is None or request.form is None:
        return render_template('list.html',
                               error='no search expression specified')

    # Validate the form inputs
    if request.form['expression'] is None or len(
            request.form['expression']) == 0:
        return render_template('list.html',
                               error='no search expression specified')

    expression = request.form['expression']
    expression = expression.strip()

    grepbefore = request.form['grepbefore']
    grepafter = request.form['grepafter']
    validfiles = session.get('validfiles')

    print "Grep (before) " + str(grepbefore)
    print "Grep (after) " + str(grepafter)
    print "Expression (regex) " + expression
    print "Filelist " + str(validfiles)

    options = grin.Options()
    options['before_context'] = int(grepbefore)
    options['after_context'] = int(grepbefore)
    options['use_color'] = False
    options['show_filename'] = False
    options['show_match'] = True
    options['show_emacs'] = False
    options['show_line_numbers'] = True

    print "Grin Options: " + str(options)

    output = ""
    filepaths = []

    # TODO: Fix regular epxressions
    searchregexp = re.compile(expression)

    grindef = grin.GrepText(searchregexp, options)
    anchorcount = 1
    for file in validfiles:
        filepath = validfiles.get(file)[0]
        report = grindef.grep_a_file(filepath)
        if report:

            # Generate the anchor tag
            output += '<a name="filename' + str(
                anchorcount) + '"></a><h2>' + filepath + '</h2>'

            filepaths.append(filepath)
            reporttext = report.split("\n")
            for text in reporttext:
                if text:
                    output += "line " + text + "<br>"
            anchorcount += 1

    # Return error msg if no results found
    if not output:
        return render_template('list.html',
                               error='No results found for search expression')

    # Decode the output
    encodedoutput = output.decode('utf-8')

    # Highlight the matches
    expression = expression.decode('utf-8')

    highlight = '<span class="highlightmatch">' + expression + '</span>'

    highlightedoutput = encodedoutput.replace(expression, highlight)

    return render_template('results.html',
                           output=highlightedoutput,
                           filepaths=filepaths,
                           expression=expression)