117117
118118from six .moves import range
119119import six
120- from contextlib import contextmanager
121120import itertools
122121import os .path
123122import warnings
139138from .. import NoDataError
140139
141140
142- class FH (object ):
143- """Smart file handle object
144-
145- Remembers last known position as `last_pos`
146-
147- Provides two context managers:
148- - from_start
149- - from_last_position
150- """
151- def __init__ (self , filename ):
152- self .filename = filename
153- # last known position of the file handle
154- self .last_pos = 0
155- # where we store the file handle
156- self .fh = None
157-
158- @contextmanager
159- def from_last_position (self , remember_last = False ):
160- fh = self .fh = util .anyopen (self .filename , 'r' )
161- fh .seek (self .last_pos )
162- try :
163- yield fh
164- finally :
165- if remember_last :
166- self .last_pos = fh .tell ()
167- fh .close ()
168-
169- @contextmanager
170- def from_start (self ):
171- fh = self .fh = util .anyopen (self .filename , 'r' )
172- try :
173- yield fh
174- finally :
175- fh .close ()
176-
177-
178141class Timestep (object ):
179142 """Timestep data for one frame
180143
@@ -1371,7 +1334,7 @@ class NewReader(ProtoReader):
13711334 """Super top secret magical class that fixes all known (and unknown) bugs"""
13721335 def __init__ (self , filename , convert_units = None , ** kwargs ):
13731336 self .filename = filename
1374- self ._fh = FH ( filename )
1337+ self ._last_fh_pos = 0 # last known position of file handle
13751338
13761339 if convert_units is None :
13771340 convert_units = flags ['convert_lengths' ]
@@ -1390,15 +1353,15 @@ def __init__(self, filename, convert_units=None, **kwargs):
13901353
13911354 def _update_last_fh_position (self , pos ):
13921355 """Update the last known position of the file handle"""
1393- self ._fh . last_pos = pos
1356+ self ._last_fh_pos = pos
13941357
13951358 def rewind (self ):
13961359 self ._reopen ()
13971360 self .next ()
13981361
13991362 def _full_iter (self ):
14001363 self ._reopen ()
1401- with self . _fh . from_start ( ) as self ._file :
1364+ with util . openany ( self . filename , 'r' ) as self ._file :
14021365 while True :
14031366 try :
14041367 yield self ._read_next_timestep ()
@@ -1407,15 +1370,16 @@ def _full_iter(self):
14071370 raise StopIteration
14081371
14091372 def _sliced_iter (self , frames ):
1410- with self . _fh . from_start ( ) as self ._file :
1373+ with util . openany ( self . filename , 'r' ) as self ._file :
14111374 for f in frames :
14121375 yield self ._read_frame (f )
1413- self .rewind ()
1414- raise StopIteration
1376+ self .rewind ()
1377+ raise StopIteration
14151378
14161379 def _goto_frame (self , i ):
1417- with self ._fh .from_start () as self ._file :
1418- return self ._read_frame (i )
1380+ with util .openany (self .filename , 'r' ) as self ._file :
1381+ ts = self ._read_frame (i )
1382+ return ts
14191383
14201384 def __iter__ (self ):
14211385 return self ._full_iter ()
@@ -1429,7 +1393,6 @@ def apply_limits(frame):
14291393 "" .format (frame , len (self )))
14301394 return frame
14311395
1432-
14331396 if isinstance (item , int ):
14341397 return self ._goto_frame (apply_limits (item ))
14351398 elif isinstance (item , (list , np .ndarray )):
@@ -1438,19 +1401,22 @@ def apply_limits(frame):
14381401 return self ._sliced_iter (item )
14391402
14401403 def __next__ (self ):
1441- return self .next ()
1404+ with util .openany (self .filename , 'r' ) as self ._file :
1405+ try :
1406+ self ._file .seek (self ._last_fh_pos )
1407+ ts = self ._read_next_timestep ()
1408+ except (EOFError , IOError ):
1409+ self .rewind ()
1410+ raise StopIteration
1411+ else :
1412+ self ._last_fh_pos = self ._file .tell ()
1413+ return ts
14421414
1443- def next (self ):
1444- try :
1445- with self ._fh .from_last_position (remember_last = True ) as self ._file :
1446- return self ._read_next_timestep ()
1447- except (EOFError , IOError ):
1448- self .rewind ()
1449- raise StopIteration
1415+ next = __next__
14501416
14511417 def close (self ):
14521418 # "close" by moving last position to start
1453- self ._fh . last_pos = 0
1419+ self ._last_fh_pos = 0
14541420
14551421
14561422class ChainReader (ProtoReader ):
0 commit comments