fix: optionals return default or empty string

pull/861/head
Callum Leslie 3 years ago
parent 869bfd874a
commit d0a7697256
No known key found for this signature in database
GPG Key ID: D382C4AFEECEAA90

@ -3,21 +3,21 @@ client_id = { optional = false, nmin = 12, nmax = 30, explanation = "the ID of y
client_secret = { optional = false, nmin = 20, nmax = 40, explanation = "the SECRET of your Reddit app of SCRIPT type", example = "fFAGRNJru1FTz70BzhT3Zg", regex = "^[-a-zA-Z0-9._~+/]+=*$", input_error = "The client ID can only contain printable characters.", oob_error = "The secret should be over 20 and under 40 characters, double check your input." }
username = { optional = false, nmin = 3, nmax = 20, explanation = "the username of your reddit account", example = "asdfghjkl", regex = "^[-_0-9a-zA-Z]+$", oob_error = "A username HAS to be between 3 and 20 characters" }
password = { optional = false, nmin = 8, explanation = "the password of your reddit account", example = "fFAGRNJru1FTz70BzhT3Zg", oob_error = "Password too short" }
2fa = { optional = false, type = "bool", options = [
2fa = { optional = true, type = "bool", options = [
true,
false,
], default = false, explanation = "Whether you have Reddit 2FA enabled, Valid options are True and False", example = true }
[reddit.thread]
random = { optional = false, options = [
random = { optional = true, options = [
true,
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, default = "", regex = "^((?!://|://).)*$", explanation = "Used if you want to use a specific post.", example = "urdtfx" }
post_id = { optional = true, default = "", 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" }
post_lang = { default = "", optional = false, explanation = "The language you would like to translate to - leave blank for none.", example = "es-cr"}
post_lang = { default = "", optional = true, explanation = "The language you would like to translate to.", example = "es-cr"}
[settings]
allow_nsfw = { optional = false, type = "bool", default = false, example = false, options = [
@ -30,7 +30,7 @@ theme = { optional = false, default = "light", example = "dark", options = [
], explanation = "sets the Reddit theme, either LIGHT or DARK" }
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 = false, type = "bool", default = false, example = false, options = [
storymode = { optional = true, type = "bool", default = false, example = false, options = [
true,
false,
] }

@ -54,7 +54,7 @@ def handle_input(
+ "\n[green]This is an optional value. Do you want to skip it? (y/n)"
)
if input().casefold().startswith("y"):
return None
return default if default is not NotImplemented else ""
if default is not NotImplemented:
console.print(
"[green]"

@ -23,11 +23,10 @@ def crawl(obj: dict, func=lambda x, y: print(x, y, end="\n"), path: list = []):
def check(value, checks, name):
def get_check_value(key, default_result):
return checks[key] if key in checks else default_result
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:
@ -96,20 +95,16 @@ def check(value, checks, name):
+ "[#C0CAF5 bold]"
+ str(name)
+ "[#F7768E bold]=",
extra_info=checks["explanation"] if "explanation" in checks else "",
check_type=eval(checks["type"]) if "type" in checks else False,
default=checks["default"] if "default" in checks else NotImplemented,
match=checks["regex"] if "regex" in checks else "",
err_message=checks["input_error"]
if "input_error" in checks
else "Incorrect input",
nmin=checks["nmin"] if "nmin" in checks else None,
nmax=checks["nmax"] if "nmax" in checks else None,
oob_error=checks["oob_error"]
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,
extra_info=get_check_value("explanation", ""),
check_type=eval(get_check_value("type", "False")),
default=get_check_value("default", NotImplemented),
match=get_check_value("regex", ""),
err_message=get_check_value("input_error", "Incorrect input"),
nmin=get_check_value("nmin", None),
nmax=get_check_value("nmax", None),
oob_error=get_check_value("oob_error", "Input out of bounds(Value too high/low/long/short)"),
options=get_check_value("options", None),
optional=get_check_value("optional", False),
)
return value
@ -125,8 +120,6 @@ 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)
@ -185,7 +178,6 @@ If you see any prompts, that means that you have unset/incorrectly set variables
"""
)
crawl(template, check_vars)
config["skip_opt"] = True
with open(config_file, "w") as f:
toml.dump(config, f)
return config

Loading…
Cancel
Save