Skip to content

Commit

Permalink
refactor: make library_test.py accept JSON config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Reichmuth committed Jan 31, 2024
1 parent 7060fae commit 79956dd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,6 @@ dmypy.json

# Cython debug symbols
cython_debug/

# Optional config file with router configuration for test script
library_test.json
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "python",
"request": "launch",
"program": "library_test.py",
"args": ["-p", "<your password>"],
"args": ["-f", "library_test.json"],
"console": "integratedTerminal",
"justMyCode": true
}
Expand Down
16 changes: 16 additions & 0 deletions library_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import argparse
import asyncio
import json
import logging
import os

import aiohttp

Expand Down Expand Up @@ -30,8 +32,21 @@ def get_arguments() -> tuple[argparse.ArgumentParser, argparse.Namespace]:
"--username", "-u", type=str, default="vodafone", help="Set router username"
)
parser.add_argument("--password", "-p", type=str, help="Set router password")
parser.add_argument(
"--configfile",
"-f",
type=str,
help="Load options from JSON config file. Command line options override those in the file.",
)

arguments = parser.parse_args()
if arguments.configfile:
# Re-parse the command line, taking the options in the optional JSON file as a basis
if os.path.exists(arguments.configfile):
with open(arguments.configfile) as f:
arguments = parser.parse_args(
namespace=argparse.Namespace(**json.load(f))
)

return parser, arguments

Expand All @@ -42,6 +57,7 @@ async def main() -> None:

if not args.password:
print("You have to specify a password")
parser.print_help()
exit(1)

print("Determining device type")
Expand Down

0 comments on commit 79956dd

Please # to comment.