#NEWLINE# @ classmethod #NEWLINE# def parseTransaction( cls_ , txn_ofx ) : #NEWLINE# #INDENT# '\n        Parse a transaction in ofx-land and return a Transaction object.\n        ' #NEWLINE# transaction = Transaction ( ) #NEWLINE# type_tag = txn_ofx . find ( 'trntype' ) #NEWLINE# if hasattr ( type_tag , 'contents' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# transaction . type = type_tag . contents [ 0 ] . lower ( ) . strip ( ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'Empty transaction type' ) ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'No Transaction type (a required field)' ) ) #NEWLINE# #UNINDENT# #UNINDENT# name_tag = txn_ofx . find ( 'name' ) #NEWLINE# if hasattr ( name_tag , 'contents' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# transaction . payee = name_tag . contents [ 0 ] . strip ( ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'Empty transaction name' ) ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'No Transaction name (a required field)' ) ) #NEWLINE# #UNINDENT# #UNINDENT# memo_tag = txn_ofx . find ( 'memo' ) #NEWLINE# if hasattr ( memo_tag , 'contents' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# transaction . memo = memo_tag . contents [ 0 ] . strip ( ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# amt_tag = txn_ofx . find ( 'trnamt' ) #NEWLINE# if hasattr ( amt_tag , 'contents' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# transaction . amount = cls_ . toDecimal ( amt_tag ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# raise OfxParserException ( 'Invalid Transaction Date' ) #NEWLINE# #UNINDENT# except decimal . InvalidOperation : #NEWLINE# #INDENT# if ( amt_tag . contents [ 0 ] . strip ( ) in ( 'null' , '-null' ) ) : #NEWLINE# #INDENT# transaction . amount = 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise OfxParserException ( ( six . u ( "Invalid Transaction Amount: '%s'" ) % amt_tag . contents [ 0 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# except TypeError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'No Transaction Amount (a required field)' ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'Missing Transaction Amount (a required field)' ) ) #NEWLINE# #UNINDENT# date_tag = txn_ofx . find ( 'dtposted' ) #NEWLINE# if hasattr ( date_tag , 'contents' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# transaction . date = cls_ . parseOfxDateTime ( date_tag . contents [ 0 ] . strip ( ) ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# raise OfxParserException ( 'Invalid Transaction Date' ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# ve = sys . exc_info ( ) [ 1 ] #NEWLINE# raise OfxParserException ( str ( ve ) ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'No Transaction Date (a required field)' ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'Missing Transaction Date (a required field)' ) ) #NEWLINE# #UNINDENT# id_tag = txn_ofx . find ( 'fitid' ) #NEWLINE# if hasattr ( id_tag , 'contents' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# transaction . id = id_tag . contents [ 0 ] . strip ( ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'Empty FIT id (a required                     field)' ) ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'No FIT id (a required field)' ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'Missing FIT id (a required                                      field)' ) ) #NEWLINE# #UNINDENT# sic_tag = txn_ofx . find ( 'sic' ) #NEWLINE# if hasattr ( sic_tag , 'contents' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# transaction . sic = sic_tag . contents [ 0 ] . strip ( ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'Empty transaction Standard                                          Industry Code (SIC)' ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( transaction . sic is not None ) and ( transaction . sic in mcc . codes ) ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# transaction . mcc = mcc . codes . get ( transaction . sic , '' ) . get ( 'combined                     description' ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'Empty transaction Merchant Category                     Code (MCC)' ) ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# if cls . _fail_fast : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# checknum_tag = txn_ofx . find ( 'checknum' ) #NEWLINE# if hasattr ( checknum_tag , 'contents' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# transaction . checknum = checknum_tag . contents [ 0 ] . strip ( ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# raise OfxParserException ( six . u ( 'Empty Check (or other reference)                     number' ) ) #NEWLINE# #UNINDENT# #UNINDENT# return transaction