Changeset - df0c3546fd5c
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-07-16 14:39:31
brettcsmith@brettcsmith.org
data: Add Account.load_from_books convenience classmethod.
2 files changed with 25 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
...
 
@@ -131,128 +131,133 @@ class AccountMeta(MutableMapping[MetaKey, MetaValue]):
 
        return None if self._closing is None else self._closing.date
 

	
 
    @property
 
    def close_meta(self) -> Optional[Meta]:
 
        return None if self._closing is None else self._closing.meta
 

	
 
    @property
 
    def currencies(self) -> Optional[Sequence[Currency]]:
 
        return self._opening.currencies
 

	
 
    @property
 
    def open_date(self) -> datetime.date:
 
        return self._opening.date
 

	
 
    @property
 
    def open_meta(self) -> Meta:
 
        return self._opening.meta
 

	
 

	
 
class Account(str):
 
    """Account name string
 

	
 
    This is a string that names an account, like Assets:Bank:Checking
 
    or Income:Donations. This class provides additional methods for common
 
    account name parsing and queries.
 
    """
 
    __slots__ = ()
 

	
 
    ACCOUNT_RE: Pattern
 
    SEP = bc_account.sep
 
    _meta_map: MutableMapping[str, AccountMeta] = {}
 
    _options_map: OptionsMap
 

	
 
    @classmethod
 
    def load_options_map(cls, options_map: OptionsMap) -> None:
 
        cls._options_map = options_map
 
        roots: Sequence[str] = bc_options.get_account_types(options_map)
 
        cls.ACCOUNT_RE = re.compile(
 
            r'^(?:{})(?:{}[A-Z0-9][-A-Za-z0-9]*)+$'.format(
 
                '|'.join(roots), cls.SEP,
 
            ))
 

	
 
    @classmethod
 
    def load_opening(cls, opening: Open) -> None:
 
        cls._meta_map[opening.account] = AccountMeta(opening)
 

	
 
    @classmethod
 
    def load_closing(cls, closing: Close) -> None:
 
        try:
 
            cls._meta_map[closing.account].add_closing(closing)
 
        except KeyError:
 
            raise ValueError(
 
                f"tried to load {closing.account} close directive before open",
 
            ) from None
 

	
 
    @classmethod
 
    def load_openings_and_closings(cls, entries: Iterable[Directive]) -> None:
 
        for entry in entries:
 
            # type ignores because Beancount's directives aren't type-checkable.
 
            if isinstance(entry, bc_data.Open):
 
                cls.load_opening(entry)  # type:ignore[arg-type]
 
            elif isinstance(entry, bc_data.Close):
 
                cls.load_closing(entry)  # type:ignore[arg-type]
 

	
 
    @classmethod
 
    def load_from_books(cls, entries: Iterable[Directive], options_map: OptionsMap) -> None:
 
        cls.load_options_map(options_map)
 
        cls.load_openings_and_closings(entries)
 

	
 
    @classmethod
 
    def is_account(cls, s: str) -> bool:
 
        return cls.ACCOUNT_RE.fullmatch(s) is not None
 

	
 
    @property
 
    def meta(self) -> AccountMeta:
 
        return self._meta_map[self]
 

	
 
    def is_cash_equivalent(self) -> bool:
 
        return (
 
            self.is_under('Assets:') is not None
 
            and self.is_under('Assets:Prepaid', 'Assets:Receivable') is None
 
        )
 

	
 
    def is_checking(self) -> bool:
 
        return self.is_cash_equivalent() and ':Check' in self
 

	
 
    def is_credit_card(self) -> bool:
 
        return self.is_under('Liabilities:CreditCard') is not None
 

	
 
    def is_opening_equity(self) -> bool:
 
        return self.is_under('Equity:Funds', 'Equity:OpeningBalance') is not None
 

	
 
    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
 

	
 
    def _find_part_slice(self, index: int) -> slice:
 
        if index < 0:
 
            raise ValueError(f"bad part index {index!r}")
 
        start = 0
 
        for _ in range(index):
 
            try:
 
                start = self.index(self.SEP, start) + 1
 
            except ValueError:
 
                raise IndexError("part index {index!r} out of range") from None
tests/test_data_account.py
Show inline comments
...
 
@@ -252,64 +252,84 @@ def test_load_closing_without_opening(clean_account_meta):
 
def test_load_openings_and_closings(clean_account_meta):
 
    entries = [
 
        Open({'lineno': 1, 'income-type': 'Donations'},
 
             Date(2000, 3, 1), 'Income:Donations', None, None),
 
        Open({'lineno': 2},
 
             Date(2000, 3, 1), 'Income:Other', None, None),
 
        Open({'lineno': 3, 'asset-type': 'Cash equivalent'},
 
             Date(2000, 4, 1), 'Assets:Checking', ['USD', 'EUR'], Booking.STRICT),
 
        testutil.Transaction(date=Date(2000, 4, 10), postings=[
 
            ('Income:Donations', -10),
 
            ('Assets:Checking', 10),
 
        ]),
 
        Close({'lineno': 30, 'why': 'Changed banks'},
 
              Date(2000, 5, 1), 'Assets:Checking')
 
    ]
 
    data.Account.load_openings_and_closings(iter(entries))
 
    check_account_meta('Income:Donations', entries[0])
 
    check_account_meta('Income:Other', entries[1])
 
    check_account_meta('Assets:Checking', entries[2], entries[-1])
 

	
 
@pytest.mark.parametrize('account_s', [
 
    'Assets:Bank:Checking',
 
    'Equity:Funds:Restricted',
 
    'Expenses:Other',
 
    'Income:Donations',
 
    'Liabilities:CreditCard:Visa',
 
])
 
def test_is_account(account_s):
 
    assert data.Account.is_account(account_s)
 

	
 
@pytest.mark.parametrize('account_s', [
 
    'Assets:Bank:12-345',
 
    'Equity:Funds:Restricted',
 
    'Expenses:Other',
 
    'Income:Donations',
 
    'Liabilities:CreditCard:Visa0123',
 
])
 
def test_is_account(clean_account_meta, account_s):
 
    assert data.Account.is_account(account_s)
 

	
 
@pytest.mark.parametrize('account_s', [
 
    'Assets:checking',
 
    'Assets::Cash',
 
    'Equity',
 
    'Liabilities:Credit Card',
 
    'income:Donations',
 
    'Expenses:Banking_Fees',
 
    'Revenue:Grants',
 
])
 
def test_is_not_account(clean_account_meta, account_s):
 
    assert not data.Account.is_account(account_s)
 

	
 
@pytest.mark.parametrize('account_s,expected', [
 
    ('Revenue:Donations', True),
 
    ('Costs:Other', True),
 
    ('Income:Donations', False),
 
    ('Expenses:Other', False),
 
])
 
def test_is_account_respects_configured_roots(clean_account_meta, account_s, expected):
 
    config = bc_options.OPTIONS_DEFAULTS.copy()
 
    config['name_expenses'] = 'Costs'
 
    config['name_income'] = 'Revenue'
 
    data.Account.load_options_map(config)
 
    assert data.Account.is_account(account_s) == expected
 

	
 
def test_load_from_books(clean_account_meta):
 
    entries = [
 
        Open({'lineno': 310}, Date(2001, 1, 1), 'Assets:Bank:Checking', ['USD'], None),
 
        Open({'lineno': 315}, Date(2001, 2, 1), 'Revenue:Donations', None, Booking.STRICT),
 
        testutil.Transaction(date=Date(2001, 2, 10), postings=[
 
            ('Revenue:Donations', -10),
 
            ('Assets:Bank:Checking', 10),
 
        ]),
 
        Close({'lineno': 320}, Date(2001, 3, 1), 'Assets:Bank:Checking'),
 
    ]
 
    config = bc_options.OPTIONS_DEFAULTS.copy()
 
    config['name_expenses'] = 'Costs'
 
    config['name_income'] = 'Revenue'
 
    data.Account.load_from_books(entries, config)
 
    for post in entries[2].postings:
 
        assert data.Account.is_account(post.account)
 
    check_meta = data.Account(entries[0].account).meta
 
    assert check_meta.open_date == entries[0].date
 
    assert check_meta.close_date == entries[-1].date
0 comments (0 inline, 0 general)