#!/usr/bin/env python
import re
from circuits.io import File
from circuits import Event, Component
SEEK_END = 2
LINESEP = re.compile("\r?\n")
def splitLines(s, buffer):
lines = LINESEP.split(buffer + s)
return lines[:-1], lines[-1]
class Follow(Component):
def __init__(self, filename):
super(Follow, self).__init__()
fd = File(filename, "r")
fd.seek(0, SEEK_END)
fd.register(self)
class LineBuffer(Component):
def __init__(self):
super(LineBuffer, self).__init__()
self._data = ""
def read(self, data):
lines, self._data = splitLines(data, self._data)
for line in lines:
self.push(Event(line), "line", self.channel)
class Grep(Component):
def __init__(self, pattern):
super(Grep, self).__init__()
self._pattern = pattern
def line(self, line):
if self._pattern in line:
print line
(Follow("/tmp/foo") + LineBuffer() + Grep("pants")).run()