Add __iter__(), add() and filter() to Config class

This commit is contained in:
Takeshi KOMIYA
2016-11-17 20:24:58 +09:00
parent e6cd347f6c
commit 7bf4f76c3b
5 changed files with 25 additions and 11 deletions

View File

@@ -582,11 +582,11 @@ class Sphinx(object):
# type: (unicode, Any, Union[bool, unicode], Any) -> None
logger.debug('[app] adding config value: %r',
(name, default, rebuild) + ((types,) if types else ())) # type: ignore
if name in self.config.values:
if name in self.config:
raise ExtensionError('Config value %r already present' % name)
if rebuild in (False, True):
rebuild = rebuild and 'env' or ''
self.config.values[name] = (default, rebuild, types)
self.config.add(name, default, rebuild, types)
def add_event(self, name):
# type: (unicode) -> None

View File

@@ -190,9 +190,7 @@ class StandaloneHTMLBuilder(Builder):
def get_outdated_docs(self): # type: ignore
# type: () -> Iterator[unicode]
cfgdict = dict((name, self.config[name])
for (name, desc) in iteritems(self.config.values)
if desc[1] == 'html')
cfgdict = dict((confval.name, confval.value) for confval in self.config.filter('html'))
self.config_hash = get_stable_hash(cfgdict)
self.tags_hash = get_stable_hash(sorted(self.tags)) # type: ignore
old_config_hash = old_tags_hash = ''

View File

@@ -13,6 +13,7 @@ import re
from os import path, getenv
from six import PY2, PY3, iteritems, string_types, binary_type, text_type, integer_types
from typing import Any, NamedTuple
from sphinx.errors import ConfigError
from sphinx.locale import l_
@@ -23,7 +24,7 @@ from sphinx.util.pycompat import execfile_, NoneType
if False:
# For type annotation
from typing import Any, Callable, Tuple # NOQA
from typing import Any, Callable, Generator, Iterator, Tuple # NOQA
from sphinx.util.tags import Tags # NOQA
logger = logging.getLogger(__name__)
@@ -43,6 +44,10 @@ CONFIG_PERMITTED_TYPE_WARNING = "The config value `{name}' has type `{current.__
CONFIG_TYPE_WARNING = "The config value `{name}' has type `{current.__name__}', " \
"defaults to `{default.__name__}'."
ConfigValue = NamedTuple('ConfigValue', [('name', str),
('value', Any),
('rebuild', str)])
class ENUM(object):
"""represents the config value should be a one of candidates.
@@ -307,3 +312,16 @@ class Config(object):
def __contains__(self, name):
# type: (unicode) -> bool
return name in self.values
def __iter__(self):
# type: () -> Iterator[ConfigValue]
for name, value in iteritems(self.values):
yield ConfigValue(name, getattr(self, name), value[1]) # type: ignore
def add(self, name, default, rebuild, types):
# type: (str, Any, str, Any) -> None
self.values[name] = (default, rebuild, types)
def filter(self, rebuild):
# type: (str) -> Iterator[ConfigValue]
return (value for value in self if value.rebuild == rebuild) # type: ignore

View File

@@ -533,10 +533,8 @@ class BuildEnvironment(object):
else:
# check if a config value was changed that affects how
# doctrees are read
for key, descr in iteritems(config.values):
if descr[1] != 'env':
continue
if self.config[key] != config[key]:
for confval in config.filter('env'):
if self.config[confval.name] != confval.value:
msg = '[config changed] '
config_changed = True
break

View File

@@ -57,7 +57,7 @@ class IfConfig(Directive):
def process_ifconfig_nodes(app, doctree, docname):
# type: (Sphinx, nodes.Node, unicode) -> None
ns = dict((k, app.config[k]) for k in app.config.values)
ns = dict((confval.name, confval.value) for confval in app.config)
ns.update(app.config.__dict__.copy())
ns['builder'] = app.builder.name
for node in doctree.traverse(ifconfig):