Changeset - da056917bfa4
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-09-05 18:59:13
brettcsmith@brettcsmith.org
meta_repo_links: Check all non-RT links. RT#12517

This is less "future-proof," but the thing is, it's premature to try
to anticipate what other link formats will be in the future. See
discussion in comments.
3 files changed with 22 insertions and 4 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/meta_repo_links.py
Show inline comments
...
 
@@ -11,61 +11,73 @@
 
# 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 .. import rtutil
 
from ..beancount_types import (
 
    MetaKey,
 
    MetaValue,
 
    Posting,
 
    Transaction,
 
)
 

	
 
from typing import (
 
    MutableMapping,
 
    Optional,
 
)
 

	
 
class MetaRepoLinks(core.TransactionHook):
 
    HOOK_GROUPS = frozenset(['linkcheck'])
 
    LINK_METADATA = data.LINK_METADATA.difference('rt-id')
 
    PATH_PUNCT_RE = re.compile(r'[:/]')
 
    SKIP_FLAGS = '!'
 

	
 
    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,
 
                     post: Optional[Posting]=None,
 
    ) -> errormod.Iter:
 
        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:
 
                    match = self.PATH_PUNCT_RE.search(link)
 
                    if match and match.group(0) == ':':
 
                    # FUTURE DEVELOPMENT NOTE: As of this writing we have two
 
                    # link checkers. Right now the division of reporting
 
                    # responsibility is: MetaRTLinks reports problems with any
 
                    # link that starts with `rt:`, while this checker reports
 
                    # problems with anything else.
 
                    # If we add more link checkers in the future, we might need
 
                    # to give more thought about which checker is responsible
 
                    # for flagging links in "unknown formats." But today, I
 
                    # can't write better code that anticipates that need
 
                    # without having any idea of what future link formats will
 
                    # look like. Today, asking the RT parser "can you handle
 
                    # this?" is the safest way to make sure all bad links get
 
                    # reported.
 
                    if rtutil.RT.parse(link) is not None:
 
                        pass
 
                    elif not (self.repo_path / link).exists():
 
                        yield errormod.BrokenLinkError(txn, key, link)
 

	
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        if self._run_on_txn(txn):
 
            yield from self._check_links(txn.meta, txn)
 
            for post in txn.postings:
 
                if post.meta is not None:
 
                    yield from self._check_links(post.meta, txn, post)
setup.py
Show inline comments
 
#!/usr/bin/env python3
 

	
 
from setuptools import setup
 

	
 
setup(
 
    name='conservancy_beancount',
 
    description="Plugin, library, and reports for reading Conservancy's books",
 
    version='1.9.6',
 
    version='1.9.7',
 
    author='Software Freedom Conservancy',
 
    author_email='info@sfconservancy.org',
 
    license='GNU AGPLv3+',
 

	
 
    install_requires=[
 
        'babel>=2.6',  # Debian:python3-babel
 
        'beancount>=2.2',  # Debian:beancount
 
        'GitPython>=2.0',  # Debian:python3-git
 
        # 1.4.1 crashes when trying to save some documents.
 
        'odfpy>=1.4.0,!=1.4.1',  # Debian:python3-odf
 
        'PyYAML>=3.0',  # Debian:python3-yaml
 
        'regex',  # Debian:python3-regex
tests/test_meta_repo_links.py
Show inline comments
...
 
@@ -36,24 +36,30 @@ METADATA_KEYS = [
 
    'statement',
 
    'tax-statement',
 
]
 

	
 
GOOD_LINKS = [Path(s) for s in [
 
    'Projects/project-data.yml',
 
    'Projects/project-list.yml',
 
]]
 

	
 
BAD_LINKS = [Path(s) for s in [
 
    'NonexistentDirectory/NonexistentFile1.txt',
 
    'NonexistentDirectory/NonexistentFile2.txt',
 
    'egproto:',
 
    'egproto:123',
 
    'egproto:123/456',
 
    'egproto:foo'
 
    'egproto:/foo/bar',
 
    ';egproto::',
 
]]
 

	
 
NOT_FOUND_MSG = '{} not found in repository: {}'.format
 

	
 
def build_meta(keys=None, *sources):
 
    if keys is None:
 
        keys = iter(METADATA_KEYS)
 
    sources = (itertools.cycle(src) for src in sources)
 
    return {key: ' '.join(str(x) for x in rest)
 
            for key, *rest in zip(keys, *sources)}
 

	
 
@pytest.fixture(scope='module')
0 comments (0 inline, 0 general)