Skip to content
This repository was archived by the owner on May 8, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions fixofx.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env python

# Copyright 2005-2010 Wesabe, Inc.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -57,67 +57,67 @@ def fixpath(filename):
pass


def convert(text, filetype, verbose=False, fid="UNKNOWN", org="UNKNOWN",
def convert(text, filetype, verbose=False, fid="UNKNOWN", org="UNKNOWN",
bankid="UNKNOWN", accttype="UNKNOWN", acctid="UNKNOWN",
balance="UNKNOWN", curdef=None, lang="ENG", dayfirst=False,
balance="UNKNOWN", curdef=None, lang="ENG", dayfirst=False,
debug=False):

# This finishes a verbosity message started by the caller, where the
# caller explains the source command-line option and this explains the
# source format.
if verbose:
if verbose:
sys.stderr.write("Converting from %s format.\n" % filetype)

if options.debug and (filetype in ["OFC", "QIF"] or filetype.startswith("OFX")):
sys.stderr.write("Starting work on raw text:\n")
sys.stderr.write(rawtext + "\n\n")

if filetype.startswith("OFX/2"):
if verbose: sys.stderr.write("No conversion needed; returning unmodified.\n")

# The file is already OFX 2 -- return it unaltered, ignoring
# any of the parameters passed to this method.
return text

elif filetype.startswith("OFX"):
if verbose: sys.stderr.write("Converting to OFX/2.0...\n")

# This will throw a ParseException if it is unable to recognize
# the source format.
response = ofx.Response(text, debug=debug)
response = ofx.Response(text, debug=debug)
return response.as_xml(original_format=filetype)

elif filetype == "OFC":
if verbose: sys.stderr.write("Beginning OFC conversion...\n")
converter = ofxtools.OfcConverter(text, fid=fid, org=org, curdef=curdef,
lang=lang, debug=debug)

# This will throw a ParseException if it is unable to recognize
# the source format.
if verbose:
if verbose:
sys.stderr.write("Converting to OFX/1.02...\n\n%s\n\n" %
converter.to_ofx102())
sys.stderr.write("Converting to OFX/2.0...\n")

return converter.to_xml()

elif filetype == "QIF":
if verbose: sys.stderr.write("Beginning QIF conversion...\n")
converter = ofxtools.QifConverter(text, fid=fid, org=org,
bankid=bankid, accttype=accttype,
acctid=acctid, balance=balance,
bankid=bankid, accttype=accttype,
acctid=acctid, balance=balance,
curdef=curdef, lang=lang, dayfirst=dayfirst,
debug=debug)

# This will throw a ParseException if it is unable to recognize
# the source format.
if verbose:
if verbose:
sys.stderr.write("Converting to OFX/1.02...\n\n%s\n\n" %
converter.to_ofx102())
sys.stderr.write("Converting to OFX/2.0...\n")

return converter.to_xml()

else:
raise TypeError("Unable to convert source format '%s'." % filetype)

Expand Down Expand Up @@ -168,9 +168,9 @@ def convert(text, filetype, verbose=False, fid="UNKNOWN", org="UNKNOWN",

if options.filename:
if os.path.isfile(options.filename):
if options.verbose:
if options.verbose:
sys.stderr.write("Reading from '%s'\n." % options.filename)

try:
srcfile = open(options.filename, 'rU')
rawtext = srcfile.read()
Expand All @@ -180,19 +180,19 @@ def convert(text, filetype, verbose=False, fid="UNKNOWN", org="UNKNOWN",
print "Exiting."
sys.stderr.write("fixofx failed with error code 1\n")
sys.exit(1)

else:
print "'%s' does not appear to be a file. Try --help." % options.filename
sys.stderr.write("fixofx failed with error code 2\n")
sys.exit(2)

else:
if options.verbose:
if options.verbose:
sys.stderr.write("Reading from standard input.\n")

stdin_universal = os.fdopen(os.dup(sys.stdin.fileno()), "rU")
rawtext = stdin_universal.read()

if rawtext == "" or rawtext is None:
print "No input. Pipe a file to convert to the script,\n" + \
"or call with -f. Call with --help for more info."
Expand All @@ -208,16 +208,16 @@ def convert(text, filetype, verbose=False, fid="UNKNOWN", org="UNKNOWN",
# rather than parsing the file to make sure. (Parsing will fail
# below if the guess is wrong on OFX/1 and QIF.)
filetype = ofx.FileTyper(rawtext).trust()

if options.type:
print "Input file type is %s." % filetype
sys.exit(0)
elif options.debug:
sys.stderr.write("Input file type is %s.\n" % filetype)
converted = convert(rawtext, filetype, verbose=options.verbose,
fid=options.fid, org=options.org, bankid=options.bankid,
accttype=options.accttype, acctid=options.acctid,

converted = convert(rawtext, filetype, verbose=options.verbose,
fid=options.fid, org=options.org, bankid=options.bankid,
accttype=options.accttype, acctid=options.acctid,
balance=options.balance, curdef=options.curdef,
lang=options.lang, dayfirst=options.dayfirst,
debug=options.debug)
Expand Down
24 changes: 17 additions & 7 deletions lib/ofxtools/qif_converter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright 2005-2010 Wesabe, Inc.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -318,8 +318,20 @@ def _clean_txn_amount(self, txn):
# Okay, now strip out whitespace padding.
txn_amount = txn_amount.strip()

# Some QIF files have dollar signs in the amount. Hey, why not?
txn_amount = txn_amount.replace('$', '', 1)
# Some QIF files have dollar signs in the amount
# Some QIF files have sterling signs in the amount (in latin1 encoding :/ )
# Let's assume this is going to be true in lots of places
for currency_sign in (
'$', # USD and other currencies in
# most places and most encodings
'\xa3', # GBP latin1
'\xc2\xa3', # GBP utf-8
'\xe2\x82\xac', # Euro utf8
'\xc2\xa5', # Yuan utf8
'\x810\x846', # Yuan Guobiao
'\xa2D', # Yuan Big5
):
txn_amount = txn_amount.replace(currency_sign, '', 1)

# Some QIF files (usually from non-US banks) put the minus sign at
# the end of the amount, rather than at the beginning. Let's fix that.
Expand Down Expand Up @@ -630,5 +642,3 @@ def _ofx_txn(self, txn):

def _check_field(self, key, txn):
return txn.has_key(key) and txn[key].strip() != ""


Loading