From ef9ac9de1721789c2e609d6d6b8a713844ed1875 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 13 Jun 2022 22:28:50 -0400 Subject: [PATCH] sample config module --- utils/config.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 utils/config.py diff --git a/utils/config.py b/utils/config.py new file mode 100644 index 0000000..78beb99 --- /dev/null +++ b/utils/config.py @@ -0,0 +1,30 @@ +# 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)