Skip to content

Commit

Permalink
fava_options: use dataclass
Browse files Browse the repository at this point in the history
Also use underscores in option names internally.This allows using the
option names as variable names (in both Python and Javascript).
  • Loading branch information
yagebu committed Jan 13, 2022
1 parent 16fcbe7 commit 233d30a
Show file tree
Hide file tree
Showing 31 changed files with 161 additions and 214 deletions.
2 changes: 1 addition & 1 deletion frontend/src/editor/EditorMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$options.filename,
...$options.include.filter((f) => f !== $options.filename),
];
$: insertEntryOptions = $favaOptions["insert-entry"];
$: insertEntryOptions = $favaOptions.insert_entry;
function goToFileAndLine(filename: string, line?: number) {
const url = urlFor("editor/", { file_path: filename, line });
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/editor/SourceEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
];
editor.focus();
const opts = $favaOptions["insert-entry"].filter(
const opts = $favaOptions.insert_entry.filter(
(f) => f.filename === data.file_path
);
const line = parseInt(
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export const ledgerDataValidator = object({
currencies: array(string),
errors: number,
favaOptions: object({
"auto-reload": boolean,
"currency-column": number,
auto_reload: boolean,
currency_column: number,
indent: number,
locale: union(string, constant(null)),
"insert-entry": array(
insert_entry: array(
object({ date: string, filename: string, lineno: number, re: string })
),
}),
Expand Down
10 changes: 5 additions & 5 deletions src/fava/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get_locale() -> Optional[str]:
The locale that should be used for Babel. If not given as an option to
Fava, guess from browser.
"""
lang = g.ledger.fava_options["language"]
lang = g.ledger.fava_options.language
if lang is not None:
return lang
return request.accept_languages.best_match(["en"] + LANGUAGES)
Expand Down Expand Up @@ -180,7 +180,7 @@ def url_for(endpoint: str, **values: Any) -> str:

def url_for_source(**kwargs: Any) -> str:
"""URL to source file (possibly link to external editor)."""
if g.ledger.fava_options["use-external-editor"]:
if g.ledger.fava_options.use_external_editor:
return (
f"beancount://{kwargs.get('file_path')}"
+ f"?lineno={kwargs.get('line', 1)}"
Expand Down Expand Up @@ -276,9 +276,9 @@ def index() -> werkzeug.wrappers.response.Response:
if not g.beancount_file_slug:
g.beancount_file_slug = next(iter(app.config["LEDGERS"]))
index_url = url_for("index")
default_path = app.config["LEDGERS"][g.beancount_file_slug].fava_options[
"default-page"
]
default_path = app.config["LEDGERS"][
g.beancount_file_slug
].fava_options.default_page
return redirect(f"{index_url}{default_path}")


Expand Down
5 changes: 2 additions & 3 deletions src/fava/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
from fava.core.charts import ChartModule
from fava.core.entries_by_type import group_entries_by_type
from fava.core.extensions import ExtensionModule
from fava.core.fava_options import DEFAULTS
from fava.core.fava_options import FavaOptions
from fava.core.fava_options import parse_options
from fava.core.file import FileModule
Expand Down Expand Up @@ -210,7 +209,7 @@ def __init__(self, path: str) -> None:
self.commodities: Dict[str, Commodity] = {}

#: A dict with all of Fava's option values.
self.fava_options: FavaOptions = DEFAULTS
self.fava_options: FavaOptions = FavaOptions()

self._date_first: Optional[datetime.date] = None
self._date_last: Optional[datetime.date] = None
Expand Down Expand Up @@ -363,7 +362,7 @@ def get_account_sign(self, account_name: str) -> int:
def root_tree_closed(self) -> Tree:
"""A root tree for the balance sheet."""
tree = Tree(self.entries)
tree.cap(self.options, self.fava_options["unrealized"])
tree.cap(self.options, self.fava_options.unrealized)
return tree

def interval_balances(
Expand Down
2 changes: 1 addition & 1 deletion src/fava/core/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def load_file(self) -> None:
self.links = get_all_links(all_entries)
self.tags = get_all_tags(all_entries)
self.years = get_active_years(
all_entries, self.ledger.fava_options["fiscal-year-end"]
all_entries, self.ledger.fava_options.fiscal_year_end
)

account_ranker = ExponentialDecayRanker(
Expand Down
2 changes: 1 addition & 1 deletion src/fava/core/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def is_document_or_import_file(filename: str, ledger: "FavaLedger") -> bool:
document.filename for document in ledger.all_entries_by_type.Document
]
import_directories = [
ledger.join_path(d) for d in ledger.fava_options["import-dirs"]
ledger.join_path(d) for d in ledger.fava_options.import_dirs
]
if filename in filenames:
return True
Expand Down
Loading

0 comments on commit 233d30a

Please sign in to comment.