Re: [sphinx-dev] How to override values in conf.py by values read from a Settings.yml (YAML) file?

2012-07-25 Thread Sebastian Wiesner
2012/7/23 Martin Bless m.bl...@gmx.de:
 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 sphinx-dev@googlegroups.com.
To unsubscribe from this group, send email to 
sphinx-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sphinx-dev?hl=en.



Re: [sphinx-dev] How to override values in conf.py by values read from a Settings.yml (YAML) file?

2012-07-24 Thread Takayuki Shimizukawa
Hi Martin,

2012/7/23 Martin Bless m.bl...@gmx.de:
 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?

conf.py is loaded into the Sphinx by execfile() function [1].
Therefore, there is no dictionary object of conf.py module.

[1] Sphinx-1.1 series is using another code equivalent to the execfile().

I made a minimum working sample.

   ## conf.py:
   # ...
   # normal settings ...
   # ...
   version = release = '1.0'

   more = {'release': 'foo', 'version': 'bar'}  # parse_yaml() #
return a dictionary
   for k in more:
   exec(%s = %r % (k, more[k]))


index.rst::

   Welcome to conf.py test
   =

   :release: |release|
   :version: |version|


and generated index.txt by `make text`::

   Welcome to conf.py test
   ***

   release:
  foo

   version:
  bar


 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?

Hmm.., there is no guarantee that code will always work.

   http://docs.python.org/library/functions.html#locals
   The contents of this dictionary should not be modified; changes may
   not affect the values of local and free variables used by the interpreter.

Best regards.
-- 
Takayuki SHIMIZUKAWA
Sphinx-users.jp

-- 
You received this message because you are subscribed to the Google Groups 
sphinx-dev group.
To post to this group, send email to sphinx-dev@googlegroups.com.
To unsubscribe from this group, send email to 
sphinx-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sphinx-dev?hl=en.



[sphinx-dev] How to override values in conf.py by values read from a Settings.yml (YAML) file?

2012-07-23 Thread Martin Bless
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?


In other words: 
If in conf.py there is:
   html_theme = 'sphinx'
   k = 'html_theme'
   v = 'basictheme'

Q: How do I write `k` = v?


In other words:
Q: How do I access the dictionary in conf.py that dir() queries?


This does not work:
   _dict_[k] = v

This does not work 
because it turns out that '__name__' has the value '__builtins__':
   sys.modules[__name__].__dict__[k] = v


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?


... all the best ...

Martin

-- 
http://mbless.de




-- 
You received this message because you are subscribed to the Google Groups 
sphinx-dev group.
To post to this group, send email to sphinx-dev@googlegroups.com.
To unsubscribe from this group, send email to 
sphinx-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sphinx-dev?hl=en.