Skip to content

Commit

Permalink
Add UNCONFORMED_ACCOUNT_KEY to importer entries
Browse files Browse the repository at this point in the history
If a posting was predicted by smart_importer or was entered by hand, it should be editable.
This was suggested and discussed in #60
  • Loading branch information
dumbPy committed Dec 23, 2020
1 parent 8db33f6 commit 226742a
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions beancount_import/source/generic_importer_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .description_based_source import DescriptionBasedSource, get_pending_and_invalid_entries
from .mint import _get_key_from_posting

UNCONFIRMED = "unconfirmed_account"

class ImporterSource(DescriptionBasedSource):
def __init__(self,
Expand Down Expand Up @@ -64,7 +65,7 @@ def prepare(self, journal: 'JournalEditor', results: SourceResults) -> None:
hashed_entries = OrderedDict() #type: Dict[Hashable, Directive]
for entry in f_entries:
key_ = self._get_key_from_imported_entry(entry)
self._add_description(entry)
self._add_description_and_unconfirmed_account_key(entry)
hashed_entries.setdefault(key_, []).append(entry)
# deduplicate across statements
for key_ in hashed_entries:
Expand All @@ -84,24 +85,20 @@ def prepare(self, journal: 'JournalEditor', results: SourceResults) -> None:
make_import_result=self._make_import_result,
results=results)

def _add_description(self, entry: Transaction):
def _add_description_and_unconfirmed_account_key(self, entry: Transaction):
if not isinstance(entry, Transaction): return None
postings = entry.postings #type: List[Posting]
to_mutate = []
for i, posting in enumerate(postings):
if posting.account != self.account: continue
if isinstance(posting.meta, dict):
for i, posting in enumerate(entry.postings):
if not isinstance(posting.meta, dict):
# replace posting with a clone with meta dict
entry.postings[i] = posting._replace(meta={})
posting = entry.postings[i]
if posting.account == self.account:
# add description
posting.meta["source_desc"] = entry.narration
posting.meta["date"] = entry.date
break
else:
to_mutate.append(i)
break
for i in to_mutate:
p = postings.pop(i)
p = Posting(p.account, p.units, p.cost, p.price, p.flag,
{"source_desc":entry.narration, "date": entry.date})
postings.insert(i, p)
# add unconfirmed account key
posting[UNCONFIRMED] = ""

def _get_source_posting(self, entry:Transaction) -> Optional[Posting]:
for posting in entry.postings:
Expand Down

0 comments on commit 226742a

Please sign in to comment.