Package Bio :: Module File
[hide private]
[frames] | no frames]

Source Code for Module Bio.File

  1  # Copyright 1999 by Jeffrey Chang.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  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  # For with statement in Python 2.5 
 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
61 62 -class UndoHandle(object):
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 """
72 - def __init__(self, handle):
73 self._handle = handle 74 self._saved = []
75
76 - def __iter__(self):
77 return self
78
79 - def next(self):
80 next = self.readline() 81 if not next: 82 raise StopIteration 83 return next
84
85 - def readlines(self, *args, **keywds):
86 lines = self._saved + self._handle.readlines(*args,**keywds) 87 self._saved = [] 88 return lines
89
90 - def readline(self, *args, **keywds):
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
113 - def saveline(self, line):
114 if line: 115 self._saved = [line] + self._saved
116
117 - def peekline(self):
118 if self._saved: 119 line = self._saved[0] 120 else: 121 line = self._handle.readline() 122 self.saveline(line) 123 return line
124
125 - def tell(self):
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
134 - def __getattr__(self, attr):
135 return getattr(self._handle, attr)
136
137 - def __enter__(self):
138 return self
139
140 - def __exit__(self, type, value, traceback):
141 self._handle.close()
142
143 144 # I could make this faster by using cStringIO. 145 # However, cStringIO (in v1.52) does not implement the 146 # readlines method. 147 -class StringHandle(StringIO.StringIO):
148 - def __init__(self, buffer=''):
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 #This isn't available on Python 3, but we don't care much as SGMLStripper 158 #is obsolete 159 pass 160 else:
161 - class SGMLStripper(object):
162 """Object to strip SGML tags (OBSOLETE)."""
163 - class MyParser(sgmllib.SGMLParser):
164 - def __init__(self):
165 sgmllib.SGMLParser.__init__(self) 166 self.data = ''
167 - def handle_data(self, data):
168 self.data = self.data + data
169
170 - def __init__(self):
171 import warnings 172 import Bio 173 warnings.warn("This class is deprecated, and is likely to be removed in a future version of Biopython", Bio.BiopythonDeprecationWarning) 174 self._parser = SGMLStripper.MyParser()
175
176 - def strip(self, str):
177 """S.strip(str) -> string 178 179 Strip the SGML tags from str. 180 181 """ 182 if not str: # empty string, don't do anything. 183 return '' 184 # I need to make sure that I don't return an empty string if 185 # the buffer is not empty. This can happen if there's a newline 186 # character embedded within a tag. Thus, I'll first check to 187 # see if the last character is a newline. If it is, and it's stripped 188 # away, I'll add it back. 189 is_newline = str[-1] in ['\n', '\r'] 190 191 self._parser.data = '' # clear the parser's data (don't reset) 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