Guest User

Untitled

a guest
Apr 4th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. '''
  2. This script parses environment variables and gets set of three variables at the time.
  3.  
  4. It then calls function with those arguments (database, username, password) based on pattern:
  5. DB_<UNIQUENAME>_DATABASE
  6. DB_<UNIQUENAME>_USERNAME
  7. DB_<UNIQUENAME>_PASSWORD
  8. '''
  9.  
  10. import re
  11.  
  12. # mimics environment variables
  13. ENVS = {
  14. 'DB_X_USERNAME': 'x-username',
  15. 'DB_X_PASSWORD': 'x-password',
  16. 'DB_X_DATABASE': 'x-database',
  17. 'DB_Y_USERNAME': 'y-username',
  18. 'DB_Y_PASSWORD': 'y-password',
  19. 'DB_Y_DATABASE': 'y-database',
  20. 'TEMP': 'some random value',
  21. 'DBNAME': 'should not include'
  22. }
  23.  
  24. def get_uniqe_names(envs):
  25. '''Creates a set of unique names from environment variables based on regex.'''
  26. name_set = set()
  27. pattern = r'^DB_[A-Z]+_DATABASE'
  28. for key in envs.keys():
  29. if re.search(pattern, key):
  30. name_set.add(key.split('_')[1])
  31. return name_set
  32.  
  33. def main():
  34. '''Call a function if three variables are found from environment variables'''
  35. for name in get_uniqe_names(ENVS):
  36. database = ENVS.get(f'DB_{name}_DATABASE')
  37. username = ENVS.get(f'DB_{name}_USERNAME')
  38. password = ENVS.get(f'DB_{name}_PASSWORD')
  39. if all((database, username, password)):
  40. print('calling function with:', database, username, password)
  41.  
  42. if __name__ == '__main__':
  43. main()
Add Comment
Please, Sign In to add comment