|
3 | 3 | import os
|
4 | 4 | import sys
|
5 | 5 | from argparse import ArgumentParser, Namespace
|
| 6 | +from configparser import ConfigParser |
6 | 7 | from datetime import datetime
|
7 | 8 | from pathlib import Path
|
8 | 9 | from typing import Optional
|
@@ -70,6 +71,13 @@ def __init__(self):
|
70 | 71 | action="store_true",
|
71 | 72 | help="report version and exit",
|
72 | 73 | )
|
| 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 | + ) |
73 | 81 | parser.add_argument(
|
74 | 82 | "root_folder",
|
75 | 83 | help="root of the local folders to download into",
|
@@ -487,6 +495,28 @@ def main(self, test_args: Optional[dict] = None):
|
487 | 495 | print("\nERROR: Please supply root_folder in which to save photos")
|
488 | 496 | exit(1)
|
489 | 497 |
|
| 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 | + |
490 | 520 | root_folder = Path(args.root_folder).absolute()
|
491 | 521 | db_path = Path(args.db_path) if args.db_path else root_folder
|
492 | 522 | if not root_folder.exists():
|
|
0 commit comments