Skip to content

Commit

Permalink
Split config file
Browse files Browse the repository at this point in the history
  • Loading branch information
ManiMatter committed May 24, 2024
1 parent 327d53f commit c7cd204
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 85 deletions.
86 changes: 3 additions & 83 deletions config/config.py → config/definitions.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,8 @@
#!/usr/bin/env python
import sys
import os
import configparser
import json
########################################################################################################################
# Check if in Docker
IS_IN_DOCKER = os.environ.get('IS_IN_DOCKER')
IMAGE_TAG = os.environ.get('IMAGE_TAG', 'Local')
SHORT_COMMIT_ID = os.environ.get('SHORT_COMMIT_ID', 'n/a')

########################################################################################################################
def config_section_map(section):
'Load the config file into a dictionary'
dict1 = {}
options = config.options(section)
for option in options:
try:
dict1[option] = config.get(section, option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1

def cast(value, type_):
return type_(value)

def get_config_value(key, config_section, is_mandatory, datatype, default_value = None):
'Return for each key the corresponding value from the Docker Environment or the Config File'
if IS_IN_DOCKER:
config_value = os.environ.get(key)
if config_value is not None:
# print(f'The value retrieved for [{config_section}]: {key} is "{config_value}"')
config_value = config_value
# return config_value
elif is_mandatory:
print(f'[ ERROR ]: Variable not specified in Docker environment: {key}' )
sys.exit(0)
else:
# return default_value
# print(f'The default value used for [{config_section}]: {key} is "{default_value}" (data type: {type(default_value).__name__})')
config_value = default_value
from config.parser import get_config_value
from config.env_vars import *

else:
try:
config_value = config_section_map(config_section).get(key)
except configparser.NoSectionError:
config_value = None
if config_value is not None:
# print(f'The value retrieved for [{config_section}]: {key} is "{config_value}"')
config_value = config_value
# return config_value
elif is_mandatory:
print(f'[ ERROR ]: Mandatory variable not specified in config file, section [{config_section}]: {key} (data type: {datatype.__name__})')
sys.exit(0)
else:
# return default_value
# print(f'The default value used for [{config_section}]: {key} is "{default_value}" (data type: {type(default_value).__name__})')
config_value = default_value

# Apply data type
try:
if datatype == bool:
config_value = eval(str(config_value).capitalize())
elif datatype == list:
config_value = json.loads(config_value)
elif config_value is not None:
config_value = cast(config_value, datatype)
except Exception as e:
print(f'[ ERROR ]: The value retrieved for [{config_section}]: {key} is "{config_value}" and cannot be converted to data type {datatype}')
print(e)
sys.exit(0)
return config_value

########################################################################################################################
# Load Config File
config_file_name = 'config.conf'
config_file_full_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), config_file_name)
sys.tracebacklimit = 0 # dont show stack traces in prod mode
config = configparser.ConfigParser()
config.optionxform = str # maintain capitalization of config keys
config.read(config_file_full_path)

########################################################################################################################
# Load Config
# Define data types and default values for settingsDict variables
# General
LOG_LEVEL = get_config_value('LOG_LEVEL', 'general', False, str, 'INFO')
TEST_RUN = get_config_value('TEST_RUN', 'general', False, bool, False)
Expand Down
4 changes: 4 additions & 0 deletions config/env_vars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os
IS_IN_DOCKER = os.environ.get('IS_IN_DOCKER')
IMAGE_TAG = os.environ.get('IMAGE_TAG', 'Local')
SHORT_COMMIT_ID = os.environ.get('SHORT_COMMIT_ID', 'n/a')
79 changes: 79 additions & 0 deletions config/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python
import sys
import os
import configparser
import json
from config.env_vars import *

# Configures how to parse configuration file
config_file_name = 'config.conf'
config_file_full_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), config_file_name)
sys.tracebacklimit = 0 # dont show stack traces in prod mode
config = configparser.ConfigParser()
config.optionxform = str # maintain capitalization of config keys
config.read(config_file_full_path)


def config_section_map(section):
'Load the config file into a dictionary'
dict1 = {}
options = config.options(section)
for option in options:
try:
dict1[option] = config.get(section, option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1

def cast(value, type_):
return type_(value)

def get_config_value(key, config_section, is_mandatory, datatype, default_value = None):
'Return for each key the corresponding value from the Docker Environment or the Config File'
if IS_IN_DOCKER:
config_value = os.environ.get(key)
if config_value is not None:
# print(f'The value retrieved for [{config_section}]: {key} is "{config_value}"')
config_value = config_value
# return config_value
elif is_mandatory:
print(f'[ ERROR ]: Variable not specified in Docker environment: {key}' )
sys.exit(0)
else:
# return default_value
# print(f'The default value used for [{config_section}]: {key} is "{default_value}" (data type: {type(default_value).__name__})')
config_value = default_value

else:
try:
config_value = config_section_map(config_section).get(key)
except configparser.NoSectionError:
config_value = None
if config_value is not None:
# print(f'The value retrieved for [{config_section}]: {key} is "{config_value}"')
config_value = config_value
# return config_value
elif is_mandatory:
print(f'[ ERROR ]: Mandatory variable not specified in config file, section [{config_section}]: {key} (data type: {datatype.__name__})')
sys.exit(0)
else:
# return default_value
# print(f'The default value used for [{config_section}]: {key} is "{default_value}" (data type: {type(default_value).__name__})')
config_value = default_value

# Apply data type
try:
if datatype == bool:
config_value = eval(str(config_value).capitalize())
elif datatype == list:
config_value = json.loads(config_value)
elif config_value is not None:
config_value = cast(config_value, datatype)
except Exception as e:
print(f'[ ERROR ]: The value retrieved for [{config_section}]: {key} is "{config_value}" and cannot be converted to data type {datatype}')
print(e)
sys.exit(0)
return config_value


2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
logger = verboselogs.VerboseLogger(__name__)
import json
# Import Functions
from config.config import settingsDict
from config.definitions import settingsDict
from src.utils.loadScripts import *
from src.decluttarr import queueCleaner
from src.utils.rest import rest_get, rest_post
Expand Down
2 changes: 1 addition & 1 deletion src/utils/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import requests
from requests.exceptions import RequestException
import json
from config.config import settingsDict
from config.definitions import settingsDict

# GET
async def rest_get(url, api_key=None, params=None, cookies=None):
Expand Down

0 comments on commit c7cd204

Please sign in to comment.