sphinx_build_tools.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8; mode: python -*-
  2. """Implement some sphinx-build tools.
  3. """
  4. import os
  5. import sys
  6. from sphinx.util.pycompat import execfile_
  7. # ------------------------------------------------------------------------------
  8. def load_sphinx_config(namespace):
  9. # ------------------------------------------------------------------------------
  10. u"""Load an additional configuration file into *namespace*.
  11. The name of the configuration file is taken from the environment
  12. ``SPHINX_CONF``. The external configuration file extends (or overwrites) the
  13. configuration values from the origin ``conf.py``. With this you are able to
  14. maintain *build themes*. To your docs/conf.py add::
  15. from sphinx_build_tools import load_sphinx_config
  16. ...
  17. # Since loadConfig overwrites settings from the global namespace, it has to be
  18. # the last statement in the conf.py file
  19. load_sphinx_config(globals())
  20. """
  21. config_file = os.environ.get("SPHINX_CONF", None)
  22. if (config_file is not None
  23. and os.path.normpath(namespace["__file__"]) != os.path.normpath(config_file) ):
  24. config_file = os.path.abspath(config_file)
  25. if os.path.isfile(config_file):
  26. sys.stdout.write(
  27. "load additional sphinx-config: %s\n"
  28. % config_file)
  29. config = namespace.copy()
  30. config['__file__'] = config_file
  31. execfile_(config_file, config)
  32. del config['__file__']
  33. namespace.update(config)
  34. else:
  35. sys.stderr.write(
  36. "WARNING: additional sphinx-config not found: %s\n"
  37. % config_file)