Skip to content
This repository has been archived by the owner on May 8, 2018. It is now read-only.

Take a look #5

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a88cd87
sets encoding to utf-8
vandersonmota Dec 2, 2010
cd3043e
force xml encoding to utf-8
vandersonmota Dec 2, 2010
5c3402c
added string parameter support
vandersonmota Dec 2, 2010
a877d7f
ignora arquivos .swp
vandersonmota Dec 2, 2010
c845d00
remove \r from raw_string
vandersonmota Dec 3, 2010
5579eff
fix bad ofc by removing inline closing "tags"
vandersonmota Dec 8, 2010
af5ff9f
enables !Type:Invst
vandersonmota Dec 8, 2010
3eaef4b
removes silly ipdb
vandersonmota Dec 8, 2010
bd093a5
fixes bad ofc and tries to parse
vandersonmota Dec 9, 2010
c3b8264
fixes bad ofc and tries to parse
vandersonmota Dec 9, 2010
35f569d
Merge branch 'master' of github.com:myfreecomm/fixofx
vandersonmota Dec 9, 2010
bc7322c
Handles special case where there is no bankinfo and TRNRS instead of …
vandersonmota Jan 14, 2011
b6d7b51
ignoring stuff
vandersonmota Mar 25, 2011
55d7e8e
tests for the ofcconverter
vandersonmota Mar 25, 2011
f18338e
translates chknum to checknum
vandersonmota Mar 25, 2011
cca18a8
removes swp
vandersonmota Jul 6, 2011
b736c5a
removes duplication in test utils
vandersonmota Jul 6, 2011
996b852
fixes problem with ofc with empty tags
vandersonmota Jul 7, 2011
26f5887
strip_empty_tags is an utility function now.
vandersonmota Jul 7, 2011
a36d2ad
improving tests readability
vandersonmota Jul 7, 2011
321604d
testing for stripping empty tags
vandersonmota Jul 7, 2011
4d98ae2
fixes problem with max recursion depth limit
vandersonmota Aug 9, 2011
ffb6751
remove blank lines before converting
dx7 Nov 11, 2011
4ce1e8d
fix ofc parser. blank tag LEDGR causing error.
dx7 Nov 11, 2011
55bfcdb
fixing broken tests
tinogomes Jan 22, 2014
41be9d9
importing ofx file with blank memo
tinogomes Jan 22, 2014
49249e3
Merge pull request #1 from myfreecomm/fix/import_ofx_with_blank_memo
tinogomes Jan 22, 2014
82e6f54
import category from qif files
tinogomes Jan 22, 2014
fa66467
Fix parser error when OFX contains a self closed tag
leandrost Sep 17, 2015
ce97d0f
Merge pull request #2 from myfreecomm/fix-parse-self-closed-tag
leandrost Sep 18, 2015
05f3b6e
Add test to tag with line break
leandrost Sep 25, 2018
9345c3b
Strips line break from OBSV tags
leandrost Sep 25, 2018
a84176f
Merge pull request #3 from myfreecomm/fix-parse-line-break
lmansur Sep 26, 2018
a0920ce
Fix multiline tags
leandrost Sep 27, 2018
0363194
Refactoring regexp strings
leandrost Sep 28, 2018
6eb5c28
Merge pull request #4 from myfreecomm/fix-line-break
leandrost Sep 28, 2018
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.pyc
*.swp
*.swo
77 changes: 43 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,69 @@ def fixpath(filename):
pass


def convert(text, filetype, verbose=False, fid="UNKNOWN", org="UNKNOWN",
def convert(filecontent, 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):


text = os.linesep.join(s for s in filecontent.splitlines() if s)

# 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 @@ -148,6 +150,8 @@ def convert(text, filetype, verbose=False, fid="UNKNOWN", org="UNKNOWN",
help="(QIF only) Account balance to use in output")
parser.add_option("--dayfirst", action="store_true", dest="dayfirst", default=False,
help="(QIF only) Parse dates day first (UK format)")
parser.add_option("-s", "--string", dest="string", default=None,
help="string to convert")
(options, args) = parser.parse_args()

#
Expand All @@ -168,9 +172,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 +184,24 @@ 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)

elif options.string:
if options.verbose:
sys.stderr.write("Reading from string\n")
rawtext = options.string.replace('\r','')

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 +217,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
7 changes: 4 additions & 3 deletions lib/ofx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# coding: utf-8
# 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
7 changes: 4 additions & 3 deletions lib/ofx/account.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#coding: utf-8
# 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
10 changes: 6 additions & 4 deletions lib/ofx/builder.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#coding: utf-8
# 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 @@ -121,7 +122,7 @@ def __call__(self, *values, **params):
# also be added here, unfortunately.
__all__ = ['ACCTID', 'ACCTINFORQ', 'ACCTINFOTRNRQ', 'ACCTTYPE', 'APPID',
'APPVER', 'AVAILBAL', 'BALAMT', 'BANKACCTFROM', 'BANKID', 'BANKMSGSRQV1',
'BANKMSGSRSV1', 'BANKTRANLIST', 'BROKERID', 'CCACCTFROM', 'CCSTMTENDRQ',
'BANKMSGSRSV1', 'BANKTRANLIST', 'BROKERID', 'CATEGORY', 'CCACCTFROM', 'CCSTMTENDRQ',
'CCSTMTENDTRNRQ', 'CCSTMTRQ', 'CCSTMTRS', 'CCSTMTTRNRQ', 'CCSTMTTRNRS',
'CHARSET', 'CHECKNUM', 'CLIENTROUTING', 'CLTCOOKIE', 'CODE', 'COMPRESSION',
'CREDITCARDMSGSRQV1', 'CREDITCARDMSGSRSV1', 'CURDEF', 'DATA', 'DOCUMENT',
Expand Down Expand Up @@ -152,6 +153,7 @@ def __call__(self, *values, **params):
BANKMSGSRSV1 = Tag("BANKMSGSRSV1", aggregate=True)
BANKTRANLIST = Tag("BANKTRANLIST", aggregate=True)
BROKERID = Tag("BROKERID")
CATEGORY = Tag("CATEGORY")
CCACCTFROM = Tag("CCACCTFROM", aggregate=True)
CCSTMTENDRQ = Tag("CCSTMTENDRQ", aggregate=True)
CCSTMTENDTRNRQ = Tag("CCSTMTENDTRNRQ", aggregate=True)
Expand Down
7 changes: 4 additions & 3 deletions lib/ofx/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#coding: utf-8
# 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
18 changes: 6 additions & 12 deletions lib/ofx/document.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#coding: utf-8
# 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 @@ -40,15 +41,8 @@ def as_xml(self, original_format=None, date_format=None):
# method so that we can start to survey what encodings
# we're actually seeing, and use that to maybe be smarter
# about this in the future.
encoding = ""
if self.parse_dict["header"]["ENCODING"] == "USASCII":
encoding = "US-ASCII"
elif self.parse_dict["header"]["ENCODING"] == "UNICODE":
encoding = "UTF-8"
elif self.parse_dict["header"]["ENCODING"] == "NONE":
encoding = "UTF-8"
else:
encoding = self.parse_dict["header"]["ENCODING"]
#forcing encoding to utf-8
encoding = "UTF-8"

xml += """<?xml version="1.0" encoding="%s"?>\n""" % encoding
xml += """<?OFX OFXHEADER="200" VERSION="200" """ + \
Expand Down
15 changes: 8 additions & 7 deletions lib/ofx/error.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#coding: utf-8
# 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.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# ofx.error - OFX error message exception
#
# ofx.error - OFX error message exception
#


class Error(Exception):
Expand Down Expand Up @@ -96,12 +97,12 @@ def __init__(self, summary, code=None, severity=None, message=None):
def interpret_code(self, code=None):
if code is None:
code = self.code

if self.codetable.has_key(code):
return self.codetable[code]
else:
return "Unknown error code"

def str(self):
format = "%s\n(%s %s: %s)"
return format % (self.msg, self.severity, self.code,
Expand Down
Loading