Changeset - 46cfc558ecbb
[Not reviewed]
0 5 0
Brett Smith - 4 years ago 2020-03-28 17:36:56
brettcsmith@brettcsmith.org
plugin: Link checkers use Metadata class.
5 files changed with 60 insertions and 14 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/errors.py
Show inline comments
...
 
@@ -46,34 +46,38 @@ class BrokenLinkError(Error):
 
            source,
 
        )
 

	
 
class BrokenRTLinkError(Error):
 
    def __init__(self, txn, key, link, parsed=True, source=None):
 
        if parsed:
 
            msg_fmt = "{} not found in RT: {}"
 
        else:
 
            msg_fmt = "{} link is malformed: {}"
 
        super().__init__(
 
            msg_fmt.format(key, link),
 
            txn,
 
            source,
 
        )
 

	
 
class ConfigurationError(Error):
 
    def __init__(self, message, entry=None, source=None):
 
        if source is None:
 
            source = {}
 
        self._fill_source(source)
 
        super().__init__(message, entry, source)
 

	
 

	
 
class InvalidMetadataError(Error):
 
    def __init__(self, txn, key, value=None, post=None, source=None):
 
    def __init__(self, txn, key, value=None, post=None, need_type=str, source=None):
 
        if post is None:
 
            srcname = 'transaction'
 
        else:
 
            srcname = post.account
 
        if value is None:
 
            msg = "{} missing {}".format(srcname, key)
 
        else:
 
        elif isinstance(value, need_type):
 
            msg = "{} has invalid {}: {}".format(srcname, key, value)
 
        else:
 
            msg = "{} has wrong type of {}: expected {} but is a {}".format(
 
                srcname, key, need_type.__name__, type(value).__name__,
 
            )
 
        super().__init__(msg, txn, source)
conservancy_beancount/plugin/meta_repo_links.py
Show inline comments
...
 
@@ -2,58 +2,67 @@
 
# Copyright © 2020  Brett Smith
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU Affero General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import re
 

	
 
from . import core
 
from .. import config as configmod
 
from .. import data
 
from .. import errors as errormod
 
from ..beancount_types import (
 
    MetaKey,
 
    MetaValue,
 
    Posting,
 
    Transaction,
 
)
 

	
 
from typing import (
 
    Mapping,
 
    MutableMapping,
 
    Optional,
 
)
 

	
 
class MetaRepoLinks(core.TransactionHook):
 
    HOOK_GROUPS = frozenset(['linkcheck'])
 
    PATH_PUNCT_RE = re.compile(r'[:/]')
 

	
 
    def __init__(self, config: configmod.Config) -> None:
 
        repo_path = config.repository_path()
 
        if repo_path is None:
 
            raise errormod.ConfigurationError("no repository configured")
 
        self.repo_path = repo_path
 

	
 
    def _check_links(self,
 
                     meta: MutableMapping[MetaKey, MetaValue],
 
                     txn: Transaction,
 
                     meta: Mapping[MetaKey, MetaValue],
 
                     post: Optional[Posting]=None,
 
    ) -> errormod.Iter:
 
        for key in data.LINK_METADATA.intersection(meta):
 
            for link in str(meta[key]).split():
 
        metadata = data.Metadata(meta)
 
        for key in data.LINK_METADATA:
 
            try:
 
                links = metadata.get_links(key)
 
            except TypeError:
 
                yield errormod.InvalidMetadataError(txn, key, meta[key], post)
 
            else:
 
                for link in links:
 
                    match = self.PATH_PUNCT_RE.search(link)
 
                    if match and match.group(0) == ':':
 
                        pass
 
                    elif not (self.repo_path / link).exists():
 
                        yield errormod.BrokenLinkError(txn, key, link)
 

	
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        yield from self._check_links(txn, txn.meta)
 
        yield from self._check_links(txn.meta, txn)
 
        for post in txn.postings:
 
            if post.meta is not None:
 
                yield from self._check_links(txn, post.meta)
 
                yield from self._check_links(post.meta, txn, post)
conservancy_beancount/plugin/meta_rt_links.py
Show inline comments
 
"""meta_rt_links - Check that RT links are valid"""
 
# Copyright © 2020  Brett Smith
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU Affero General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
from . import core
 
from .. import config as configmod
 
from .. import data
 
from .. import errors as errormod
 
from ..beancount_types import (
 
    MetaKey,
 
    MetaValue,
 
    Posting,
 
    Transaction,
 
)
 

	
 
from typing import (
 
    Mapping,
 
    MutableMapping,
 
    Optional,
 
)
 

	
 
class MetaRTLinks(core.TransactionHook):
 
    HOOK_GROUPS = frozenset(['linkcheck', 'network', 'rt'])
 
    LINK_METADATA = data.LINK_METADATA.union(['rt-id'])
 

	
 
    def __init__(self, config: configmod.Config) -> None:
 
        rt_wrapper = config.rt_wrapper()
 
        if rt_wrapper is None:
 
            raise errormod.ConfigurationError("can't log in to RT")
 
        self.rt = rt_wrapper
 

	
 
    def _check_links(self,
 
                     meta: MutableMapping[MetaKey, MetaValue],
 
                     txn: Transaction,
 
                     meta: Mapping[MetaKey, MetaValue],
 
                     post: Optional[Posting]=None,
 
    ) -> errormod.Iter:
 
        for key in self.LINK_METADATA.intersection(meta):
 
            for link in str(meta[key]).split():
 
        metadata = data.Metadata(meta)
 
        for key in self.LINK_METADATA:
 
            try:
 
                links = metadata.get_links(key)
 
            except TypeError:
 
                yield errormod.InvalidMetadataError(txn, key, meta[key], post)
 
            else:
 
                for link in links:
 
                    if not link.startswith('rt:'):
 
                        continue
 
                    parsed = self.rt.parse(link)
 
                    if parsed is None or not self.rt.exists(*parsed):
 
                        yield errormod.BrokenRTLinkError(txn, key, link, parsed)
 

	
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        yield from self._check_links(txn, txn.meta)
 
        yield from self._check_links(txn.meta, txn)
 
        for post in txn.postings:
 
            if post.meta is not None:
 
                yield from self._check_links(txn, post.meta)
 
                yield from self._check_links(post.meta, txn, post)
tests/test_meta_repo_links.py
Show inline comments
...
 
@@ -79,48 +79,60 @@ def test_good_post_links(hook):
 
        ('Assets:Cash', 5),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
def test_bad_txn_links(hook):
 
    meta = build_meta(None, BAD_LINKS)
 
    txn = testutil.Transaction(**meta, postings=[
 
        ('Income:Donations', -5),
 
        ('Assets:Cash', 5),
 
    ])
 
    expected = {NOT_FOUND_MSG(key, value) for key, value in meta.items()}
 
    actual = {error.message for error in hook.run(txn)}
 
    assert expected == actual
 

	
 
def test_bad_post_links(hook):
 
    meta = build_meta(None, BAD_LINKS)
 
    txn = testutil.Transaction(postings=[
 
        ('Income:Donations', -5, meta.copy()),
 
        ('Assets:Cash', 5),
 
    ])
 
    expected = {NOT_FOUND_MSG(key, value) for key, value in meta.items()}
 
    actual = {error.message for error in hook.run(txn)}
 
    assert expected == actual
 

	
 
@pytest.mark.parametrize('value', testutil.NON_STRING_METADATA_VALUES)
 
def test_bad_metadata_type(hook, value):
 
    txn = testutil.Transaction(**{'check': value}, postings=[
 
        ('Income:Donations', -5),
 
        ('Assets:Cash', 5),
 
    ])
 
    expected = {'transaction has wrong type of check: expected str but is a {}'.format(
 
        type(value).__name__,
 
    )}
 
    actual = {error.message for error in hook.run(txn)}
 
    assert expected == actual
 

	
 
@pytest.mark.parametrize('ext_doc', [
 
    'rt:123',
 
    'rt:456/789',
 
    'rt://ticket/23',
 
    'rt://ticket/34/attachments/567890',
 
])
 
def test_docs_outside_repository_not_checked(hook, ext_doc):
 
    txn = testutil.Transaction(
 
        receipt='{} {} {}'.format(GOOD_LINKS[0], ext_doc, BAD_LINKS[1]),
 
        postings=[
 
            ('Income:Donations', -5),
 
            ('Assets:Cash', 5),
 
        ])
 
    expected = {NOT_FOUND_MSG('receipt', BAD_LINKS[1])}
 
    actual = {error.message for error in hook.run(txn)}
 
    assert expected == actual
 

	
 
def test_mixed_results(hook):
 
    txn = testutil.Transaction(
 
        approval='{} {}'.format(*GOOD_LINKS),
 
        contract='{} {}'.format(BAD_LINKS[0], GOOD_LINKS[1]),
 
        postings=[
 
            ('Income:Donations', -5, {'invoice': '{} {}'.format(*BAD_LINKS)}),
 
            ('Assets:Cash', 5, {'statement': '{} {}'.format(GOOD_LINKS[0], BAD_LINKS[1])}),
tests/test_meta_rt_links.py
Show inline comments
...
 
@@ -98,48 +98,60 @@ def test_good_post_links(hook):
 
def test_bad_txn_links(hook, link_source, format_error):
 
    meta = build_meta(None, link_source)
 
    txn = testutil.Transaction(**meta, postings=[
 
        ('Income:Donations', -5),
 
        ('Assets:Cash', 5),
 
    ])
 
    expected = {format_error(key, value) for key, value in meta.items()}
 
    actual = {error.message for error in hook.run(txn)}
 
    assert expected == actual
 

	
 
@pytest.mark.parametrize('link_source,format_error', [
 
    (MALFORMED_LINKS, MALFORMED_MSG),
 
    (NOT_FOUND_LINKS, NOT_FOUND_MSG),
 
])
 
def test_bad_post_links(hook, link_source, format_error):
 
    meta = build_meta(None, link_source)
 
    txn = testutil.Transaction(postings=[
 
        ('Income:Donations', -5, meta.copy()),
 
        ('Assets:Cash', 5),
 
    ])
 
    expected = {format_error(key, value) for key, value in meta.items()}
 
    actual = {error.message for error in hook.run(txn)}
 
    assert expected == actual
 

	
 
@pytest.mark.parametrize('value', testutil.NON_STRING_METADATA_VALUES)
 
def test_bad_metadata_type(hook, value):
 
    txn = testutil.Transaction(**{'rt-id': value}, postings=[
 
        ('Income:Donations', -5),
 
        ('Assets:Cash', 5),
 
    ])
 
    expected = {'transaction has wrong type of rt-id: expected str but is a {}'.format(
 
        type(value).__name__,
 
    )}
 
    actual = {error.message for error in hook.run(txn)}
 
    assert expected == actual
 

	
 
@pytest.mark.parametrize('ext_doc', [
 
    'statement.txt',
 
    'https://example.org/',
 
])
 
def test_docs_outside_rt_not_checked(hook, ext_doc):
 
    txn = testutil.Transaction(
 
        receipt='{} {} {}'.format(GOOD_LINKS[0], ext_doc, MALFORMED_LINKS[1]),
 
        postings=[
 
            ('Income:Donations', -5),
 
            ('Assets:Cash', 5),
 
        ])
 
    expected = {MALFORMED_MSG('receipt', MALFORMED_LINKS[1])}
 
    actual = {error.message for error in hook.run(txn)}
 
    assert expected == actual
 

	
 
def test_mixed_results(hook):
 
    txn = testutil.Transaction(
 
        approval='{} {}'.format(*GOOD_LINKS),
 
        contract='{} {}'.format(MALFORMED_LINKS[0], GOOD_LINKS[1]),
 
        postings=[
 
            ('Income:Donations', -5, {'invoice': '{} {}'.format(*NOT_FOUND_LINKS)}),
 
            ('Assets:Cash', 5, {'statement': '{} {}'.format(GOOD_LINKS[0], MALFORMED_LINKS[1])}),
 
        ])
 
    expected = {
0 comments (0 inline, 0 general)