Skip to content

Commit

Permalink
add events to beancount output (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrai2 authored May 14, 2024
1 parent 3ec8958 commit 493e77a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ beancount: # here you can add beancount options and plugins that should be adde
- "beancount.plugins.coherent_cost"
- "beancount.plugins.nounused"
- "beancount.plugins.auto"
events:
2024-05-05: type description string # optional events that should be added to the output, the
# first space is used to split between space and description
```

## Execute g2b
Expand Down
20 changes: 18 additions & 2 deletions g2b/g2b.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""This module provides a converter that can translate a gnucash csv export into a beancount file"""

import datetime
import logging
import re
from functools import cached_property
Expand Down Expand Up @@ -90,7 +91,13 @@ def _gnucash_config(self) -> Dict:
@cached_property
def _bean_config(self) -> Dict:
"""Returns configurations only related to the beancount export"""
return self._configs.get("beancount")
config = self._configs.get("beancount")
switched_to_bean_event = {datetime.date.today(): "misc Changed from GnuCash to Beancount"}
if "events" in config:
config["events"].update(switched_to_bean_event)
else:
config["events"] = switched_to_bean_event
return config

@cached_property
def _account_rename_patterns(self) -> List:
Expand Down Expand Up @@ -125,10 +132,11 @@ def write_beancount_file(self) -> None:
self._prepare_csv()
openings = self._get_open_account_directives()
transactions = self._get_transaction_directives()
events = self._events()
with open(self._output_path, "w", encoding="utf8") as file:
file.write(self._get_header_str())
file.write(self._get_commodities_str())
printer.print_entries(openings + transactions, file=file)
printer.print_entries(events + openings + transactions, file=file)
self._logger.info("Finished writing beancount file: '%s'", self._output_path)
self._verify_output()

Expand Down Expand Up @@ -343,6 +351,14 @@ def _verify_output(self) -> None:
if validation_errors:
self._logger.warning("Found %s validation errors", len(validation_errors))

def _events(self):
"""Parse beancount configuration and create event directives"""
events = []
for date, event_description in self._bean_config.get("events", {}).items():
event_type, description = event_description.split(" ", maxsplit=1)
events.append(data.Event(date=date, type=event_type, description=description, meta={}))
return events


@click.command()
@click.option(
Expand Down
34 changes: 34 additions & 0 deletions tests/test_g2b.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ def test_configs_raises_on_invalid_yaml(self, tmp_path):
with pytest.raises(G2BException, match="Error while parsing config file"):
_ = GnuCashCSV2Beancount(Path(), Path(), config_path)

def test_bean_config_adds_switched_to_beancount_event_if_no_events_are_configured(
self, tmp_path
):
config_path = tmp_path / "config.yaml"
config_path.write_text(yaml.dump(self.test_config))
g2b = GnuCashCSV2Beancount(Path(), Path(), config_path)
assert g2b._bean_config.get("events") == {
datetime.date.today(): "misc Changed from GnuCash to Beancount"
}

def test_bean_config_adds_switched_to_beancount_event_if_events_are_configured(self, tmp_path):
config_path = tmp_path / "config.yaml"
self.test_config["beancount"]["events"] = {datetime.date.today(): "test Test Event"}
config_path.write_text(yaml.dump(self.test_config))
g2b = GnuCashCSV2Beancount(Path(), Path(), config_path)
assert g2b._bean_config.get("events") == {
datetime.date.today(): "test Test Event",
datetime.date.today(): "misc Changed from GnuCash to Beancount",
}

def test_converter_config_returns_only_converter_configurations(self, tmp_path):
config_path = tmp_path / "config.yaml"
config_path.write_text(yaml.dump(self.test_config))
Expand Down Expand Up @@ -425,3 +445,17 @@ def test_verify_output_calls_beancount_validate(self, mock_validate, tmp_path):
g2b = GnuCashCSV2Beancount(gnucash_path, Path(), config_path)
g2b._verify_output()
mock_validate.assert_called()

def test_events_created_beancount_event_objects(self, tmp_path):
config_path = tmp_path / "config.yaml"
config_path.write_text(yaml.dump(self.test_config))
g2b = GnuCashCSV2Beancount(Path(), Path(), config_path)
events = g2b._events()
assert events == [
data.Event(
date=datetime.date.today(),
type="misc",
description="Changed from GnuCash to Beancount",
meta={},
)
]

0 comments on commit 493e77a

Please sign in to comment.