How to use the fava.core.helpers.FavaModule function in fava

To help you get started, we’ve selected a few fava examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github beancount / fava / fava / core / number.py View on Github external
"""Formatting numbers."""

import copy

from babel.core import Locale, UnknownLocaleError
from beancount.core.display_context import Precision

from fava.core.helpers import FavaModule
from fava.core.fava_options import OptionError


class DecimalFormatModule(FavaModule):
    """Formatting numbers."""

    def __init__(self, ledger):
        super().__init__(ledger)
        self.locale = None
        self.patterns = {}
        self.default_pattern = None

    def load_file(self):
        self.locale = None

        locale_option = self.ledger.fava_options["locale"]
        if self.ledger.options["render_commas"] and not locale_option:
            locale_option = "en"
            self.ledger.fava_options["locale"] = locale_option
github beancount / fava / fava / core / budgets.py View on Github external
from beancount.core.data import Custom
from beancount.core.number import Decimal

from fava.util.date import (
    Interval,
    days_in_daterange,
    number_of_days_in_period,
)
from fava.core.helpers import FavaModule

Budget = namedtuple("Budget", "account date_start period number currency")
BudgetError = namedtuple("BudgetError", "source message entry")


class BudgetModule(FavaModule):
    """Parses budget entries."""

    def __init__(self, ledger):
        super().__init__(ledger)
        self.budget_entries = None

    def load_file(self):
        self.budget_entries, errors = parse_budgets(
            self.ledger.all_entries_by_type[Custom]
        )
        self.ledger.errors.extend(errors)

    def calculate(self, account_name, begin_date, end_date):
        """Calculate the budget for an account in an interval."""
        return calculate_budget(
            self.budget_entries, account_name, begin_date, end_date
github beancount / fava / fava / core / misc.py View on Github external
from collections import namedtuple
import datetime
import io
import re

from beancount.core.data import Custom, Event
from beancount.core import amount

from fava.core.helpers import FavaModule
from fava.core.fava_options import DEFAULTS

FavaError = namedtuple("FavaError", "source message entry")


class FavaMisc(FavaModule):
    """Provides access to some miscellaneous reports."""

    def __init__(self, ledger):
        super().__init__(ledger)
        self.sidebar_links = None
        self.upcoming_events = None

    def load_file(self):
        custom_entries = self.ledger.all_entries_by_type[Custom]
        self.sidebar_links = sidebar_links(custom_entries)

        self.upcoming_events = upcoming_events(
            self.ledger.all_entries_by_type[Event],
            self.ledger.fava_options["upcoming-events"],
        )
github beancount / fava / fava / core / file.py View on Github external
"""Reading/writing Beancount files."""

import codecs
from hashlib import sha256
import os
import re

from beancount.core import data, flags
from beancount.parser.printer import format_entry

from fava.core.helpers import FavaAPIException, FavaModule
from fava.core.misc import align


class FileModule(FavaModule):
    """Functions related to reading/writing to Beancount files."""

    def list_sources(self):
        """List source files.

        Returns:
            A list of all sources files, with the main file listed first.

        """
        main_file = self.ledger.beancount_file_path
        return [main_file] + sorted(
            filter(
                lambda x: x != main_file,
                [
                    os.path.join(os.path.dirname(main_file), filename)
                    for filename in self.ledger.options["include"]
github beancount / fava / fava / core / ingest.py View on Github external
account = importer.file_account(file)
    except Exception:
        account = ""
    try:
        date = importer.file_date(file)
    except Exception:
        date = datetime.date.today()
    try:
        name = importer.file_name(file)
    except Exception:
        name = path.basename(filename)

    return FileImportInfo(importer.name(), account, date, name)


class IngestModule(FavaModule):
    """Exposes ingest functionality."""

    def __init__(self, ledger):
        super().__init__(ledger)
        self.config = []
        self.importers = {}
        self.mtime = None

    @property
    def module_path(self):
        """The path to the importer configuration."""
        config_path = self.ledger.fava_options["import-config"]
        if not config_path:
            return None
        return self.ledger.join_path(config_path)
github beancount / fava / fava / core / attributes.py View on Github external
"""Attributes for auto-completion."""

from beancount.core import getters
from beancount.core.data import Transaction

from fava.core.helpers import FavaModule
from fava.util.ranking import ExponentialDecayRanker


class AttributesModule(FavaModule):
    """Some attributes of the ledger (mostly for auto-completion)."""

    def __init__(self, ledger):
        super().__init__(ledger)
        self.accounts = None
        self.currencies = None
        self.payees = None
        self.links = None
        self.tags = None
        self.years = None

    def load_file(self):
        all_entries = self.ledger.all_entries
        self.links = getters.get_all_links(all_entries)
        self.tags = getters.get_all_tags(all_entries)
        self.years = list(getters.get_active_years(all_entries))[::-1]
github beancount / fava / fava / core / extensions.py View on Github external
"""Fava extensions"""

import os
import inspect

from beancount.core.data import Custom

from fava.core.helpers import FavaModule
from fava.ext import find_extensions


class ExtensionModule(FavaModule):
    """Fava extensions."""

    def __init__(self, ledger):
        super().__init__(ledger)
        self._instances = {}
        self.reports = []

    def load_file(self):
        all_extensions = []
        custom_entries = self.ledger.all_entries_by_type[Custom]
        _extension_entries = extension_entries(custom_entries)

        for extension in _extension_entries:
            extensions, errors = find_extensions(
                os.path.dirname(self.ledger.beancount_file_path), extension
            )
github beancount / fava / fava / core / query_shell.py View on Github external
"""For using the Beancount shell from Fava."""

import contextlib
import io
import textwrap

from beancount.core.data import Query
from beancount.query import query_compile, query_execute, query_parser, shell
from beancount.query.query import run_query
from beancount.utils import pager

from fava.core.helpers import FavaAPIException, FavaModule
from fava.util.excel import to_csv, to_excel, HAVE_EXCEL


class QueryShell(shell.BQLShell, FavaModule):
    """A light wrapper around Beancount's shell."""

    # pylint: disable=too-many-instance-attributes

    def __init__(self, ledger):
        self.ledger = ledger
        self.buffer = io.StringIO()
        self.result = None
        super().__init__(True, None, self.buffer)
        self.stdout = self.buffer
        self.entries = None
        self.errors = None
        self.options_map = None
        self.queries = []

    def load_file(self):
github beancount / fava / fava / core / charts.py View on Github external
if isinstance(o, (datetime.date, Amount, Position)):
            return str(o)
        if isinstance(o, (set, frozenset)):
            return list(o)
        try:
            return JSONEncoder.default(self, o)
        except TypeError:
            return str(o)


def inv_to_dict(inventory):
    """Convert an inventory to a simple cost->number dict."""
    return {pos.units.currency: pos.units.number for pos in inventory}


class ChartModule(FavaModule):
    """Return data for the various charts in Fava."""

    __slots__ = ["ledger"]

    def events(self, event_type=None):
        """All events for a given event type."""
        return [
            {
                "type": entry.type,
                "date": entry.date,
                "description": entry.description,
            }
            for entry in self.ledger.events(event_type)
        ]

    def hierarchy(self, account_name, begin=None, end=None):