Skip to content

Commit 536637d

Browse files
committed
Fixes #14
1 parent a91f79e commit 536637d

File tree

4 files changed

+83
-38
lines changed

4 files changed

+83
-38
lines changed

firefly_cli/cli.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def do_edit(self, argslist):
113113

114114
def help_edit(self):
115115
self.poutput(
116-
"Edits connection credentials:\n\t> edit url http://<FireflyIII URL>:<Port>\n\t> edit api <API key>"
116+
"Edits connection credentials:\n\t> edit url https://<FireflyIII URL>:<Port>\n\t> edit api <API key>"
117117
)
118118

119119
@cmd2.with_argparser(Parser.accounts())
@@ -149,9 +149,11 @@ def do_add(self, parser):
149149

150150
tab_header, tab_body = trans.get_tabulates()
151151
self.poutput(f"Transaction header:\n{tab_header}\n")
152-
self.poutput(f"Transaction Body:\n{tab_body}\n")
152+
self.poutput(f"Transaction body:\n{tab_body}\n")
153153

154-
if prompt_continue(extra_line=False, extra_msg=" adding the transaction"):
154+
if parser.bypass_prompt or prompt_continue(
155+
extra_line=False, extra_msg=" adding the transaction"
156+
):
155157
try:
156158
response = self.api.create_transaction(trans)
157159

@@ -176,12 +178,11 @@ def do_exit(self, _):
176178
return True
177179

178180
def help_exit(self):
179-
return self.poutput("exit the application. Shorthand: x q Ctrl-D.")
181+
return self.poutput("Exits the application. Shorthand: q Ctrl-D.")
180182

181183
def default(self):
182-
self.poutput(
183-
'Input not recognised. Please type "help" to list the available commands'
184-
)
184+
# todo default when arg not recognised
185+
pass
185186

186187
do_q = do_exit
187188
help_q = help_exit

firefly_cli/configs.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,29 @@
44

55
from xdg.BaseDirectory import xdg_config_home
66

7-
if os.getenv("FIREFLY_CLI_CONFIG"):
8-
config_file_path = Path(os.environ["FIREFLY_CLI_CONFIG"])
9-
else:
10-
config_file_path = Path(xdg_config_home).joinpath("firefly-cli", "firefly-cli.ini")
117

12-
# Create dir if not exists
13-
config_file_path.parent.mkdir(parents=True, exist_ok=True)
8+
def config_file_path():
9+
if os.getenv("FIREFLY_CLI_CONFIG"):
10+
config_file_path = Path(os.environ["FIREFLY_CLI_CONFIG"])
11+
else:
12+
config_file_path = Path(xdg_config_home).joinpath(
13+
"firefly-cli", "firefly-cli.ini"
14+
)
15+
16+
# Create dir if not exists
17+
config_file_path.parent.mkdir(parents=True, exist_ok=True)
18+
19+
return config_file_path
1420

1521

1622
def load_configs():
1723
configs = configparser.ConfigParser()
1824

1925
# No config file loaded because it was not available/not existent
20-
if len(configs.read(config_file_path)) < 1:
26+
if len(configs.read(config_file_path())) < 1:
2127
print("File not found, creating the file..")
2228

23-
with open(config_file_path, "w") as f:
29+
with open(config_file_path(), "w") as f:
2430
configs["firefly-cli"] = {}
2531
save_configs_to_file(configs)
2632

@@ -29,12 +35,12 @@ def load_configs():
2935

3036
def save_configs_to_file(configs):
3137
try:
32-
with open(config_file_path, "w") as f:
38+
with open(config_file_path(), "w") as f:
3339
configs.write(f)
34-
print("Config file saved at {}".format(str(config_file_path)))
40+
print("Config file saved at {}".format(str(config_file_path())))
3541
except:
3642
print(
3743
"An error has occurred while saving file to {}".format(
38-
str(config_file_path)
44+
str(config_file_path())
3945
)
4046
)

firefly_cli/parser.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ def description_provider(cmd, limit=10):
2929

3030
class Parser:
3131
@staticmethod
32-
def entrypoint():
32+
def entrypoint(prog=None):
3333
parser = ArgumentParser(
3434
description="A command line interface for conveniently entering expenses in Firefly III.\nRun without arguments to start interactive mode.",
3535
usage="firefly-cli [-h] [-v]",
3636
add_help=False,
37+
prog=None,
3738
)
3839

3940
# Optional arguments (json header)
@@ -50,10 +51,8 @@ def entrypoint():
5051
return parser
5152

5253
@staticmethod
53-
def accounts():
54-
parser = Cmd2ArgumentParser(
55-
description="Shows account information.",
56-
)
54+
def accounts(prog=None):
55+
parser = Cmd2ArgumentParser(description="Shows account information.", prog=prog)
5756

5857
# Optional arguments (json header)
5958
parser.add_argument("--json", action="store_true")
@@ -79,17 +78,28 @@ def accounts():
7978
return parser
8079

8180
@staticmethod
82-
def add():
81+
def add(prog=None):
8382

8483
parser = Cmd2ArgumentParser(
8584
description="Adds a new transaction to FireflyIII.",
8685
usage="add [comma-separated arguments] [-h] [--optional-arguments]",
86+
prog=None,
8787
)
8888

8989
# Positional arguments
90-
parser.add_argument("transaction", nargs="*", help="Transaction data.")
90+
parser.add_argument(
91+
"transaction",
92+
nargs="*",
93+
help="Transaction data in comma-separated format: Amount, Description , Source account, Destination account, Category, Budget",
94+
)
9195

9296
# Optional arguments (json header)
97+
parser.add_argument(
98+
"-y",
99+
dest="bypass_prompt",
100+
action="store_true",
101+
help="Bypass confirmation prompt.",
102+
)
93103
parser.add_argument(
94104
"--apply-rules",
95105
default=True,
@@ -174,3 +184,8 @@ def add():
174184
)
175185

176186
return parser
187+
188+
@staticmethod
189+
def safe_string(args):
190+
args_str = " ".join(list(map(lambda x: f"'{x}'" if " " in x else x, args)))
191+
return args_str

test/test_parser.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import json
2-
from datetime import datetime
3-
from pathlib import Path
1+
import shlex
2+
import sys
3+
from unittest.mock import patch
44

5-
import pytest
5+
from firefly_cli.parser import Parser
66

7-
from firefly_cli import utils
87

9-
test_data = Path(__file__).parent.joinpath("test_data")
8+
class TestAutocomplete:
9+
def test_accounts_name_provider(self):
10+
pass
11+
12+
def test_description_provider(self):
13+
pass
1014

1115

1216
class TestParser:
@@ -19,10 +23,29 @@ def test_accounts(self):
1923
def test_add(self):
2024
pass
2125

22-
23-
class TestAutocomplete:
24-
def test_accounts_name_provider(self):
25-
pass
26-
27-
def test_description_provider(self):
28-
pass
26+
def test_safe_string(self):
27+
test_argv = [
28+
"script.py",
29+
"add",
30+
"--source-name",
31+
"My Bank Account",
32+
"--amount",
33+
"499",
34+
"--description",
35+
"Food",
36+
"--destination-name",
37+
"Shop A",
38+
]
39+
expected_args_str = "add --source-name 'My Bank Account' --amount 499 --description Food --destination-name 'Shop A'"
40+
41+
with patch.object(sys, "argv", test_argv):
42+
args, ffargs = Parser.entrypoint().parse_known_args()
43+
44+
args_str = Parser.safe_string(ffargs)
45+
args = Parser.add().parse_args(shlex.split(args_str))
46+
47+
assert args_str == expected_args_str
48+
assert args.source_name == "My Bank Account"
49+
assert args.amount == 499
50+
assert args.description == "Food"
51+
assert args.destination_name == "Shop A"

0 commit comments

Comments
 (0)