<s> """<STR_LIT>""" <EOL> from dwca . exceptions import InvalidArchive <EOL> class Row ( object ) : <EOL> """<STR_LIT>""" <EOL> def _build_str ( self , source_str , id_str ) : <EOL> txt = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> extension_flag = "<STR_LIT>" if ( hasattr ( self , '<STR_LIT>' ) and ( len ( self . extensions ) > <NUM_LIT:0> ) ) else "<STR_LIT>" <EOL> if hasattr ( self , '<STR_LIT>' ) and ( self . source_metadata is not None ) : <EOL> source_metadata_flag = '<STR_LIT>' <EOL> else : <EOL> source_metadata_flag = '<STR_LIT>' <EOL> return txt . format ( rowtype = self . rowtype , <EOL> source_str = source_str , <EOL> data = self . data , <EOL> id_row = id_str , <EOL> extension_flag = extension_flag , <EOL> source_metadata_flag = source_metadata_flag ) <EOL> def __init__ ( self , csv_line , descriptor ) : <EOL> self . descriptor = descriptor <EOL> self . rowtype = self . descriptor . type <EOL> line_ending = self . descriptor . lines_terminated_by <EOL> field_ending = self . descriptor . fields_terminated_by <EOL> fields_enclosed_by = self . descriptor . fields_enclosed_by <EOL> self . raw_fields = [ ] <EOL> for f in csv_line . rstrip ( line_ending ) . split ( field_ending ) : <EOL> self . raw_fields . append ( f . strip ( fields_enclosed_by ) ) <EOL> self . data = { } <EOL> for f in self . descriptor . fields : <EOL> if f [ '<STR_LIT:default>' ] is not None : <EOL> self . data [ f [ '<STR_LIT>' ] ] = f [ '<STR_LIT:default>' ] <EOL> else : <EOL> field_index = int ( f [ '<STR_LIT:index>' ] ) <EOL> try : <EOL> self . data [ f [ '<STR_LIT>' ] ] = self . raw_fields [ field_index ] <EOL> except IndexError : <EOL> msg = '<STR_LIT>' . format ( i = field_index ) <EOL> raise InvalidArchive ( msg ) <EOL> class CoreRow ( Row ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> id_str = "<STR_LIT>" + str ( self . id ) <EOL> return super ( CoreRow , self ) . _build_str ( "<STR_LIT>" , id_str ) <EOL> def __init__ ( self , line , section_descriptor ) : <EOL> super ( CoreRow , self ) . __init__ ( line , section_descriptor ) <EOL> if self . descriptor . id_index is not None : <EOL> self . id = self . raw_fields [ self . descriptor . id_index ] <EOL> else : <EOL> self . id = None <EOL> def link_source_metadata ( self , archive_source_metadata ) : <EOL> field_name = '<STR_LIT>' <EOL> if ( archive_source_metadata and ( field_name in self . data ) ) : <EOL> try : <EOL> m = archive_source_metadata [ self . data [ field_name ] ] <EOL> except KeyError : <EOL> m = None <EOL> else : <EOL> m = None <EOL> self . source_metadata = m <EOL> def link_extension_files ( self , extension_data_files ) : <EOL> self . extension_data_files = extension_data_files <EOL> @ property <EOL> def extensions ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _extensions = [ ] <EOL> for csv in self . extension_data_files : <EOL> [ self . _extensions . append ( r ) for r in csv . get_all_rows_by_coreid ( self . id ) ] <EOL> return self . _extensions <EOL> def __key ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . descriptor , self . id , self . data , self . extensions , self . source_metadata , <EOL> self . rowtype , self . raw_fields ) <EOL> def __eq__ ( self , other ) : <EOL> return self . __key ( ) == other . __key ( ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . __key ( ) ) <EOL> class ExtensionRow ( Row ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> id_str = "<STR_LIT>" + str ( self . core_id ) <EOL> return super ( ExtensionRow , self ) . _build_str ( "<STR_LIT>" , id_str ) <EOL> def __init__ ( self , line , descriptor ) : <EOL> super ( ExtensionRow , self ) . __init__ ( line , descriptor ) <EOL> self . core_id = self . raw_fields [ descriptor . coreid_index ] <EOL> def __key ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . descriptor , self . core_id , self . data , self . rowtype , self . raw_fields ) <EOL> def __eq__ ( self , other ) : <EOL> return self . __key ( ) == other . __key ( ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . __key ( ) ) </s>
