From e4057d80a432fc121b7824838640dc6d7b5cb4e7 Mon Sep 17 00:00:00 2001 From: rgalonso Date: Thu, 5 Apr 2018 12:34:50 -0400 Subject: [PATCH 1/2] optional support for binary files Added an optional argument to constructor to specify the mode argument to the file open command. Defaults to 'r', for backwards-compatibility. The specific use case this addresses is supporting binary-encoded encoded files by specifying the mode as 'rb'. While using this module to parse pcap files generated by Wireshark, I found this to be a necessary change. --- tail.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tail.py b/tail.py index 24f69d0..3351d87 100644 --- a/tail.py +++ b/tail.py @@ -28,7 +28,7 @@ class Tail(object): ''' Represents a tail command. ''' - def __init__(self, tailed_file): + def __init__(self, tailed_file, open_mode = 'r'): ''' Initiate a Tail instance. Check for file validity, assigns callback function to standard out. @@ -38,6 +38,7 @@ def __init__(self, tailed_file): self.check_file_validity(tailed_file) self.tailed_file = tailed_file self.callback = sys.stdout.write + self.open_mode = open_mode def follow(self, s=1): ''' Do a tail follow. If a callback function is registered it is called with every new line. @@ -46,9 +47,9 @@ def follow(self, s=1): Arguments: s - Number of seconds to wait between each iteration; Defaults to 1. ''' - with open(self.tailed_file) as file_: + with open(self.tailed_file, self.open_mode) as file_: # Go to the end of file - file_.seek(0,2) + file_.seek(0,0) while True: curr_position = file_.tell() line = file_.readline() From 4c25450d4023e01ad2df03db932320c76ae200da Mon Sep 17 00:00:00 2001 From: rgalonso Date: Thu, 5 Apr 2018 12:39:19 -0400 Subject: [PATCH 2/2] start at end of file (original behavior) Last commit accidentally checked in a debug change where I started the initial seek at the beginning of the file instead of the end. Restoring this to the end so that the only delta is just the binary support intended. --- tail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tail.py b/tail.py index 3351d87..fa206a1 100644 --- a/tail.py +++ b/tail.py @@ -49,7 +49,7 @@ def follow(self, s=1): with open(self.tailed_file, self.open_mode) as file_: # Go to the end of file - file_.seek(0,0) + file_.seek(0,2) while True: curr_position = file_.tell() line = file_.readline()