From 4d9ce59f24d516a8414b13d1ec4af351fd4d3da9 Mon Sep 17 00:00:00 2001 From: CordlessCoder Date: Tue, 5 Jul 2022 23:25:15 +0300 Subject: [PATCH] feat: correct optional variable behavour --- .config.template.toml | 10 ++-- .env.template | 86 --------------------------- main.py | 2 +- utils/console.py | 5 ++ utils/{tomlchecker.py => settings.py} | 16 +++-- 5 files changed, 23 insertions(+), 96 deletions(-) delete mode 100644 .env.template rename utils/{tomlchecker.py => settings.py} (91%) diff --git a/.config.template.toml b/.config.template.toml index 1c5ee43..d12d30b 100644 --- a/.config.template.toml +++ b/.config.template.toml @@ -15,21 +15,21 @@ random = { optional = true, options = [ false, ], default = false, type = "bool", explanation = "If set to no, it will ask you a thread link to extract the thread, if yes it will randomize it. Default: 'False'", example = "True" } subreddit = { optional = false, regex = "[_0-9a-zA-Z]+$", nmin = 3, nmax = 21, explanation = "what subreddit to pull posts from, the name of the sub, not the URL", example = "AskReddit", oob_error = "A subreddit name HAS to be between 3 and 20 characters" } -post_id = { optional = false, regex = "^((?!://|://).)*$", explanation = "Used if you want to use a specific post.", example = "urdtfx" } +post_id = { optional = true, regex = "^((?!://|://).)*$", explanation = "Used if you want to use a specific post.", example = "urdtfx" } max_comment_length = { default = 500, optional = false, nmin = 10, nmax = 10000, type = "int", explanation = "max number of characters a comment can have. default is 500", example = 500, oob_error = "the max comment length should be between 10 and 10000" } [settings] -allow_nsfw = { optional = true, type = "bool", default = false, example = false, options = [ +allow_nsfw = { optional = false, type = "bool", default = false, example = false, options = [ true, false, ], explanation = "Whether to allow NSFW content, True or False" } -theme = { optional = true, default = "light", example = "dark", options = [ +theme = { optional = false, default = "light", example = "dark", options = [ "dark", "light", ], explanation = "sets the Reddit theme, either LIGHT or DARK" } -times_to_run = { optional = true, default = 1, example = 2, explanation = "used if you want to run multiple times. set to an int e.g. 4 or 29 or 1", type = "int", nmin = 1, oob_error = "It's very hard to run something less than once." } -opacity = { optional = true, default = 0.9, example = 0.8, explanation = "Sets the opacity of the comments when overlayed over the background", type = "float", nmin = 0, nmax = 1, oob_error = "The opacity HAS to be between 0 and 1", input_error = "The opacity HAS to be a decimal number between 0 and 1" } +times_to_run = { optional = false, default = 1, example = 2, explanation = "used if you want to run multiple times. set to an int e.g. 4 or 29 or 1", type = "int", nmin = 1, oob_error = "It's very hard to run something less than once." } +opacity = { optional = false, default = 0.9, example = 0.8, explanation = "Sets the opacity of the comments when overlayed over the background", type = "float", nmin = 0, nmax = 1, oob_error = "The opacity HAS to be between 0 and 1", input_error = "The opacity HAS to be a decimal number between 0 and 1" } storymode = { optional = true, type = "bool", default = false, example = false, options = [ true, false, diff --git a/.env.template b/.env.template deleted file mode 100644 index 77f2acf..0000000 --- a/.env.template +++ /dev/null @@ -1,86 +0,0 @@ - -REDDIT_CLIENT_ID="" #fFAGRNJru1FTz70BzhT3Zg -#EXPLANATION the ID of your Reddit app of SCRIPT type -#RANGE 12:30 -#MATCH_REGEX [-a-zA-Z0-9._~+/]+=*$ -#OOB_ERROR The ID should be over 12 and under 30 characters, double check your input. - -REDDIT_CLIENT_SECRET="" #fFAGRNJru1FTz70BzhT3Zg -#EXPLANATION the SECRET of your Reddit app of SCRIPT type -#RANGE 20:40 -#MATCH_REGEX [-a-zA-Z0-9._~+/]+=*$ -#OOB_ERROR The secret should be over 20 and under 40 characters, double check your input. - -REDDIT_USERNAME="" #asdfghjkl -#EXPLANATION the username of your reddit account -#RANGE 3:20 -#MATCH_REGEX [-_0-9a-zA-Z]+$ -#OOB_ERROR A username HAS to be between 3 and 20 characters - -REDDIT_PASSWORD="" #fFAGRNJru1FTz70BzhT3Zg -#EXPLANATION the password of your reddit account -#RANGE 8:None -#OOB_ERROR Password too short - -#OPTIONAL -RANDOM_THREAD="no" -# If set to no, it will ask you a thread link to extract the thread, if yes it will randomize it. Default: "no" - -REDDIT_2FA="" #no -#MATCH_REGEX ^(yes|no) -#EXPLANATION Whether you have Reddit 2FA enabled, Valid options are "yes" and "no" - -SUBREDDIT="AskReddit" -#EXPLANATION what subreddit to pull posts from, the name of the sub, not the URL -#RANGE 3:20 -#MATCH_REGEX [_0-9a-zA-Z]+$ -#OOB_ERROR A subreddit name HAS to be between 3 and 20 characters - -ALLOW_NSFW="False" -#EXPLANATION Whether to allow NSFW content, True or False -#MATCH_REGEX ^(True|False)$ - -POST_ID="" -#MATCH_REGEX ^((?!://|://).)*$ -#EXPLANATION Used if you want to use a specific post. example of one is urdtfx - -THEME="LIGHT" #dark -#EXPLANATION sets the Reddit theme, either LIGHT or DARK -#MATCH_REGEX ^(dark|light|DARK|LIGHT)$ - -TIMES_TO_RUN="" #2 -#EXPLANATION used if you want to run multiple times. set to an int e.g. 4 or 29 and leave blank for 1 - -MAX_COMMENT_LENGTH="500" #500 -#EXPLANATION max number of characters a comment can have. default is 500 -#RANGE 0:10000 -#MATCH_TYPE int -#OOB_ERROR the max comment length should be between 0 and 10000 - -OPACITY="1" #.8 -#EXPLANATION Sets the opacity of the comments when overlayed over the background -#RANGE 0:1 -#MATCH_TYPE float -#OOB_ERROR The opacity HAS to be between 0 and 1 - -# If you want to translate the comments to another language, set the language code here. -# If empty, no translation will be done. -POSTLANG="" -#EXPLANATION Activates the translation feature, set the language code for translate or leave blank - -TTSCHOICE="Polly" -#EXPLANATION the backend used for TTS. Without anything specified, the user will be prompted to choose one. -# IMPORTANT NOTE: if you use translate, you need to set this to googletranslate or tiktok and use custom voice in your language - -STREAMLABS_VOICE="Joanna" -#EXPLANATION Sets the voice for the Streamlabs Polly TTS Engine. Check the file for more information on different voices. - -AWS_VOICE="Joanna" -#EXPLANATION Sets the voice for the AWS Polly TTS Engine. Check the file for more information on different voices. - -TIKTOK_VOICE="en_us_006" -#EXPLANATION Sets the voice for the TikTok TTS Engine. Check the file for more information on different voices. - -#OPTIONAL -STORYMODE="False" -# IN-PROGRESS - not yet implemented diff --git a/main.py b/main.py index b7fdc5c..8fc3b59 100755 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ from os import name from reddit.subreddit import get_subreddit_threads from utils.cleanup import cleanup from utils.console import print_markdown, print_step -from utils.tomlchecker import check_toml +from utils.settings import check_toml # from utils.checker import envUpdate from video_creation.background import download_background, chop_background_video diff --git a/utils/console.py b/utils/console.py index 46396f2..310247d 100644 --- a/utils/console.py +++ b/utils/console.py @@ -46,7 +46,12 @@ def handle_input( extra_info="", options: list = None, default=NotImplemented, + optional=False, ): + if optional: + console.print(message + "\n[green]This is an optional value. Do you want to skip it? (y/n)") + if input().casefold().startswith("y"): + return None if default is not NotImplemented: console.print( "[green]" diff --git a/utils/tomlchecker.py b/utils/settings.py similarity index 91% rename from utils/tomlchecker.py rename to utils/settings.py index c4e8c5a..927c500 100755 --- a/utils/tomlchecker.py +++ b/utils/settings.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # import os import toml -from rich import pretty from rich.console import Console import re @@ -25,6 +24,8 @@ def check(value, checks, name): incorrect = False if value == {}: + if skip_opt and "optional" in checks and checks["optional"] is True: + return None incorrect = True if not incorrect and "type" in checks: try: @@ -88,6 +89,7 @@ def check(value, checks, name): if "oob_error" in checks else "Input out of bounds(Value too high/low/long/short)", options=checks["options"] if "options" in checks else None, + optional=checks["optional"] if "optional" in checks else False, ) return value @@ -103,10 +105,13 @@ def crawl_and_check(obj: dict, path: list, checks: dict = {}, name=""): def check_vars(path, checks): global config + global skip_opt + skip_opt = "skip_opt" in config crawl_and_check(config, path, checks) -def check_toml(template_file, config_file) -> bool: +def check_toml(template_file, config_file) -> (bool, dict): + global config try: template = toml.load(template_file) except Exception as error: @@ -115,7 +120,6 @@ def check_toml(template_file, config_file) -> bool: ) return False try: - global config config = toml.load(config_file) except (toml.TomlDecodeError): console.print( @@ -160,7 +164,7 @@ If you see any prompts, that means that you have unset/incorrectly set variables """ ) crawl(template, check_vars) - # pretty.pprint(config) + config["skip_opt"] = True with open(config_file, "w") as f: toml.dump(config, f) return config @@ -168,3 +172,7 @@ If you see any prompts, that means that you have unset/incorrectly set variables if __name__ == "__main__": check_toml(".config.template.toml", "config.toml") + +if __name__ == "__main__": + check_toml(".config.template.toml", "config.toml") + check_toml(".config.template.toml", "config.toml")