1
2
3
4
5
6 """Code for more fancy file handles.
7
8
9 Classes:
10
11 UndoHandle File object decorator with support for undo-like operations.
12
13 StringHandle Wraps a file object around a string. This is now DEPRECATED,
14 and is likely to be removed in a future release of Biopython.
15
16 SGMLStripper Object that strips SGML. This is now DEPRECATED, and is likely
17 to be removed in a future release of Biopython.
18
19 """
20
21 from __future__ import with_statement
22 import contextlib
23 import StringIO
24
25
26 @contextlib.contextmanager
27 -def as_handle(handleish, mode='r', **kwargs):
28 """
29 Context manager for arguments that can be passed to
30 SeqIO and AlignIO read, write, and parse methods: either file objects or strings.
31
32 When given a string, returns a file handle open to handleish with provided
33 mode which will be closed when the manager exits.
34
35 All other inputs are returned, and are *not* closed
36
37 - handleish - Either a string or file handle
38 - mode - Mode to open handleish (used only if handleish is a string)
39 - kwargs - Further arguments to pass to open(...)
40
41 Example:
42
43 >>> with as_handle('seqs.fasta', 'w') as fp:
44 ... fp.write('>test\nACGT')
45 >>> fp.closed
46 True
47
48 >>> handle = open('seqs.fasta', 'w')
49 >>> with as_handle(handle) as fp:
50 ... fp.write('>test\nACGT')
51 >>> fp.closed
52 False
53 >>> fp.close()
54 """
55 if isinstance(handleish, basestring):
56 with open(handleish, mode, **kwargs) as fp:
57 yield fp
58 else:
59 yield handleish
60
63 """A Python handle that adds functionality for saving lines.
64
65 Saves lines in a LIFO fashion.
66
67 Added methods:
68 saveline Save a line to be returned next time.
69 peekline Peek at the next line without consuming it.
70
71 """
73 self._handle = handle
74 self._saved = []
75
78
84
86 lines = self._saved + self._handle.readlines(*args,**keywds)
87 self._saved = []
88 return lines
89
91 if self._saved:
92 line = self._saved.pop(0)
93 else:
94 line = self._handle.readline(*args,**keywds)
95 return line
96
97 - def read(self, size=-1):
98 if size == -1:
99 saved = "".join(self._saved)
100 self._saved[:] = []
101 else:
102 saved = ''
103 while size > 0 and self._saved:
104 if len(self._saved[0]) <= size:
105 size = size - len(self._saved[0])
106 saved = saved + self._saved.pop(0)
107 else:
108 saved = saved + self._saved[0][:size]
109 self._saved[0] = self._saved[0][size:]
110 size = 0
111 return saved + self._handle.read(size)
112
114 if line:
115 self._saved = [line] + self._saved
116
118 if self._saved:
119 line = self._saved[0]
120 else:
121 line = self._handle.readline()
122 self.saveline(line)
123 return line
124
126 lengths = map(len, self._saved)
127 sum = reduce(lambda x, y: x+y, lengths, 0)
128 return self._handle.tell() - sum
129
130 - def seek(self, *args):
131 self._saved = []
132 self._handle.seek(*args)
133
135 return getattr(self._handle, attr)
136
139
140 - def __exit__(self, type, value, traceback):
142
149 import warnings
150 import Bio
151 warnings.warn("This class is deprecated, and is likely to be removed in a future version of Biopython. Please use the class StringIO in the module StringIO in the Python standard library instead", Bio.BiopythonDeprecationWarning)
152 StringIO.StringIO.__init__(self, buffer)
153
154 try:
155 import sgmllib
156 except ImportError:
157
158
159 pass
160 else:
162 """Object to strip SGML tags (OBSOLETE)."""
169
175
177 """S.strip(str) -> string
178
179 Strip the SGML tags from str.
180
181 """
182 if not str:
183 return ''
184
185
186
187
188
189 is_newline = str[-1] in ['\n', '\r']
190
191 self._parser.data = ''
192 self._parser.feed(str)
193 if self._parser.data:
194 str = self._parser.data
195 elif is_newline:
196 str = '\n'
197 else:
198 str = ''
199 return str
200