config_parser.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import sys
  2. from os import path, environ
  3. import hiyapyco
  4. import yaml
  5. configs = (
  6. 'application',
  7. 'logging',
  8. 'namespaces',
  9. 'flask',
  10. )
  11. # This will hold a dict of all configuration values.
  12. config = {}
  13. # Parse configuration
  14. CONFIG_DIR = environ.get(
  15. 'FCREPO_CONFIG_DIR',
  16. path.dirname(path.dirname(path.abspath(__file__))) + '/etc.defaults')
  17. print('Reading configuration at {}'.format(CONFIG_DIR))
  18. for cname in configs:
  19. file = '{}/{}.yml'.format(CONFIG_DIR , cname)
  20. with open(file, 'r') as stream:
  21. config[cname] = yaml.load(stream, yaml.SafeLoader)
  22. # Merge default and test configurations.
  23. error_msg = '''
  24. **************
  25. ** WARNING! **
  26. **************
  27. Your test {} store location is set to be the same as the production location.
  28. This means that if you run a test suite, your live data may be wiped clean!
  29. Please review your configuration before starting.
  30. '''
  31. test_config = {'application': hiyapyco.load(CONFIG_DIR + '/application.yml',
  32. CONFIG_DIR + '/test.yml', method=hiyapyco.METHOD_MERGE)}
  33. if config['application']['store']['ldp_rs']['location'] \
  34. == test_config['application']['store']['ldp_rs']['location']:
  35. raise RuntimeError(error_msg.format('RDF'))
  36. sys.exit()
  37. if config['application']['store']['ldp_nr']['path'] \
  38. == test_config['application']['store']['ldp_nr']['path']:
  39. raise RuntimeError(error_msg.format('binary'))
  40. sys.exit()