<s> from struct import unpack <EOL> CR = '<STR_LIT>' <EOL> LF = '<STR_LIT>' <EOL> IS1 = '<STR_LIT>' <EOL> IS2 = '<STR_LIT>' <EOL> IS3 = '<STR_LIT>' <EOL> LABEL_LEN = <NUM_LIT> <EOL> LABEL_FORMAT = '<STR_LIT>' <EOL> TAG_LEN = <NUM_LIT:3> <EOL> DEFAULT_ENCODING = '<STR_LIT>' <EOL> SUBFIELD_DELIMITER = '<STR_LIT>' <EOL> class IsoFile ( object ) : <EOL> def __init__ ( self , filename , encoding = DEFAULT_ENCODING ) : <EOL> self . file = open ( filename , '<STR_LIT:rb>' ) <EOL> self . encoding = encoding <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> return IsoRecord ( self ) <EOL> __next__ = next <EOL> def read ( self , size ) : <EOL> '''<STR_LIT>''' <EOL> chunks = [ ] <EOL> count = <NUM_LIT:0> <EOL> while count < size : <EOL> chunk = self . file . read ( size - count ) <EOL> if len ( chunk ) == <NUM_LIT:0> : <EOL> break <EOL> chunk = chunk . replace ( CR + LF , '<STR_LIT>' ) <EOL> if CR in chunk : <EOL> chunk = chunk . replace ( CR , '<STR_LIT>' ) <EOL> if LF in chunk : <EOL> chunk = chunk . replace ( LF , '<STR_LIT>' ) <EOL> count += len ( chunk ) <EOL> chunks . append ( chunk ) <EOL> return '<STR_LIT>' . join ( chunks ) <EOL> def close ( self ) : <EOL> self . file . close ( ) <EOL> class IsoRecord ( object ) : <EOL> label_part_names = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . split ( ) <EOL> rec_len = <NUM_LIT:0> <EOL> def __init__ ( self , iso_file = None ) : <EOL> self . iso_file = iso_file <EOL> self . load_label ( ) <EOL> self . load_directory ( ) <EOL> self . load_fields ( ) <EOL> def __len__ ( self ) : <EOL> return self . rec_len <EOL> def load_label ( self ) : <EOL> label = self . iso_file . read ( LABEL_LEN ) <EOL> if len ( label ) == <NUM_LIT:0> : <EOL> raise StopIteration <EOL> elif len ( label ) != <NUM_LIT> : <EOL> raise ValueError ( '<STR_LIT>' % label ) <EOL> parts = unpack ( LABEL_FORMAT , label ) <EOL> for name , part in zip ( self . label_part_names , parts ) : <EOL> if name . endswith ( '<STR_LIT>' ) or name . endswith ( '<STR_LIT>' ) : <EOL> part = int ( part ) <EOL> setattr ( self , name , part ) <EOL> def show_label ( self ) : <EOL> for name in self . label_part_names : <EOL> print ( '<STR_LIT>' % ( name , getattr ( self , name ) ) ) <EOL> def load_directory ( self ) : <EOL> fmt_dir = '<STR_LIT>' % ( self . fld_len_len , self . start_len , self . impl_len ) <EOL> entry_len = TAG_LEN + self . fld_len_len + self . start_len + self . impl_len <EOL> self . directory = [ ] <EOL> while True : <EOL> char = self . iso_file . read ( <NUM_LIT:1> ) <EOL> if char . isdigit ( ) : <EOL> entry = char + self . iso_file . read ( entry_len - <NUM_LIT:1> ) <EOL> entry = Field ( * unpack ( fmt_dir , entry ) ) <EOL> self . directory . append ( entry ) <EOL> else : <EOL> break <EOL> def load_fields ( self ) : <EOL> for field in self . directory : <EOL> if self . indicator_len > <NUM_LIT:0> : <EOL> field . indicator = self . iso_file . read ( self . indicator_len ) <EOL> value = self . iso_file . read ( len ( field ) ) <EOL> assert len ( value ) == len ( field ) <EOL> field . value = value [ : - <NUM_LIT:1> ] <EOL> self . iso_file . read ( <NUM_LIT:1> ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> for field in self . directory : <EOL> yield ( field ) <EOL> __next__ = next <EOL> def dump ( self ) : <EOL> for field in self . directory : <EOL> print ( '<STR_LIT>' % ( field . tag , field . value ) ) <EOL> class Field ( object ) : <EOL> def __init__ ( self , tag , len , start , impl ) : <EOL> self . tag = tag <EOL> self . len = int ( len ) <EOL> self . start = int ( start ) <EOL> self . impl = impl <EOL> def show ( self ) : <EOL> for name in '<STR_LIT>' . split ( ) : <EOL> print ( '<STR_LIT>' % ( name , getattr ( self , name ) ) ) <EOL> def __len__ ( self ) : <EOL> return self . len <EOL> def test ( ) : <EOL> import doctest <EOL> doctest . testfile ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
