Skip to content
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

added capability to specify command line options in a configuration file (.ini) #491

Merged
merged 1 commit into from
Jun 4, 2024
Merged
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
30 changes: 30 additions & 0 deletions src/gphotos_sync/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
from argparse import ArgumentParser, Namespace
from configparser import ConfigParser
from datetime import datetime
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -70,6 +71,13 @@ def __init__(self):
action="store_true",
help="report version and exit",
)
parser.add_argument(
"-c",
"--conf",
action="store",
help="use the .ini configuration file to initialise arguments. "
"Command line provided arguments will superceed the ones in the config file",
)
parser.add_argument(
"root_folder",
help="root of the local folders to download into",
Expand Down Expand Up @@ -481,6 +489,28 @@ def main(self, test_args: Optional[dict] = None):
print("\nERROR: Please supply root_folder in which to save photos")
exit(1)

if args.conf is not None:
if not Path(args.conf).exists():
print("\nERROR: Provided config file does not exist")
exit(1)
config = ConfigParser()
config.read(args.conf)
# we need to use our own dict to store the options as ConfigParser config
# object does not support storing booleans
options = {}

for key in config["GENERAL"]:
# overload and convert "true" and "false" strings to booleans
if config.get("GENERAL", key).lower() in ["true", "false"]:
options[key] = config.getboolean("GENERAL", key) # type: ignore
else:
options[key] = config.get("GENERAL", key) # type: ignore

self.parser.set_defaults(**options)

# overload the options provided in the .ini file with ones in command line
args = self.parser.parse_args(test_args) # type: ignore

root_folder = Path(args.root_folder).absolute()
db_path = Path(args.db_path) if args.db_path else root_folder
if not root_folder.exists():
Expand Down
Loading