<s> """<STR_LIT>""" <EOL> import contextlib <EOL> from contextlib import contextmanager <EOL> from io import StringIO <EOL> import logging <EOL> import tempfile <EOL> from openrcv import utils <EOL> from openrcv . utils import logged_open , NoImplementation , ReprMixin <EOL> log = logging . getLogger ( __name__ ) <EOL> def tracked ( source , iterable ) : <EOL> """<STR_LIT>""" <EOL> for i , item in enumerate ( iterable , start = <NUM_LIT:1> ) : <EOL> try : <EOL> yield item <EOL> except Exception as exc : <EOL> raise type ( exc ) ( "<STR_LIT>" % ( source , i , item ) ) <EOL> @ utils . coroutine <EOL> def _sink ( write , target ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> item = yield <EOL> write ( target , item ) <EOL> @ utils . coroutine <EOL> def converting_pipe ( convert , target ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> item = ( yield ) <EOL> item = convert ( item ) <EOL> target . send ( item ) <EOL> class StreamResourceMixin ( ReprMixin ) : <EOL> @ classmethod <EOL> def create ( cls , * args , ** kwargs ) : <EOL> return cls ( * args , ** kwargs ) <EOL> def copy ( self ) : <EOL> raise NoImplementation ( self ) <EOL> def move ( self , dest ) : <EOL> """<STR_LIT>""" <EOL> raise NoImplementation ( self ) <EOL> def count ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . reading ( ) as stream : <EOL> return sum ( <NUM_LIT:1> for item in stream ) <EOL> @ contextmanager <EOL> def replacement ( self ) : <EOL> """<STR_LIT>""" <EOL> temp_resource = self . copy ( ) <EOL> try : <EOL> yield temp_resource <EOL> except : <EOL> temp_resource . close ( ) <EOL> raise <EOL> else : <EOL> temp_resource . move ( dest = self ) <EOL> class NullStreamResource ( StreamResourceMixin ) : <EOL> """<STR_LIT>""" <EOL> @ contextmanager <EOL> def reading ( self ) : <EOL> yield iter ( ( ) ) <EOL> @ contextmanager <EOL> def writing ( self ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> class StreamResourceBase ( StreamResourceMixin ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def make_temp ( cls ) : <EOL> raise NoImplementation ( cls ) <EOL> def _delete ( self ) : <EOL> raise NoImplementation ( cls ) <EOL> @ contextmanager <EOL> def open_read ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NoImplementation ( self ) <EOL> def write ( self , f , item ) : <EOL> raise NoImplementation ( self ) <EOL> @ contextmanager <EOL> def open_write ( self ) : <EOL> raise NoImplementation ( self ) <EOL> @ contextmanager <EOL> def reading ( self ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( "<STR_LIT>" % self ) <EOL> with self . open_read ( ) as f : <EOL> try : <EOL> gen = tracked ( self , f ) <EOL> try : <EOL> yield gen <EOL> except Exception as exc : <EOL> gen . throw ( exc ) <EOL> finally : <EOL> gen . close ( ) <EOL> @ contextmanager <EOL> def writing ( self ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( "<STR_LIT>" % self ) <EOL> with self . open_write ( ) as stream : <EOL> gen = _sink ( self . write , stream ) <EOL> try : <EOL> yield gen <EOL> finally : <EOL> gen . close ( ) <EOL> class ListResource ( StreamResourceBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , seq = None ) : <EOL> """<STR_LIT>""" <EOL> if seq is None : <EOL> seq = [ ] <EOL> self . _seq = seq <EOL> def copy ( self ) : <EOL> new_seq = self . _seq . copy ( ) <EOL> return self . create ( seq = new_seq ) <EOL> def move ( self , dest ) : <EOL> dest . _seq = self . _seq <EOL> def delete ( self ) : <EOL> self . _seq . clear ( ) <EOL> @ classmethod <EOL> def make_temp ( cls ) : <EOL> return cls ( ) <EOL> @ classmethod <EOL> @ contextmanager <EOL> def temp ( cls ) : <EOL> temp_resource = cls . make_temp ( ) <EOL> try : <EOL> yield temp_resource <EOL> finally : <EOL> temp_resource . delete ( ) <EOL> @ contextmanager <EOL> def replacement ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . temp ( ) as temp_resource : <EOL> try : <EOL> yield temp_resource <EOL> finally : <EOL> self . _seq [ : ] = temp_resource . _seq <EOL> @ contextmanager <EOL> def open_read ( self ) : <EOL> yield iter ( self . _seq ) <EOL> def write ( self , target , item ) : <EOL> target . append ( item ) <EOL> @ contextmanager <EOL> def open_write ( self ) : <EOL> self . delete ( ) <EOL> yield self . _seq <EOL> class FilePathResource ( StreamResourceBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path , encoding = None , ** kwargs ) : <EOL> if encoding is None : <EOL> encoding = '<STR_LIT:ascii>' <EOL> self . path = path <EOL> self . encoding = encoding <EOL> self . kwargs = kwargs <EOL> @ classmethod <EOL> def make_temp ( cls ) : <EOL> return tempfile . SpooledTemporaryFile ( encoding = self . encoding ) <EOL> @ contextmanager <EOL> def _open ( self , mode ) : <EOL> with logged_open ( self . path , mode , encoding = self . encoding , ** self . kwargs ) as f : <EOL> yield f <EOL> def open_read ( self ) : <EOL> return self . _open ( "<STR_LIT:r>" ) <EOL> def write ( self , f , item ) : <EOL> f . write ( item ) <EOL> def open_write ( self ) : <EOL> return self . _open ( "<STR_LIT:w>" ) <EOL> class _ReadWriteFileBase ( StreamResourceBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file_ ) : <EOL> self . file = file_ <EOL> def repr_info ( self ) : <EOL> return "<STR_LIT>" . format ( self . encoding , self . file ) <EOL> @ classmethod <EOL> def make_temp ( cls ) : <EOL> return tempfile . NamedTemporaryFile ( mode = '<STR_LIT>' , encoding = '<STR_LIT:utf-8>' ) <EOL> def _open ( self ) : <EOL> raise NoImplementation ( self ) <EOL> @ contextmanager <EOL> def open_read ( self ) : <EOL> self . _open ( ) <EOL> yield self . file <EOL> def write ( self , f , item ) : <EOL> f . write ( item ) <EOL> @ contextmanager <EOL> def open_write ( self ) : <EOL> self . _open ( ) <EOL> self . file . truncate ( ) <EOL> yield self . file <EOL> def close ( self ) : <EOL> try : <EOL> close = self . file . close <EOL> except AttributeError : <EOL> if self . file is not None : <EOL> raise <EOL> else : <EOL> close ( ) <EOL> class ReadWriteFileResource ( _ReadWriteFileBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file_ , encoding = None ) : <EOL> self . encoding = encoding <EOL> self . file = file_ <EOL> def _open ( self ) : <EOL> self . file . seek ( <NUM_LIT:0> ) <EOL> class TempFileResource ( _ReadWriteFileBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , encoding = None ) : <EOL> if encoding is None : <EOL> encoding = '<STR_LIT:ascii>' <EOL> self . encoding = encoding <EOL> self . file = None <EOL> @ classmethod <EOL> def create ( cls , * args , ** kwargs ) : <EOL> return cls ( * args , ** kwargs ) <EOL> @ classmethod <EOL> def create_temp ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> resource = cls . create ( * args , ** kwargs ) <EOL> return contextlib . closing ( resource ) <EOL> def copy ( self ) : <EOL> return self . create ( encoding = self . encoding ) <EOL> def move ( self , dest ) : <EOL> dest . file = self . file <EOL> def _open ( self ) : <EOL> f = self . file <EOL> try : <EOL> seek = f . seek <EOL> except AttributeError : <EOL> f = tempfile . SpooledTemporaryFile ( mode = '<STR_LIT>' , encoding = self . encoding ) <EOL> self . file = f <EOL> seek = f . seek <EOL> seek ( <NUM_LIT:0> ) <EOL> class StandardResource ( StreamResourceBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file_ ) : <EOL> self . file = file_ <EOL> @ contextmanager <EOL> def _open ( self ) : <EOL> yield self . file <EOL> def open_read ( self ) : <EOL> return self . _open ( ) <EOL> def write ( self , f , item ) : <EOL> f . write ( item ) <EOL> def open_write ( self ) : <EOL> return self . _open ( ) <EOL> class StringResource ( StreamResourceBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , contents = None ) : <EOL> self . contents = contents <EOL> @ classmethod <EOL> def make_temp ( cls ) : <EOL> return cls ( ) <EOL> @ contextmanager <EOL> def open_read ( self ) : <EOL> yield StringIO ( self . contents ) <EOL> def write ( self , f , item ) : <EOL> f . write ( item ) <EOL> @ contextmanager <EOL> def open_write ( self ) : <EOL> with StringIO ( ) as f : <EOL> yield f <EOL> self . contents = f . getvalue ( ) <EOL> class WrappedResourceMixin ( StreamResourceMixin ) : <EOL> def repr_info ( self ) : <EOL> return "<STR_LIT>" % self . resource <EOL> def move ( self , dest ) : <EOL> self . resource . move ( dest . resource ) <EOL> class WrapperResource ( WrappedResourceMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource ) : <EOL> """<STR_LIT>""" <EOL> self . resource = resource <EOL> def copy ( self ) : <EOL> new_resource = self . resource . copy ( ) <EOL> return self . create ( resource = new_resource ) <EOL> def make_temp ( self ) : <EOL> temp_resource = self . resource . make_temp ( ) <EOL> return self . __class__ ( temp_resource ) <EOL> def reading ( self ) : <EOL> return self . resource . reading ( ) <EOL> def writing ( self ) : <EOL> return self . resource . writing ( ) <EOL> class Converter ( object ) : <EOL> def from_resource ( self , item ) : <EOL> raise NoImplementation ( self ) <EOL> def to_resource ( self , item ) : <EOL> raise NoImplementation ( self ) <EOL> class ConvertingResource ( WrappedResourceMixin ) : <EOL> def __init__ ( self , resource , converter ) : <EOL> """<STR_LIT>""" <EOL> self . converter = converter <EOL> self . resource = resource <EOL> def copy ( self ) : <EOL> new_resource = self . resource . copy ( ) <EOL> return self . create ( resource = new_resource , converter = self . converter ) <EOL> @ contextmanager <EOL> def reading ( self ) : <EOL> convert = self . converter . from_resource <EOL> with self . resource . reading ( ) as gen : <EOL> yield ( convert ( item ) for item in gen ) <EOL> @ contextmanager <EOL> def writing ( self ) : <EOL> convert = self . converter . to_resource <EOL> with self . resource . writing ( ) as gen : <EOL> new_gen = converting_pipe ( convert , target = gen ) <EOL> try : <EOL> yield new_gen <EOL> finally : <EOL> new_gen . close ( ) </s>
