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

Commit 9ce7e64

Browse files
authored
Merge pull request #491 from rui-nar/use-options-from-config-file
added capability to specify command line options in a configuration file (.ini)
2 parents 74f9aef + 529a010 commit 9ce7e64

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/gphotos_sync/__main__.py

+30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55
from argparse import ArgumentParser, Namespace
6+
from configparser import ConfigParser
67
from datetime import datetime
78
from pathlib import Path
89
from typing import Optional
@@ -70,6 +71,13 @@ def __init__(self):
7071
action="store_true",
7172
help="report version and exit",
7273
)
74+
parser.add_argument(
75+
"-c",
76+
"--conf",
77+
action="store",
78+
help="use the .ini configuration file to initialise arguments. "
79+
"Command line provided arguments will superceed the ones in the config file",
80+
)
7381
parser.add_argument(
7482
"root_folder",
7583
help="root of the local folders to download into",
@@ -487,6 +495,28 @@ def main(self, test_args: Optional[dict] = None):
487495
print("\nERROR: Please supply root_folder in which to save photos")
488496
exit(1)
489497

498+
if args.conf is not None:
499+
if not Path(args.conf).exists():
500+
print("\nERROR: Provided config file does not exist")
501+
exit(1)
502+
config = ConfigParser()
503+
config.read(args.conf)
504+
# we need to use our own dict to store the options as ConfigParser config
505+
# object does not support storing booleans
506+
options = {}
507+
508+
for key in config["GENERAL"]:
509+
# overload and convert "true" and "false" strings to booleans
510+
if config.get("GENERAL", key).lower() in ["true", "false"]:
511+
options[key] = config.getboolean("GENERAL", key) # type: ignore
512+
else:
513+
options[key] = config.get("GENERAL", key) # type: ignore
514+
515+
self.parser.set_defaults(**options)
516+
517+
# overload the options provided in the .ini file with ones in command line
518+
args = self.parser.parse_args(test_args) # type: ignore
519+
490520
root_folder = Path(args.root_folder).absolute()
491521
db_path = Path(args.db_path) if args.db_path else root_folder
492522
if not root_folder.exists():

0 commit comments

Comments
 (0)