diff options
Diffstat (limited to 'documentation/sphinx/yocto-vars.py')
-rw-r--r-- | documentation/sphinx/yocto-vars.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/documentation/sphinx/yocto-vars.py b/documentation/sphinx/yocto-vars.py new file mode 100644 index 0000000000..8083d7da19 --- /dev/null +++ b/documentation/sphinx/yocto-vars.py | |||
@@ -0,0 +1,47 @@ | |||
1 | #!/usr/bin/env python | ||
2 | import re | ||
3 | import sys | ||
4 | |||
5 | import sphinx | ||
6 | from sphinx.application import Sphinx | ||
7 | |||
8 | # This extension uses pyyaml, report an explicit | ||
9 | # error message if it's not installed | ||
10 | try: | ||
11 | import yaml | ||
12 | except ImportError: | ||
13 | sys.stderr.write("The Yocto Project Sphinx documentation requires PyYAML.\ | ||
14 | \nPlease make sure to install pyyaml python package.\n") | ||
15 | sys.exit(1) | ||
16 | |||
17 | __version__ = '1.0' | ||
18 | |||
19 | # Variables substitutions. Uses {VAR} subst using variables defined in poky.yaml | ||
20 | # Each .rst file is processed after source-read event (subst_vars_replace runs once per file) | ||
21 | subst_vars = {} | ||
22 | |||
23 | def subst_vars_replace(app: Sphinx, docname, source): | ||
24 | result = source[0] | ||
25 | for k in subst_vars: | ||
26 | result = result.replace("&"+k+";", subst_vars[k]) | ||
27 | source[0] = result | ||
28 | |||
29 | PATTERN = re.compile(r'&(.*?);') | ||
30 | def expand(val, src): | ||
31 | return PATTERN.sub(lambda m: expand(src.get(m.group(1), ''), src), val) | ||
32 | |||
33 | def setup(app: Sphinx): | ||
34 | #FIXME: if poky.yaml changes, files are not reprocessed. | ||
35 | with open("poky.yaml") as file: | ||
36 | subst_vars.update(yaml.load(file, Loader=yaml.FullLoader)) | ||
37 | |||
38 | for k in subst_vars: | ||
39 | subst_vars[k] = expand(subst_vars[k], subst_vars) | ||
40 | |||
41 | app.connect('source-read', subst_vars_replace) | ||
42 | |||
43 | return dict( | ||
44 | version = __version__, | ||
45 | parallel_read_safe = True, | ||
46 | parallel_write_safe = True | ||
47 | ) | ||