Esempio n. 1
0
def tag_discovered_documents(
        entries: Entries,
        options_map: BeancountOptions) -> Tuple[Entries, List[BeancountError]]:
    """Tag automatically added documents."""
    if "documents" not in options_map or not options_map["documents"]:
        return entries, []

    for index, entry in enumerate(entries):
        if isinstance(entry, Document) and entry.meta["lineno"] == 0:
            entries[index] = entry._replace(
                tags=add_to_set(entry.tags, "discovered"))

    return entries, []
Esempio n. 2
0
def test_add_to_set_basic() -> None:
    assert add_to_set(None, "test") == {"test"}
    assert add_to_set(set(), "test") == {"test"}
    assert add_to_set({"test"}, "test") == {"test"}
Esempio n. 3
0
def test_add_to_set_no_mutation() -> None:
    test_set = {"test"}
    assert add_to_set(test_set, "test2") == {"test", "test2"}
    assert test_set == {"test"}
Esempio n. 4
0
def link_documents(entries: Entries,
                   _: Any) -> tuple[Entries, list[DocumentError]]:
    """Link entries to documents."""

    errors = []

    # All document indices by their full file path.
    by_fullname = {}
    # All document indices by their file basename.
    by_basename = defaultdict(list)

    for index, entry in enumerate(entries):
        if isinstance(entry, Document):
            by_fullname[entry.filename] = index
            by_basename[basename(entry.filename)].append((index, entry))

    for index, entry in enumerate(entries):
        disk_docs = [
            value for key, value in entry.meta.items()
            if key.startswith("document")
        ]

        if not disk_docs:
            continue

        hash_ = hash_entry(entry)[:8]
        entry_accounts = get_entry_accounts(entry)
        for disk_doc in disk_docs:
            documents = [
                j for j, document in by_basename[disk_doc]
                if document.account in entry_accounts
            ]
            disk_doc_path = normpath(
                join(dirname(entry.meta["filename"]), disk_doc))
            if disk_doc_path in by_fullname:
                documents.append(by_fullname[disk_doc_path])

            if not documents:
                errors.append(
                    DocumentError(
                        entry.meta,
                        f"Document not found: '{disk_doc}'",
                        entry,
                    ))
                continue

            for j in documents:
                # Since we might link a document multiple times, we have to use
                # the index for the replacement here.
                doc: Document = entries[j]  # type: ignore
                entries[j] = doc._replace(
                    links=add_to_set(doc.links, hash_),
                    tags=add_to_set(doc.tags, "linked"),
                )

            # The other entry types do not support links, so only add links for
            # txns.
            if isinstance(entry, Transaction):
                entries[index] = entry._replace(
                    links=add_to_set(entry.links, hash_))

    return entries, errors