From 9bfc122bd4cc7f300706b06b2b71c23645b7d79d Mon Sep 17 00:00:00 2001 From: Joshua Slui Date: Sun, 2 Jul 2023 15:23:00 +0200 Subject: [PATCH 1/4] if !config.json, check for .env --- utilities/data.py | 63 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/utilities/data.py b/utilities/data.py index bd0a890..53a9a28 100644 --- a/utilities/data.py +++ b/utilities/data.py @@ -34,39 +34,40 @@ def get_data() -> dict: If you do not have a config.json file, you can use environment variables. :return: The data from the config.json file. """ - usejson = True # Set to True to a config.json - if usejson: + try: + with open('config.json', 'r', encoding="UTF-8") as file: + data = load(file) + except FileNotFoundError: + print("No config.json file found. Trying to use dotenv...") try: - with open('config.json', 'r', encoding="UTF-8") as file: - data = load(file) - except FileNotFoundError: - print('config.json not found! Exiting now...') - sysexit() - except decoder.JSONDecodeError: - print('config.json is not valid! Exiting now...') - sysexit() - if not usejson: - try: - data = { - "Token": getenv('TOKEN'), - "Prefix": getenv('PREFIX'), - "Owners": getenv('OWNERS').split(','), - "FeatureGuilds": getenv('FEATURE_GUILDS').split(','), - "Database": { - "Host": getenv('DB_HOST'), - "User": getenv('DB_USER'), - "Password": getenv('DB_PASSWORD'), - "Database": getenv('DB_DATABASE'), - "Port": getenv('DB_PORT') - }, - "Logs": { - "JoinWebhook": getenv('LOGS_JOINWEBHOOK'), - "LeaveWebhook": getenv('LOGS_LEAVEWEBHOOK') + from dotenv import load_dotenv + load_dotenv() + except ModuleNotFoundError: + exit("You did not install the dotenv module! Exiting now...") + else: + try: + data = { + "Token": getenv('TOKEN'), + "Prefix": getenv('PREFIX'), + "Owners": getenv('OWNERS').split(','), + "FeatureGuilds": getenv('FEATURE_GUILDS').split(','), + "Database": { + "Host": getenv('DB_HOST'), + "User": getenv('DB_USER'), + "Password": getenv('DB_PASSWORD'), + "Database": getenv('DB_DATABASE'), + "Port": getenv('DB_PORT') + }, + "Logs": { + "JoinWebhook": getenv('LOGS_JOINWEBHOOK'), + "LeaveWebhook": getenv('LOGS_LEAVEWEBHOOK') + } } - } - except AttributeError: - print('You did not fill out the environment variables! Exiting now...') - sysexit() + except AttributeError: + exit('No environment variables could be found. Exiting now...') + except decoder.JSONDecodeError: + print('config.json is not valid! Exiting now...') + sysexit() return data From a780084da4a592b03e5d32dee5f1464e92bfcef6 Mon Sep 17 00:00:00 2001 From: Joshua Slui Date: Sun, 2 Jul 2023 15:26:17 +0200 Subject: [PATCH 2/4] PYLINT.... --- utilities/data.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/utilities/data.py b/utilities/data.py index 53a9a28..b5edd72 100644 --- a/utilities/data.py +++ b/utilities/data.py @@ -3,11 +3,6 @@ from os import getenv from sys import exit as sysexit -try: - from dotenv import load_dotenv - load_dotenv() -except ModuleNotFoundError: - print('You did not install the dotenv module! You will not be able to use a .env file.') try: from mcstatus import JavaServer from mcstatus.pinger import PingResponse @@ -43,7 +38,7 @@ def get_data() -> dict: from dotenv import load_dotenv load_dotenv() except ModuleNotFoundError: - exit("You did not install the dotenv module! Exiting now...") + sysexit("You did not install the dotenv module! Exiting now...") else: try: data = { @@ -66,8 +61,7 @@ def get_data() -> dict: except AttributeError: exit('No environment variables could be found. Exiting now...') except decoder.JSONDecodeError: - print('config.json is not valid! Exiting now...') - sysexit() + sysexit('config.json is not valid! Exiting now...') return data From 8111e4e21035e9a4e4f15d6061f3d091df993c68 Mon Sep 17 00:00:00 2001 From: Joshua Slui Date: Sun, 2 Jul 2023 15:32:43 +0200 Subject: [PATCH 3/4] I hate pylint so much --- utilities/data.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/utilities/data.py b/utilities/data.py index b5edd72..e5e33d7 100644 --- a/utilities/data.py +++ b/utilities/data.py @@ -3,12 +3,18 @@ from os import getenv from sys import exit as sysexit +try: + from dotenv import load_dotenv + + load_dotenv() +except ModuleNotFoundError: + print("You did not install the dotenv module! .env files can not be used.") + dotenv_installed = False try: from mcstatus import JavaServer from mcstatus.pinger import PingResponse except ModuleNotFoundError: - print('You did not install the mcstatus module! Exiting now...') - sysexit() + sysexit('You did not install the mcstatus module! Exiting now...') class Colors: @@ -34,11 +40,8 @@ def get_data() -> dict: data = load(file) except FileNotFoundError: print("No config.json file found. Trying to use dotenv...") - try: - from dotenv import load_dotenv - load_dotenv() - except ModuleNotFoundError: - sysexit("You did not install the dotenv module! Exiting now...") + if not dotenv_installed: + sysexit('You did not install the dotenv module and no config.json was provided! Exiting now...') else: try: data = { @@ -59,7 +62,7 @@ def get_data() -> dict: } } except AttributeError: - exit('No environment variables could be found. Exiting now...') + sysexit('One or more environment variables could be found. Exiting now...') except decoder.JSONDecodeError: sysexit('config.json is not valid! Exiting now...') return data From caa331211f126e412037f7dc5806e91ad316e705 Mon Sep 17 00:00:00 2001 From: Joshua Slui Date: Sun, 2 Jul 2023 15:35:51 +0200 Subject: [PATCH 4/4] I hate pylint so much --- utilities/data.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/utilities/data.py b/utilities/data.py index e5e33d7..981679f 100644 --- a/utilities/data.py +++ b/utilities/data.py @@ -3,13 +3,15 @@ from os import getenv from sys import exit as sysexit +DOTENV_INSTALLED = True + try: from dotenv import load_dotenv load_dotenv() except ModuleNotFoundError: print("You did not install the dotenv module! .env files can not be used.") - dotenv_installed = False + DOTENV_INSTALLED = False try: from mcstatus import JavaServer from mcstatus.pinger import PingResponse @@ -40,7 +42,7 @@ def get_data() -> dict: data = load(file) except FileNotFoundError: print("No config.json file found. Trying to use dotenv...") - if not dotenv_installed: + if not DOTENV_INSTALLED: sysexit('You did not install the dotenv module and no config.json was provided! Exiting now...') else: try: