# me2/plugs/not.py
#
#

""" negative grep. """

## me2 imports

from me2.lib.examples import examples
from me2.lib.commands import cmnds
from me2.utils.generic import waitforqueue

## basic imports

import getopt
import re

## not command

def handle_not(bot, ievent):
    """ negative grep. """
    if not ievent.inqueue:
        ievent.reply('use not in a pipeline')
        return
    if not ievent.rest:
        ievent.reply('not <txt>')
        return
    try: (options, rest) = getopt.getopt(ievent.args, 'r')
    except getopt.GetoptError, ex:
        ievent.reply(str(ex))
        return
    result = waitforqueue(ievent.inqueue, 10)
    if not result:
        ievent.reply('no data to grep on')
        return
    doregex = False
    for i, j in options:
        if i == '-r': doregex = True
    res = []
    if doregex:
        try: reg = re.compile(' '.join(rest))
        except Exception, ex:
            ievent.reply("can't compile regex: %s" % str(ex))
            return
        for i in result:
            if not re.search(reg, i): res.append(i)
    else:
        for i in result:
            if ievent.rest not in str(i): res.append(i)
    if not res: ievent.reply('no result')
    else: ievent.reply('results', res)

cmnds.add('not', handle_not, ['USER', 'GUEST'])
examples.add('not', 'reverse grep used in pipelines', 'list | not todo')
