config_parser.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import logging
  2. import sys
  3. from os import chdir, environ, getcwd, path
  4. import hiyapyco
  5. import yaml
  6. import lakesuperior
  7. logger = logging.getLogger(__name__)
  8. default_config_dir = environ.get(
  9. 'FCREPO_CONFIG_DIR',
  10. path.join(
  11. path.dirname(path.abspath(lakesuperior.__file__)), 'etc.defaults'))
  12. """
  13. Default configuration directory.
  14. This value falls back to the provided ``etc.defaults`` directory if the
  15. ``FCREPO_CONFIG_DIR`` environment variable is not set.
  16. This value can still be overridden by custom applications by passing the
  17. ``config_dir`` value to :func:`parse_config` explicitly.
  18. """
  19. def parse_config(config_dir=None):
  20. """
  21. Parse configuration from a directory.
  22. This is normally called by the standard endpoints (``lsup_admin``, web
  23. server, etc.) or by a Python client by importing
  24. :py:mod:`lakesuperior.env_setup` but an application using a non-default
  25. configuration may specify an alternative configuration directory.
  26. The directory must have the same structure as the one provided in
  27. ``etc.defaults``.
  28. :param config_dir: Location on the filesystem of the configuration
  29. directory. The default is set by the ``FCREPO_CONFIG_DIR`` environment
  30. variable or, if this is not set, the ``etc.defaults`` stock directory.
  31. """
  32. configs = (
  33. 'application',
  34. 'logging',
  35. 'namespaces',
  36. 'flask',
  37. )
  38. if not config_dir:
  39. config_dir = default_config_dir
  40. # This will hold a dict of all configuration values.
  41. _config = {}
  42. print('Reading configuration at {}'.format(config_dir))
  43. for cname in configs:
  44. file = path.join(config_dir, '{}.yml'.format(cname))
  45. with open(file, 'r') as stream:
  46. _config[cname] = yaml.load(stream, yaml.SafeLoader)
  47. if not _config['application']['data_dir']:
  48. _config['application']['data_dir'] = path.join(
  49. lakesuperior.basedir, 'data')
  50. data_dir = _config['application']['data_dir']
  51. _config['application']['store']['ldp_nr']['location'] = path.join(
  52. data_dir, 'ldpnr_store')
  53. _config['application']['store']['ldp_rs']['location'] = path.join(
  54. data_dir, 'ldprs_store')
  55. # If log handler file names are relative, they will be relative to the
  56. # data dir.
  57. oldwd = getcwd()
  58. chdir(data_dir)
  59. for handler in _config['logging']['handlers'].values():
  60. if 'filename' in handler:
  61. handler['filename'] = path.realpath(handler['filename'])
  62. chdir(oldwd)
  63. logger.info('Graph store location: {}'.format(
  64. _config['application']['store']['ldp_rs']['location']))
  65. logger.info('Binary store location: {}'.format(
  66. _config['application']['store']['ldp_nr']['location']))
  67. return _config
  68. # Load default configuration.
  69. config = parse_config()