<s> import os <EOL> import re <EOL> import sys <EOL> import time <EOL> import email . Parser <EOL> from karesansui . lib . dict_op import DictOp <EOL> from karesansui . lib . parser . base . null_parser import nullParser as Parser <EOL> from karesansui . lib . utils import array_replace <EOL> from karesansui . lib . utils import preprint_r <EOL> """<STR_LIT>""" <EOL> class emlParser ( Parser ) : <EOL> _module = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> self . dop = DictOp ( ) <EOL> self . dop . addconf ( self . _module , { } ) <EOL> self . base_parser_name = Parser . __name__ <EOL> self . encoding = "<STR_LIT>" <EOL> pass <EOL> def source_file ( self ) : <EOL> try : <EOL> retval = self . get_source_file ( ) <EOL> except : <EOL> retval = [ ] <EOL> return retval <EOL> def _parse_mail ( self , mail ) : <EOL> retval = { } <EOL> try : <EOL> body = mail . get_payload ( ) <EOL> boundary = mail . get_boundary ( ) <EOL> charsets = mail . get_charsets ( ) <EOL> content_charset = mail . get_content_charset ( ) <EOL> content_maintype = mail . get_content_maintype ( ) <EOL> content_subtype = mail . get_content_subtype ( ) <EOL> content_type = mail . get_content_type ( ) <EOL> default_type = mail . get_default_type ( ) <EOL> filename = mail . get_filename ( ) <EOL> params = mail . get_params ( ) <EOL> is_multipart = mail . is_multipart ( ) <EOL> if type ( body ) is list : <EOL> new_body = [ ] <EOL> for _mail in body : <EOL> new_body . append ( self . _parse_mail ( _mail ) ) <EOL> body = new_body <EOL> retval = { } <EOL> retval [ '<STR_LIT:body>' ] = body <EOL> retval [ '<STR_LIT>' ] = boundary <EOL> retval [ '<STR_LIT>' ] = charsets <EOL> retval [ '<STR_LIT>' ] = content_charset <EOL> retval [ '<STR_LIT>' ] = content_maintype <EOL> retval [ '<STR_LIT>' ] = content_subtype <EOL> retval [ '<STR_LIT>' ] = content_type <EOL> retval [ '<STR_LIT>' ] = default_type <EOL> retval [ '<STR_LIT:filename>' ] = filename <EOL> retval [ '<STR_LIT>' ] = params <EOL> retval [ '<STR_LIT>' ] = is_multipart <EOL> except : <EOL> pass <EOL> return retval <EOL> def parse ( self , file = None , message = None ) : <EOL> retval = { } <EOL> try : <EOL> if file is not None and os . path . exists ( file ) : <EOL> message = open ( file ) . read ( ) <EOL> mail = email . Parser . Parser ( ) . parsestr ( message ) <EOL> headers = mail . _headers <EOL> msgs = self . _parse_mail ( mail ) <EOL> if type ( msgs [ '<STR_LIT:body>' ] ) is list : <EOL> rawbody = "<STR_LIT>" <EOL> if file is not None and os . path . exists ( file ) : <EOL> f = open ( file ) <EOL> line = f . readline ( ) <EOL> while line : <EOL> line = f . readline ( ) <EOL> if line . rstrip ( ) == "<STR_LIT>" : <EOL> rawbody += line <EOL> rawbody += '<STR_LIT>' . join ( f . readlines ( ) ) <EOL> break <EOL> f . close <EOL> else : <EOL> in_body = False <EOL> for line in message . split ( "<STR_LIT:\n>" ) : <EOL> if line . rstrip ( ) == "<STR_LIT>" : <EOL> in_body = True <EOL> if in_body is True : <EOL> rawbody += line + "<STR_LIT:\n>" <EOL> elif type ( msgs [ '<STR_LIT:body>' ] ) is str : <EOL> rawbody = msgs [ '<STR_LIT:body>' ] <EOL> retval [ "<STR_LIT>" ] = headers <EOL> retval [ "<STR_LIT>" ] = msgs <EOL> retval [ "<STR_LIT>" ] = rawbody <EOL> except : <EOL> pass <EOL> return retval <EOL> def read_conf ( self , extra_args = None ) : <EOL> retval = { } <EOL> self . dop . addconf ( self . _module , { } ) <EOL> try : <EOL> self . set_source_file ( extra_args [ "<STR_LIT:file>" ] ) <EOL> for _file in self . source_file ( ) : <EOL> if os . path . exists ( _file ) : <EOL> mail = self . parse ( file = _file ) <EOL> headers = mail [ '<STR_LIT>' ] <EOL> msgs = mail [ '<STR_LIT>' ] <EOL> rawbody = mail [ '<STR_LIT>' ] <EOL> self . dop . add ( self . _module , [ _file , '<STR_LIT>' ] , msgs ) <EOL> self . dop . add ( self . _module , [ _file , '<STR_LIT>' ] , headers ) <EOL> for _header in headers : <EOL> self . dop . add ( self . _module , [ _file , '<STR_LIT>' , _header [ <NUM_LIT:0> ] ] , _header [ <NUM_LIT:1> ] ) <EOL> self . dop . add ( self . _module , [ _file , '<STR_LIT>' ] , rawbody ) <EOL> except : <EOL> pass <EOL> try : <EOL> message = extra_args [ "<STR_LIT:message>" ] <EOL> try : <EOL> message = message . encode ( self . encoding ) <EOL> except : <EOL> pass <EOL> if message != "<STR_LIT>" and message is not None : <EOL> mail = self . parse ( message = message ) <EOL> headers = mail [ '<STR_LIT>' ] <EOL> msgs = mail [ '<STR_LIT>' ] <EOL> rawbody = mail [ '<STR_LIT>' ] <EOL> self . dop . add ( self . _module , [ "<STR_LIT>" , '<STR_LIT>' ] , msgs ) <EOL> self . dop . add ( self . _module , [ "<STR_LIT>" , '<STR_LIT>' ] , headers ) <EOL> for _header in headers : <EOL> self . dop . add ( self . _module , [ "<STR_LIT>" , '<STR_LIT>' , _header [ <NUM_LIT:0> ] ] , _header [ <NUM_LIT:1> ] ) <EOL> self . dop . add ( self . _module , [ "<STR_LIT>" , '<STR_LIT>' ] , rawbody ) <EOL> except : <EOL> pass <EOL> return self . dop . getconf ( self . _module ) <EOL> def write_conf ( self , conf_arr = { } , extra_args = None , dryrun = False ) : <EOL> retval = True <EOL> return retval <EOL> """<STR_LIT:U+0020>""" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> """<STR_LIT>""" <EOL> message = """<STR_LIT>""" <EOL> parser = emlParser ( ) <EOL> dop = DictOp ( ) <EOL> eml = "<STR_LIT>" <EOL> extra_args = { "<STR_LIT:message>" : message } <EOL> extra_args = { "<STR_LIT:file>" : eml } <EOL> extra_args = { "<STR_LIT:message>" : open ( eml ) . read ( ) } <EOL> dop . addconf ( "<STR_LIT>" , parser . read_conf ( extra_args = extra_args ) ) <EOL> conf = dop . getconf ( "<STR_LIT>" ) <EOL> preprint_r ( conf ) </s>
