From 98a313305f0d554a179b93695d333199feb5266c Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 24 Nov 2010 19:36:34 +0100 Subject: RefLog: added entry_at method, which is a faster way of reading single entries, including test --- refs/log.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'refs/log.py') diff --git a/refs/log.py b/refs/log.py index 129803b4..6c734ad4 100644 --- a/refs/log.py +++ b/refs/log.py @@ -173,6 +173,37 @@ class RefLog(list, Serializable): return yield new_entry(line.strip()) #END endless loop + + @classmethod + def entry_at(cls, filepath, index): + """:return: RefLogEntry at the given index + :param filepath: full path to the index file from which to read the entry + :param index: python list compatible index, i.e. it may be negative to + specifiy an entry counted from the end of the list + + :raise IndexError: If the entry didn't exist + .. note:: This method is faster as it only parses the entry at index, skipping + all other lines. Nonetheless, the whole file has to be read if + the index is negative + """ + fp = open(filepath, 'rb') + if index < 0: + return RefLogEntry.from_line(fp.readlines()[index].strip()) + else: + # read until index is reached + for i in xrange(index+1): + line = fp.readline() + if not line: + break + #END abort on eof + #END handle runup + + if i != index or not line: + raise IndexError + #END handle exception + + return RefLogEntry.from_line(line.strip()) + #END handle index def to_file(self, filepath): """Write the contents of the reflog instance to a file at the given filepath. -- cgit v1.2.1