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
...
 
@@ -20,3 +20,2 @@ import datetime
 
import beancount.core.data as bc_data
 
from .plugin import errors
 

	
...
 
@@ -35,4 +34,2 @@ from typing import (
 
Account = bc_data.Account
 
Error = errors._BaseError
 
ErrorIter = Iterable[Error]
 
MetaKey = str
...
 
@@ -42,10 +39,2 @@ 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):
conservancy_beancount/errors.py
Show inline comments
 
file renamed from conservancy_beancount/plugin/errors.py to conservancy_beancount/errors.py
...
 
@@ -16,3 +16,7 @@
 

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

	
 
class Error(Exception):
 
    def __init__(self, message, entry, source=None):
...
 
@@ -30,3 +34,5 @@ class _BaseError(Exception):
 

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

	
 
class InvalidMetadataError(Error):
 
    def __init__(self, txn, post, key, value=None, source=None):
conservancy_beancount/plugin/__init__.py
Show inline comments
...
 
@@ -30,6 +30,5 @@ from typing import (
 
)
 
from .._typing import (
 
from ..beancount_types import (
 
    ALL_DIRECTIVES,
 
    Directive,
 
    Error,
 
)
...
 
@@ -39,2 +38,5 @@ from .core import (
 
)
 
from ..errors import (
 
    Error,
 
)
 

	
conservancy_beancount/plugin/core.py
Show inline comments
...
 
@@ -20,5 +20,6 @@ import re
 

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

	
 
from typing import (
 
    Any,
 
    FrozenSet,
...
 
@@ -31,8 +32,5 @@ from typing import (
 
)
 
from .._typing import (
 
from ..beancount_types import (
 
    Account,
 
    Directive,
 
    Error,
 
    ErrorIter,
 
    LessComparable,
 
    MetaKey,
...
 
@@ -64,3 +62,3 @@ class Hook(Generic[Entry], metaclass=abc.ABCMeta):
 
    @abc.abstractmethod
 
    def run(self, entry: Entry) -> ErrorIter: ...
 
    def run(self, entry: Entry) -> errormod.Iter: ...
 

	
...
 
@@ -74,2 +72,10 @@ TransactionHook = Hook[Transaction]
 

	
 
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)
...
 
@@ -202,3 +208,3 @@ class _PostingHook(TransactionHook, metaclass=abc.ABCMeta):
 

	
 
    def run(self, txn: Transaction) -> ErrorIter:
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        if self._run_on_txn(txn):
...
 
@@ -209,3 +215,3 @@ 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: ...
 

	
...
 
@@ -231,6 +237,6 @@ class _NormalizePostingMetadataHook(_PostingHook):
 

	
 
    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:
...
 
@@ -238,3 +244,3 @@ class _NormalizePostingMetadataHook(_PostingHook):
 
                set_value = self._default_value(txn, post)
 
            except errormod._BaseError as error_:
 
            except errormod.Error as error_:
 
                error = error_
conservancy_beancount/plugin/meta_expense_allocation.py
Show inline comments
...
 
@@ -17,3 +17,3 @@
 
from . import core
 
from .._typing import (
 
from ..beancount_types import (
 
    MetaValueEnum,
conservancy_beancount/plugin/meta_tax_implication.py
Show inline comments
...
 
@@ -19,3 +19,3 @@ import decimal
 
from . import core
 
from .._typing import (
 
from ..beancount_types import (
 
    Posting,
tests/test_plugin_run.py
Show inline comments
...
 
@@ -20,3 +20,3 @@ from . import testutil
 

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

	
...
 
@@ -27,3 +27,3 @@ HOOK_REGISTRY = plugin.HookRegistry()
 
class TransactionCounter:
 
    DIRECTIVE = _typing.Transaction
 
    DIRECTIVE = beancount_types.Transaction
 
    HOOK_GROUPS = frozenset()
...
 
@@ -36,3 +36,3 @@ class TransactionCounter:
 
class PostingCounter(TransactionCounter):
 
    DIRECTIVE = _typing.Transaction
 
    DIRECTIVE = beancount_types.Transaction
 
    HOOK_GROUPS = frozenset(['posting'])
0 comments (0 inline, 0 general)