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
...
 
@@ -163,64 +163,69 @@ class Account(str):
 

	
 
    @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.
tests/test_data_account.py
Show inline comments
...
 
@@ -284,32 +284,52 @@ def test_is_account(account_s):
 
    '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)