0

Just want to know is there any proper way to load multiple config files to python scripts. Directory structure as below.

 dau
   |-APPS
   |---kafka
   |---brokers
   |-ENVS

As per the above, my base directory is dau. I'm planing to hold the script in Kafka and Broker directories. All global environments store in ENVS directory with ".ini" format. I want to load those ini files to all the script without adding one by one, because we may have to add more environments files in the future , in that case we don't have to add them manually on each and every scripts.

Sample env.ini

[DEV]
SERVER_NAME = dev123.abcd.net

i was trying to use the answer of below link, but still we have to add them manually, or if the parent path change in the dau directory, we have to edit the code.

Stack-flow-answer

SLS
  • 445
  • 1
  • 6
  • 20
  • Unclear for me. What is the reason for adding a config file to a Python script without changing the script to use the new config parameters? Or are you trying to provide sets of environment variables to your scripts, which is a different problem from the linked question? – Serge Ballesta Aug 23 '18 at 10:26
  • I need to use multiple config files (ini files) and call it from single location for multiple script. – SLS Aug 24 '18 at 03:48

1 Answers1

0

Hi I came up with below solution, Thanks for the support.

Below code will get the all .ini file as list and return.

import os

def All_env_files():
    try:
        BASE_PATH = os.path.abspath(os.path.join(__file__,"../.."))

        ENV_INI_FILES = [os.path.join(BASE_PATH + '/ENVS/',each) for each in os.listdir(BASE_PATH + '/ENVS') if each.endswith('.ini')]

        return ENV_INI_FILES
    except ValueError:
        raise ValueError('Issue with Gathering Files from ENVS Directory')

Below code will take the list ini files and provide it to ConfigParser.

import ConfigParser, sys , os
"""
This is for kafka broker status check 
"""
#Get Base path
Base_PATH = os.path.abspath(os.path.join(__file__,"../../.."))
sys.path.insert(0, Base_PATH)
#Importing configs python file on ../Configs.py
import Configs, edpCMD

#Taking All the ENVS ini file as list
List_ENVS = Configs.All_env_files()

Feel free to provide any shorter way to this.

SLS
  • 445
  • 1
  • 6
  • 20