diff --git a/beancount_import/source/generic_importer_source.py b/beancount_import/source/generic_importer_source.py index aa218a4a..94814af1 100644 --- a/beancount_import/source/generic_importer_source.py +++ b/beancount_import/source/generic_importer_source.py @@ -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, @@ -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: @@ -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.meta[UNCONFIRMED] = True def _get_source_posting(self, entry:Transaction) -> Optional[Posting]: for posting in entry.postings: