Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
cli: harden config parsing and logging override handling
Sameer6305b54d885bf2a8
diff --git a/semantica/cli.py b/semantica/cli.pyindex c1a23748..fbc4ace5 100644--- a/semantica/cli.py+++ b/semantica/cli.py@@ -5,10 +5,10 @@ This module provides the command-line interface for the Semantica framework,enabling users to interact with the framework via terminal commands."""-from dataclasses import dataclassimport json+from dataclasses import dataclassfrom pathlib import Path-from typing import TYPE_CHECKING, Callable, Optional, Sequence+from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Sequenceimport yaml@@ -34,6 +34,7 @@ class CLIContext:config_path: Optional[str]config: Configlog_level: str+ log_level_override: Optional[str] = Noneframework: Optional["Semantica"] = None@@ -49,16 +50,13 @@ def _run_with_error_handling(action: Callable[[], None]) -> None:raise click.ClickException(f"Unexpected error: {exc}") from exc-def _build_runtime_config(config_path: Optional[str], log_level: Optional[str]) -> Config:- """Resolve CLI config from file plus global flag overrides."""- config_manager = ConfigManager()-- if config_path:- file_path = Path(config_path)- suffix = file_path.suffix.lower()+def _load_config_data(file_path: Path) -> Dict[str, Any]:+ """Load and validate raw YAML/JSON config data."""+ suffix = file_path.suffix.lower()+ try:if suffix in (".yaml", ".yml"):with file_path.open("r", encoding="utf-8") as handle:- config_data = yaml.safe_load(handle) or {}+ config_data = yaml.safe_load(handle)elif suffix == ".json":with file_path.open("r", encoding="utf-8") as handle:config_data = json.load(handle)@@ -67,6 +65,28 @@ def _build_runtime_config(config_path: Optional[str], log_level: Optional[str])"Unsupported configuration file format: "f"{suffix}. Supported formats: .yaml, .yml, .json")+ except (json.JSONDecodeError, yaml.YAMLError, UnicodeDecodeError) as exc:+ raise click.ClickException(+ f"Failed to parse configuration file '{file_path}': {exc}"+ ) from exc++ if not isinstance(config_data, dict):+ raise click.ClickException(+ "Configuration file must contain a mapping/object at the root."+ )++ return config_data+++def _build_runtime_config(+ config_path: Optional[str],+ log_level: Optional[str],+) -> Config:+ """Resolve CLI config from file plus global flag overrides."""+ config_manager = ConfigManager()++ if config_path:+ config_data = _load_config_data(Path(config_path))else:config_data = {}@@ -101,10 +121,13 @@ def _run_build(cli_ctx: CLIContext, sources: Sequence[str]) -> None:processed = stats.get("sources_processed")if processed is not None:console.print(- f"[bold green]Success:[/bold green] Knowledge base build completed for {processed} source(s)."+ "[bold green]Success:[/bold green] Knowledge base build completed "+ f"for {processed} source(s).")else:- console.print("[bold green]Success:[/bold green] Knowledge base build completed.")+ console.print(+ "[bold green]Success:[/bold green] Knowledge base build completed."+ )def _run_build_command(@@ -116,7 +139,9 @@ def _run_build_command(if command_config_path:command_ctx = CLIContext(config_path=command_config_path,- config=_build_runtime_config(command_config_path, cli_ctx.log_level),+ config=_build_runtime_config(+ command_config_path, cli_ctx.log_level_override+ ),log_level=cli_ctx.log_level,)_run_build(command_ctx, source)@@ -135,7 +160,10 @@ def _run_build_command()@click.option("--log-level",- type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], case_sensitive=False),+ type=click.Choice(+ ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],+ case_sensitive=False,+ ),default=None,help="Override logging level for this CLI invocation.",)@@ -145,10 +173,12 @@ def main(ctx: click.Context, config_path: Optional[str], log_level: Optional[strtry:config = _build_runtime_config(config_path=config_path, log_level=log_level)setup_logging(config=config.get("logging", {}))+ effective_log_level = config.get("logging.level", "INFO")ctx.obj = CLIContext(config_path=config_path,config=config,- log_level=config.get("logging.level", "INFO"),+ log_level=effective_log_level,+ log_level_override=log_level.upper() if log_level else None,)except click.ClickException:raise@@ -194,10 +224,12 @@ def config_group(ctx: click.Context) -> None:@click.pass_objdef info(cli_ctx: CLIContext):"""Display information about Semantica."""+def _action() -> None:console.print(f"[bold blue]Semantica Framework[/bold blue] v{__version__}")console.print(- "A comprehensive Python framework for transforming unstructured data into semantic layers."+ "A comprehensive Python framework for transforming unstructured data "+ "into semantic layers.")table = Table(title="Framework Components")@@ -218,10 +250,22 @@ def info(cli_ctx: CLIContext):@kg.command("build")@click.option("--source", "-s", multiple=True, help="Data sources to process.")[email protected]("-c", "--config", "command_config_path", type=click.Path(exists=True, dir_okay=False, resolve_path=True, path_type=str), default=None, help="Path to YAML/JSON config file.")+ "-c",+ "--config",+ "command_config_path",+ type=click.Path(exists=True, dir_okay=False, resolve_path=True, path_type=str),+ default=None,+ help="Path to YAML/JSON config file.",+)@click.pass_obj-def kg_build(cli_ctx: CLIContext, source: Sequence[str], command_config_path: Optional[str]):+def kg_build(+ cli_ctx: CLIContext,+ source: Sequence[str],+ command_config_path: Optional[str],+):"""Build a knowledge base from sources."""+def _action() -> None:_run_build_command(cli_ctx, source, command_config_path)@@ -230,10 +274,22 @@ def kg_build(cli_ctx: CLIContext, source: Sequence[str], command_config_path: Op@main.command("build", hidden=True)@click.option("--source", "-s", multiple=True, help="Data sources to process.")[email protected]("-c", "--config", "command_config_path", type=click.Path(exists=True, dir_okay=False, resolve_path=True, path_type=str), default=None, help="Path to YAML/JSON config file.")+ "-c",+ "--config",+ "command_config_path",+ type=click.Path(exists=True, dir_okay=False, resolve_path=True, path_type=str),+ default=None,+ help="Path to YAML/JSON config file.",+)@click.pass_obj-def build_alias(cli_ctx: CLIContext, source: Sequence[str], command_config_path: Optional[str]):+def build_alias(+ cli_ctx: CLIContext,+ source: Sequence[str],+ command_config_path: Optional[str],+):"""Backward-compatible alias for 'kg build'."""+def _action() -> None:_run_build_command(cli_ctx, source, command_config_path)diff --git a/tests/test_cli_foundation.py b/tests/test_cli_foundation.pyindex 181d9dbc..3401b86b 100644--- a/tests/test_cli_foundation.py+++ b/tests/test_cli_foundation.py@@ -13,7 +13,10 @@ def test_root_help_shows_expected_groups(runner):result = runner.invoke(cli_module.main, ["--help"])assert result.exit_code == 0- assert "Semantica - Semantic Layer & Knowledge Engineering Framework" in result.output+ assert (+ "Semantica - Semantic Layer & Knowledge Engineering Framework"+ in result.output+ )assert "kg" in result.outputassert "pipeline" in result.outputassert "serve" in result.output@@ -37,6 +40,29 @@ def test_kg_build_help_shows_source_and_config_flags(runner):assert "-c" in result.output+def test_command_config_keeps_own_logging_without_global_override(runner, monkeypatch):+ captured = {}++ def fake_run_build(cli_ctx, sources):+ captured["logging_level"] = cli_ctx.config.get("logging.level")+ captured["sources"] = list(sources)++ monkeypatch.setattr(cli_module, "_run_build", fake_run_build)++ with runner.isolated_filesystem():+ with open("cfg.yml", "w", encoding="utf-8") as handle:+ handle.write("logging:\n level: DEBUG\n")++ result = runner.invoke(+ cli_module.main,+ ["kg", "build", "-s", "README.md", "-c", "cfg.yml"],+ )++ assert result.exit_code == 0+ assert captured["sources"] == ["README.md"]+ assert captured["logging_level"] == "DEBUG"++@pytest.mark.parametrize("argv",[@@ -130,6 +156,37 @@ def test_invalid_command_config_error_is_clean_and_click_safe(runner):assert "Traceback" not in result.output+ "file_name, config_text, expected_error",+ [+ ("cfg.json", "{not-json", "Failed to parse configuration file"),+ (+ "cfg.yml",+ "- item\n- item2\n",+ "Configuration file must contain a mapping/object",+ ),+ ],+)+def test_command_config_parse_errors_are_clean_and_click_safe(+ runner,+ file_name,+ config_text,+ expected_error,+):+ with runner.isolated_filesystem():+ with open(file_name, "w", encoding="utf-8") as handle:+ handle.write(config_text)++ result = runner.invoke(+ cli_module.main,+ ["kg", "build", "-s", "README.md", "-c", file_name],+ )++ assert result.exit_code != 0+ assert expected_error in result.output+ assert "Traceback" not in result.output++@pytest.mark.parametrize("argv",[