Skip to content

Commit

Permalink
add: gnucash prices to beancount output (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrai2 authored May 17, 2024
1 parent 8916490 commit 53a205b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 16 additions & 1 deletion g2b/g2b.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ def write_beancount_file(self) -> None:
events = self._get_event_directives()
balance_statements = self._get_balance_directives()
commodities = self._get_commodities()
prices = self._get_prices()
with open(self._output_path, "w", encoding="utf8") as file:
printer.print_entries(
commodities + openings + events + transactions + balance_statements,
commodities + openings + events + prices + transactions + balance_statements,
file=file,
prefix=self._get_header_str(),
)
Expand Down Expand Up @@ -301,6 +302,20 @@ def _get_open_account_directives(self, transactions):
)
return openings

def _get_prices(self):
prices = []
for price in self._book.prices:
prices.append(
data.Price(
meta={"filename": self._filepath, "lineno": -1},
currency=price.commodity.mnemonic.replace(" ", ""),
amount=amount.Amount(number=price.value, currency=price.currency.mnemonic),
date=price.date,
)
)
prices.sort(key=lambda x: x.date)
return prices


@click.command()
@click.version_option(message="%(version)s")
Expand Down
9 changes: 9 additions & 0 deletions tests/test_g2b.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,12 @@ def test_postings_have_no_flag_if__flag_postings__is_set_to_false(self, tmp_path
assert transaction.flag == "*"
for posting in transaction.postings:
assert posting.flag is None

def test_get_prices_returns_beancount_prices(self, tmp_path):
config_path = tmp_path / "config.yaml"
config_path.write_text(yaml.dump(self.test_config))
g2b = GnuCash2Beancount(self.gnucash_path, Path(), config_path)
g2b._read_gnucash_book()
prices = g2b._get_prices()
for price in prices:
assert isinstance(price, data.Price)

0 comments on commit 53a205b

Please sign in to comment.