Skip to content

Add User Configuration File CLI Argument #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ofxtools/scripts/ofxget.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Iterator,
ChainMap,
)
from pathlib import Path

# 3rd party imports
try:
Expand Down Expand Up @@ -192,6 +193,9 @@ def add_subparser(
default=0,
help="Give more output (option can be repeated)",
)
parser.add_argument(
"--config", type=Path, default=None, help="Use custom configuration file"
),
# Higher-level configs (e.g. account #s)
# imply lower-level configs (e.g. username/passwd)
if stmt:
Expand Down Expand Up @@ -1596,6 +1600,16 @@ def main() -> None:
argparser.print_help()
sys.exit()

if hasattr(args_, "config") and isinstance(args_.config, Path):
global USERCONFIGPATH
USERCONFIGPATH = args_.config.resolve()
if not USERCONFIGPATH.exists():
msg = "Can't find custom configuration file"
logger.error(msg)
raise RuntimeError(msg)
USERCFG.read([CONFIGPATH, USERCONFIGPATH])
logger.debug(f"Using custom configuration: {USERCONFIGPATH}")

args = merge_config(args_, USERCFG)
REQUEST_HANDLERS[args["request"]](args)

Expand Down
3 changes: 3 additions & 0 deletions tests/data/custom.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[server0]
ofxhome: 0
url: https://ofx.test.com
19 changes: 19 additions & 0 deletions tests/test_ofxget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,25 @@ def testSavePasswdNoKeyring(self):
# set_password.assert_called_once_with("ofxtools", "myserver", "t0ps3kr1t")


class CustomConfigTestCase(unittest.TestCase):
def testCustomConfig(self):
import os

this_dir = os.path.dirname(os.path.abspath(__file__))
config_file = os.path.join(this_dir, "data", "custom.cfg")

def _merge_config(*args):
return {"verbose": 1, "request": "list", "server": "server0"}

with patch("sys.argv", ["main", "list", "server0", "--config", config_file]):
with patch.multiple(
"ofxtools.scripts.ofxget",
merge_config=_merge_config,
list_fis=DEFAULT,
) as MOCKS:
ofxget.main()


class MainTestCase(unittest.TestCase):
def testMain(self):
args = argparse.Namespace(verbose=1, request="list")
Expand Down