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