Coverage for /builds/svesch/cikit/src/cikit/factory.py: 100%
17 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-09 14:17 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-09 14:17 +0000
1# SPDX-License-Identifier: GPL-3.0-or-later
2"""Factory helpers for building cikit data objects from a CI environment.
4This module bridges the CI environment abstraction with the cikit data API by
5extracting configuration values from the environment and constructing
6ready-to-use cikit data instances and run contexts.
8Author:
9 Sven Schlender
10License:
11 SPDX-License-Identifier: GPL-3.0-or-later
12Date:
13 2026-04-16
14"""
16from cikit.data import CikitData, RunContext, build_cikit_data
17from cikit.project_meta import load_project_file
20def _resolve_cikit_data_project(env) -> str:
21 """Return the effective project key used for cikit data addressing.
23 The project key is built from ``PROJECT_NAME`` and the optional
24 ``tool.cikit.data_scope`` value from ``pyproject.toml``. Without a scope,
25 the logical project name is used unchanged.
27 Args:
28 env: Resolved CI environment.
30 Returns:
31 Effective project key sent to the cikit data service.
32 """
33 project = env["PROJECT_NAME"]
34 project_meta = load_project_file(env["PROJECT_FILE"])
35 scope = project_meta.get("data_scope", "")
37 return f"{scope}/{project}" if scope else project
40def build_cikit_data_from_env(env) -> tuple[str, CikitData]:
41 """Return pipeline id and configured cikit data instance.
43 This helper reads all cikit-data-related configuration values from the
44 resolved CI environment and creates a ready-to-use cikit data instance.
45 It centralizes the configuration logic used by the cikit data macros.
47 Args:
48 env: Resolved CI environment.
50 Returns:
51 Pipeline ID and configured ``CikitData`` instance.
52 """
53 pipeline_id = env.pipeline_id
54 project = _resolve_cikit_data_project(env)
55 cache_dir = env["CIKIT_DATA_DIR"]
56 base_url = env.get("CIKIT_DATA_BASE_URL")
57 token = env.get("CIKIT_DATA_API_TOKEN")
59 return pipeline_id, build_cikit_data(project, cache_dir, base_url, token)
62def build_run_context_from_env(env, run_name: str) -> RunContext:
63 """Return a configured RunContext for a given run name.
65 Args:
66 env: Resolved CI environment.
67 run_name: Logical run name.
69 Returns:
70 Configured run context bound to the given run.
71 """
72 pipeline_id, cikit_data = build_cikit_data_from_env(env)
74 return RunContext(cikit_data, pipeline_id, run_name)