Skip to content

Commit 1251fad

Browse files
committed
Add remove dangling option for import command
1 parent 9b2b325 commit 1251fad

File tree

3 files changed

+207
-160
lines changed

3 files changed

+207
-160
lines changed

beanhub_cli/import_cli.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from beancount_parser.parser import make_parser
99
from beanhub_extract.data_types import Transaction
1010
from beanhub_extract.utils import strip_base_path
11+
from beanhub_import.data_types import BeancountTransaction
1112
from beanhub_import.data_types import DeletedTransaction
1213
from beanhub_import.data_types import GeneratedTransaction
1314
from beanhub_import.data_types import ImportDoc
@@ -55,8 +56,15 @@
5556
default="main.bean",
5657
help="The path to main entry beancount file",
5758
)
59+
@click.option(
60+
"--remove-dangling",
61+
is_flag=True,
62+
help="Remove dangling transactions (existing imported transactions in Beancount files without corresponding generated transactions)",
63+
)
5864
@pass_env
59-
def main(env: Environment, config: str, workdir: str, beanfile: str):
65+
def main(
66+
env: Environment, config: str, workdir: str, beanfile: str, remove_dangling: bool
67+
):
6068
config_path = pathlib.Path(config)
6169
with config_path.open("rt") as fo:
6270
doc_payload = yaml.safe_load(fo)
@@ -146,14 +154,18 @@ def main(env: Environment, config: str, workdir: str, beanfile: str):
146154
new_tree = parser.parse(bean_content)
147155
else:
148156
env.logger.info(
149-
"Applying change sets (add=%s, update=%s, remove=%s) to %s",
157+
"Applying change sets (add=%s, update=%s, remove=%s, dangling=%s) with remove_dangling=%s to %s",
150158
len(change_set.add),
151159
len(change_set.update),
152160
len(change_set.remove),
161+
len(change_set.dangling),
162+
remove_dangling,
153163
target_file,
154164
)
155165
tree = parser.parse(target_file.read_text())
156-
new_tree = apply_change_set(tree=tree, change_set=change_set)
166+
new_tree = apply_change_set(
167+
tree=tree, change_set=change_set, remove_dangling=remove_dangling
168+
)
157169

158170
with target_file.open("wt") as fo:
159171
formatter = Formatter()
@@ -173,7 +185,24 @@ def main(env: Environment, config: str, workdir: str, beanfile: str):
173185
if txn.id not in deleted_txn_ids:
174186
continue
175187
table.add_row(
176-
escape(str(target_file)),
188+
escape(str(target_file)) + f":{txn.lineno}",
189+
str(txn.id),
190+
)
191+
rich.print(Padding(table, (1, 0, 0, 4)))
192+
193+
dangling_action = "Delete" if remove_dangling else "Ignored"
194+
table = Table(
195+
title=f"Dangling Transactions ({dangling_action})",
196+
box=box.SIMPLE,
197+
header_style=TABLE_HEADER_STYLE,
198+
expand=True,
199+
)
200+
table.add_column("File", style=TABLE_COLUMN_STYLE)
201+
table.add_column("Id", style=TABLE_COLUMN_STYLE)
202+
for target_file, change_set in change_sets.items():
203+
for txn in change_set.dangling:
204+
table.add_row(
205+
escape(str(target_file)) + f":{txn.lineno}",
177206
str(txn.id),
178207
)
179208
rich.print(Padding(table, (1, 0, 0, 4)))

0 commit comments

Comments
 (0)