From 37a3d83725f0b0b191b8c308d19d72edaca0fe6d Mon Sep 17 00:00:00 2001 From: Robert Pufky Date: Sun, 7 Jun 2020 13:01:26 -0700 Subject: [PATCH] Add --config option for explicitly stating configuration file to use. aliparlakci/bulk-downloader-for-reddit#122 Address comments in pull review. * Removed vertical space. * Added config file existence check with log message. --- README.md | 10 +++++++++- script.py | 27 +++++++++++++++++---------- src/arguments.py | 10 ++++++++-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4912563..641651e 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,15 @@ Example usage: **`--no-download`** Takes a file directory as a parameter and skips the posts if it matches with the post IDs inside the file. It also saves the newly downloaded posts to the given file. Example usage: **`--downloaded-posts D:\bdfr\ALL_POSTS.txt`** - + +## **`--config`** +Specify a `config.json` file to use. This will disable reading from the default `Bulk downloader for reddit/config.json` file, as well as `--use-local-config` option. + +Example usage: **`--config /etc/bdfr/config.json`** +Example usage: **`-c ~/config.json`** +Example usage: **`--config c:\Users\Me\Downloads\config.json`** +Example usage: **`-c c:\config.json`** + ## ❔ FAQ ### I am running the script on a headless machine or on a remote server. How can I authenticate my reddit account? diff --git a/script.py b/script.py index 38c622c..c2202c1 100644 --- a/script.py +++ b/script.py @@ -256,14 +256,24 @@ def printLogo(): ) def main(): + arguments = Arguments.parse() + GLOBAL.arguments = arguments - if not Path(GLOBAL.defaultConfigDirectory).is_dir(): - os.makedirs(GLOBAL.defaultConfigDirectory) - - if Path("config.json").exists(): - GLOBAL.configDirectory = Path("config.json") + if arguments.config: + if arguments.use_local_config: + sys.exit() + if Path(arguments.config).exists(): + GLOBAL.configDirectory = Path(arguments.config) + else: + VanillaPrint("custom config",arguments.config,"not found. Exiting.") + sys.exit() else: - GLOBAL.configDirectory = GLOBAL.defaultConfigDirectory / "config.json" + if not Path(GLOBAL.defaultConfigDirectory).is_dir(): + os.makedirs(GLOBAL.defaultConfigDirectory) + if Path("config.json").exists(): + GLOBAL.configDirectory = Path("config.json") + else: + GLOBAL.configDirectory = GLOBAL.defaultConfigDirectory / "config.json" try: GLOBAL.config = Config(GLOBAL.configDirectory).generate() except InvalidJSONFile as exception: @@ -274,9 +284,6 @@ def main(): sys.argv = sys.argv + GLOBAL.config["options"].split() - arguments = Arguments.parse() - GLOBAL.arguments = arguments - if arguments.set_filename: Config(GLOBAL.configDirectory).setCustomFileName() sys.exit() @@ -362,4 +369,4 @@ if __name__ == "__main__": print(GLOBAL.log_stream.getvalue()) if not GLOBAL.arguments.quit: input("\nPress enter to quit\n") - \ No newline at end of file + diff --git a/src/arguments.py b/src/arguments.py index dd7c0e9..7dfff31 100644 --- a/src/arguments.py +++ b/src/arguments.py @@ -153,9 +153,15 @@ class Arguments: action="store_true", help="Just saved posts into a the POSTS.json file without downloading" ) - + + parser.add_argument("--config","-c", + help="Specify exact config.json file to use. " \ + "Disables reading from 'Bulk downloader for " \ + "reddit/config.json' and --use-local-config " \ + "option." + ) if arguments == []: return parser.parse_args() else: - return parser.parse_args(arguments) \ No newline at end of file + return parser.parse_args(arguments)