Ajout de la propriété « duration » sur l'objet musicxml.Part. Modification dans le...
[minwii.git] / src / minwii / backwardsfilereader.py
1 ## {{{ http://code.activestate.com/recipes/439045/ (r2)
2 #!/usr/bin/env python
3 # -*-mode: python; coding: iso-8859-1 -*-
4 #
5 # Copyright (c) Peter Astrand <astrand@cendio.se>
6 # $Id$
7 # $URL$
8
9 import string
10
11 class BackwardsReader:
12 """Read a file line by line, backwards"""
13
14 def readline(self):
15 while 1:
16 newline_pos = string.rfind(self.buf, "\n")
17 pos = self.file.tell()
18 if newline_pos != -1:
19 # Found a newline
20 line = self.buf[newline_pos+1:]
21 self.buf = self.buf[:newline_pos]
22 if pos != 0 or newline_pos != 0 or self.trailing_newline:
23 line += "\n"
24 return line
25 else:
26 if pos == 0:
27 # Start-of-file
28 return ""
29 else:
30 # Need to fill buffer
31 toread = min(self.BLKSIZE, pos)
32 self.file.seek(-toread, 1)
33 self.buf = self.file.read(toread) + self.buf
34 self.file.seek(-toread, 1)
35 if pos - toread == 0:
36 self.buf = "\n" + self.buf
37
38 def __init__(self, file, BLKSIZE=4096):
39 self.file = file
40 self.BLKSIZE = BLKSIZE
41 self.buf = ""
42 self.file.seek(-1, 2)
43 self.trailing_newline = 0
44 lastchar = self.file.read(1)
45 if lastchar == "\n":
46 self.trailing_newline = 1
47 self.file.seek(-1, 2)