From 30d371278aebe88aa3623c88b996f4af5776503a 2020-03-29 14:30:54 From: Brett Smith Date: 2020-03-29 14:30:54 Subject: [PATCH] plugin: Refactor hooks to use new payment-related methods. --- diff --git a/conservancy_beancount/plugin/meta_receipt.py b/conservancy_beancount/plugin/meta_receipt.py index 0c4655993e94811c567ff2c026953b99090cb614..013a4d5d81c3321387f9820a0def54e1020cc5dd 100644 --- a/conservancy_beancount/plugin/meta_receipt.py +++ b/conservancy_beancount/plugin/meta_receipt.py @@ -15,6 +15,7 @@ # along with this program. If not, see . from . import core +from .. import config as configmod from .. import data from .. import errors as errormod from ..beancount_types import ( @@ -23,14 +24,16 @@ from ..beancount_types import ( ) class MetaReceipt(core._RequireLinksPostingMetadataHook): - DEFAULT_STOP_AMOUNT = 0 METADATA_KEY = 'receipt' + def __init__(self, config: configmod.Config) -> None: + self.payment_threshold = -abs(config.payment_threshold()) + def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool: return bool( (post.account.is_real_asset() or post.account.is_under('Liabilities')) - and post.units.number - and post.units.number < self.DEFAULT_STOP_AMOUNT + and post.units.number is not None + and post.units.number < self.payment_threshold ) def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter: diff --git a/conservancy_beancount/plugin/meta_tax_implication.py b/conservancy_beancount/plugin/meta_tax_implication.py index 2716dd1aff78ce31d477b818c8343198ceee1e5b..3a74f45d15390d01532a5c36cb1c7e46aa2a99aa 100644 --- a/conservancy_beancount/plugin/meta_tax_implication.py +++ b/conservancy_beancount/plugin/meta_tax_implication.py @@ -17,13 +17,12 @@ import decimal from . import core +from .. import config as configmod from .. import data from ..beancount_types import ( Transaction, ) -DEFAULT_STOP_AMOUNT = decimal.Decimal(0) - class MetaTaxImplication(core._NormalizePostingMetadataHook): VALUES_ENUM = core.MetadataEnum('tax-implication', [ '1099', @@ -45,9 +44,8 @@ class MetaTaxImplication(core._NormalizePostingMetadataHook): 'W2', ]) + def __init__(self, config: configmod.Config) -> None: + self.payment_threshold = config.payment_threshold() + def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool: - return bool( - post.account.is_real_asset() - and post.units.number - and post.units.number < DEFAULT_STOP_AMOUNT - ) + return post.is_payment(self.payment_threshold) diff --git a/tests/testutil.py b/tests/testutil.py index 01060048b347633db284e18db050ae8a22e4ce92..4e06dc439452ad95c31884f00dd47a4da18ed6be 100644 --- a/tests/testutil.py +++ b/tests/testutil.py @@ -144,10 +144,12 @@ class Transaction: class TestConfig: - def __init__(self, + def __init__(self, *, + payment_threshold=0, repo_path=None, rt_client=None, ): + self._payment_threshold = Decimal(payment_threshold) self.repo_path = test_path(repo_path) self._rt_client = rt_client if rt_client is None: @@ -155,6 +157,9 @@ class TestConfig: else: self._rt_wrapper = rtutil.RT(rt_client) + def payment_threshold(self): + return self._payment_threshold + def repository_path(self): return self.repo_path