topmark.pipeline.context.policy¶
topmark / pipeline / context / policy
Policy helpers for interpreting configuration in the pipeline context.
This module centralizes helpers that interpret the effective FrozenPolicy in the context of a single file. Functions here operate on a ProcessingContext instance to decide whether certain operations (for example, inserting into empty files or tolerating mixed newlines) are permitted.
PolicyContext ¶
Bases: Protocol
Minimum context surface required by policy helpers.
is_effectively_empty
property
¶
Whether the file image is effectively empty.
Returns whether the decoded, BOM-stripped text image contains no non-whitespace characters. Newlines and other whitespace are allowed. This is the broad notion of "empty" used for most policy decisions.
is_logically_empty
property
¶
Whether the file is "logically empty".
Returns whether the file is "logically empty": after BOM stripping,
it contains optional horizontal whitespace and at most one trailing
newline sequence (LF/CRLF/CR), and nothing else. This is a stricter subset
of is_effectively_empty and is useful to preserve stable round-trips for
files that are effectively placeholders.
is_empty_for_insert ¶
Return whether this file should be treated as "empty" for insertion decisions.
This helper interprets the effective per-type policy setting
FrozenPolicy.empty_insert_mode
and maps it onto the file's observed state.
The distinction matters because TopMark may need to preserve stable round-trips:
- A true empty file (0 bytes) is represented by
FsStatus.EMPTY. - A file can be logically empty (BOM stripped, optional whitespace, optional single trailing newline) even when it is not 0 bytes.
- A file can be effectively empty (no non-whitespace characters) while containing multiple blank lines.
The selected mode controls which of these categories is considered "empty" when deciding whether inserting a header is allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context (or compatible protocol) providing filesystem status and decoded image emptiness flags. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the current file qualifies as empty under the configured insertion mode. |
Notes
- This predicate is only about classification of emptiness for insert gating.
It does not check whether insertion is allowed; use
allow_insert_into_empty_likefor that. FsStatus.EMPTYis always treated as empty, regardless of mode.
Source code in src/topmark/pipeline/context/policy.py
is_empty_for_insert_unchanged_by_default ¶
Return True when an insertion-empty file should default to UNCHANGED.
This helper is for bucketing/reporting, not mutation.
It captures the default policy interpretation for files that are classified as "empty for insertion":
- If the file counts as empty under
is_empty_for_insert(ctx), and - policy does not allow inserting into such files,
then TopMark should generally treat the file as compliant / unchanged rather than as "missing header".
This avoids surprising "would insert" or "missing header" outcomes for placeholder files such as:
- true 0-byte files
- BOM-only files
- newline-only files
- other empty-like images covered by the configured
EmptyInsertMode
The actual definition of "empty" is delegated to is_empty_for_insert(ctx),
so this helper automatically obeys the effective EmptyInsertMode
(bytes_empty, logical_empty, or whitespace_empty).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context (or compatible protocol). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the file is classified as empty-for-insert and policy does not |
bool
|
allow inserting into such files; otherwise False. |
Notes
- Use this in outcome bucketing and summaries.
- Mutation steps should instead use
allow_insert_into_empty_like(ctx).
Source code in src/topmark/pipeline/context/policy.py
allow_insert_into_empty_like ¶
Return True if policy permits inserting a header into an empty-like file.
This is the primary policy gate used by planner/updater when a file has no meaningful body content.
The decision is a conjunction of:
1) whether the file is considered empty for insert (see
is_empty_for_insert), and
2) whether the effective policy enables insertion into empty files
(FrozenPolicy.allow_header_in_empty_files).
Because this helper delegates classification to is_empty_for_insert, it
automatically obeys the configured EmptyInsertMode (bytes_empty,
logical_empty, or whitespace_empty).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context (or compatible protocol). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if insertion is allowed for this file given its empty-like state and the |
bool
|
effective policy. |
Guidance
- Use this in mutation steps (planner/updater) when deciding whether an insert/update is allowed to proceed.
- Do not use it to skip reading/analysis steps; those should be governed by filesystem/content feasibility (e.g., unreadable/binary/mixed-newlines).
Source code in src/topmark/pipeline/context/policy.py
allow_empty_header ¶
Return True if the effective policy allows empty header insertion.
This helper inspects the effective per-type policy (global configuration overlaid by per-type overrides) and reports whether an empty rendered header is considered acceptable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context containing status and configuration. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Source code in src/topmark/pipeline/context/policy.py
allow_content_reflow ¶
Return True if the effective policy allows content reflow.
This covers transformations that may adjust layout or whitespace around
the header region and are controlled by the allow_reflow flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context containing status and configuration. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if policy allows reflowing content around the header, |
bool
|
False otherwise. |
Source code in src/topmark/pipeline/context/policy.py
allow_mixed_line_endings ¶
Return True if policy allows proceeding despite mixed line endings.
This helper is used by early pipeline steps (e.g., ReaderStep) when the
sniffer detected mixed line endings (FsStatus.MIXED_LINE_ENDINGS) and
the project policy has opted into tolerating them.
Policy fields
- If the effective
FrozenPolicydefinesignore_mixed_line_endingsand it is True, we allow proceeding onMIXED_LINE_ENDINGS.
Notes
- This function is forward-compatible: it uses
getattr(...)so it returns False for unknown fields on older Policy versions (safe default). - We always allow when
FsStatusis already OK/EMPTY; for EMPTY, your existingallow_insert_into_empty_like()governs header insertion later.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context containing filesystem status and configuration. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Source code in src/topmark/pipeline/context/policy.py
allow_bom_before_shebang ¶
Return True if policy allows proceeding despite a BOM before the shebang.
This helper is used by early pipeline steps (e.g., ReaderStep) when the
sniffer detected a BOM before the shebang (FsStatus.BOM_BEFORE_SHEBANG)
and the project policy has opted into tolerating it.
Policy fields
- If the effective
FrozenPolicydefinesignore_bom_before_shebangand it is True, we allow proceeding onBOM_BEFORE_SHEBANG.
Notes
- This function is forward-compatible: it uses
getattr(...)so it returns False for unknown fields on older Policy versions (safe default). - We always allow when
FsStatusis already OK/EMPTY; for EMPTY, your existingallow_insert_into_empty_like()governs header insertion later.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context containing filesystem status and configuration. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Source code in src/topmark/pipeline/context/policy.py
check_permitted_by_policy ¶
Whether the active check policy allows the intended header mutation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context for the current file. |
required |
Returns:
| Type | Description |
|---|---|
bool | None
|
|
bool | None
|
|
bool | None
|
|
Source code in src/topmark/pipeline/context/policy.py
331 332 333 334 335 336 337 338 339 340 341 342 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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
would_change ¶
Return whether a change would occur (tri-state).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context for the current file. |
required |
Returns:
| Type | Description |
|---|---|
bool | None
|
|
bool | None
|
or the strip step prepared/attempted a removal), |
bool | None
|
(e.g., UNCHANGED or strip NOT_NEEDED), and |
bool | None
|
comparison was skipped/pending and the strip step did not run. |
Source code in src/topmark/pipeline/context/policy.py
can_change ¶
Return whether a mutation can be applied safely for this file.
This helper answers a narrow question:
If the pipeline intends to mutate this file, is that mutation structurally and operationally allowed to proceed?
It combines three categories of checks:
1) Baseline feasibility The file must have resolved successfully, strip preparation must not have failed, and the header state must not be one of the malformed states that block safe mutation.
2) Normal files
For ordinary files (FsStatus.OK), mutation is allowed once baseline
feasibility is satisfied.
3) Empty / empty-like files
For files that are considered "empty for insert", mutation is allowed only
when policy explicitly permits inserting into such files. Importantly,
emptiness classification is delegated to is_empty_for_insert(ctx), which
obeys the configured EmptyInsertMode.
This means:
- true 0-byte files may be mutable if policy allows it
- logically empty placeholders may be mutable if policy allows it
- whitespace-only files may be mutable if policy allows it
(depending on EmptyInsertMode)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context for the current file. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a mutation is structurally and operationally safe to apply, |
bool
|
otherwise False. |
Source code in src/topmark/pipeline/context/policy.py
would_add_or_update ¶
Intent for check/apply: True if we'd insert or replace a header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context for the current file. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Source code in src/topmark/pipeline/context/policy.py
effective_would_add_or_update ¶
True iff add/update is intended, feasible, and allowed by policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context for the current file. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Source code in src/topmark/pipeline/context/policy.py
would_strip ¶
Intent for strip: True if a removal would occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context for the current file. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Source code in src/topmark/pipeline/context/policy.py
effective_would_strip ¶
True iff a strip is intended and feasible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PolicyContext
|
Processing context for the current file. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|