Changeset - 3fbc14d377ac
[Not reviewed]
2 5 2
Brett Smith - 4 years ago 2020-03-15 20:03:57
brettcsmith@brettcsmith.org
Improve organization between modules.

* Rename _typing to beancount_types to better reflect what it is.
* LessComparable isn't a Beancount type, so move that to
plugin.core with its dependent helper classes.
* Errors are a core Beancount concept, so move that module to the
top level and have it include appropriate type definitions.
7 files changed with 35 insertions and 32 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/beancount_types.py
Show inline comments
 
file renamed from conservancy_beancount/_typing.py to conservancy_beancount/beancount_types.py
 
"""Type definitions for Conservancy Beancount code"""
 
"""Type definitions for Beancount data structures"""
 
# Copyright © 2020  Brett Smith
 
#
...
 
@@ -19,5 +19,4 @@ import datetime
 

	
 
import beancount.core.data as bc_data
 
from .plugin import errors
 

	
 
from typing import (
...
 
@@ -34,6 +33,4 @@ from typing import (
 

	
 
Account = bc_data.Account
 
Error = errors._BaseError
 
ErrorIter = Iterable[Error]
 
MetaKey = str
 
MetaValue = Any
...
 
@@ -41,12 +38,4 @@ MetaValueEnum = str
 
Posting = bc_data.Posting
 

	
 
class LessComparable(metaclass=abc.ABCMeta):
 
    @abc.abstractmethod
 
    def __le__(self, other: Any) -> bool: ...
 

	
 
    @abc.abstractmethod
 
    def __lt__(self, other: Any) -> bool: ...
 

	
 

	
 
class Directive(NamedTuple):
 
    meta: bc_data.Meta
conservancy_beancount/errors.py
Show inline comments
 
file renamed from conservancy_beancount/plugin/errors.py to conservancy_beancount/errors.py
...
 
@@ -15,5 +15,9 @@
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
class _BaseError(Exception):
 
from typing import (
 
    Iterable,
 
)
 

	
 
class Error(Exception):
 
    def __init__(self, message, entry, source=None):
 
        self.message = message
...
 
@@ -29,5 +33,7 @@ class _BaseError(Exception):
 

	
 

	
 
class InvalidMetadataError(_BaseError):
 
Iter = Iterable[Error]
 

	
 
class InvalidMetadataError(Error):
 
    def __init__(self, txn, post, key, value=None, source=None):
 
        if value is None:
conservancy_beancount/plugin/__init__.py
Show inline comments
...
 
@@ -29,8 +29,7 @@ from typing import (
 
    Type,
 
)
 
from .._typing import (
 
from ..beancount_types import (
 
    ALL_DIRECTIVES,
 
    Directive,
 
    Error,
 
)
 
from .core import (
...
 
@@ -38,4 +37,7 @@ from .core import (
 
    HookName,
 
)
 
from ..errors import (
 
    Error,
 
)
 

	
 
__plugins__ = ['run']
conservancy_beancount/plugin/core.py
Show inline comments
...
 
@@ -19,7 +19,8 @@ import datetime
 
import re
 

	
 
from . import errors as errormod
 
from .. import errors as errormod
 

	
 
from typing import (
 
    Any,
 
    FrozenSet,
 
    Generic,
...
 
@@ -30,10 +31,7 @@ from typing import (
 
    TypeVar,
 
)
 
from .._typing import (
 
from ..beancount_types import (
 
    Account,
 
    Directive,
 
    Error,
 
    ErrorIter,
 
    LessComparable,
 
    MetaKey,
 
    MetaValue,
...
 
@@ -63,5 +61,5 @@ class Hook(Generic[Entry], metaclass=abc.ABCMeta):
 

	
 
    @abc.abstractmethod
 
    def run(self, entry: Entry) -> ErrorIter: ...
 
    def run(self, entry: Entry) -> errormod.Iter: ...
 

	
 
    def __init_subclass__(cls):
...
 
@@ -73,4 +71,12 @@ TransactionHook = Hook[Transaction]
 
### HELPER CLASSES
 

	
 
class LessComparable(metaclass=abc.ABCMeta):
 
    @abc.abstractmethod
 
    def __le__(self, other: Any) -> bool: ...
 

	
 
    @abc.abstractmethod
 
    def __lt__(self, other: Any) -> bool: ...
 

	
 

	
 
CT = TypeVar('CT', bound=LessComparable)
 
class _GenericRange(Generic[CT]):
...
 
@@ -201,5 +207,5 @@ class _PostingHook(TransactionHook, metaclass=abc.ABCMeta):
 
        return True
 

	
 
    def run(self, txn: Transaction) -> ErrorIter:
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        if self._run_on_txn(txn):
 
            for index, post in enumerate(txn.postings):
...
 
@@ -208,5 +214,5 @@ class _PostingHook(TransactionHook, metaclass=abc.ABCMeta):
 

	
 
    @abc.abstractmethod
 
    def post_run(self, txn: Transaction, post: Posting, post_index: int) -> ErrorIter: ...
 
    def post_run(self, txn: Transaction, post: Posting, post_index: int) -> errormod.Iter: ...
 

	
 

	
...
 
@@ -230,12 +236,12 @@ class _NormalizePostingMetadataHook(_PostingHook):
 
        raise errormod.InvalidMetadataError(txn, post, self.METADATA_KEY)
 

	
 
    def post_run(self, txn: Transaction, post: Posting, post_index: int) -> ErrorIter:
 
    def post_run(self, txn: Transaction, post: Posting, post_index: int) -> errormod.Iter:
 
        source_value = self._meta_get(txn, post, self.METADATA_KEY)
 
        set_value = source_value
 
        error: Optional[Error] = None
 
        error: Optional[errormod.Error] = None
 
        if source_value is None:
 
            try:
 
                set_value = self._default_value(txn, post)
 
            except errormod._BaseError as error_:
 
            except errormod.Error as error_:
 
                error = error_
 
        else:
conservancy_beancount/plugin/meta_expense_allocation.py
Show inline comments
...
 
@@ -16,5 +16,5 @@
 

	
 
from . import core
 
from .._typing import (
 
from ..beancount_types import (
 
    MetaValueEnum,
 
    Posting,
conservancy_beancount/plugin/meta_tax_implication.py
Show inline comments
...
 
@@ -18,5 +18,5 @@ import decimal
 

	
 
from . import core
 
from .._typing import (
 
from ..beancount_types import (
 
    Posting,
 
    Transaction,
tests/test_plugin_run.py
Show inline comments
...
 
@@ -19,5 +19,5 @@ import pytest
 
from . import testutil
 

	
 
from conservancy_beancount import plugin, _typing
 
from conservancy_beancount import beancount_types, plugin
 

	
 
CONFIG_MAP = {}
...
 
@@ -26,5 +26,5 @@ HOOK_REGISTRY = plugin.HookRegistry()
 
@HOOK_REGISTRY.add_hook
 
class TransactionCounter:
 
    DIRECTIVE = _typing.Transaction
 
    DIRECTIVE = beancount_types.Transaction
 
    HOOK_GROUPS = frozenset()
 

	
...
 
@@ -35,5 +35,5 @@ class TransactionCounter:
 
@HOOK_REGISTRY.add_hook
 
class PostingCounter(TransactionCounter):
 
    DIRECTIVE = _typing.Transaction
 
    DIRECTIVE = beancount_types.Transaction
 
    HOOK_GROUPS = frozenset(['posting'])
 

	
0 comments (0 inline, 0 general)