<s> '''<STR_LIT>''' <EOL> import os <EOL> from gzip import GzipFile <EOL> from bz2 import BZ2File <EOL> from io import BytesIO , UnsupportedOperation <EOL> from . . py3k import asstr , asbytes <EOL> from . . openers import Opener , ImageOpener <EOL> from . . tmpdirs import InTemporaryDirectory <EOL> from . . volumeutils import BinOpener <EOL> from nose . tools import ( assert_true , assert_false , assert_equal , <EOL> assert_not_equal , assert_raises ) <EOL> from . . testing import error_warnings <EOL> class Lunk ( object ) : <EOL> closed = False <EOL> def __init__ ( self , message ) : <EOL> self . message = message <EOL> def write ( self ) : <EOL> pass <EOL> def read ( self ) : <EOL> return self . message <EOL> def test_Opener ( ) : <EOL> fobj = Opener ( __file__ ) <EOL> assert_equal ( fobj . mode , '<STR_LIT:rb>' ) <EOL> fobj . close ( ) <EOL> with Opener ( __file__ ) as fobj : <EOL> assert_equal ( fobj . mode , '<STR_LIT:rb>' ) <EOL> with Opener ( __file__ , '<STR_LIT:r>' ) as fobj : <EOL> assert_equal ( fobj . mode , '<STR_LIT:r>' ) <EOL> with Opener ( __file__ , mode = '<STR_LIT:r>' ) as fobj : <EOL> assert_equal ( fobj . mode , '<STR_LIT:r>' ) <EOL> message = b"<STR_LIT>" <EOL> for obj in ( BytesIO ( message ) , Lunk ( message ) ) : <EOL> with Opener ( obj ) as fobj : <EOL> assert_equal ( fobj . read ( ) , message ) <EOL> assert_false ( obj . closed ) <EOL> fobj = Opener ( obj , mode = '<STR_LIT:r>' ) <EOL> def test_Opener_various ( ) : <EOL> message = b"<STR_LIT>" <EOL> bz2_fileno = hasattr ( BZ2File , '<STR_LIT>' ) <EOL> with InTemporaryDirectory ( ) : <EOL> sobj = BytesIO ( ) <EOL> for input in ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> sobj ) : <EOL> with Opener ( input , '<STR_LIT:wb>' ) as fobj : <EOL> fobj . write ( message ) <EOL> assert_equal ( fobj . tell ( ) , len ( message ) ) <EOL> if input == sobj : <EOL> input . seek ( <NUM_LIT:0> ) <EOL> with Opener ( input , '<STR_LIT:rb>' ) as fobj : <EOL> message_back = fobj . read ( ) <EOL> assert_equal ( message , message_back ) <EOL> if input == sobj : <EOL> assert_raises ( UnsupportedOperation , fobj . fileno ) <EOL> elif input . endswith ( '<STR_LIT>' ) and not bz2_fileno : <EOL> assert_raises ( AttributeError , fobj . fileno ) <EOL> else : <EOL> assert_not_equal ( fobj . fileno ( ) , <NUM_LIT:0> ) <EOL> def test_BinOpener ( ) : <EOL> with error_warnings ( ) : <EOL> assert_raises ( DeprecationWarning , <EOL> BinOpener , '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> class TestImageOpener : <EOL> def setUp ( self ) : <EOL> self . compress_ext_map = ImageOpener . compress_ext_map . copy ( ) <EOL> def teardown ( self ) : <EOL> ImageOpener . compress_ext_map = self . compress_ext_map <EOL> def test_vanilla ( self ) : <EOL> with InTemporaryDirectory ( ) : <EOL> with ImageOpener ( '<STR_LIT>' , '<STR_LIT:w>' ) as fobj : <EOL> assert_true ( hasattr ( fobj . fobj , '<STR_LIT>' ) ) <EOL> with ImageOpener ( '<STR_LIT>' , '<STR_LIT:w>' ) as fobj : <EOL> assert_true ( hasattr ( fobj . fobj , '<STR_LIT>' ) ) <EOL> def test_new_association ( self ) : <EOL> def file_opener ( fileish , mode ) : <EOL> return open ( fileish , mode ) <EOL> n_associations = len ( ImageOpener . compress_ext_map ) <EOL> ImageOpener . compress_ext_map [ '<STR_LIT>' ] = ( file_opener , ( '<STR_LIT>' , ) ) <EOL> assert_equal ( n_associations + <NUM_LIT:1> , len ( ImageOpener . compress_ext_map ) ) <EOL> assert_true ( '<STR_LIT>' in ImageOpener . compress_ext_map ) <EOL> with InTemporaryDirectory ( ) : <EOL> with ImageOpener ( '<STR_LIT>' , '<STR_LIT:w>' ) : <EOL> pass <EOL> assert_true ( os . path . exists ( '<STR_LIT>' ) ) <EOL> assert_false ( '<STR_LIT>' in Opener . compress_ext_map ) <EOL> def test_file_like_wrapper ( ) : <EOL> message = b"<STR_LIT>" <EOL> sobj = BytesIO ( ) <EOL> fobj = Opener ( sobj ) <EOL> assert_equal ( fobj . tell ( ) , <NUM_LIT:0> ) <EOL> fobj . write ( message ) <EOL> assert_equal ( fobj . tell ( ) , len ( message ) ) <EOL> fobj . seek ( <NUM_LIT:0> ) <EOL> assert_equal ( fobj . tell ( ) , <NUM_LIT:0> ) <EOL> assert_equal ( fobj . read ( <NUM_LIT:6> ) , message [ : <NUM_LIT:6> ] ) <EOL> assert_false ( fobj . closed ) <EOL> fobj . close ( ) <EOL> assert_true ( fobj . closed ) <EOL> assert_equal ( fobj . name , None ) <EOL> def test_compressionlevel ( ) : <EOL> with open ( __file__ , '<STR_LIT:rb>' ) as fobj : <EOL> my_self = fobj . read ( ) <EOL> many_selves = my_self * <NUM_LIT:50> <EOL> class MyOpener ( Opener ) : <EOL> default_compresslevel = <NUM_LIT:5> <EOL> with InTemporaryDirectory ( ) : <EOL> for ext in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for opener , default_val in ( ( Opener , <NUM_LIT:1> ) , ( MyOpener , <NUM_LIT:5> ) ) : <EOL> sizes = { } <EOL> for compresslevel in ( '<STR_LIT:default>' , <NUM_LIT:1> , <NUM_LIT:5> ) : <EOL> fname = '<STR_LIT>' + ext <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT:wb>' } <EOL> if compresslevel != '<STR_LIT:default>' : <EOL> kwargs [ '<STR_LIT>' ] = compresslevel <EOL> with opener ( fname , ** kwargs ) as fobj : <EOL> fobj . write ( many_selves ) <EOL> with open ( fname , '<STR_LIT:rb>' ) as fobj : <EOL> my_selves_smaller = fobj . read ( ) <EOL> sizes [ compresslevel ] = len ( my_selves_smaller ) <EOL> assert_equal ( sizes [ '<STR_LIT:default>' ] , sizes [ default_val ] ) <EOL> assert_true ( sizes [ <NUM_LIT:1> ] > sizes [ <NUM_LIT:5> ] ) <EOL> def test_compressed_ext_case ( ) : <EOL> contents = b'<STR_LIT>' <EOL> class StrictOpener ( Opener ) : <EOL> compress_ext_icase = False <EOL> exts = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> with InTemporaryDirectory ( ) : <EOL> with open ( __file__ , '<STR_LIT:rb>' ) as a_file : <EOL> file_class = type ( a_file ) <EOL> for ext in exts : <EOL> fname = '<STR_LIT>' + ext <EOL> with Opener ( fname , '<STR_LIT:wb>' ) as fobj : <EOL> fobj . write ( contents ) <EOL> with Opener ( fname , '<STR_LIT:rb>' ) as fobj : <EOL> assert_equal ( fobj . read ( ) , contents ) <EOL> os . unlink ( fname ) <EOL> with StrictOpener ( fname , '<STR_LIT:wb>' ) as fobj : <EOL> fobj . write ( contents ) <EOL> with StrictOpener ( fname , '<STR_LIT:rb>' ) as fobj : <EOL> assert_equal ( fobj . read ( ) , contents ) <EOL> lext = ext . lower ( ) <EOL> if lext != ext : <EOL> assert_true ( isinstance ( fobj . fobj , file_class ) ) <EOL> elif lext == '<STR_LIT>' : <EOL> assert_true ( isinstance ( fobj . fobj , GzipFile ) ) <EOL> else : <EOL> assert_true ( isinstance ( fobj . fobj , BZ2File ) ) <EOL> def test_name ( ) : <EOL> sobj = BytesIO ( ) <EOL> lunk = Lunk ( '<STR_LIT>' ) <EOL> with InTemporaryDirectory ( ) : <EOL> for input in ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> sobj , <EOL> lunk ) : <EOL> exp_name = input if type ( input ) == type ( '<STR_LIT>' ) else None <EOL> with Opener ( input , '<STR_LIT:wb>' ) as fobj : <EOL> assert_equal ( fobj . name , exp_name ) <EOL> def test_set_extensions ( ) : <EOL> with InTemporaryDirectory ( ) : <EOL> with Opener ( '<STR_LIT>' , '<STR_LIT:w>' ) as fobj : <EOL> assert_true ( hasattr ( fobj . fobj , '<STR_LIT>' ) ) <EOL> with Opener ( '<STR_LIT>' , '<STR_LIT:w>' ) as fobj : <EOL> assert_false ( hasattr ( fobj . fobj , '<STR_LIT>' ) ) <EOL> class MyOpener ( Opener ) : <EOL> compress_ext_map = Opener . compress_ext_map . copy ( ) <EOL> compress_ext_map [ '<STR_LIT>' ] = Opener . gz_def <EOL> with MyOpener ( '<STR_LIT>' , '<STR_LIT:w>' ) as fobj : <EOL> assert_true ( hasattr ( fobj . fobj , '<STR_LIT>' ) ) <EOL> def test_close_if_mine ( ) : <EOL> with InTemporaryDirectory ( ) : <EOL> sobj = BytesIO ( ) <EOL> lunk = Lunk ( '<STR_LIT>' ) <EOL> for input in ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> sobj , <EOL> lunk ) : <EOL> fobj = Opener ( input , '<STR_LIT:wb>' ) <EOL> has_closed = hasattr ( fobj . fobj , '<STR_LIT>' ) <EOL> if has_closed : <EOL> assert_false ( fobj . closed ) <EOL> fobj . close_if_mine ( ) <EOL> is_str = type ( input ) is type ( '<STR_LIT>' ) <EOL> if has_closed : <EOL> assert_equal ( fobj . closed , is_str ) <EOL> def test_iter ( ) : <EOL> lines = """<STR_LIT>""" . split ( '<STR_LIT:\n>' ) <EOL> with InTemporaryDirectory ( ) : <EOL> sobj = BytesIO ( ) <EOL> for input , does_t in ( ( '<STR_LIT>' , True ) , <EOL> ( '<STR_LIT>' , False ) , <EOL> ( '<STR_LIT>' , False ) , <EOL> ( sobj , True ) ) : <EOL> with Opener ( input , '<STR_LIT:wb>' ) as fobj : <EOL> for line in lines : <EOL> fobj . write ( asbytes ( line + os . linesep ) ) <EOL> with Opener ( input , '<STR_LIT:rb>' ) as fobj : <EOL> for back_line , line in zip ( fobj , lines ) : <EOL> assert_equal ( asstr ( back_line ) . rstrip ( ) , line ) <EOL> if not does_t : <EOL> continue <EOL> with Opener ( input , '<STR_LIT>' ) as fobj : <EOL> for back_line , line in zip ( fobj , lines ) : <EOL> assert_equal ( back_line . rstrip ( ) , line ) <EOL> lobj = Opener ( Lunk ( '<STR_LIT>' ) ) <EOL> assert_raises ( TypeError , list , lobj ) </s>
