config_parser.py 2.9 KB

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