<s> """<STR_LIT>""" <EOL> import string <EOL> from email . parser import Parser <EOL> import pkg_resources <EOL> HEADER_META_1_0 = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT:version>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:description>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> HEADER_META_1_1 = HEADER_META_1_0 + ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> HEADER_META_1_2 = HEADER_META_1_1 + ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> HEADER_META = { <EOL> '<STR_LIT:1.0>' : HEADER_META_1_0 , <EOL> '<STR_LIT>' : HEADER_META_1_1 , <EOL> '<STR_LIT>' : HEADER_META_1_2 , <EOL> } <EOL> METADATA_NAME = '<STR_LIT>' <EOL> def parse_metadata ( metadata ) : <EOL> """<STR_LIT>""" <EOL> parsed_metadata = Parser ( ) . parsestr ( metadata ) <EOL> metadata_spec = set ( HEADER_META [ parsed_metadata [ '<STR_LIT>' ] ] ) <EOL> key_exist = set ( [ s . lower ( ) for s in parsed_metadata . keys ( ) ] ) <EOL> return ( parsed_metadata , key_exist . intersection ( metadata_spec ) ) <EOL> def clean_lead_ws_description ( metadata , field_name ) : <EOL> """<STR_LIT>""" <EOL> def calc_leading ( line ) : <EOL> return len ( line ) - len ( line . lstrip ( ) ) <EOL> def most_common ( lst ) : <EOL> return max ( set ( lst ) , key = lst . count ) <EOL> if field_name . lower ( ) == '<STR_LIT:description>' : <EOL> leading_ws_count = [ calc_leading ( line ) for line in metadata . splitlines ( ) ] <EOL> most_common_ws_count = most_common ( leading_ws_count ) <EOL> strip_split = metadata . strip ( ) . splitlines ( ) <EOL> return '<STR_LIT:\n>' . join ( [ line [ most_common_ws_count : ] if line . startswith ( '<STR_LIT:U+0020>' * most_common_ws_count ) else line <EOL> for line in strip_split ] ) <EOL> else : <EOL> return '<STR_LIT:U+0020>' . join ( [ line . strip ( ) for line in metadata . splitlines ( ) ] ) <EOL> def field_process ( field_name , field_value ) : <EOL> """<STR_LIT>""" <EOL> if field_name == '<STR_LIT>' : <EOL> root = { } <EOL> for line in field_value : <EOL> d = root <EOL> path_split = tuple ( [ s . strip ( ) for s in line . split ( '<STR_LIT>' ) ] ) <EOL> for level in path_split : <EOL> if level in d : <EOL> d = d [ level ] <EOL> else : <EOL> b = { } <EOL> d [ level ] = b <EOL> d = b <EOL> return root <EOL> if isinstance ( field_value , list ) : <EOL> return field_value <EOL> f_value = clean_lead_ws_description ( field_value , field_name ) <EOL> if hasattr ( f_value , '<STR_LIT>' ) : <EOL> f_value = f_value . decode ( '<STR_LIT:utf-8>' ) <EOL> if f_value == '<STR_LIT>' : <EOL> return None <EOL> if field_name == '<STR_LIT>' : <EOL> f_value = field_value . split ( '<STR_LIT:U+002C>' if '<STR_LIT:U+002C>' in field_value else '<STR_LIT:U+0020>' ) <EOL> return f_value <EOL> def metadata_to_dict ( parsed_metadata , key_known ) : <EOL> """<STR_LIT>""" <EOL> mdict = { } <EOL> for field in set ( parsed_metadata . keys ( ) ) : <EOL> all_values = parsed_metadata . get_all ( field ) <EOL> if len ( all_values ) == <NUM_LIT:1> : <EOL> all_values = all_values [ <NUM_LIT:0> ] <EOL> fl_name = field . lower ( ) <EOL> fl_processed = field_process ( fl_name , all_values ) <EOL> if fl_processed : <EOL> mdict [ fl_name ] = fl_processed <EOL> return mdict </s>
