Skip to content

Commit dfdbc89

Browse files
author
Your Name
committed
fix error code 4
1 parent 444ec7c commit dfdbc89

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

beanborg/bb_archive.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import shutil
1010
import sys
1111
from datetime import datetime
12+
from typing import Optional
1213

1314
import fire
1415
from fire.decorators import SetParseFns
@@ -37,18 +38,29 @@ class BeanborgArchiver:
3738
bb_archive --config_file=/path/to/config.yaml --debug=true
3839
"""
3940

40-
@SetParseFns(config_file=str, debug=bool)
41-
def __call__(self, config_file: str, debug: bool = False) -> int:
41+
@SetParseFns(f=str, config_file=str, debug=bool)
42+
def __call__(
43+
self,
44+
f: Optional[str] = None,
45+
config_file: Optional[str] = None,
46+
debug: bool = False,
47+
):
4248
"""Archive the processed CSV file.
4349
4450
Args:
51+
f: Path to the configuration file (shorthand for --config-file)
4552
config_file: Path to the configuration file
4653
debug: Enable debug mode
4754
4855
Returns:
4956
int: 0 for success, 1 for failure
5057
"""
51-
config = init_config(config_file,debug)
58+
final_config_file = f or config_file
59+
if not final_config_file:
60+
raise ValueError(
61+
"Configuration file must be specified using either -f or --config-file"
62+
)
63+
config = init_config(final_config_file, debug)
5264

5365
target_csv = os.path.join(config.csv.target, config.csv.ref + ".csv")
5466

beanborg/bb_import.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
__copyright__ = "Copyright (C) 2024 Luciano Fiandesio"
55
__license__ = "GNU GPLv2"
66

7+
from typing import Optional
8+
79
import fire
810
from fire.decorators import SetParseFns
911

@@ -31,17 +33,29 @@ class BeanborgImporter:
3133
bb_import --config_file=config.yaml --fix_only=true
3234
"""
3335

34-
@SetParseFns(config_file=str, debug=bool, fix_only=bool)
35-
def __call__(self, config_file: str, debug: bool = False, fix_only: bool = False):
36+
@SetParseFns(f=str, config_file=str, debug=bool, fix_only=bool)
37+
def __call__(
38+
self,
39+
f: Optional[str] = None,
40+
config_file: Optional[str] = None,
41+
debug: bool = False,
42+
fix_only: bool = False,
43+
):
3644
"""Import transactions from CSV to beancount.
3745
3846
Args:
47+
f: Path to the configuration file (shorthand for --config-file)
3948
config_file: Path to the configuration file
4049
debug: Enable debug mode
4150
fix_only: Only fix uncategorized transactions
4251
"""
52+
final_config_file = f or config_file
53+
if not final_config_file:
54+
raise ValueError(
55+
"Configuration file must be specified using either -f or --config-file"
56+
)
4357
imp = Importer()
44-
return imp.import_transactions(config_file, debug, fix_only)
58+
return imp.import_transactions(final_config_file, debug, fix_only)
4559

4660

4761
def main():

beanborg/bb_mover.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import glob
88
import os
99
import shutil
10+
import sys
1011
from subprocess import CalledProcessError, check_call
1112
from typing import Optional
1213

@@ -70,7 +71,7 @@ def _process(self, config_file: str, debug: bool = False):
7071
path = os.path.expanduser(config.csv.download_path)
7172
if not os.path.isdir(path):
7273
rprint(f"[red]folder: {config.csv.download_path} does not exist![/red]")
73-
return 1
74+
sys.exit(-1)
7475

7576
if not os.path.isdir(config.csv.target):
7677
os.mkdir(config.csv.target)
@@ -83,20 +84,20 @@ def _process(self, config_file: str, debug: bool = False):
8384
f"more than one file starting with {config.csv.name} "
8485
f"found in {config.csv.download_path}. Cannot continue."
8586
)
86-
return 1
87+
sys.exit(-1)
8788

8889
if file_count == 0:
8990
rprint(
9091
f"[red]No file found in [bold]{config.csv.download_path}[/bold] "
9192
f"with name starting with: [bold]{config.csv.name}[/bold][/red]"
9293
)
93-
return 1
94+
sys.exit(-1)
9495

9596
if config.csv.post_script_path and not os.path.isfile(
9697
config.csv.post_script_path
9798
):
9899
print(f"No post-move script found: {config.csv.post_script_path}")
99-
return 1
100+
sys.exit(-1)
100101

101102
for f in os.listdir(path):
102103
if f.startswith(config.csv.name):
@@ -120,7 +121,6 @@ def _process(self, config_file: str, debug: bool = False):
120121
f"[red]An error occurred executing: {config.csv.post_script_path}\n{str(e)}[/red]"
121122
)
122123
print("Done :)")
123-
return 0
124124

125125

126126
def main():

beanborg/rule_engine/rules_engine.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class RuleEngineError(Exception):
126126
"""Base exception for rule engine errors."""
127127

128128
def __init__(self, message: str, context: Optional[RuleErrorContext] = None):
129+
"""Initialize the exception."""
129130
self.context = context or RuleErrorContext()
130131
super().__init__(message)
131132

@@ -134,6 +135,7 @@ class RuleLoadError(RuleEngineError):
134135
"""Exception raised when rule loading fails."""
135136

136137
def __str__(self) -> str:
138+
"""Return a string representation of the exception."""
137139
context_info = []
138140
if self.context.rule_name:
139141
context_info.append(f"rule='{self.context.rule_name}'")
@@ -201,21 +203,21 @@ def _load_rules(self) -> None:
201203
try:
202204
if rule_name in custom_rules:
203205
logger.debug("Loading custom rule: %s", rule_name)
204-
self.rules[rule_name] = RuleDef(custom_rules[rule_name], rule_props)
206+
self.rules[rule_name] = RuleDef(
207+
custom_rules[rule_name], rule_props
208+
)
205209
elif rule_name in self.BUILT_IN_RULES:
206210
# Load from built-in rules
207211
logger.debug("Loading built-in rule: %s", rule_name)
208212
unique_name = f"{rule_name}|{uuid.uuid4().hex.upper()[:6]}"
209213
self.rules[unique_name] = RuleDef(
210-
self.BUILT_IN_RULES[rule_name], rule_props
211-
)
214+
self.BUILT_IN_RULES[rule_name], rule_props
215+
)
212216
else:
213217
logger.error("Unknown rule: %s", rule_name)
214218
raise RuleLoadError(f"Unknown rule type: {rule_name}")
215219
except Exception as e:
216-
raise RuleLoadError(
217-
f"Failed to load rule '{rule_name}'"
218-
) from e
220+
raise RuleLoadError(f"Failed to load rule '{rule_name}'") from e
219221
except Exception as e:
220222
if not isinstance(e, RuleLoadError):
221223
raise RuleLoadError("Failed to load rules") from e

0 commit comments

Comments
 (0)