<s> import ofx <EOL> import ofxtools <EOL> import re <EOL> import sys <EOL> from ofx . builder import * <EOL> class OfcConverter : <EOL> def __init__ ( self , ofc , fid = "<STR_LIT>" , org = "<STR_LIT>" , curdef = None , <EOL> lang = "<STR_LIT>" , debug = False ) : <EOL> self . ofc = ofc <EOL> self . fid = fid <EOL> self . org = org <EOL> self . curdef = curdef <EOL> self . lang = lang <EOL> self . debug = debug <EOL> self . bankid = "<STR_LIT>" <EOL> self . accttype = "<STR_LIT>" <EOL> self . acctid = "<STR_LIT>" <EOL> self . balance = "<STR_LIT>" <EOL> self . start_date = "<STR_LIT>" <EOL> self . end_date = "<STR_LIT>" <EOL> self . parsed_ofc = None <EOL> self . acct_types = { "<STR_LIT:0>" : "<STR_LIT>" , <EOL> "<STR_LIT:1>" : "<STR_LIT>" , <EOL> "<STR_LIT:2>" : "<STR_LIT>" , <EOL> "<STR_LIT:3>" : "<STR_LIT>" , <EOL> "<STR_LIT:4>" : "<STR_LIT>" , <EOL> "<STR_LIT:5>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> self . txn_types = { "<STR_LIT:0>" : "<STR_LIT>" , <EOL> "<STR_LIT:1>" : "<STR_LIT>" , <EOL> "<STR_LIT:2>" : "<STR_LIT>" , <EOL> "<STR_LIT:3>" : "<STR_LIT>" , <EOL> "<STR_LIT:4>" : "<STR_LIT>" , <EOL> "<STR_LIT:5>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> if self . debug : sys . stderr . write ( "<STR_LIT>" ) <EOL> parser = ofxtools . OfcParser ( debug = debug ) <EOL> self . parsed_ofc = parser . parse ( self . ofc ) <EOL> if self . debug : sys . stderr . write ( "<STR_LIT>" ) <EOL> try : <EOL> self . bankid = self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> acct_code = self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> self . accttype = self . acct_types . get ( acct_code , "<STR_LIT>" ) <EOL> self . acctid = self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> self . bankid = self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> acct_code = self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> self . accttype = self . acct_types . get ( acct_code , "<STR_LIT>" ) <EOL> self . acctid = self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> self . balance = self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> self . start_date = self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> self . end_date = self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> def to_ofx102 ( self ) : <EOL> if self . debug : sys . stderr . write ( "<STR_LIT>" ) <EOL> return DOCUMENT ( self . _ofx_header ( ) , <EOL> OFX ( self . _ofx_signon ( ) , <EOL> self . _ofx_stmt ( ) ) ) <EOL> def to_xml ( self ) : <EOL> ofx102 = self . to_ofx102 ( ) <EOL> if self . debug : <EOL> sys . stderr . write ( ofx102 + "<STR_LIT:\n>" ) <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> response = ofx . Response ( ofx102 , debug = self . debug ) <EOL> if self . debug : sys . stderr . write ( "<STR_LIT>" ) <EOL> xml = response . as_xml ( original_format = "<STR_LIT>" ) <EOL> return xml <EOL> def _ofx_header ( self ) : <EOL> return HEADER ( <EOL> OFXHEADER ( "<STR_LIT:100>" ) , <EOL> DATA ( "<STR_LIT>" ) , <EOL> VERSION ( "<STR_LIT>" ) , <EOL> SECURITY ( "<STR_LIT>" ) , <EOL> ENCODING ( "<STR_LIT>" ) , <EOL> CHARSET ( "<STR_LIT>" ) , <EOL> COMPRESSION ( "<STR_LIT>" ) , <EOL> OLDFILEUID ( "<STR_LIT>" ) , <EOL> NEWFILEUID ( "<STR_LIT>" ) ) <EOL> def _ofx_signon ( self ) : <EOL> return SIGNONMSGSRSV1 ( <EOL> SONRS ( <EOL> STATUS ( <EOL> CODE ( "<STR_LIT:0>" ) , <EOL> SEVERITY ( "<STR_LIT>" ) , <EOL> MESSAGE ( "<STR_LIT>" ) ) , <EOL> DTSERVER ( self . end_date ) , <EOL> LANGUAGE ( self . lang ) , <EOL> FI ( <EOL> ORG ( self . org ) , <EOL> FID ( self . fid ) ) ) ) <EOL> def _ofx_stmt ( self ) : <EOL> if self . curdef is None : <EOL> curdef = "<STR_LIT>" <EOL> else : <EOL> curdef = self . curdef <EOL> if self . accttype == "<STR_LIT>" : <EOL> return CREDITCARDMSGSRSV1 ( <EOL> CCSTMTTRNRS ( <EOL> TRNUID ( "<STR_LIT:0>" ) , <EOL> self . _ofx_status ( ) , <EOL> CCSTMTRS ( <EOL> CURDEF ( curdef ) , <EOL> CCACCTFROM ( <EOL> ACCTID ( self . acctid ) ) , <EOL> self . _ofx_txns ( ) , <EOL> self . _ofx_ledgerbal ( ) , <EOL> self . _ofx_availbal ( ) ) ) ) <EOL> else : <EOL> return BANKMSGSRSV1 ( <EOL> STMTTRNRS ( <EOL> TRNUID ( "<STR_LIT:0>" ) , <EOL> self . _ofx_status ( ) , <EOL> STMTRS ( <EOL> CURDEF ( curdef ) , <EOL> BANKACCTFROM ( <EOL> BANKID ( self . bankid ) , <EOL> ACCTID ( self . acctid ) , <EOL> ACCTTYPE ( self . accttype ) ) , <EOL> self . _ofx_txns ( ) , <EOL> self . _ofx_ledgerbal ( ) , <EOL> self . _ofx_availbal ( ) ) ) ) <EOL> def _ofx_status ( self ) : <EOL> return STATUS ( <EOL> CODE ( "<STR_LIT:0>" ) , <EOL> SEVERITY ( "<STR_LIT>" ) , <EOL> MESSAGE ( "<STR_LIT>" ) ) <EOL> def _ofx_ledgerbal ( self ) : <EOL> return LEDGERBAL ( <EOL> BALAMT ( self . balance ) , <EOL> DTASOF ( self . end_date ) ) <EOL> def _ofx_availbal ( self ) : <EOL> return AVAILBAL ( <EOL> BALAMT ( self . balance ) , <EOL> DTASOF ( self . end_date ) ) <EOL> def _ofx_txns ( self ) : <EOL> txns = "<STR_LIT>" <EOL> last_date = None <EOL> txn_index = <NUM_LIT:1> <EOL> for item in self . parsed_ofc [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> if item [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> txn = item . asDict ( ) <EOL> if txn . has_key ( '<STR_LIT>' ) : <EOL> txn = txn [ '<STR_LIT>' ] . asDict ( ) <EOL> txn_date = txn [ "<STR_LIT>" ] <EOL> if txn_date != last_date : <EOL> last_date = txn_date <EOL> txn_index = <NUM_LIT:1> <EOL> txn_amt = txn [ "<STR_LIT>" ] <EOL> txn_type = self . txn_types . get ( txn [ "<STR_LIT>" ] ) <EOL> if txn_type is None : <EOL> if txn_amt . startswith ( '<STR_LIT:->' ) : <EOL> txn [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> else : <EOL> txn [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> txn [ "<STR_LIT>" ] = "<STR_LIT>" % ( self . org , self . accttype , <EOL> txn_date , txn_index , <EOL> txn_amt ) <EOL> txns += self . _ofx_txn ( txn ) <EOL> txn_index += <NUM_LIT:1> <EOL> return BANKTRANLIST ( <EOL> DTSTART ( self . start_date ) , <EOL> DTEND ( self . end_date ) , <EOL> txns ) <EOL> def _ofx_txn ( self , txn ) : <EOL> fields = [ ] <EOL> if self . _check_field ( "<STR_LIT>" , txn ) : <EOL> fields . append ( TRNTYPE ( txn [ "<STR_LIT>" ] . strip ( ) ) ) <EOL> if self . _check_field ( "<STR_LIT>" , txn ) : <EOL> fields . append ( DTPOSTED ( txn [ "<STR_LIT>" ] . strip ( ) ) ) <EOL> if self . _check_field ( "<STR_LIT>" , txn ) : <EOL> fields . append ( TRNAMT ( txn [ "<STR_LIT>" ] . strip ( ) ) ) <EOL> if self . _check_field ( "<STR_LIT>" , txn ) : <EOL> fields . append ( CHECKNUM ( txn [ "<STR_LIT>" ] . strip ( ) ) ) <EOL> if self . _check_field ( "<STR_LIT>" , txn ) : <EOL> fields . append ( FITID ( txn [ "<STR_LIT>" ] . strip ( ) ) ) <EOL> if self . _check_field ( "<STR_LIT>" , txn ) : <EOL> fields . append ( NAME ( txn [ "<STR_LIT>" ] . strip ( ) ) ) <EOL> if self . _check_field ( "<STR_LIT>" , txn ) : <EOL> fields . append ( MEMO ( txn [ "<STR_LIT>" ] . strip ( ) ) ) <EOL> return STMTTRN ( * fields ) <EOL> def _check_field ( self , key , txn ) : <EOL> return txn . has_key ( key ) and txn [ key ] . strip ( ) != "<STR_LIT>" </s>
