Skip to content

Commit

Permalink
🔧 Merge defaults into config earlier (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell authored Oct 28, 2024
1 parent 8aa192b commit 94b0273
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions sphinx_needs/needs.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ def setup(app: Sphinx) -> dict[str, Any]:
# Make connections to events
app.connect("config-inited", load_config_from_toml, priority=10) # runs early
app.connect("config-inited", load_config)
app.connect("config-inited", check_configuration)
app.connect("config-inited", merge_default_configs)
app.connect("config-inited", check_configuration, priority=600) # runs late

app.connect("env-before-read-docs", prepare_env)
app.connect("env-before-read-docs", load_external_needs)
Expand Down Expand Up @@ -589,6 +590,31 @@ def prepare_env(app: Sphinx, env: BuildEnvironment, _docnames: list[str]) -> Non
# Otherwise, the service may get registered later by an external sphinx-needs extension
services.register(name, service["class"], **service["class_init"])

# Set time measurement flag
if needs_config.debug_measurement:
debug.START_TIME = timer() # Store the rough start time of Sphinx build
debug.EXECUTE_TIME_MEASUREMENTS = True

if needs_config.debug_filters:
with contextlib.suppress(FileNotFoundError):
Path(str(app.outdir), "debug_filters.jsonl").unlink()


def merge_default_configs(_app: Sphinx, config: Config) -> None:
"""Merge built-in defaults with user configuration."""
needs_config = NeedsSphinxConfig(config)

needs_config.layouts = {**LAYOUTS, **needs_config.layouts}

needs_config.flow_configs = {
**NEEDFLOW_CONFIG_DEFAULTS,
**needs_config.flow_configs,
}
needs_config.graphviz_styles = {
**GRAPHVIZ_STYLE_DEFAULTS,
**needs_config.graphviz_styles,
}

# Register built-in functions
for need_common_func in NEEDS_COMMON_FUNCTIONS:
_NEEDS_CONFIG.add_function(need_common_func)
Expand Down Expand Up @@ -632,28 +658,9 @@ def prepare_env(app: Sphinx, env: BuildEnvironment, _docnames: list[str]) -> Non
)

needs_config.extra_links = common_links + needs_config.extra_links
needs_config.layouts = {**LAYOUTS, **needs_config.layouts}

needs_config.flow_configs = {
**NEEDFLOW_CONFIG_DEFAULTS,
**needs_config.flow_configs,
}
needs_config.graphviz_styles = {
**GRAPHVIZ_STYLE_DEFAULTS,
**needs_config.graphviz_styles,
}

# Set time measurement flag
if needs_config.debug_measurement:
debug.START_TIME = timer() # Store the rough start time of Sphinx build
debug.EXECUTE_TIME_MEASUREMENTS = True

if needs_config.debug_filters:
with contextlib.suppress(FileNotFoundError):
Path(str(app.outdir), "debug_filters.jsonl").unlink()


def check_configuration(_app: Sphinx, config: Config) -> None:
def check_configuration(app: Sphinx, config: Config) -> None:
"""Checks the configuration for invalid options.
E.g. defined need-option, which is already defined internally
Expand Down

0 comments on commit 94b0273

Please sign in to comment.