<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import threading <EOL> from six import StringIO <EOL> class _RWBuf ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , io ) : <EOL> self . _lock = threading . Lock ( ) <EOL> self . _io = io <EOL> self . _readpos = <NUM_LIT:0> <EOL> def read ( self , size = - <NUM_LIT:1> ) : <EOL> with self . _lock : <EOL> self . _io . seek ( self . _readpos ) <EOL> ret = self . _io . read ( ) if size == - <NUM_LIT:1> else self . _io . read ( size ) <EOL> self . _readpos = self . _io . tell ( ) <EOL> return ret <EOL> def read_from ( self , pos , size = - <NUM_LIT:1> ) : <EOL> with self . _lock : <EOL> self . _io . seek ( pos ) <EOL> return self . _io . read ( ) if size == - <NUM_LIT:1> else self . _io . read ( size ) <EOL> def write ( self , s ) : <EOL> with self . _lock : <EOL> self . do_write ( str ( s ) ) <EOL> self . _io . flush ( ) <EOL> def flush ( self ) : <EOL> with self . _lock : <EOL> self . _io . flush ( ) <EOL> def close ( self ) : <EOL> self . _io . close ( ) <EOL> def do_write ( self , s ) : <EOL> raise NotImplementedError <EOL> class InMemoryRWBuf ( _RWBuf ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( InMemoryRWBuf , self ) . __init__ ( StringIO ( ) ) <EOL> self . _writepos = <NUM_LIT:0> <EOL> def do_write ( self , s ) : <EOL> self . _io . seek ( self . _writepos ) <EOL> self . _io . write ( s ) <EOL> self . _writepos = self . _io . tell ( ) <EOL> class FileBackedRWBuf ( _RWBuf ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , backing_file ) : <EOL> _RWBuf . __init__ ( self , open ( backing_file , '<STR_LIT>' ) ) <EOL> self . fileno = self . _io . fileno <EOL> def do_write ( self , s ) : <EOL> self . _io . write ( s ) </s>
