config_parser.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import sys
  2. from os import path, environ
  3. import hiyapyco
  4. import yaml
  5. import lakesuperior
  6. default_config_dir = environ.get('FCREPO_CONFIG_DIR', path.dirname(
  7. path.abspath(lakesuperior.__file__)) + '/etc.defaults')
  8. """
  9. Default configuration directory.
  10. This value falls back to the provided ``etc.defaults`` directory if the
  11. ``FCREPO_CONFIG_DIR`` environment variable is not set.
  12. This value can still be overridden by custom applications by passing the
  13. ``config_dir`` value to :func:`parse_config` explicitly.
  14. """
  15. def parse_config(config_dir=None):
  16. """
  17. Parse configuration from a directory.
  18. This is normally called by the standard endpoints (``lsup_admin``, web
  19. server, etc.) or by a Python client by importing
  20. :py:mod:`lakesuperior.env_setup` but an application using a non-default
  21. configuration may specify an alternative configuration directory.
  22. The directory must have the same structure as the one provided in
  23. ``etc.defaults``.
  24. :param config_dir: Location on the filesystem of the configuration
  25. directory. The default is set by the ``FCREPO_CONFIG_DIR`` environment
  26. variable or, if this is not set, the ``etc.defaults`` stock directory.
  27. """
  28. configs = (
  29. 'application',
  30. 'logging',
  31. 'namespaces',
  32. 'flask',
  33. )
  34. if not config_dir:
  35. config_dir = default_config_dir
  36. # This will hold a dict of all configuration values.
  37. _config = {}
  38. print('Reading configuration at {}'.format(config_dir))
  39. for cname in configs:
  40. file = '{}/{}.yml'.format(config_dir , cname)
  41. with open(file, 'r') as stream:
  42. _config[cname] = yaml.load(stream, yaml.SafeLoader)
  43. error_msg = '''
  44. **************
  45. ** WARNING! **
  46. **************
  47. Your test {} store location is set to be the same as the production
  48. location. This means that if you run a test suite, your live data may be
  49. wiped clean!
  50. Please review your configuration before starting.
  51. '''
  52. # Merge default and test configurations.
  53. _test_config = {'application': hiyapyco.load(
  54. config_dir + '/application.yml',
  55. config_dir + '/test.yml', method=hiyapyco.METHOD_MERGE)}
  56. if _config['application']['store']['ldp_rs']['location'] \
  57. == _test_config['application']['store']['ldp_rs']['location']:
  58. raise RuntimeError(error_msg.format('RDF'))
  59. sys.exit()
  60. if _config['application']['store']['ldp_nr']['path'] \
  61. == _test_config['application']['store']['ldp_nr']['path']:
  62. raise RuntimeError(error_msg.format('binary'))
  63. sys.exit()
  64. return _config, _test_config
  65. # Load default configuration.
  66. config, test_config = parse_config()