commit
51831ddbd9
@ -0,0 +1,38 @@
|
||||
[reddit.creds]
|
||||
client_id = { optional = false, nmin = 12, nmax = 30, explanation = "the ID 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 ID should be over 12 and under 30 characters, double check your input." }
|
||||
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 = "JasonLovesDoggo", 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 = 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 = 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 = true, default = "", regex = "^((?!://|://)[+a-zA-Z])*$", 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 = 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 = [true,
|
||||
false,
|
||||
], explanation = "Whether to allow NSFW content, True or False" }
|
||||
theme = { optional = false, default = "dark", example = "light", options = ["dark",
|
||||
"light",
|
||||
], 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 = true, type = "bool", default = false, example = false, options = [true,
|
||||
false,
|
||||
] }
|
||||
background_choice = { optional = true, default = "minecraft", example = "minecraft", options = ["minecraft", "gta", "rocket-league", "motor-gta"], explanation = "Sets the background for the video" }
|
||||
|
||||
[settings.tts]
|
||||
choice = { optional = false, default = "", options = ["streamlabspolly", "tiktok", "googletranslate", "awspolly", ], example = "streamlabspolly", explanation = "The backend used for TTS generation. This can be left blank and you will be prompted to choose at runtime." }
|
||||
aws_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for AWS Polly" }
|
||||
streamlabs_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for Streamlabs Polly" }
|
||||
tiktok_voice = { optional = false, default = "en_us_006", example = "en_us_006", explanation = "The voice used for TikTok TTS" }
|
@ -0,0 +1,10 @@
|
||||
name: Lint
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: psf/black@stable
|
@ -1,193 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from rich import box
|
||||
import re
|
||||
import dotenv
|
||||
from utils.console import handle_input
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
def check_env() -> bool:
|
||||
"""Checks to see what's been put in .env
|
||||
|
||||
Returns:
|
||||
bool: Whether or not everything was put in properly
|
||||
"""
|
||||
if not os.path.exists(".env.template"):
|
||||
console.print("[red]Couldn't find .env.template. Unable to check variables.")
|
||||
return True
|
||||
if not os.path.exists(".env"):
|
||||
console.print("[red]Couldn't find the .env file, creating one now.")
|
||||
with open(".env", "x", encoding="utf-8") as file:
|
||||
file.write("")
|
||||
success = True
|
||||
with open(".env.template", "r", encoding="utf-8") as template:
|
||||
# req_envs = [env.split("=")[0] for env in template.readlines() if "=" in env]
|
||||
matching = {}
|
||||
explanations = {}
|
||||
bounds = {}
|
||||
types = {}
|
||||
oob_errors = {}
|
||||
examples = {}
|
||||
req_envs = []
|
||||
var_optional = False
|
||||
for line in template.readlines():
|
||||
if line.startswith("#") is not True and "=" in line and var_optional is not True:
|
||||
req_envs.append(line.split("=")[0])
|
||||
if "#" in line:
|
||||
examples[line.split("=")[0]] = "#".join(line.split("#")[1:]).strip()
|
||||
elif "#OPTIONAL" in line:
|
||||
var_optional = True
|
||||
elif line.startswith("#MATCH_REGEX "):
|
||||
matching[req_envs[-1]] = line.removeprefix("#MATCH_REGEX ")[:-1]
|
||||
var_optional = False
|
||||
elif line.startswith("#OOB_ERROR "):
|
||||
oob_errors[req_envs[-1]] = line.removeprefix("#OOB_ERROR ")[:-1]
|
||||
var_optional = False
|
||||
elif line.startswith("#RANGE "):
|
||||
bounds[req_envs[-1]] = tuple(
|
||||
map(
|
||||
lambda x: float(x) if x != "None" else None,
|
||||
line.removeprefix("#RANGE ")[:-1].split(":"),
|
||||
)
|
||||
)
|
||||
var_optional = False
|
||||
elif line.startswith("#MATCH_TYPE "):
|
||||
types[req_envs[-1]] = eval(line.removeprefix("#MATCH_TYPE ")[:-1].split()[0])
|
||||
var_optional = False
|
||||
elif line.startswith("#EXPLANATION "):
|
||||
explanations[req_envs[-1]] = line.removeprefix("#EXPLANATION ")[:-1]
|
||||
var_optional = False
|
||||
else:
|
||||
var_optional = False
|
||||
missing = set()
|
||||
incorrect = set()
|
||||
dotenv.load_dotenv()
|
||||
for env in req_envs:
|
||||
value = os.getenv(env)
|
||||
if value is None:
|
||||
missing.add(env)
|
||||
continue
|
||||
if env in matching.keys():
|
||||
re.match(matching[env], value) is None and incorrect.add(env)
|
||||
if env in bounds.keys() and env not in types.keys():
|
||||
len(value) >= bounds[env][0] or (
|
||||
len(bounds[env]) > 1 and bounds[env][1] >= len(value)
|
||||
) or incorrect.add(env)
|
||||
continue
|
||||
if env in types.keys():
|
||||
try:
|
||||
temp = types[env](value)
|
||||
if env in bounds.keys():
|
||||
(bounds[env][0] <= temp or incorrect.add(env)) and len(bounds[env]) > 1 and (
|
||||
bounds[env][1] >= temp or incorrect.add(env)
|
||||
)
|
||||
except ValueError:
|
||||
incorrect.add(env)
|
||||
|
||||
if len(missing):
|
||||
table = Table(
|
||||
title="Missing variables",
|
||||
highlight=True,
|
||||
show_lines=True,
|
||||
box=box.ROUNDED,
|
||||
border_style="#414868",
|
||||
header_style="#C0CAF5 bold",
|
||||
title_justify="left",
|
||||
title_style="#C0CAF5 bold",
|
||||
)
|
||||
table.add_column("Variable", justify="left", style="#7AA2F7 bold", no_wrap=True)
|
||||
table.add_column("Explanation", justify="left", style="#BB9AF7", no_wrap=False)
|
||||
table.add_column("Example", justify="center", style="#F7768E", no_wrap=True)
|
||||
table.add_column("Min", justify="right", style="#F7768E", no_wrap=True)
|
||||
table.add_column("Max", justify="left", style="#F7768E", no_wrap=True)
|
||||
for env in missing:
|
||||
table.add_row(
|
||||
env,
|
||||
explanations[env] if env in explanations.keys() else "No explanation given",
|
||||
examples[env] if env in examples.keys() else "",
|
||||
str(bounds[env][0]) if env in bounds.keys() and bounds[env][1] is not None else "",
|
||||
str(bounds[env][1])
|
||||
if env in bounds.keys() and len(bounds[env]) > 1 and bounds[env][1] is not None
|
||||
else "",
|
||||
)
|
||||
console.print(table)
|
||||
success = False
|
||||
if len(incorrect):
|
||||
table = Table(
|
||||
title="Incorrect variables",
|
||||
highlight=True,
|
||||
show_lines=True,
|
||||
box=box.ROUNDED,
|
||||
border_style="#414868",
|
||||
header_style="#C0CAF5 bold",
|
||||
title_justify="left",
|
||||
title_style="#C0CAF5 bold",
|
||||
)
|
||||
table.add_column("Variable", justify="left", style="#7AA2F7 bold", no_wrap=True)
|
||||
table.add_column("Current value", justify="left", style="#F7768E", no_wrap=False)
|
||||
table.add_column("Explanation", justify="left", style="#BB9AF7", no_wrap=False)
|
||||
table.add_column("Example", justify="center", style="#F7768E", no_wrap=True)
|
||||
table.add_column("Min", justify="right", style="#F7768E", no_wrap=True)
|
||||
table.add_column("Max", justify="left", style="#F7768E", no_wrap=True)
|
||||
for env in incorrect:
|
||||
table.add_row(
|
||||
env,
|
||||
os.getenv(env),
|
||||
explanations[env] if env in explanations.keys() else "No explanation given",
|
||||
str(types[env].__name__) if env in types.keys() else "str",
|
||||
str(bounds[env][0]) if env in bounds.keys() else "None",
|
||||
str(bounds[env][1]) if env in bounds.keys() and len(bounds[env]) > 1 else "None",
|
||||
)
|
||||
missing.add(env)
|
||||
console.print(table)
|
||||
success = False
|
||||
if success is True:
|
||||
return True
|
||||
console.print(
|
||||
"[green]Do you want to automatically overwrite incorrect variables and add the missing variables? (y/n)"
|
||||
)
|
||||
if not input().casefold().startswith("y"):
|
||||
console.print("[red]Aborting: Unresolved missing variables")
|
||||
return False
|
||||
if len(incorrect):
|
||||
with open(".env", "r+", encoding="utf-8") as env_file:
|
||||
lines = []
|
||||
for line in env_file.readlines():
|
||||
line.split("=")[0].strip() not in incorrect and lines.append(line)
|
||||
env_file.seek(0)
|
||||
env_file.write("\n".join(lines))
|
||||
env_file.truncate()
|
||||
console.print("[green]Successfully removed incorrectly set variables from .env")
|
||||
with open(".env", "a", encoding="utf-8") as env_file:
|
||||
for env in missing:
|
||||
env_file.write(
|
||||
env
|
||||
+ "="
|
||||
+ ('"' if env not in types.keys() else "")
|
||||
+ str(
|
||||
handle_input(
|
||||
"[#F7768E bold]" + env + "[#C0CAF5 bold]=",
|
||||
types[env] if env in types.keys() else False,
|
||||
matching[env] if env in matching.keys() else ".*",
|
||||
explanations[env]
|
||||
if env in explanations.keys()
|
||||
else "Incorrect input. Try again.",
|
||||
bounds[env][0] if env in bounds.keys() else None,
|
||||
bounds[env][1] if env in bounds.keys() and len(bounds[env]) > 1 else None,
|
||||
oob_errors[env] if env in oob_errors.keys() else "Input too long/short.",
|
||||
extra_info="[#C0CAF5 bold]⮶ "
|
||||
+ (explanations[env] if env in explanations.keys() else "No info available"),
|
||||
)
|
||||
)
|
||||
+ ('"' if env not in types.keys() else "")
|
||||
+ "\n"
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_env()
|
@ -1,46 +0,0 @@
|
||||
# write a class that takes .env file and parses it into a dictionary
|
||||
from dotenv import dotenv_values
|
||||
|
||||
DEFAULTS = {
|
||||
"SUBREDDIT": "AskReddit",
|
||||
"ALLOW_NSFW": "False",
|
||||
"POST_ID": "",
|
||||
"THEME": "DARK",
|
||||
"REDDIT_2FA": "no",
|
||||
"TIMES_TO_RUN": "",
|
||||
"MAX_COMMENT_LENGTH": "500",
|
||||
"OPACITY": "1",
|
||||
"VOICE": "en_us_001",
|
||||
"STORYMODE": "False",
|
||||
}
|
||||
|
||||
|
||||
class Config:
|
||||
def __init__(self):
|
||||
self.raw = dotenv_values("../.env")
|
||||
self.load_attrs()
|
||||
|
||||
def __getattr__(self, attr): # code completion for attributes fix.
|
||||
return getattr(self, attr)
|
||||
|
||||
def load_attrs(self):
|
||||
for key, value in self.raw.items():
|
||||
self.add_attr(key, value)
|
||||
|
||||
def add_attr(self, key, value):
|
||||
if value is None or value == "":
|
||||
setattr(self, key, DEFAULTS[key])
|
||||
else:
|
||||
setattr(self, key, str(value))
|
||||
|
||||
|
||||
config = Config()
|
||||
|
||||
print(config.SUBREDDIT)
|
||||
# def temp():
|
||||
# root = ''
|
||||
# if isinstance(root, praw.models.Submission):
|
||||
# root_type = 'submission'
|
||||
# elif isinstance(root, praw.models.Comment):
|
||||
# root_type = 'comment'
|
||||
#
|
@ -0,0 +1,189 @@
|
||||
#!/usr/bin/env python
|
||||
import toml
|
||||
from rich.console import Console
|
||||
import re
|
||||
|
||||
from typing import Tuple, Dict
|
||||
|
||||
from utils.console import handle_input
|
||||
|
||||
|
||||
console = Console()
|
||||
config = dict # autocomplete
|
||||
|
||||
def crawl(obj: dict, func=lambda x, y: print(x, y, end="\n"), path=None):
|
||||
if path is None: # path Default argument value is mutable
|
||||
path = []
|
||||
for key in obj.keys():
|
||||
if type(obj[key]) is dict:
|
||||
crawl(obj[key], func, path + [key])
|
||||
continue
|
||||
func(path + [key], obj[key])
|
||||
|
||||
|
||||
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 == {}:
|
||||
incorrect = True
|
||||
if not incorrect and "type" in checks:
|
||||
try:
|
||||
value = eval(checks["type"])(value)
|
||||
except:
|
||||
incorrect = True
|
||||
|
||||
if (
|
||||
not incorrect and "options" in checks and value not in checks["options"]
|
||||
): # FAILSTATE Value is not one of the options
|
||||
incorrect = True
|
||||
if (
|
||||
not incorrect
|
||||
and "regex" in checks
|
||||
and (
|
||||
(isinstance(value, str) and re.match(checks["regex"], value) is None)
|
||||
or not isinstance(value, str)
|
||||
)
|
||||
): # FAILSTATE Value doesn't match regex, or has regex but is not a string.
|
||||
incorrect = True
|
||||
|
||||
if (
|
||||
not incorrect
|
||||
and not hasattr(value, "__iter__")
|
||||
and (
|
||||
("nmin" in checks and checks["nmin"] is not None and value < checks["nmin"])
|
||||
or (
|
||||
"nmax" in checks
|
||||
and checks["nmax"] is not None
|
||||
and value > checks["nmax"]
|
||||
)
|
||||
)
|
||||
):
|
||||
incorrect = True
|
||||
if (
|
||||
not incorrect
|
||||
and hasattr(value, "__iter__")
|
||||
and (
|
||||
(
|
||||
"nmin" in checks
|
||||
and checks["nmin"] is not None
|
||||
and len(value) < checks["nmin"]
|
||||
)
|
||||
or (
|
||||
"nmax" in checks
|
||||
and checks["nmax"] is not None
|
||||
and len(value) > checks["nmax"]
|
||||
)
|
||||
)
|
||||
):
|
||||
incorrect = True
|
||||
|
||||
if incorrect:
|
||||
value = handle_input(
|
||||
message=(
|
||||
(
|
||||
("[blue]Example: " + str(checks["example"]) + "\n")
|
||||
if "example" in checks
|
||||
else ""
|
||||
)
|
||||
+ "[red]"
|
||||
+ ("Non-optional ", "Optional ")[
|
||||
"optional" in checks and checks["optional"] is True
|
||||
]
|
||||
)
|
||||
+ "[#C0CAF5 bold]"
|
||||
+ str(name)
|
||||
+ "[#F7768E bold]=",
|
||||
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
|
||||
|
||||
|
||||
def crawl_and_check(obj: dict, path: list, checks: dict = {}, name=""):
|
||||
if len(path) == 0:
|
||||
return check(obj, checks, name)
|
||||
if path[0] not in obj.keys():
|
||||
obj[path[0]] = {}
|
||||
obj[path[0]] = crawl_and_check(obj[path[0]], path[1:], checks, path[0])
|
||||
return obj
|
||||
|
||||
|
||||
def check_vars(path, checks):
|
||||
global config
|
||||
crawl_and_check(config, path, checks)
|
||||
|
||||
|
||||
def check_toml(template_file, config_file) -> Tuple[bool, Dict]:
|
||||
global config
|
||||
config = None
|
||||
try:
|
||||
template = toml.load(template_file)
|
||||
except Exception as error:
|
||||
console.print(
|
||||
f"[red bold]Encountered error when trying to to load {template_file}: {error}"
|
||||
)
|
||||
return False
|
||||
try:
|
||||
config = toml.load(config_file)
|
||||
except toml.TomlDecodeError:
|
||||
console.print(
|
||||
f"""[blue]Couldn't read {config_file}.
|
||||
Overwrite it?(y/n)"""
|
||||
)
|
||||
if not input().startswith("y"):
|
||||
print("Unable to read config, and not allowed to overwrite it. Giving up.")
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
with open(config_file, "w") as f:
|
||||
f.write("")
|
||||
except:
|
||||
console.print(
|
||||
f"[red bold]Failed to overwrite {config_file}. Giving up.\nSuggestion: check {config_file} permissions for the user."
|
||||
)
|
||||
return False
|
||||
except FileNotFoundError:
|
||||
console.print(
|
||||
f"""[blue]Couldn't find {config_file}
|
||||
Creating it now."""
|
||||
)
|
||||
try:
|
||||
with open(config_file, "x") as f:
|
||||
f.write("")
|
||||
config = {}
|
||||
except:
|
||||
console.print(
|
||||
f"[red bold]Failed to write to {config_file}. Giving up.\nSuggestion: check the folder's permissions for the user."
|
||||
)
|
||||
return False
|
||||
|
||||
console.print(
|
||||
"""\
|
||||
[blue bold]###############################
|
||||
# #
|
||||
# Checking TOML configuration #
|
||||
# #
|
||||
###############################
|
||||
If you see any prompts, that means that you have unset/incorrectly set variables, please input the correct values.\
|
||||
"""
|
||||
)
|
||||
crawl(template, check_vars)
|
||||
with open(config_file, "w") as f:
|
||||
toml.dump(config, f)
|
||||
return config
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_toml(".config.template.toml", "config.toml")
|
Loading…
Reference in new issue