Hi Andrew,<br><br>I wrote a class that follows a file (eg. log file) and provides an iterator to walk through it. Don't know if it may be of any use for you (or others).<br><br><span style="font-family: courier new,monospace;">
class FileFollower(object):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> """Iterate through a file while it is updated.</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> >>> file = FileFollower("/tmp/testfile")</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
>>> file.interval = 5</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> >>> for line in file:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ... print line</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> """</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> interval = 1</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
def __init__(self, filename, interval=None):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> self.filename = filename</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.interval = interval or self.interval</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> self.stat
= None</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> self.offset = 0</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
self.lines = []</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> self.running = True</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> #</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # File following</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def follow(self):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
while self.running:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if self.hasChanged():</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
data = self.readChange()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if data:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.dataReceived(data)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> break
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> time.sleep(self.interval)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def hasChanged(self):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> stat = os.stat(self.filename)</span>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if stat != self.stat:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
self.stat = stat</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return True</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
return False</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def readChange(self):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> file = open(self.filename)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> file.seek(self.offset)</span>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> data = file.read()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
self.offset = file.tell()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> file.close()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
return data</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # Data buffering</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
def dataReceived(self, data):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> lines = data.split(os.linesep)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> lines = lines[:-1]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for line in lines:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.lineReceived(line)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
def lineReceived(self, line):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> self.lines.append(line)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # Iterator implementation
</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def __iter__(self):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return self</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
def next(self):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if not self.lines:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
self.follow()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> line = self.lines.pop(0)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
return line</span><br><br><br><div><span class="gmail_quote">2007/8/5, Andrew E <<a href="mailto:andrew@ellerton.net">andrew@ellerton.net</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Dear Twisted Experts (... meant in a nice way :) )<br><br>I'm not sure where to start.<br><br>I need to write a small server that:<br><br> - reads lines in a log file as they are appended<br> - reads input from a socket as it becomes available
<br> - does an analysis of both (like, what time was input received in the<br>log, and the output received via the socket)<br> - outputs a summary report<br><br>Socket I/O is easy - but I'm not sure how to include file reading ...
<br>its bound to be easy.<br><br>Any tips?<br><br>Thanks :)<br><br>Andrew<br><br>_______________________________________________<br>Twisted-Python mailing list<br><a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com
</a><br><a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br></blockquote></div><br>