Skip to content

Commit

Permalink
Only accounts that get importer metadata on their postings should be …
Browse files Browse the repository at this point in the history
…in results.accounts.
  • Loading branch information
carljm committed Dec 16, 2020
1 parent 5f603ee commit 4a0b537
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 42 deletions.
53 changes: 18 additions & 35 deletions beancount_import/source/schwab_csv.py
Expand Up @@ -238,7 +238,8 @@ def get_info(self) -> InfoDict:
line=self.line,
)

def get_auto_accounts(self) -> List[str]:
def get_accounts(self) -> List[str]:
"""Get any accounts for which this importer is authoritative."""
return []


Expand Down Expand Up @@ -291,6 +292,9 @@ def get_primary_account(self) -> str:
sub = self.get_sub_account()
return f"{self.account}:{sub}" if sub is not None else self.account

def get_accounts(self) -> List[str]:
return [self.get_primary_account()]

def get_cost(self) -> Optional[CostSpec]:
return None

Expand All @@ -313,13 +317,6 @@ def get_meta(self) -> Meta:
def get_narration_prefix(self) -> str:
raise NotImplementedError()

def get_auto_accounts(self) -> List[str]:
ret = [self.get_primary_account()]
other = self.get_other_account()
if other != FIXME_ACCOUNT:
ret.append(other)
return ret


@dataclass(frozen=True)
class StockPlanActivity(TransactionEntry):
Expand Down Expand Up @@ -456,13 +453,8 @@ def get_narration_prefix(self) -> str:
def get_cap_gains_account(self) -> str:
return f"{self.capital_gains_account}:{self.symbol}"

def get_auto_accounts(self) -> List[str]:
ret = super().get_auto_accounts()
ret.append(self.get_cap_gains_account())
if self.fees is not None:
ret.append(self.fees_account)
return ret

def get_accounts(self) -> List[str]:
return [self.get_primary_account(), self.get_other_account()]

@dataclass(frozen=True)
class Buy(TransactionEntry):
Expand Down Expand Up @@ -521,11 +513,8 @@ def get_postings(self) -> List[Posting]:
def get_narration_prefix(self) -> str:
return "BUYSTOCK"

def get_auto_accounts(self) -> List[str]:
ret = super().get_auto_accounts()
if self.fees is not None:
ret.append(self.fees_account)
return ret
def get_accounts(self) -> List[str]:
return [self.get_primary_account(), self.get_other_account()]


@dataclass(frozen=True)
Expand Down Expand Up @@ -591,9 +580,6 @@ def get_directive(self) -> Balance:
diff_amount=None,
)

def get_auto_accounts(self) -> List[str]:
return [self.account]


class EntryProcessor:
def __init__(self, journal: JournalEditor) -> None:
Expand All @@ -603,7 +589,7 @@ def __init__(self, journal: JournalEditor) -> None:
) = get_account_mapping(journal.accounts, POSTING_META_ACCOUNT_KEY)
self.journal = journal
self.missing_accounts: Set[str] = set()
self.auto_accounts: Set[str] = set()
self.found_accounts: Set[str] = set()

def process_entry(self, raw_entry: RawEntry) -> Optional[TransactionEntry]:
account = self.schwab_to_account.get(raw_entry.account)
Expand All @@ -619,7 +605,7 @@ def process_entries(
for raw_entry in raw_entries:
processed = self.process_entry(raw_entry)
if processed is not None:
self.auto_accounts.update(processed.get_auto_accounts())
self.found_accounts.update(processed.get_accounts())
yield processed

def process_positions(
Expand All @@ -631,7 +617,6 @@ def process_positions(
self.missing_accounts.add(raw_position.account)
return None
balance = raw_position.get_balance(account)
self.auto_accounts.update(balance.get_auto_accounts())
price = raw_position.get_price()
yield (balance, price)

Expand Down Expand Up @@ -688,6 +673,8 @@ def prepare(self, journal: JournalEditor, results: SourceResults) -> None:

processor = EntryProcessor(journal)
account_set = set(processor.account_to_schwab.keys())
base_accounts = tuple(f"{a}:" for a in account_set)
account_set.update(a for a in journal.accounts if a.startswith(base_accounts))

balance_entries: List[BalanceEntry] = []
price_entries: List[PriceEntry] = []
Expand All @@ -700,7 +687,7 @@ def prepare(self, journal: JournalEditor, results: SourceResults) -> None:

source_entries = list(processor.process_entries(self.raw_entries))

account_set.update(processor.auto_accounts)
account_set.update(processor.found_accounts)

self._get_pending_and_invalid_entries(
source_entries=source_entries,
Expand Down Expand Up @@ -731,7 +718,7 @@ def _get_pending_and_invalid_entries(

for entry in journal_entries:
if isinstance(entry, Balance):
bkey = self._get_key_from_balance(entry, account_set)
bkey = self._get_key_from_balance(entry)
if bkey is not None:
matched_balances.add(bkey)
elif isinstance(entry, Price):
Expand Down Expand Up @@ -781,7 +768,7 @@ def _get_pending_and_invalid_entries(
for balance_entry in balance_entries:
import_result = balance_entry.get_import_result()
for directive in import_result.entries:
bkey = self._get_key_from_balance(directive, account_set)
bkey = self._get_key_from_balance(directive)
if bkey and bkey not in matched_balances:
results.add_pending_entry(import_result)

Expand All @@ -802,7 +789,7 @@ def _get_key_from_posting(
) -> Optional[PostingKey]:
if posting.meta is None:
return None
if posting.account not in account_set:
if not posting.account in account_set:
return None
source_desc = cast(str, posting.meta.get(SOURCE_DESC_KEYS[0], ""))
if not source_desc:
Expand All @@ -821,11 +808,7 @@ def _get_key_from_posting(
source_desc,
)

def _get_key_from_balance(
self, entry: Balance, account_set: AbstractSet[str]
) -> Optional[BalanceKey]:
if entry.account not in account_set:
return None
def _get_key_from_balance(self, entry: Balance) -> Optional[BalanceKey]:
return (
entry.account,
entry.date,
Expand Down
7 changes: 0 additions & 7 deletions testdata/source/schwab_csv/test_basic/accounts.txt
Expand Up @@ -5,10 +5,3 @@ Assets:Schwab:Intelligent-4321
Assets:Schwab:Intelligent-4321:Cash
Assets:Schwab:Intelligent-4321:FNDA
Assets:Schwab:Intelligent-4321:SCHA
Expenses:Brokerage-Fees:Schwab
Income:Capital-Gains:Schwab:FB
Income:Capital-Gains:Schwab:FNDA
Income:Capital-Gains:Schwab:SCHA
Income:Dividend:Schwab
Income:Dividend:Schwab:HYLB
Income:Dividend:Schwab:SCHP

0 comments on commit 4a0b537

Please sign in to comment.