mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add __iter__(), add() and filter() to Config class
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 = ''
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user