config_parser.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import sys
  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. if 'FCREPO_CONFIG_DIR' in os.environ:
  15. CONFIG_DIR = os.environ['FCREPO_CONFIG_DIR']
  16. else:
  17. CONFIG_DIR = os.path.dirname(os.path.abspath(__file__)) + '/etc'
  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 endpoint is set to be the same as the production endpoint.
  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()