topmark.api¶
topmark.api ¶
Public TopMark API (stable surface).
This package provides a small, typed façade for running TopMark programmatically.
Design goals: - Provide a stable entry point for integrations without requiring the CLI (Click). - Keep inputs JSON/TOML-friendly (plain mappings) while maintaining strict internal typing. - Return machine-friendly results (dataclasses / TypedDicts) without ANSI formatting.
Stability policy:
- Symbols exported by topmark.api via __all__ are the supported surface,
including stable re-exports such as Outcome.
- Adding optional parameters with defaults is allowed in minor releases.
- Removing/renaming symbols or changing return shapes is a breaking change.
- Low-level runtime helpers may expose ApiPipelineRun
when callers need resolved config, selected files, processing contexts, and a
fatal pipeline-level exit code before conversion into public result DTOs.
High-level concepts:
- Recognized file types exist in the file type registry.
- Supported file types are recognized and have a processor bound.
- Diagnostics returned by the API are JSON-friendly and use string severities:
"info", "warning", "error".
- Pipeline outcome values are shared core primitives re-exported here as
Outcome for public API convenience.
- High-level commands return stable result DTOs such as
RunResult and
ProbeRunResult. Lower-level runtime
orchestration returns ApiPipelineRun
for integrations that intentionally work with processing contexts.
Configuration contract:
- Public pipeline functions (probe(), check(), strip()) accept an optional plain mapping
(mirroring the TOML/pyproject structure) or an immutable
FrozenConfig.
- Passing config=None triggers layered discovery (defaults → user → project) using the
same rules as the CLI.
- The internal MutableConfig builder is not part of
the public API; it exists to perform discovery/merging and is frozen immediately into an immutable
FrozenConfig before execution.
Example
from topmark import api
config = {
"fields": {
"project": "TopMark",
"license": "MIT",
},
"header": {
"fields": [
"file",
"project",
"license",
]
},
"formatting": {
"align_fields": True,
},
"files": {
"include_file_types": ["python"],
"exclude_patterns": [".venv"],
},
"policy_by_type": {
"python": {
"allow_header_in_empty_files": True,
},
},
}
run: api.RunResult = api.check(
["src"],
config=config,
diff=True,
report="actionable",
)
assert run.summary.get("unchanged", 0) >= 0
ApiPipelineRun
dataclass
¶
Resolved runtime state and pipeline results for one API execution.
This value object is the low-level orchestration result returned by the internal API runtime helpers. It exposes the fully resolved runtime config, the selected file list, the produced processing contexts, and any fatal pipeline-level exit code.
Attributes:
| Name | Type | Description |
|---|---|---|
effective_cfg |
FrozenConfig
|
Final runtime config after layered discovery, runtime overlays, and execution-scoped adjustments. |
file_list |
list[Path]
|
Files selected for pipeline execution after discovery and filtering. |
results |
list[ProcessingContext]
|
Processing contexts produced by pipeline execution. For probe runs, this may also include synthetic contexts representing missing or filtered explicit inputs. |
exit_code |
ExitCode | None
|
Fatal pipeline-level exit code, if one was encountered. |
Notes
This object intentionally exposes internal processing contexts rather
than stable public DTOs. Higher-level public API helpers are expected
to normalize these contexts into stable machine-facing result objects
such as RunResult and
ProbeRunResult.
DiagnosticEntry ¶
Bases: TypedDict
JSON-friendly diagnostic entry for API consumers.
Notes
- Uses string literal levels for stability and easy serialization.
- Mirrors internal pipeline diagnostics but does not expose internal enums/classes.
FileResult
dataclass
¶
Result for a single file after pipeline execution and view filtering.
This object is a pure data carrier intended for machine consumption (JSON, NDJSON, API callers). It deliberately contains no pre-rendered human-facing messages or ANSI formatting.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path
|
Absolute or workspace-relative path to the file. |
outcome |
Outcome
|
Stable, high-level outcome bucket describing the
file's final state (e.g. |
diff |
str | None
|
Unified diff as a string when available.
|
bucket_key |
str | None
|
Public bucket identifier corresponding to the
outcome classification (typically |
bucket_label |
str | None
|
Human-oriented explanation for the bucket, derived from the first-seen classification reason. Intended for display purposes only. |
Notes
outcomeis the authoritative semantic signal and is stable across versions.bucket_keymirrors the public outcome value and exists to support uniform bucketing and summaries.bucket_labelis best-effort, may change between versions, and should not be relied upon for programmatic decisions.- Any human-readable or colorized summaries must be generated by
presentation helpers (e.g. in
topmark.presentation), not by the API.
FileTypeInfo ¶
Bases: TypedDict
Stable metadata about a registered file type.
This is the JSON-friendly public view returned by registry-facing API helpers. It describes discovery metadata, binding state, and the effective placement policy for the file type.
Attributes:
| Name | Type | Description |
|---|---|---|
local_key |
str
|
File type local key (compatibility identifier). |
namespace |
str
|
Namespace that owns the file type. |
qualified_key |
str
|
Canonical file type key. |
description |
str
|
Human description. |
bound |
bool
|
Whether the file type currently has an effective processor binding. |
extensions |
Sequence[str]
|
Known filename extensions (without dots). |
filenames |
Sequence[str]
|
Exact filenames matched (for example, |
patterns |
Sequence[str]
|
Full-match regular-expression patterns. |
skip_processing |
bool
|
Whether the type is discoverable but not processed. |
has_content_matcher |
bool
|
Whether a content matcher exists. |
has_insert_checker |
bool
|
Whether a header insert checker exists. |
policy |
FileTypePolicyInfo
|
Policy/strategy for header placement. |
ProbeCandidateInfo
dataclass
¶
Stable public view of one file-type resolution candidate.
Attributes:
| Name | Type | Description |
|---|---|---|
file_type |
str
|
Public file type identifier for the candidate. |
qualified_key |
str
|
Canonical namespaced file type key. |
score |
int
|
Resolver score used only for explanatory ordering within this probe result. The exact scoring algorithm is internal and may evolve. |
selected |
bool
|
Whether this candidate was selected as the effective file type. |
rank |
int
|
One-based rank after resolver tie-breaking. |
matched_by |
tuple[str, ...]
|
Stable string tokens describing which resolver signals matched, such as extension, filename, pattern, content, or content_error. |
Notes
This object deliberately omits internal registry objects, matcher instances,
and raw scoring details. Consumers should treat selected, rank, and
matched_by as the stable decision signals; score is explanatory only.
ProbeFileResult
dataclass
¶
Probe result for a single path.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path
|
Path that was probed, reported as missing, or reported as filtered during explicit-input discovery. |
status |
str
|
Stable string status for the probe result, derived from the internal resolver status without exposing internal enum classes. |
reason |
str
|
Stable string reason describing why the path reached that status. |
selected_file_type |
str | None
|
Public local identifier of the selected file type, if one resolved successfully. |
selected_processor |
str | None
|
Public local identifier of the selected processor, if one is bound for the selected file type. |
candidates |
Sequence[ProbeCandidateInfo]
|
Ordered, normalized candidate list considered during file-type resolution. Empty for missing or discovery-filtered paths. |
Notes
status and reason are plain strings to keep the public API JSON-friendly
and independent from internal resolver enum classes. Candidate internals are
reduced to ProbeCandidateInfo records.
ProbeRunResult
dataclass
¶
Aggregate result of a TopMark probe run.
Attributes:
| Name | Type | Description |
|---|---|---|
files |
Sequence[ProbeFileResult]
|
Ordered sequence of per-path probe results. |
summary |
Mapping[str, int]
|
Mapping from public probe status strings to counts for |
had_errors |
bool
|
|
diagnostics |
dict[str, list[DiagnosticEntry]] | None
|
Optional mapping from file path to public diagnostic entries for the probe result set. |
diagnostic_totals |
DiagnosticTotals | None
|
Aggregate diagnostic counts across the probe result set. |
Notes
This is the public API shape for topmark.api.probe(). It intentionally
exposes resolution as stable primitive values and DTOs instead of returning
ProcessingContext, ResolutionProbeResult, or other internal structures.
ProcessorInfo ¶
Bases: TypedDict
Stable metadata about a registered header processor.
This is the JSON-friendly public view returned by registry-facing API helpers. It describes the processor's identity, binding state, and comment delimiter metadata.
Attributes:
| Name | Type | Description |
|---|---|---|
local_key |
str
|
Processor local key (compatibility identifier). |
namespace |
str
|
Namespace that owns the processor. |
qualified_key |
str
|
Canonical processor key. |
description |
str
|
Human description. |
bound |
bool
|
Whether the processor currently participates in at least one effective file-type binding. |
line_indent |
str
|
Line comment indent (if applicable). |
line_prefix |
str
|
Line comment prefix (if applicable). |
line_suffix |
str
|
Line comment suffix (if applicable). |
block_prefix |
str
|
Block comment prefix (if applicable). |
block_suffix |
str
|
Block comment suffix (if applicable). |
RunResult
dataclass
¶
RunResult(
*,
files,
summary,
had_errors,
skipped=0,
written=0,
failed=0,
bucket_summary=None,
diagnostics=None,
diagnostic_totals=None,
diagnostic_totals_all=None,
)
Aggregate result of a TopMark run after view-level filtering.
This structure summarizes per-file results, outcome counts, and diagnostics in a JSON-friendly form suitable for API consumers and machine-readable output formats.
Attributes:
| Name | Type | Description |
|---|---|---|
files |
Sequence[FileResult]
|
Ordered sequence of per-file results after report-scope filtering. |
summary |
Mapping[str, int]
|
Mapping from public outcome values
( |
had_errors |
bool
|
|
skipped |
int
|
Number of results excluded by report-scope filtering. |
written |
int
|
Number of files successfully written
(only meaningful when |
failed |
int
|
Number of files that failed to write
(only meaningful when |
bucket_summary |
Mapping[str, int] | None
|
Optional summary aggregated by bucket key rather than by outcome alone. When present, this is primarily intended to support CLI-style reporting. |
diagnostics |
dict[str, list[DiagnosticEntry]] | None
|
Optional mapping from file path to public diagnostic entries for the returned view. |
diagnostic_totals |
DiagnosticTotals | None
|
Aggregate diagnostic counts across the returned (filtered) view. |
diagnostic_totals_all |
DiagnosticTotals | None
|
Aggregate diagnostic counts across the entire run (pre report filtering). |
Notes
summaryis strictly derived fromfilesand reflects only the filtered view.bucket_summaryis optional and more presentation-oriented thansummary; consumers should not assume it is always present.- No fields in this object contain formatted or colorized output. Presentation is the responsibility of higher layers (CLI or UI).
Outcome ¶
Bases: str, Enum
Stable per-file outcome bucket used by the public API.
Values mirror the high-level outcome categories exposed by the CLI and API.
Consumers should prefer Outcome (and Outcome.value) for programmatic
decisions rather than relying on human-facing labels.
VersionInfo
dataclass
¶
Prepared payload for topmark version (and the API equivalent).
VersionInfo is part of the stable API surface.
Attributes:
| Name | Type | Description |
|---|---|---|
version_text |
str
|
TopMark version text in the requested output format. |
version_format |
VersionFormatLiteral
|
The format of |
err |
Exception | None
|
Conversion error when |
check ¶
check(
paths,
*,
apply=False,
diff=False,
config=None,
policy=None,
policy_by_type=None,
include_file_types=None,
exclude_file_types=None,
report="all",
prune_views=False,
)
Validate or apply TopMark headers for the given paths.
This is the programmatic equivalent of the CLI topmark check. It preserves
the same discovery behavior when config is None and accepts optional
policy overlays that are applied after discovery, before the pipeline runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
Iterable[Path | str]
|
Files and/or directories to process. Globs are allowed by the caller; TopMark will recurse and filter internally. |
required |
apply
|
bool
|
If |
False
|
diff
|
bool
|
If |
False
|
config
|
Mapping[str, object] | None
|
Optional plain mapping or immutable
|
None
|
policy
|
PublicPolicy | None
|
Optional global policy overrides in the public API shape. These are merged after discovery using standard policy resolution. |
None
|
policy_by_type
|
Mapping[str, PublicPolicy] | None
|
Optional per-type policy overrides in the public API shape, merged after discovery. |
None
|
include_file_types
|
Sequence[str] | None
|
Optional whitelist of file type identifiers to restrict discovery. |
None
|
exclude_file_types
|
Sequence[str] | None
|
Optional blacklist of file type identifiers to exclude from discovery. |
None
|
report
|
PublicReportScopeLiteral
|
Reporting scope for the returned API view ( |
'all'
|
prune_views
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
RunResult
|
Filtered per-file outcomes, counts, diagnostics, and write stats. |
Notes
Reporting/view filtering is handled by the public view layer. It does not
change which files are eligible to be written when apply=True.
Source code in src/topmark/api/commands/pipeline.py
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 130 131 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
probe ¶
probe(
paths,
*,
config=None,
policy=None,
policy_by_type=None,
include_file_types=None,
exclude_file_types=None,
prune_views=False,
)
Explain how paths resolve to TopMark file types and processors.
This is the programmatic equivalent of the CLI topmark probe. It runs the
read-only probe pipeline and returns a stable public view of file-type
resolution, selected processors, and normalized candidate information.
The returned objects intentionally do not expose internal resolution enums,
pipeline contexts, or registry implementation details. Use string fields and
ProbeCandidateInfo values for programmatic decisions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
Iterable[Path | str]
|
Files and/or directories to probe. Globs are allowed by the caller; TopMark will recurse and filter internally. |
required |
config
|
Mapping[str, object] | None
|
Optional plain mapping or immutable
|
None
|
policy
|
PublicPolicy | None
|
Optional global policy overrides. For probe, this is primarily
useful for resolver-affecting options such as |
None
|
policy_by_type
|
Mapping[str, PublicPolicy] | None
|
Optional per-type policy overrides merged after discovery. |
None
|
include_file_types
|
Sequence[str] | None
|
Optional whitelist of file type identifiers to restrict discovery. |
None
|
exclude_file_types
|
Sequence[str] | None
|
Optional blacklist of file type identifiers to exclude from discovery. |
None
|
prune_views
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ProbeRunResult
|
Resolved runtime config, selected file list, probe results, and any |
ProbeRunResult
|
fatal pipeline-level exit code. |
Notes
probe() is always read-only. It has no apply or diff mode because it
explains resolution rather than planning or applying header mutations.
Source code in src/topmark/api/commands/pipeline.py
strip ¶
strip(
paths,
*,
apply=False,
diff=False,
config=None,
policy=None,
policy_by_type=None,
include_file_types=None,
exclude_file_types=None,
report="all",
prune_views=False,
)
Remove TopMark headers from files (dry-run or apply).
This is the programmatic equivalent of the CLI topmark strip. When config
is None, the function performs the same project discovery as the CLI and
then applies optional policy overlays before running the pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
Iterable[Path | str]
|
Files and/or directories to process. Globs are allowed. |
required |
apply
|
bool
|
If |
False
|
diff
|
bool
|
If |
False
|
config
|
Mapping[str, object] | None
|
Optional plain mapping or immutable
|
None
|
policy
|
PublicPolicy | None
|
Optional global policy overrides in the public API shape. Strip flows are currently policy-agnostic, but this is accepted for forward compatibility. |
None
|
policy_by_type
|
Mapping[str, PublicPolicy] | None
|
Optional per-type policy overrides in the public API shape. |
None
|
include_file_types
|
Sequence[str] | None
|
Optional whitelist of file type identifiers to restrict discovery. |
None
|
exclude_file_types
|
Sequence[str] | None
|
Optional blacklist of file type identifiers to exclude from discovery. |
None
|
report
|
PublicReportScopeLiteral
|
Reporting scope for the returned API view ( |
'all'
|
prune_views
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
RunResult
|
Resolved runtime config, selected file list, filtered results, and any |
RunResult
|
fatal pipeline-level exit code. |
Notes
Reporting/view filtering is handled by the public view layer and does not modify pipeline write decisions.
Source code in src/topmark/api/commands/pipeline.py
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 214 215 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
list_filetypes ¶
Return metadata about registered file types.
Returns:
| Type | Description |
|---|---|
list[FileTypeInfo]
|
A list of |
Notes
For object-level access, prefer FileTypeRegistry from
topmark.registry. This function returns metadata rather than the
registry objects themselves.
Source code in src/topmark/api/commands/registry.py
list_processors ¶
Return metadata about registered header processors.
Returns:
| Type | Description |
|---|---|
list[ProcessorInfo]
|
A list of |
Notes
For object-level access, prefer HeaderProcessorRegistry from
topmark.registry. This function returns metadata rather than the
registry objects themselves.
Source code in src/topmark/api/commands/registry.py
get_version_info ¶
get_version_text ¶
Return the current TopMark version (string).
options: heading_level: 1 show_root_heading: true members_order: source filters: - "!^_"