Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- This script parses environment variables and gets set of three variables at the time.
- It then calls function with those arguments (database, username, password) based on pattern:
- DB_<UNIQUENAME>_DATABASE
- DB_<UNIQUENAME>_USERNAME
- DB_<UNIQUENAME>_PASSWORD
- '''
- import re
- # mimics environment variables
- ENVS = {
- 'DB_X_USERNAME': 'x-username',
- 'DB_X_PASSWORD': 'x-password',
- 'DB_X_DATABASE': 'x-database',
- 'DB_Y_USERNAME': 'y-username',
- 'DB_Y_PASSWORD': 'y-password',
- 'DB_Y_DATABASE': 'y-database',
- 'TEMP': 'some random value',
- 'DBNAME': 'should not include'
- }
- def get_uniqe_names(envs):
- '''Creates a set of unique names from environment variables based on regex.'''
- name_set = set()
- pattern = r'^DB_[A-Z]+_DATABASE'
- for key in envs.keys():
- if re.search(pattern, key):
- name_set.add(key.split('_')[1])
- return name_set
- def main():
- '''Call a function if three variables are found from environment variables'''
- for name in get_uniqe_names(ENVS):
- database = ENVS.get(f'DB_{name}_DATABASE')
- username = ENVS.get(f'DB_{name}_USERNAME')
- password = ENVS.get(f'DB_{name}_PASSWORD')
- if all((database, username, password)):
- print('calling function with:', database, username, password)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment