A
download ofx2xml.py
Language: Python
License: GPL
Copyright: (C) 2007 Ehud Ben-Reuven
LOC: 71
Project Info
pyfm - flat-line framework for managing fi...rc...(pyfm)
Server: Google
Type: svn
Google\p\pyfm\trunk\imports\
   __init__.py
   bll_checking.py
   bll_portfolio_activity.py
   bll_portfolio_position.py
   ...a_portfolio_activity.py
   ...a_portfolio_position.py
   csv2flt.py
   directnet_checking.py
   mycmd.py
   ofx22flt.py
   ofx2flt.py
   ofx2xml.py
   ofxml2flat.py
   scanner.py
   xls2csv.py
   xls2csv2.py

########################################################################
#  Pyfm - personal finance data-base
#  Copyright (C) 2007 Ehud Ben-Reuven
#  udi@benreuven.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
########################################################################
"""ofx2xml OFX-file-name XML-file-name [temp-file]
write OFX v1.02 file in XML format.
You should supply the name of the OFX file to read and the name of the XML file
in which the result will be written to.
This script generates a temporary file that may contain confidental information and
therefore you can specify its path, otherwise it will use a temporary secure file of its own.

OFX v1.02 is written in SGML which is harder to parse than XML
Also OFX v2 are XML based.
Hopefully the same code can be used to parse both the output of this program and OFX v2.

This program verify the HTTP header of an OFX reply remove it and then
transfer the body of the HTTP OFX reply to the execuable sox.exe

sox.exe is assumed to be located in c:\OpenSP
you can download the latest version from http://sourceforge.net/projects/openjade/, http://openjade.sourceforge.net/
(I tried OpenSP-1.5.2-win32.zip)
For a minimal configuration you will need sox.exe and ospXXX.dll

sox will need the definitions (DTD) of OFX supplied from
http://www.ofx.net/ofx/downloads/ofxdtd2.zip
The files are:
ofxact.dtd,ofxbank.dtd,ofxbill.dtd,ofxinv.dtd,ofxmail.dtd,ofxmain.dtd,ofxprof.dtd,ofxsign.dtd
place these files at c:\OpenSP
"""

import sys, os, string
import scanner


ofxtemplate=r"""
*
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:@'(TYPE1|NONE)'@
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
*
*@s('line')@
""".strip()

def ofx2xml(ofxfile,xmlfile,tmpfile=None):
    #if not os.path.exists(ofxfile):
        #print "\nFile %s not found." % (ofxfile)
        #raise Exception('File not found')

    fofx=open(ofxfile,'r')
    lofx=fofx.read()
    fofx.close()
    
    if tmpfile:
        ftmp=open(tmpfile,'w')
    else:
        ftmp=NamedTemporaryFile('w')
    scanner.namespace["post_line"]=lambda s,f=ftmp: f.write(s.replace('&','&')+'\n')
    try:
        llofx=scanner.scanner(ofxtemplate,lofx)
    except scanner.scannerror:
        for l in lofx:
            ftmp.write(l)
    except SyntaxError, v:
        print "Bad input"
        print v
        sys.exit(1)
        
    ftmp.flush()

    os.system('c:/OpenSP/osx -D c:\\OpenSP -wno-valid ofxmain.dtd '+ftmp.name+' > '+xmlfile)
    ftmp.close()
    if tmpfile:
        os.remove(tmpfile)

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print "ofx2xml <OFX-file> <XML-file> [<temporary-file>]"
        sys.exit(1)
    
    ofxfile = sys.argv[1]
    xmlfile = sys.argv[2]
    if len(sys.argv) > 3:
        tmpfile=sys.argv[3]
    else:
        tmpfile=None

    ofx2xml(ofxfile,xmlfile,tmpfile)

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us