1 | # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) |
---|
2 | # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php |
---|
3 | |
---|
4 | class FileMixin(object): |
---|
5 | |
---|
6 | """ |
---|
7 | Used to provide auxiliary methods to objects simulating files. |
---|
8 | Objects must implement write, and read if they are input files. |
---|
9 | Also they should implement close. |
---|
10 | |
---|
11 | Other methods you may wish to override: |
---|
12 | * flush() |
---|
13 | * seek(offset[, whence]) |
---|
14 | * tell() |
---|
15 | * truncate([size]) |
---|
16 | |
---|
17 | Attributes you may wish to provide: |
---|
18 | * closed |
---|
19 | * encoding (you should also respect that in write()) |
---|
20 | * mode |
---|
21 | * newlines (hard to support) |
---|
22 | * softspace |
---|
23 | """ |
---|
24 | |
---|
25 | def flush(self): |
---|
26 | pass |
---|
27 | |
---|
28 | def next(self): |
---|
29 | return self.readline() |
---|
30 | |
---|
31 | def readline(self, size=None): |
---|
32 | # @@: This is a lame implementation; but a buffer would probably |
---|
33 | # be necessary for a better implementation |
---|
34 | output = [] |
---|
35 | while 1: |
---|
36 | next = self.read(1) |
---|
37 | if not next: |
---|
38 | return ''.join(output) |
---|
39 | output.append(next) |
---|
40 | if size and size > 0 and len(output) >= size: |
---|
41 | return ''.join(output) |
---|
42 | if next == '\n': |
---|
43 | # @@: also \r? |
---|
44 | return ''.join(output) |
---|
45 | |
---|
46 | def xreadlines(self): |
---|
47 | return self |
---|
48 | |
---|
49 | def writelines(self, lines): |
---|
50 | for line in lines: |
---|
51 | self.write(line) |
---|
52 | |
---|
53 | |
---|