API Reference¶
Auto-generated code documentation.
civic_lib_core ¶
Civic Interconnect shared utilities package.
cli ¶
__main__ ¶
Entry point for Civic Dev CLI.
File: cli.main
build_api ¶
Generate and update application interface documentation using pdoc.
This script: - Locates the project root - Discovers local Python packages to document - Generates standalone HTML API documentation - Writes HTML files into the docs/api/ folder (or configured docs_api_dir)
File cli/build_api.py
main ¶
main() -> int
Generate standalone HTML API documentation using pdoc.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
exit code (0 if successful, nonzero otherwise) |
Source code in src/civic_lib_core/cli/build_api.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
bump_version ¶
Command-line tool to update version strings across key project files.
This tool replaces the old version with the new version in: - VERSION - pyproject.toml - README.md
Usage
python -m cli.bump_version OLD_VERSION NEW_VERSION
or as a subcommand: civic-dev bump-version OLD_VERSION NEW_VERSION
or shorthand: civic-dev bump OLD_VERSION NEW_VERSION
File: bump_version.py Module: cli.bump_version
bump_version_cmd ¶
bump_version_cmd(old_version: str, new_version: str) -> int
CLI subcommand handler for version bump.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Exit code (0 on success, 1 if no updates). |
Source code in src/civic_lib_core/cli/bump_version.py
62 63 64 65 66 67 68 69 70 71 72 73 | |
main ¶
main(old_version: str, new_version: str) -> int
Script-style entry point.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Exit code. |
Source code in src/civic_lib_core/cli/bump_version.py
76 77 78 79 80 81 82 | |
update_file ¶
update_file(path: Path, old: str, new: str) -> bool
Replace version string in the specified file if found.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if file was modified, False otherwise. |
Source code in src/civic_lib_core/cli/bump_version.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
check_policy ¶
CLI utility to check Civic Interconnect project policy compliance.
main ¶
main() -> int
Check current repo against Civic Interconnect policy.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
exit code (0 = OK, nonzero = errors) |
Source code in src/civic_lib_core/cli/check_policy.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
cli ¶
Developer command-line interface (CLI) for Civic Interconnect projects.
Provides cross-repo automation commands for: - Installing and verifying the local development environment - Formatting, linting, and testing the codebase - Bumping version numbers for release - Tagging and pushing release commits
Run civic-dev --help for usage across all Civic Interconnect repos.
File: cli.py
build_api_command ¶
build_api_command()
Build the docs.
Source code in src/civic_lib_core/cli/cli.py
21 22 23 24 | |
bump_version_command ¶
bump_version_command(old_version: str, new_version: str)
Update version strings across the project.
Source code in src/civic_lib_core/cli/cli.py
27 28 29 30 | |
check_policy_command ¶
check_policy_command()
Check policies.
Source code in src/civic_lib_core/cli/cli.py
33 34 35 36 | |
layout_command ¶
layout_command()
Show the current project layout.
Source code in src/civic_lib_core/cli/cli.py
39 40 41 42 | |
main ¶
main()
Entry point for the CLI application.
This function serves as the main entry point that initializes and runs the CLI application using the app() function.
Source code in src/civic_lib_core/cli/cli.py
57 58 59 60 61 62 63 | |
prepare_code ¶
prepare_code()
Format, lint, and test the codebase.
Source code in src/civic_lib_core/cli/cli.py
45 46 47 48 | |
release_command ¶
release_command()
Tag and push the current version to GitHub.
Source code in src/civic_lib_core/cli/cli.py
51 52 53 54 | |
layout ¶
CLI utility to discover and print the Civic Interconnect project layout.
File: layout.py
main ¶
main() -> None
Discover and print the project layout.
Prints a formatted summary of: - Project root - Docs directories - Source packages - Organization name - Policy file used
Source code in src/civic_lib_core/cli/layout.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
prep_code ¶
Prepare Civic Interconnect code for release or commit.
This script: - Checks whether the virtual environment may be stale (based on dependency file timestamps) - Formats code using Ruff - Lints and fixes issues with Ruff - Runs pre-commit hooks twice (first to fix, then to verify) - Executes unit tests via pytest
If dependency files changed since .venv was created, the script warns the user to rerun their setup script (e.g. setup.ps1) to reinstall the environment.
File: prep_code.py
main ¶
main() -> int
Prepare code using a comprehensive workflow.
This function performs a comprehensive code preparation workflow including: - Virtual environment validation and dependency checking - Code formatting using Ruff - Linting and automatic fixing of issues - Pre-commit hook execution and validation - Unit test execution
The function checks if the virtual environment needs to be reinstalled by comparing dependency file timestamps, then runs a series of code quality tools in sequence. If any step fails, the process is terminated early.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Exit code (0 for success, non-zero for failure). Returns 1 if virtual environment needs reinstalling, or the return code of any failed subprocess. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
When any of the code preparation steps fail during execution. |
Source code in src/civic_lib_core/cli/prep_code.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
run_check ¶
run_check(command: list[str], label: str) -> None
Run a shell command and fail fast if it errors.
Source code in src/civic_lib_core/cli/prep_code.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
should_reinstall ¶
should_reinstall() -> bool
Determine whether the virtual environment should be reinstalled.
Based on timestamps of dependency files.
Source code in src/civic_lib_core/cli/prep_code.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
release ¶
Automate the release process for Civic Interconnect applications.
This script: - Reads the version from the VERSION file - Updates pre-commit hooks - Installs the package in editable mode - Formats and lints the code - Generates up-to-date API documentation - Runs pre-commit hooks twice (fix + verify) - Runs unit tests if present - Commits changes if any are staged - Creates a new Git tag for the release - Pushes the commit and tag to the remote repository
Update the VERSION file before running this script.
File: release.py
Example
civic-dev bump-version 1.0.3 1.0.4
main ¶
main() -> int
Complete the release workflow for the current version.
Source code in src/civic_lib_core/cli/release.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
run ¶
run(cmd: str, check: bool = True) -> None
Run a shell command and log it.
Source code in src/civic_lib_core/cli/release.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
config_utils ¶
civic_lib_core/config_utils.py.
Utilities for managing configuration and environment data in Civic Interconnect projects.
Provides: - Loading environment-based API keys - Reading YAML configuration files - Reading project version information - Parsing version strings into numeric tuples
Typical usage:
from civic_lib_core import config_utils
api_key = config_utils.load_api_key("MY_API_KEY", "MyService")
config = config_utils.load_yaml_config()
version = config_utils.load_version()
major, minor, patch = config_utils.parse_version("1.2.3")
load_api_key ¶
load_api_key(env_var: str, service_name: str) -> str
Load an API key from the environment variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_var
|
str
|
Name of the environment variable. |
required |
service_name
|
str
|
Friendly name for error messaging. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The API key value. |
Exits
If the environment variable is missing or empty.
Source code in src/civic_lib_core/config_utils.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
load_version ¶
load_version(
filename: str = 'VERSION', root_dir: Path | None = None
) -> str
Load the version string from a VERSION file in the project.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the version file. |
'VERSION'
|
root_dir
|
Optional[Path]
|
Base directory to search from. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Version string like "1.2.3". |
Exits
If the file is missing or unreadable.
Source code in src/civic_lib_core/config_utils.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
load_yaml_config ¶
load_yaml_config(
filename: str = 'config.yaml',
root_dir: Path | None = None,
) -> dict[str, Any]
Load a YAML configuration file from the project root.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the YAML file. |
'config.yaml'
|
root_dir
|
Optional[Path]
|
Base directory to search from. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Parsed configuration dictionary. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If config file is missing. |
Source code in src/civic_lib_core/config_utils.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
parse_version ¶
parse_version(version: str) -> tuple[int, int, int]
Parse a version string (e.g. "1.2.3") into a tuple of integers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str
|
Version string. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
tuple[int, int, int]: Tuple of (major, minor, patch). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the version format is invalid. |
Source code in src/civic_lib_core/config_utils.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
date_utils ¶
civic_lib_core/date_utils.py.
Date and time utilities for Civic Interconnect projects.
Provides helpers to: - Generate date ranges for reports - Retrieve current UTC time or date - Format UTC datetimes into strings
Typical usage:
from civic_lib_core import date_utils
# Get today's UTC date string
today = date_utils.today_utc_str()
# Get current UTC datetime as a string
timestamp = date_utils.now_utc_str()
# Generate list of dates for the past 7 days
dates = date_utils.date_range(7)
date_range ¶
date_range(days_back: int) -> list[str]
Generate a list of date strings from days_back days ago up to today (UTC).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
days_back
|
int
|
Number of days to include, ending with today (inclusive). For example, days_back=7 returns 7 dates. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: List of UTC dates in 'YYYY-MM-DD' format, earliest to latest. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If days_back is negative. |
Source code in src/civic_lib_core/date_utils.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
now_utc ¶
now_utc() -> datetime
Return the current UTC datetime object.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
Current UTC datetime. |
Source code in src/civic_lib_core/date_utils.py
58 59 60 61 62 63 64 | |
now_utc_str ¶
now_utc_str(fmt: str = '%Y-%m-%d %H:%M:%S UTC') -> str
Return the current time in UTC as a formatted string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
str
|
Format string for datetime output. Default includes 'UTC'. |
'%Y-%m-%d %H:%M:%S UTC'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted current UTC time. |
Source code in src/civic_lib_core/date_utils.py
67 68 69 70 71 72 73 74 75 76 | |
today_utc_str ¶
today_utc_str() -> str
Return today's date in UTC in 'YYYY-MM-DD' format.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Current UTC date as a string. |
Source code in src/civic_lib_core/date_utils.py
79 80 81 82 83 84 85 | |
dev_utils ¶
civic_lib_core/dev_utils.py.
Core development utilities. Part of the Civic Interconnect agent framework.
log_suggested_paths ¶
log_suggested_paths(
response: Any,
max_depth: int = 3,
source_label: str = 'response',
) -> None
Log inferred paths to nested keys in a response object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Any
|
Parsed API response. |
required |
max_depth
|
int
|
Maximum depth to explore. |
3
|
source_label
|
str
|
Label for context in logs. |
'response'
|
Source code in src/civic_lib_core/dev_utils.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
suggest_paths ¶
suggest_paths(
response: Any,
max_depth: int = 3,
current_path: list[str] | None = None,
) -> list[tuple[list[str], str, str]]
Suggest possible nested data paths in a response object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Any
|
Parsed API response. |
required |
max_depth
|
int
|
Maximum traversal depth. |
3
|
current_path
|
list[str] | None
|
Used internally for recursion. |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[list[str], str, str]]
|
list of (path, key, summary): Potential paths to explore. |
Source code in src/civic_lib_core/dev_utils.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
fs_utils ¶
civic_lib_core/fs_utils.py.
File and path utility functions for root-relative logic. Unified utilities used across Civic Interconnect agents and libraries.
discover_project_layout ¶
discover_project_layout() -> ProjectLayout
Discover and analyze the project layout structure.
Scans the project directory to identify key components including the project root, documentation directories, source code location, valid packages, organization name, and project policy configuration.
Returns:
| Name | Type | Description |
|---|---|---|
ProjectLayout |
ProjectLayout
|
A comprehensive object containing all discovered project structure information including: - project_root: The root directory of the project - docs_dir: Main documentation directory - docs_api_dir: API documentation directory - src_dir: Source code directory - packages: List of valid Python packages found - org_name: Organization name associated with the project - policy: Loaded project policy configuration |
Note
This function performs automatic discovery and may return empty collections or None values for components that are not found or configured in the project.
Source code in src/civic_lib_core/fs_utils.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
ensure_dir ¶
ensure_dir(path: str | Path) -> Path
Ensure a directory exists, creating it and any parent directories if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The directory path to ensure exists. Can be a string or Path object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The resolved Path object of the created/existing directory. |
Raises:
| Type | Description |
|---|---|
OSError
|
If the directory cannot be created due to permissions or other filesystem issues. |
Example
ensure_dir("/path/to/new/directory") PosixPath('/path/to/new/directory')
ensure_dir(Path("relative/path")) PosixPath('/absolute/path/to/relative/path')
Source code in src/civic_lib_core/fs_utils.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
get_data_config_dir ¶
get_data_config_dir(
project_root: Path | None = None,
) -> Path
Get the data configuration directory path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path | None
|
The project root directory. If None, uses the discovered project root. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The path to the data-config directory. |
Source code in src/civic_lib_core/fs_utils.py
121 122 123 124 125 126 127 128 129 130 131 132 | |
get_docs_api_dir ¶
get_docs_api_dir(
root_dir: Path | None = None, create: bool = False
) -> Path
Determine the project's API docs subdirectory.
Tries: 1. Client repo policy (docs.api_markdown_subdir) 2. Defaults to 'docs/api'
Source code in src/civic_lib_core/fs_utils.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
get_docs_dir ¶
get_docs_dir(root_dir: Path | None = None) -> Path
Determine the project's main docs directory.
Tries: 1. Client repo policy (docs.site_dir or docs.docs_dir) 2. Defaults to 'docs'
Source code in src/civic_lib_core/fs_utils.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
get_org_name ¶
get_org_name(project_root: Path) -> str | None
Get the organization name from the project root's parent directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path
|
The root directory path of the project. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The name of the parent directory if it exists, otherwise None. |
Source code in src/civic_lib_core/fs_utils.py
177 178 179 180 181 182 183 184 185 186 | |
get_project_root ¶
get_project_root(start_path: Path | None = None) -> Path
Find the project root directory by searching for common project markers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_path
|
Path | None
|
The starting directory for the search. If None, uses the current working directory. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The resolved path to the project root directory. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no project root is found by searching upward from the start path. |
Source code in src/civic_lib_core/fs_utils.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
get_repo_package_names ¶
get_repo_package_names(
root_path: Path | None = None,
) -> list[str]
Discover all Python package names under the repo's src directory.
Returns:
| Type | Description |
|---|---|
list[str]
|
List[str]: Fully qualified package names, e.g. ['civic_lib_core', 'civic_lib_core.cli'] |
Source code in src/civic_lib_core/fs_utils.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
get_runtime_config_path ¶
get_runtime_config_path(
project_root: Path | None = None,
) -> Path
Get the runtime configuration file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path | None
|
The project root directory. If None, uses the discovered project root. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The path to the runtime_config.yaml file. |
Source code in src/civic_lib_core/fs_utils.py
248 249 250 251 252 253 254 255 256 257 258 259 | |
get_source_dir ¶
get_source_dir(root_dir: Path) -> Path | None
Get the source directory containing Python packages for the project.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_dir
|
Path
|
The root directory path of the project. |
required |
Returns:
| Type | Description |
|---|---|
Path | None
|
Path | None: The path to the source directory if found and contains valid Python packages, otherwise None. |
Source code in src/civic_lib_core/fs_utils.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
get_valid_packages ¶
get_valid_packages(src_dir: Path) -> list[Path]
Get all valid Python packages found in the source directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_dir
|
Path
|
The source directory to search for Python packages. |
required |
Returns:
| Type | Description |
|---|---|
list[Path]
|
list[Path]: A list of Path objects representing valid Python packages. Returns an empty list if no packages are found or if the source directory doesn't exist. |
Source code in src/civic_lib_core/fs_utils.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
resolve_path ¶
resolve_path(relative_path: str | Path) -> Path
Resolve a relative path against the project root directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relative_path
|
str | Path
|
The relative path to resolve. Can be a string or Path object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The resolved absolute path relative to the project root. |
Example
resolve_path("src/package") PosixPath('/absolute/path/to/project/src/package')
Source code in src/civic_lib_core/fs_utils.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
safe_filename ¶
safe_filename(name: str, max_length: int = 255) -> str
Create a safe filename by sanitizing input string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The original filename or string to sanitize. |
required |
max_length
|
int
|
Maximum length of the resulting filename. Defaults to 255. |
255
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A sanitized filename safe for filesystem use, containing only alphanumeric characters, dots, underscores, and hyphens. Spaces and path separators are converted to underscores. |
Example
safe_filename("My File/Name:Test") 'my_file_name_test'
safe_filename("", 10) 'unnamed'
Source code in src/civic_lib_core/fs_utils.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
graphql_utils ¶
civic_lib_core/graphql_utils.py.
Unified GraphQL utilities for Civic Interconnect projects.
Provides: - Consistent error handling for GraphQL transport errors - Asynchronous and synchronous helpers for paginated GraphQL queries - Utilities to fetch all pages of results from GraphQL APIs
async_paged_query
async
¶
async_paged_query(
url: str,
api_key: str,
query: Any,
data_path: Sequence[str],
page_info_path: Sequence[str] | None = None,
) -> list[dict[str, Any]]
Execute a paginated GraphQL query asynchronously and fetch all results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The GraphQL endpoint URL. |
required |
api_key
|
str
|
Bearer token for API authentication. |
required |
query
|
Any
|
The GraphQL query object to execute. |
required |
data_path
|
Sequence[str]
|
Path to the data array in the response. |
required |
page_info_path
|
Sequence[str] | None
|
Path to pageInfo in the response. If None, will attempt to infer from data_path. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
list[dict[str, Any]]: List of all records collected from all pages. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If page_info_path is None and pageInfo cannot be inferred. |
Source code in src/civic_lib_core/graphql_utils.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
fetch_paginated
async
¶
fetch_paginated(
client: Any,
query: Any,
data_key: str,
variables: Mapping[str, Any] | None = None,
) -> list[dict[str, Any]]
Fetch all paginated results from a GraphQL query using cursor-based pagination.
This function automatically handles pagination by making multiple requests to fetch all available data, using the standard GraphQL cursor-based pagination pattern with 'first', 'after', 'edges', 'pageInfo', 'hasNextPage', and 'endCursor' fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Any
|
The GraphQL client instance that supports async execution. |
required |
query
|
Any
|
The GraphQL query object to execute. |
required |
data_key
|
str
|
The key in the response data that contains the paginated results. |
required |
variables
|
Mapping[str, Any] | None
|
Additional variables to pass with the query. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
list[dict[str, Any]]: A list containing all the 'node' objects from all pages of results combined. |
Example
query = gql("query { users(first: $first, after: $after) { ... } }")
users = await fetch_paginated(client, query, "users", {"status": "active"})
Note
- Uses a fixed page size of 100 items per request
- Automatically extracts 'node' objects from GraphQL 'edges'
- Logs the total number of records fetched upon completion
Source code in src/civic_lib_core/graphql_utils.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
handle_transport_errors ¶
handle_transport_errors(
e: Exception, resource_name: str = 'resource'
) -> str
Handle GraphQL transport errors with appropriate logging and re-raising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e
|
Exception
|
The exception to handle, typically a GraphQL transport error. |
required |
resource_name
|
str
|
Name of the resource being accessed for logging context. Defaults to "resource". |
'resource'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Error message for 403 Forbidden errors, indicating access not granted. |
Raises:
| Type | Description |
|---|---|
Exception
|
Re-raises the original exception after logging the appropriate error message based on the exception type. |
Source code in src/civic_lib_core/graphql_utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
paged_query ¶
paged_query(
url: str,
api_key: str,
query: Any,
data_path: Sequence[str],
) -> list[dict[str, Any]]
Execute a paginated GraphQL query synchronously and fetch all results.
This is a synchronous wrapper around async_paged_query that uses asyncio.run to execute the asynchronous query in a blocking manner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The GraphQL endpoint URL. |
required |
api_key
|
str
|
Bearer token for API authentication. |
required |
query
|
Any
|
The GraphQL query object to execute. |
required |
data_path
|
Sequence[str]
|
Path to the data array in the response. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
list[dict[str, Any]]: List of all records collected from all pages, or empty list if an error occurs. |
Source code in src/civic_lib_core/graphql_utils.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
log_utils ¶
civic_lib_core/log_utils.py.
Centralized logging for Civic Interconnect agents and libraries.
init_logger ¶
init_logger(
log_level: str | None = None,
log_to_console: bool = True,
) -> None
Initialize Loguru logging once per session.
Loads config from project_policy.yaml if available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_level
|
Optional[str]
|
Override log level (e.g. "DEBUG"). |
None
|
log_to_console
|
bool
|
Whether to also log to stderr. |
True
|
Source code in src/civic_lib_core/log_utils.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
log_agent_end ¶
log_agent_end(
agent_name: str, status: str = 'success'
) -> None
Log the end of an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_name
|
str
|
Name of the agent. |
required |
status
|
str
|
Status text (e.g. "success" or "error"). |
'success'
|
Source code in src/civic_lib_core/log_utils.py
108 109 110 111 112 113 114 115 116 | |
log_agent_start ¶
log_agent_start(agent_name: str) -> None
Log the start of an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_name
|
str
|
Name of the agent. |
required |
Source code in src/civic_lib_core/log_utils.py
99 100 101 102 103 104 105 | |
path_utils ¶
Path utilities.
File: civic_lib_core/path_utils.py
policy_utils ¶
Policy enforcement utilities for Civic Interconnect projects.
check_policy ¶
check_policy(repo_root: Path, repo_type: str) -> list[str]
Check the project at repo_root against policy for the given repo_type.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of issues (empty list = all good) |
Source code in src/civic_lib_core/policy_utils.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
project_checks ¶
civic_lib_core/project_checks.py.
Run structural and policy checks on a Civic Interconnect project.
check_additional_files ¶
check_additional_files(
project_root: Path, policy: dict, key: str
) -> list[str]
Check additional file requirements from project policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path
|
Project root. |
required |
policy
|
dict
|
Project policy. |
required |
key
|
str
|
Policy key like 'node_project_files' or 'pwa_project_files'. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Issues for missing files. |
Source code in src/civic_lib_core/project_checks.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
check_empty_dirs ¶
check_empty_dirs(project_root: Path) -> list[str]
Find empty directories in the project.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path
|
Root of the project. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Issues for empty directories. |
Source code in src/civic_lib_core/project_checks.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
check_oversized_py_files ¶
check_oversized_py_files(
project_root: Path, src_dir: Path, policy: dict
) -> list[str]
Check for Python files exceeding allowed line limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path
|
Project root. |
required |
src_dir
|
Path
|
Source directory. |
required |
policy
|
dict
|
Project policy. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Issues for oversized files. |
Source code in src/civic_lib_core/project_checks.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
check_py_files_outside_src ¶
check_py_files_outside_src(
project_root: Path, src_dir: Path
) -> list[str]
Check for .py files outside src_dir, ignoring top-level scripts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path
|
Project root. |
required |
src_dir
|
Path
|
Source directory. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Issues for files outside src. |
Source code in src/civic_lib_core/project_checks.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
check_python_project_dirs ¶
check_python_project_dirs(
project_root: Path, policy: dict
) -> list[str]
Check required directories for Python projects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path
|
Project root. |
required |
policy
|
dict
|
Project policy. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Issues for missing dirs. |
Source code in src/civic_lib_core/project_checks.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
check_python_project_files ¶
check_python_project_files(
project_root: Path, policy: dict
) -> list[str]
Check required files for Python projects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path
|
Project root. |
required |
policy
|
dict
|
Project policy. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Issues for missing files. |
Source code in src/civic_lib_core/project_checks.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
check_required_files ¶
check_required_files(
project_root: Path, policy: dict
) -> list[str]
Check files required in all Civic Interconnect repos.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path
|
Project root. |
required |
policy
|
dict
|
Project policy. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Issues for missing required files. |
Source code in src/civic_lib_core/project_checks.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
main ¶
main() -> None
Run all checks from CLI entry point.
Prints results and exits with appropriate code.
Source code in src/civic_lib_core/project_checks.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
run_all_checks ¶
run_all_checks() -> list[str]
Run all project-level checks.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: List of issues found. |
Source code in src/civic_lib_core/project_checks.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
project_layout ¶
civic_lib_core/project_layout.py.
Discover and verify basic project layout for any Civic Interconnect client repo.
ProjectLayout ¶
Bases: NamedTuple
Represents the layout of a Civic Interconnect project.
Attributes:
| Name | Type | Description |
|---|---|---|
project_root |
Path
|
Root directory of the project. |
src_dir |
Path | None
|
Source directory, or None if not found. |
docs_dir |
Path | None
|
Documentation directory, or None if not found. |
docs_api_dir |
Path | None
|
API documentation source directory, or None if not found. |
packages |
list[Path]
|
List of package directories under src_dir. |
org_name |
str | None
|
Organization name, if detected. |
policy |
dict
|
Loaded project policy data. |
Source code in src/civic_lib_core/project_layout.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
discover_project_layout ¶
discover_project_layout() -> ProjectLayout
Discover and return the layout of the current Civic Interconnect project.
Delegates to fs_utils.discover_project_layout() to perform actual discovery.
Returns:
| Name | Type | Description |
|---|---|---|
ProjectLayout |
ProjectLayout
|
Populated project layout info. |
Source code in src/civic_lib_core/project_layout.py
41 42 43 44 45 46 47 48 49 | |
format_layout ¶
format_layout(layout: ProjectLayout) -> str
Format the layout info for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout
|
ProjectLayout
|
The layout info to format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted layout details. |
Source code in src/civic_lib_core/project_layout.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
main ¶
main() -> None
Standalone entry point for manual testing of this layout module.
Prints layout and any issues found.
Source code in src/civic_lib_core/project_layout.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
verify_layout ¶
verify_layout(layout: ProjectLayout) -> list[str]
Verify that the discovered layout satisfies expectations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout
|
ProjectLayout
|
The layout to check. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: List of issues found (empty list means all OK). |
Source code in src/civic_lib_core/project_layout.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
project_policy ¶
civic_lib_core/project_policy.py.
Load the project policy for any Civic Interconnect client repo.
load_project_policy ¶
load_project_policy(
project_root: Path | None = None,
override_file: Path | None = None,
) -> dict[str, Any]
Load Civic Interconnect project policy.
Behavior:
- Load defaults from civic_lib_core's bundled project_policy.yaml.
- If a client repo defines its own project_policy.yaml, merge its
overrides into the default policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Path | None
|
Optional project root to look for client |
None
|
override_file
|
Path | None
|
Optional path to explicitly provide a custom policy file. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
Combined policy dictionary. |
Source code in src/civic_lib_core/project_policy.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
report_archiver ¶
civic_lib_core/report_archiver.py.
Archives old Civic Interconnect agent reports by renaming them with .archived.json.
Used by admin and maintenance tools, not daily agents.
archive_old_reports ¶
archive_old_reports(
agent_dir: Path, keep_latest: bool = True
) -> list[Path]
Rename old .json reports to .archived.json, optionally keeping the latest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_dir
|
Path
|
Directory with report files. |
required |
keep_latest
|
bool
|
Whether to keep the most recent report unarchived. |
True
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
list[Path]: List of archived report file paths. |
Source code in src/civic_lib_core/report_archiver.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
archive_reports_older_than ¶
archive_reports_older_than(
agent_dir: Path, days_old: int
) -> list[Path]
Archive reports older than a specified number of days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_dir
|
Path
|
Directory with report files. |
required |
days_old
|
int
|
Number of days to retain. Older reports get archived. |
required |
Returns:
| Type | Description |
|---|---|
list[Path]
|
list[Path]: List of archived report file paths. |
Source code in src/civic_lib_core/report_archiver.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
report_constants ¶
civic_lib_core/report_constants.py.
Shared constants for report generation, reading, validation, and indexing. Used across Civic Interconnect agents and admin tools.
report_formatter ¶
civic_lib_core/report_formatter.py.
Format Civic Interconnect agent reports into various human-readable forms. Supports Markdown, plain text, and CSV formats.
format_report_as_csv ¶
format_report_as_csv(report: dict[str, Any]) -> str
Format a report dictionary as a CSV string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
dict
|
A dictionary containing report data with a 'results' key that holds a list of dictionaries to be formatted as CSV. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A CSV-formatted string with headers and data rows, or a message indicating no results are available if the results list is empty. |
Source code in src/civic_lib_core/report_formatter.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
format_report_as_markdown ¶
format_report_as_markdown(report: dict[str, Any]) -> str
Format a report dictionary as a markdown string.
Takes a report dictionary containing agent information, metadata, and results, and converts it into a well-formatted markdown document with a summary section and a sample result displayed as JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
dict[str, Any]
|
A dictionary containing report data with the following optional keys: - 'agent': Name of the agent that generated the report - 'timestamp': When the report was generated - 'agent_version': Version of the agent - 'lib_version': Version of the library used - 'record_count': Number of records processed - 'results': List of result objects |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A markdown-formatted string containing the report summary and the first result from the results list (if available) displayed as a JSON code block. |
Example
report = { ... 'agent': 'DataProcessor', ... 'timestamp': '2023-10-01T12:00:00Z', ... 'record_count': 100, ... 'results': [{'id': 1, 'status': 'success'}], ... } markdown = format_report_as_markdown(report) print(markdown)
Report Summary for DataProcessor¶
Date: 2023-10-01T12:00:00Z ...
Source code in src/civic_lib_core/report_formatter.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
format_report_as_text ¶
format_report_as_text(report: dict[str, Any]) -> str
Format a report dictionary as a human-readable text string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
dict
|
A dictionary containing report data with keys like 'agent', 'timestamp', 'agent_version', 'lib_version', 'record_count', and 'results'. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A formatted multi-line string representation of the report including metadata and the first sample result (if available). |
Example
report = { ... 'agent': 'DataCollector', ... 'timestamp': '2023-10-15 14:30:00', ... 'agent_version': '1.2.3', ... 'lib_version': '2.1.0', ... 'record_count': 150, ... 'results': [{'id': 1, 'value': 'sample'}], ... } print(format_report_as_text(report)) Report: DataCollector Date: 2023-10-15 14:30:00 Agent Version: 1.2.3 Library Version: 2.1.0 Record Count: 150
Sample Result: { "id": 1, "value": "sample" }
Source code in src/civic_lib_core/report_formatter.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
to_csv ¶
to_csv(data: list[dict[str, Any]], path: Path) -> None
Write a list of dictionaries to a CSV file.
If the data list is empty, writes "No results to export." to the file instead. The CSV header is generated from the keys of the first dictionary in the list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[dict[str, Any]]
|
A list of dictionaries to write to CSV. All dictionaries should have the same keys for proper CSV formatting. |
required |
path
|
Path
|
The file path where the CSV will be written. Will be created if it doesn't exist. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Note
The file is written with UTF-8 encoding. If data is empty, a plain text message is written instead of CSV format.
Source code in src/civic_lib_core/report_formatter.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
to_markdown ¶
to_markdown(data: list[dict[str, Any]], path: Path) -> None
Convert a list of dictionaries to a Markdown table and write it to a file.
Takes a list of dictionaries where each dictionary represents a row of data, and converts it into a Markdown table format. The keys of the first dictionary are used as column headers. If the data list is empty, writes a message indicating no results to display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[dict[str, Any]]
|
List of dictionaries containing the data to convert. Each dictionary should have the same keys which will be used as table headers. |
required |
path
|
Path
|
Path object specifying where to write the Markdown table file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
The function writes directly to the specified file path. |
Note
- Pipe characters (|) in data values are automatically escaped to preserve Markdown table formatting.
- The file is written with UTF-8 encoding.
- If data is empty, writes "No results to display." to the file.
Source code in src/civic_lib_core/report_formatter.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
report_indexer ¶
Module for generating a Markdown index of agent reports.
This module provides: - generate_index: generates a Markdown index listing the latest report from each agent
generate_index ¶
generate_index(report_dir: Path = REPORTS_DIR) -> None
Generate a Markdown index listing the latest report from each agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report_dir
|
Path
|
The base |
REPORTS_DIR
|
Source code in src/civic_lib_core/report_indexer.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
report_reader ¶
civic_lib_core/report_reader.py.
Functions for reading, inspecting, and validating Civic Interconnect agent reports. Used by dashboards, CLI tools, and indexing utilities.
get_latest_report ¶
get_latest_report(agent_dir: Path) -> Path | None
Get the most recent report file from the specified agent directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_dir
|
Path
|
Path to the agent's report folder. |
required |
Returns:
| Type | Description |
|---|---|
Path | None
|
Path | None: The latest report file, or None if none found. |
Source code in src/civic_lib_core/report_reader.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
read_latest_report ¶
read_latest_report(
agent_dir: Path, strict: bool = False
) -> dict[str, Any] | None
Read and return the contents of the latest report for a given agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_dir
|
Path
|
Path to the agent's report folder. |
required |
strict
|
bool
|
If True, raise errors on missing or invalid reports. If False, return None and log a warning. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
dict | None: Parsed report contents, or None if no report exists or format is invalid (in non-strict mode). |
Source code in src/civic_lib_core/report_reader.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
validate_report_format ¶
validate_report_format(report: dict[str, Any]) -> bool
Validate that a report contains all expected top-level keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
dict
|
The parsed report to validate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if valid, False otherwise. |
Source code in src/civic_lib_core/report_reader.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
report_summary ¶
civic_lib_core/report_summary.py.
Generates human-readable Markdown summaries of Civic Interconnect agent reports. Used optionally by agents or admin tools alongside JSON output.
write_markdown_summary ¶
write_markdown_summary(
report: dict[str, Any], path: Path
) -> None
Write a Markdown summary of a report's key metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
dict
|
The report data (already parsed). |
required |
path
|
Path
|
The output path to write the .md file. |
required |
Source code in src/civic_lib_core/report_summary.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
report_utils ¶
civic_lib_core/report_utils.py.
Basic helpers for working with Civic Interconnect reports. Part of the Civic Interconnect agent framework.
get_agent_name_from_path ¶
get_agent_name_from_path(path: Path) -> str
Extract and format the agent name from a report file path.
The agent name is derived from the parent folder of the report file, with underscores replaced by spaces and title-cased.
If the path does not have a parent directory, returns 'Unknown Agent'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path to a report file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted agent name or fallback string. |
Source code in src/civic_lib_core/report_utils.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
is_report_file ¶
is_report_file(path: Path) -> bool
Determine whether the given file path is a valid report file.
A valid report file must: - Have a ".json" extension - Begin with a date prefix (e.g., "2024-01-01")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the path matches report file format, False otherwise. |
Source code in src/civic_lib_core/report_utils.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
report_writer ¶
civic_lib_core/report_writer.py.
Functions for writing timestamped agent reports in multiple formats. Used by daily Civic Interconnect agents.
write_report ¶
write_report(
data: list[dict[str, Any]],
agent_name: str,
agent_version: str,
schema_version: str = '1.0.0',
report_dir: str | Path = REPORTS_DIR,
file_format: str = 'json',
) -> Path
Write agent output to a timestamped report file with metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[dict[str, Any]]
|
The results to include in the report. |
required |
agent_name
|
str
|
The name of the agent generating the report. |
required |
agent_version
|
str
|
The version of the agent code. |
required |
schema_version
|
str
|
The version of the report schema (default: "1.0.0"). |
'1.0.0'
|
report_dir
|
str | Path
|
Root directory where reports are saved (default: REPORTS_DIR). |
REPORTS_DIR
|
file_format
|
str
|
Output format, one of "json" or "csv" (default: "json"). |
'json'
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The full path to the saved report file. |
Source code in src/civic_lib_core/report_writer.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
schema_utils ¶
civic_lib_core/schema_utils.py.
Centralized schema change detection utilities for Civic Interconnect agents. Part of the Civic Interconnect agent framework.
detect_schema_change ¶
detect_schema_change(
old_file: Path, new_data: dict[str, Any]
) -> bool
Detect if the schema has changed by comparing the old file's hash with the new data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_file
|
Path
|
The path to the old schema file. |
required |
new_data
|
dict[str, Any]
|
The new schema data to compare against. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the schema has changed (i.e., hashes differ), False otherwise. |
Source code in src/civic_lib_core/schema_utils.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
hash_dict ¶
hash_dict(data: dict[str, Any]) -> str
Hash a JSON-serializable dictionary for change detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The dictionary to hash. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The SHA-256 hash of the JSON-encoded dictionary. |
Source code in src/civic_lib_core/schema_utils.py
43 44 45 46 47 48 49 50 51 52 53 54 55 | |
load_json ¶
load_json(path: str | Path) -> dict[str, Any]
Load a JSON file and return its contents as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The path to the JSON file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: The parsed JSON data. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
JSONDecodeError
|
If the file is not valid JSON. |
Source code in src/civic_lib_core/schema_utils.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
version_utils ¶
civic_lib_core/version_utils.py.
Version discovery utilities for Civic Interconnect projects.
Supports: - Python projects (via importlib.metadata or pyproject.toml) - Non-Python projects (via VERSION file) - JavaScript/NodeJS projects (via package.json)
This allows the Civic CLI and shared tools to work seamlessly across mixed technology stacks, ensuring consistent version handling even in frontend-only repos.
get_repo_version ¶
get_repo_version(
package_name: str = 'civic-lib-core',
root_dir: Path | None = None,
) -> str
Retrieve the project version from various sources.
- Python metadata (if package installed)
- pyproject.toml
- VERSION file
- package.json.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The discovered version string, or "0.0.0" if none found. |
Source code in src/civic_lib_core/version_utils.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
get_version_from_files ¶
get_version_from_files(root: Path) -> str | None
Check pyproject.toml, VERSION, or package.json for the project version.
Source code in src/civic_lib_core/version_utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
get_version_from_python_metadata ¶
get_version_from_python_metadata(
package_name: str,
) -> str | None
Try reading the version from installed Python package metadata.
Source code in src/civic_lib_core/version_utils.py
29 30 31 32 33 34 35 36 37 38 39 | |
yaml_utils ¶
Lightweight helpers for reading and writing YAML files.
File: yaml_utils.py
read_yaml ¶
read_yaml(path: str | Path) -> dict[str, Any]
Read and parse a YAML file into a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
YAML file path. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
Parsed YAML data. |
Source code in src/civic_lib_core/yaml_utils.py
31 32 33 34 35 36 37 38 39 40 41 42 | |
write_yaml ¶
write_yaml(data: dict[str, Any], path: str | Path) -> Path
Write a dictionary to a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Data to write. |
required |
path
|
str | Path
|
File path to write to. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The path the file was written to. |
Source code in src/civic_lib_core/yaml_utils.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |