Skip to content

Commit

Permalink
egh#52 Set currency of import
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Richards committed Apr 5, 2021
1 parent 0b674c5 commit cafa418
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
6 changes: 5 additions & 1 deletion ledgerautosync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def print_results(converter, ofx, ledger, txns, args):
not(ledger.check_transaction_by_id("ofxid", ALL_AUTOSYNC_INITIAL))):
print(converter.format_initial_balance(ofx.account.statement))
for txn in txns:
print(converter.convert(txn).format(args.indent))
print(converter.convert(txn).format(args.indent, args.assertions, args.override_currency))
if args.assertions:
print(converter.format_balance(ofx.account.statement))

Expand Down Expand Up @@ -242,6 +242,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
23 changes: 15 additions & 8 deletions ledgerautosync/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __init__(
self.date_format = date_format
self.checknum = checknum

def format(self, indent=4, assertions=True):
def format(self, indent=4, assertions=True, override_currency=None):
retval = ""
cleared_str = " "
checknum_str = ""
Expand All @@ -145,7 +145,7 @@ def format(self, indent=4, assertions=True):
for k in sorted(self.metadata.keys()):
retval += "%s; %s: %s\n" % (" " * indent, k, self.metadata[k])
for posting in self.postings:
retval += posting.format(indent, assertions)
retval += posting.format(indent, assertions, override_currency)
return retval


Expand All @@ -163,15 +163,15 @@ def __init__(
self.unit_price = unit_price
self.metadata = metadata

def format(self, indent=4, assertions=True):
def format(self, indent=4, assertions=True, override_currency=None):
space_count = 65 - indent - \
len(self.account) - len(self.amount.format())
if space_count < 2:
space_count = 2
retval = "%s%s%s%s" % (" " * indent,
self.account,
" " * space_count,
self.amount.format())
self.amount.format(override_currency))
if assertions and self.asserted is not None:
retval = "%s = %s" % (retval, self.asserted.format())
if self.unit_price is not None:
Expand All @@ -195,13 +195,17 @@ def __init__(self, number, currency, reverse=False, unlimited=False):
self.unlimited = unlimited
self.currency = currency

def format(self):
def format(self, override_currency=None):
# Commodities must be quoted in ledger if they have
# whitespace or numerals.
if re.search(r'[\s0-9]', self.currency):
currency = "\"%s\"" % (self.currency)

if override_currency is None:
if re.search(r'[\s0-9]', self.currency):
currency = "\"%s\"" % (self.currency)
else:
currency = self.currency
else:
currency = self.currency
currency = override_currency
if self.unlimited:
number = str(abs(self.number))
else:
Expand All @@ -213,6 +217,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

0 comments on commit cafa418

Please sign in to comment.