load_config.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8; mode: python -*-
  2. # pylint: disable=R0903, C0330, R0914, R0912, E0401
  3. import os
  4. import sys
  5. from sphinx.util.osutil import fs_encoding
  6. # ------------------------------------------------------------------------------
  7. def loadConfig(namespace):
  8. # ------------------------------------------------------------------------------
  9. u"""Load an additional configuration file into *namespace*.
  10. The name of the configuration file is taken from the environment
  11. ``SPHINX_CONF``. The external configuration file extends (or overwrites) the
  12. configuration values from the origin ``conf.py``. With this you are able to
  13. maintain *build themes*. """
  14. config_file = os.environ.get("SPHINX_CONF", None)
  15. if (config_file is not None
  16. and os.path.normpath(namespace["__file__"]) != os.path.normpath(config_file) ):
  17. config_file = os.path.abspath(config_file)
  18. # Let's avoid one conf.py file just due to latex_documents
  19. start = config_file.find('Documentation/')
  20. if start >= 0:
  21. start = config_file.find('/', start + 1)
  22. end = config_file.rfind('/')
  23. if start >= 0 and end > 0:
  24. dir = config_file[start + 1:end]
  25. print("source directory: %s" % dir)
  26. new_latex_docs = []
  27. latex_documents = namespace['latex_documents']
  28. for l in latex_documents:
  29. if l[0].find(dir + '/') == 0:
  30. has = True
  31. fn = l[0][len(dir) + 1:]
  32. new_latex_docs.append((fn, l[1], l[2], l[3], l[4]))
  33. break
  34. namespace['latex_documents'] = new_latex_docs
  35. # If there is an extra conf.py file, load it
  36. if os.path.isfile(config_file):
  37. sys.stdout.write("load additional sphinx-config: %s\n" % config_file)
  38. config = namespace.copy()
  39. config['__file__'] = config_file
  40. with open(config_file, 'rb') as f:
  41. code = compile(f.read(), fs_encoding, 'exec')
  42. exec(code, config)
  43. del config['__file__']
  44. namespace.update(config)
  45. else:
  46. config = namespace.copy()
  47. config['tags'].add("subproject")
  48. namespace.update(config)