<s> """<STR_LIT>""" <EOL> import binascii <EOL> import os <EOL> import sys <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Error ( Exception ) : <EOL> pass <EOL> def encode ( in_file , out_file , name = None , mode = None ) : <EOL> """<STR_LIT>""" <EOL> if in_file == '<STR_LIT:->' : <EOL> in_file = sys . stdin <EOL> elif isinstance ( in_file , basestring ) : <EOL> if name is None : <EOL> name = os . path . basename ( in_file ) <EOL> if mode is None : <EOL> try : <EOL> mode = os . stat ( in_file ) . st_mode <EOL> except AttributeError : <EOL> pass <EOL> in_file = open ( in_file , '<STR_LIT:rb>' ) <EOL> if out_file == '<STR_LIT:->' : <EOL> out_file = sys . stdout <EOL> elif isinstance ( out_file , basestring ) : <EOL> out_file = open ( out_file , '<STR_LIT:w>' ) <EOL> if name is None : <EOL> name = '<STR_LIT:->' <EOL> if mode is None : <EOL> mode = <NUM_LIT:0> <NUM_LIT> <EOL> out_file . write ( '<STR_LIT>' % ( ( mode & <NUM_LIT:0> <NUM_LIT> ) , name ) ) <EOL> data = in_file . read ( <NUM_LIT> ) <EOL> while len ( data ) > <NUM_LIT:0> : <EOL> out_file . write ( binascii . b2a_uu ( data ) ) <EOL> data = in_file . read ( <NUM_LIT> ) <EOL> out_file . write ( '<STR_LIT>' ) <EOL> def decode ( in_file , out_file = None , mode = None , quiet = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if in_file == '<STR_LIT:->' : <EOL> in_file = sys . stdin <EOL> elif isinstance ( in_file , basestring ) : <EOL> in_file = open ( in_file ) <EOL> while True : <EOL> hdr = in_file . readline ( ) <EOL> if not hdr : <EOL> raise Error ( '<STR_LIT>' ) <EOL> if not hdr . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> hdrfields = hdr . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> if len ( hdrfields ) == <NUM_LIT:3> and hdrfields [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> try : <EOL> int ( hdrfields [ <NUM_LIT:1> ] , <NUM_LIT:8> ) <EOL> break <EOL> except ValueError : <EOL> pass <EOL> if out_file is None : <EOL> out_file = hdrfields [ <NUM_LIT:2> ] . rstrip ( ) <EOL> if os . path . exists ( out_file ) : <EOL> raise Error ( '<STR_LIT>' % out_file ) <EOL> if mode is None : <EOL> mode = int ( hdrfields [ <NUM_LIT:1> ] , <NUM_LIT:8> ) <EOL> opened = False <EOL> if out_file == '<STR_LIT:->' : <EOL> out_file = sys . stdout <EOL> elif isinstance ( out_file , basestring ) : <EOL> fp = open ( out_file , '<STR_LIT:wb>' ) <EOL> try : <EOL> os . path . chmod ( out_file , mode ) <EOL> except AttributeError : <EOL> pass <EOL> out_file = fp <EOL> opened = True <EOL> s = in_file . readline ( ) <EOL> while s and s . strip ( ) != '<STR_LIT:end>' : <EOL> try : <EOL> data = binascii . a2b_uu ( s ) <EOL> except binascii . Error , v : <EOL> nbytes = ( ( ( ord ( s [ <NUM_LIT:0> ] ) - <NUM_LIT:32> ) & <NUM_LIT> ) * <NUM_LIT:4> + <NUM_LIT:5> ) // <NUM_LIT:3> <EOL> data = binascii . a2b_uu ( s [ : nbytes ] ) <EOL> if not quiet : <EOL> sys . stderr . write ( "<STR_LIT>" % v ) <EOL> out_file . write ( data ) <EOL> s = in_file . readline ( ) <EOL> if not s : <EOL> raise Error ( '<STR_LIT>' ) <EOL> if opened : <EOL> out_file . close ( ) <EOL> def test ( ) : <EOL> """<STR_LIT>""" <EOL> import optparse <EOL> parser = optparse . OptionParser ( usage = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , help = '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:text>' , help = '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> if len ( args ) > <NUM_LIT:2> : <EOL> parser . error ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> input = sys . stdin <EOL> output = sys . stdout <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> input = args [ <NUM_LIT:0> ] <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> output = args [ <NUM_LIT:1> ] <EOL> if options . decode : <EOL> if options . text : <EOL> if isinstance ( output , basestring ) : <EOL> output = open ( output , '<STR_LIT:w>' ) <EOL> else : <EOL> print sys . argv [ <NUM_LIT:0> ] , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> decode ( input , output ) <EOL> else : <EOL> if options . text : <EOL> if isinstance ( input , basestring ) : <EOL> input = open ( input , '<STR_LIT:r>' ) <EOL> else : <EOL> print sys . argv [ <NUM_LIT:0> ] , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> encode ( input , output ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
