Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #491 from rui-nar/use-options-from-config-file
Browse files Browse the repository at this point in the history
added capability to specify command line options in a configuration file (.ini)
  • Loading branch information
gilesknap authored Jun 4, 2024
2 parents 74f9aef + 529a010 commit 9ce7e64
Showing 1 changed file with 30 additions and 0 deletions.
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 @@ -487,6 +495,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

0 comments on commit 9ce7e64

Please sign in to comment.