Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#52 Add support for override the #121

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
30 changes: 25 additions & 5 deletions ledgerautosync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def make_ofx_converter(account,
shortenaccount,
security_list,
date_format,
infer_account):
infer_account,
currency):
klasses = OfxConverter.__subclasses__()
if len(klasses) > 1:
raise Exception("I found more than 1 OfxConverter subclass, but only "
Expand All @@ -112,7 +113,8 @@ def make_ofx_converter(account,
shortenaccount=shortenaccount,
security_list=security_list,
date_format=date_format,
infer_account=infer_account)
infer_account=infer_account,
currency=currency)
else:
return OfxConverter(account=account,
name=name,
Expand All @@ -125,14 +127,20 @@ def make_ofx_converter(account,
shortenaccount=shortenaccount,
security_list=security_list,
date_format=date_format,
infer_account=infer_account)
infer_account=infer_account,
currency=currency)

def sync(ledger, accounts, args):
sync = OfxSynchronizer(ledger, shortenaccount=args.shortenaccount)
for acct in accounts:
try:
(ofx, txns) = sync.get_new_txns(acct, resync=args.resync,
max_days=args.max)
if args.override_currency is None:
currency = ofx.account.statement.currency
else:
currency = args.override_currency

if ofx is not None:
converter = make_ofx_converter(account=ofx.account,
name=acct.description,
Expand All @@ -145,7 +153,8 @@ def sync(ledger, accounts, args):
shortenaccount=args.shortenaccount,
security_list=SecurityList(ofx),
date_format=args.date_format,
infer_account=args.infer_account)
infer_account=args.infer_account,
currency=currency)
print_results(converter, ofx, ledger, txns, args)
except KeyboardInterrupt:
raise
Expand All @@ -170,6 +179,11 @@ def import_ofx(ledger, args):
else:
accountname = UNKNOWN_BANK_ACCOUNT

if args.override_currency is None:
currency=ofx.account.statement.currency
else:
currency = args.override_currency

# build SecurityList (including indexing by CUSIP and ticker symbol)
security_list = SecurityList(ofx)

Expand All @@ -184,7 +198,9 @@ def import_ofx(ledger, args):
shortenaccount=args.shortenaccount,
security_list=security_list,
date_format=args.date_format,
infer_account=args.infer_account)
infer_account=args.infer_account,
currency=currency
)
print_results(converter, ofx, ledger, txns, args)


Expand Down Expand Up @@ -242,6 +258,10 @@ def run(args=None, config=None):
help='do not de-duplicate against a ledger file')
parser.add_argument('-i', '--indent', type=int, default=4,
help='number of spaces to use for indentation')
parser.add_argument('-c', '--override_currency', type=str, default=None,
help='force currency of import, added as a postfix to \
the amounts, unless it is one character in which case it goes at the front, \
also allows for empty')
parser.add_argument('--initial', action='store_true', default=False,
help='create initial balance entries')
parser.add_argument('--fid', type=int, default=None,
Expand Down
12 changes: 9 additions & 3 deletions ledgerautosync/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def __init__(self, number, currency, reverse=False, unlimited=False):
def format(self):
# Commodities must be quoted in ledger if they have
# whitespace or numerals.

if re.search(r'[\s0-9]', self.currency):
currency = "\"%s\"" % (self.currency)
else:
Expand All @@ -213,6 +214,9 @@ def format(self):
if len(currency) == 1:
# $ comes before
return "%s%s%s" % (prefix, currency, number)
elif len(currency) == 0:
return "%s%s" % (prefix, number)

else:
# USD comes after
return "%s%s %s" % (prefix, number, currency)
Expand Down Expand Up @@ -246,7 +250,8 @@ def __init__(
indent=4,
payee_format=None,
date_format=None,
infer_account=True):
infer_account=True,
override_currency=None):
self.lgr = ledger
self.indent = indent
self.unknownaccount = unknownaccount
Expand Down Expand Up @@ -283,11 +288,12 @@ def __init__(
security_list=SecurityList(
[]),
date_format=None,
infer_account=True):
infer_account=True,
currency=None):
super(OfxConverter, self).__init__(ledger=ledger,
indent=indent,
unknownaccount=unknownaccount,
currency=account.statement.currency,
currency=currency,
payee_format=payee_format,
date_format=date_format,
infer_account=infer_account)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_ofx_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ def test_indent(self):
Expenses:Misc -$0.01
""")

def test_override_currency(self):
with open(os.path.join('fixtures', 'checking.ofx'), 'rb') as ofx_file:
ofx = OfxParser.parse(ofx_file)
converter = OfxConverter(account=ofx.account, name="Foo", indent=4, override_currency="CAD")
# testing override_currency
self.assertEqual(
converter.convert(
ofx.account.statement.transactions[0]).format(),
"""2011/03/31 DIVIDEND EARNED FOR PERIOD OF 03/01/2011 THROUGH 03/31/2011 ANNUAL PERCENTAGE YIELD EARNED IS 0.05%
Foo 0.01 CAD
; ofxid: 1101.1452687~7.0000486
Expenses:Misc -0.01 CAD
""")

def test_shortenaccount(self):
with open(os.path.join('fixtures', 'checking.ofx'), 'rb') as ofx_file:
ofx = OfxParser.parse(ofx_file)
Expand Down