blob: 388be108f6e01b2b4234cdd4610dde85f1afcbee [file] [log] [blame]
Skyler Greyfc03d442024-07-17 22:00:16 +00001From 87af8b9b6f14beaade6e8d5130d062d2198ee5d9 Mon Sep 17 00:00:00 2001
2From: Skyler Grey <sky@a.starrysky.fyi>
3Date: Thu, 18 Jul 2024 20:24:24 +0000
4Subject: [PATCH 1/2] fix!: Respect default-file when inserting entries
5
6It's a common point of confusion that default-file does not set the
7default file for insertions. This patch brings the behavior into line
8with people's expectations.
9
10BREAKING-CHANGE: If you are using default-file and expecting entries to be inserted to your main file, this will no longer happen
11Fixes: beancount/fava#882
12Refs: beancount/fava#1698, beancount/fava#822
13---
14 src/fava/core/file.py | 5 ++++-
15 src/fava/help/options.md | 2 +-
16 2 files changed, 5 insertions(+), 2 deletions(-)
17
18diff --git a/src/fava/core/file.py b/src/fava/core/file.py
19index e76b7ffb9..d225a2497 100644
20--- a/src/fava/core/file.py
21+++ b/src/fava/core/file.py
22@@ -233,7 +233,10 @@ def insert_entries(self, entries: list[Directive]) -> None:
23 for entry in sorted(entries, key=_incomplete_sortkey):
24 path, updated_insert_options = insert_entry(
25 entry,
26- self.ledger.beancount_file_path,
27+ (
28+ self.ledger.fava_options.default_file
29+ or self.ledger.beancount_file_path
30+ ),
31 insert_options=fava_options.insert_entry,
32 currency_column=fava_options.currency_column,
33 indent=fava_options.indent,
34diff --git a/src/fava/help/options.md b/src/fava/help/options.md
35index 22361dcec..dbef9eb23 100644
36--- a/src/fava/help/options.md
37+++ b/src/fava/help/options.md
38@@ -113,7 +113,7 @@ a transaction, the account of the last posting is used) is matched against all
39 of the matching options before the entry date. If the entry is a Transaction and
40 no `insert-entry` option matches the account of the last posting the account of
41 the second to last posting and so on will be tried. If no `insert-entry` option
42-matches or none is given, the entry will be inserted at the end of the main
43+matches or none is given, the entry will be inserted at the end of the default
44 file.
45
46 ---
47
48From 3ff9ec604d9f6d16b4aa88107183a597f5e0c833 Mon Sep 17 00:00:00 2001
49From: Skyler Grey <sky@a.starrysky.fyi>
50Date: Thu, 18 Jul 2024 20:41:55 +0000
51Subject: [PATCH 2/2] feat: Allow specifying the filename to default-file
52
53I autogenerate some fava config, and one option I'd like to specify is
54the default file.
55
56Unfortunately, this previously had to be specified in the file which
57would become the default file, leading to a messy and fragile setup in
58which my (readonly) configuration file would become the default if the
59magical "default-file" line was ever removed from the user-editable
60config.
61
62This feature allows existing semantics around default-file to continue,
63while also providing the ability to set it in some other file if the
64existing options are unsuitable.
65---
66 src/fava/core/fava_options.py | 9 +++++++--
67 src/fava/help/options.md | 4 ++--
68 tests/test_core_fava_options.py | 2 ++
69 3 files changed, 11 insertions(+), 4 deletions(-)
70
71diff --git a/src/fava/core/fava_options.py b/src/fava/core/fava_options.py
72index 462ea29d5..23217da2b 100644
73--- a/src/fava/core/fava_options.py
74+++ b/src/fava/core/fava_options.py
75@@ -119,12 +119,17 @@ def parse_option_custom_entry( # noqa: PLR0912
76 if key not in All_OPTS:
77 raise UnknownOptionError(key)
78
79+ value = entry.values[1].value if len(entry.values) > 1 else None
80+
81 if key == "default_file":
82- filename, _lineno = get_position(entry)
83+ if value is None:
84+ filename, _lineno = get_position(entry)
85+ else:
86+ filename = value
87+
88 options.default_file = filename
89 return
90
91- value = entry.values[1].value
92 if not isinstance(value, str):
93 raise NotAStringOptionError(key)
94
95diff --git a/src/fava/help/options.md b/src/fava/help/options.md
96index dbef9eb23..968730620 100644
97--- a/src/fava/help/options.md
98+++ b/src/fava/help/options.md
99@@ -50,8 +50,8 @@ locale `en_IN` the number `1111111.33` will be rendered `11,11,111.33`,
100 ## `default-file`
101
102 Use this option to specify a default file for the editor to open. This option
103-takes no value, the file the custom entry is in will be used as the default. If
104-this option is not specified, Fava opens the main file by default.
105+may optionally take a value of a filename to be the default. If you don't
106+provide a filename, the file this custom option is in is used.
107
108 ---
109
110diff --git a/tests/test_core_fava_options.py b/tests/test_core_fava_options.py
111index 03206e6ad..9ed1e5bec 100644
112--- a/tests/test_core_fava_options.py
113+++ b/tests/test_core_fava_options.py
114@@ -27,6 +27,7 @@ def test_fava_options(load_doc_custom_entries: list[Custom]) -> None:
115 2016-04-14 custom "fava-option" "collapse_pattern" "(invalid"
116 2016-04-14 custom "fava-option" "fiscal-year-end" "01-11"
117 2016-04-14 custom "fava-option" "conversion-currencies" "USD EUR HOOLI"
118+ 2016-06-14 custom "fava-option" "default-file" "/some/file/name"
119 """
120
121 options, errors = parse_options(load_doc_custom_entries)
122@@ -50,3 +51,4 @@ def test_fava_options(load_doc_custom_entries: list[Custom]) -> None:
123 assert options.collapse_pattern == [re.compile("Account:Name")]
124 assert options.fiscal_year_end == FiscalYearEnd(1, 11)
125 assert options.conversion_currencies == ("USD", "EUR", "HOOLI")
126+ assert options.default_file == "/some/file/name"