192 lines
6.3 KiB
Python
192 lines
6.3 KiB
Python
"""Enhanced Beancount data structures for Conservancy
|
|
|
|
The classes in this module are interface-compatible with Beancount's core data
|
|
structures, and provide additional business logic that we want to use
|
|
throughout Conservancy tools.
|
|
"""
|
|
# Copyright © 2020 Brett Smith
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import collections.abc
|
|
|
|
from beancount.core import account as bc_account
|
|
|
|
from typing import (
|
|
Iterable,
|
|
Iterator,
|
|
MutableMapping,
|
|
Optional,
|
|
)
|
|
|
|
from .beancount_types import (
|
|
MetaKey,
|
|
MetaValue,
|
|
Posting as BasePosting,
|
|
Transaction,
|
|
)
|
|
|
|
LINK_METADATA = frozenset([
|
|
'approval',
|
|
'check',
|
|
'contract',
|
|
'invoice',
|
|
'purchase-order',
|
|
'receipt',
|
|
'statement',
|
|
])
|
|
|
|
class Account(str):
|
|
"""Account name string
|
|
|
|
This is a string that names an account, like Accrued:AccountsPayable
|
|
or Income:Donations. This class provides additional methods for common
|
|
account name parsing and queries.
|
|
"""
|
|
SEP = bc_account.sep
|
|
|
|
def is_income(self) -> bool:
|
|
return self.is_under('Income:', 'UnearnedIncome:') is not None
|
|
|
|
def is_real_asset(self) -> bool:
|
|
return bool(
|
|
self.is_under('Assets:')
|
|
and not self.is_under('Assets:PrepaidExpenses', 'Assets:PrepaidVacation')
|
|
)
|
|
|
|
def is_under(self, *acct_seq: str) -> Optional[str]:
|
|
"""Return a match if this account is "under" a part of the hierarchy
|
|
|
|
Pass in any number of account name strings as arguments. If this
|
|
account is under one of those strings in the account hierarchy, the
|
|
first matching string will be returned. Otherwise, None is returned.
|
|
|
|
You can use the return value of this method as a boolean if you don't
|
|
care which account string is matched.
|
|
|
|
An account is considered to be under itself:
|
|
|
|
Account('Expenses:Tax').is_under('Expenses:Tax') # returns 'Expenses:Tax'
|
|
|
|
To do a "strictly under" search, end your search strings with colons:
|
|
|
|
Account('Expenses:Tax').is_under('Expenses:Tax:') # returns None
|
|
Account('Expenses:Tax').is_under('Expenses:') # returns 'Expenses:'
|
|
|
|
This method does check that all the account boundaries match:
|
|
|
|
Account('Expenses:Tax').is_under('Exp') # returns None
|
|
"""
|
|
for prefix in acct_seq:
|
|
if self.startswith(prefix) and (
|
|
prefix.endswith(self.SEP)
|
|
or self == prefix
|
|
or self[len(prefix)] == self.SEP
|
|
):
|
|
return prefix
|
|
return None
|
|
|
|
|
|
class PostingMeta(collections.abc.MutableMapping):
|
|
"""Combined access to posting metadata with its parent transaction metadata
|
|
|
|
This lets you access posting metadata through a single dict-like object.
|
|
If you try to look up metadata that doesn't exist on the posting, it will
|
|
look for the value in the parent transaction metadata instead.
|
|
|
|
You can set and delete metadata as well. Changes only affect the metadata
|
|
of the posting, never the transaction. Changes are propagated to the
|
|
underlying Beancount data structures.
|
|
|
|
Functionally, you can think of this as identical to:
|
|
|
|
collections.ChainMap(post.meta, txn.meta)
|
|
|
|
Under the hood, this class does a little extra work to avoid creating
|
|
posting metadata if it doesn't have to.
|
|
"""
|
|
|
|
def __init__(self,
|
|
txn: Transaction,
|
|
index: int,
|
|
post: Optional[BasePosting]=None,
|
|
) -> None:
|
|
if post is None:
|
|
post = txn.postings[index]
|
|
self.txn = txn
|
|
self.index = index
|
|
self.post = post
|
|
|
|
def __iter__(self) -> Iterator[MetaKey]:
|
|
keys: Iterable[MetaKey]
|
|
if self.post.meta is None:
|
|
keys = self.txn.meta.keys()
|
|
else:
|
|
keys = frozenset(self.post.meta.keys()).union(self.txn.meta.keys())
|
|
return iter(keys)
|
|
|
|
def __len__(self) -> int:
|
|
return sum(1 for _ in self)
|
|
|
|
def __getitem__(self, key: MetaKey) -> MetaValue:
|
|
if self.post.meta:
|
|
try:
|
|
return self.post.meta[key]
|
|
except KeyError:
|
|
pass
|
|
return self.txn.meta[key]
|
|
|
|
def __setitem__(self, key: MetaKey, value: MetaValue) -> None:
|
|
if self.post.meta is None:
|
|
self.post = self.post._replace(meta={key: value})
|
|
self.txn.postings[self.index] = self.post
|
|
else:
|
|
self.post.meta[key] = value
|
|
|
|
def __delitem__(self, key: MetaKey) -> None:
|
|
if self.post.meta is None:
|
|
raise KeyError(key)
|
|
else:
|
|
del self.post.meta[key]
|
|
|
|
|
|
class Posting(BasePosting):
|
|
"""Enhanced Posting objects
|
|
|
|
This class is a subclass of Beancount's native Posting class where
|
|
specific fields are replaced with enhanced versions:
|
|
|
|
* The `account` field is an Account object
|
|
* The `meta` field is a PostingMeta object
|
|
"""
|
|
|
|
account: Account
|
|
# mypy correctly complains that our MutableMapping is not compatible
|
|
# with Beancount's meta type declaration of Optional[Dict]. IMO
|
|
# Beancount's type declaration is a smidge too specific: I think its type
|
|
# declaration should also use MutableMapping, because it would be very
|
|
# unusual for code to specifically require a Dict over that.
|
|
# If it did, this declaration would pass without issue.
|
|
meta: MutableMapping[MetaKey, MetaValue] # type:ignore[assignment]
|
|
|
|
|
|
def iter_postings(txn: Transaction) -> Iterator[Posting]:
|
|
"""Yield an enhanced Posting object for every posting in the transaction"""
|
|
for index, source in enumerate(txn.postings):
|
|
yield Posting(
|
|
Account(source.account),
|
|
*source[1:5],
|
|
# see rationale above about Posting.meta
|
|
PostingMeta(txn, index, source), # type:ignore[arg-type]
|
|
)
|