2012/7/23 Martin Bless <[email protected]>:
> Hello everybody,
>
> maybe there is an obvious solution - but I just don't see it.
>
> At the end of 'conf.py' I want to read and parse an additional
> 'Settings.yml' configuration file and add those settings to what we
> already have in 'conf.py'. If present settings from the YAML file
> should take precedence. So I need a way to programmatically define
> settings like those in the lines of conf.py. For example:
> html_theme = 'sphinx'
>
> So what I need is something like this:
>
> ## conf.py:
> # ...
> # normal settings ...
> # ...
> more = parse_yaml() # return a dictionary
> for k in more.keys():
> # now I'd like to add
> ((k)) = more[k]
> # where ((k)) should be a variable in the conf.py namespace.
>
> Q: How do I do that?
Quick and dirty: "globals().update(read_my_dict_from_yaml())"
Alternativel you can update the settings after Sphinx has parsed "conf.py":
def setup(app):
for key, value in read_my_dict_from_yaml():
setattr(app.config, key, value)
> In other words:
> If in conf.py there is:
> html_theme = 'sphinx'
> k = 'html_theme'
> v = 'basictheme'
>
> Q: How do I write `k` = v?
globals()[k] = v
> In other words:
> Q: How do I access the dictionary in conf.py that dir() queries?
Use "globals()" within "conf.py". "globals()" returns all names on
module level, and the settings in "conf.py" are module-level names.
> I just found that this seems to work:
> html_theme = 'sphinx'
> k = 'html_theme'
> v = 'basictheme'
> L = locals()
> L[k] = v
> print html_theme # -> gives 'basictheme'
>
> Q: Is that the right and best way to do this?
It works accidentally, because on module level "locals()" is
equivalent to "globals()". However, according to the documentation
"locals()" returns a *independent* dictionary. Modifications to this
dictionary are not guaranteed to propagate to the corresponding
namespace. As said above, use "globals()".
--
You received this message because you are subscribed to the Google Groups
"sphinx-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sphinx-dev?hl=en.