topmark.pipeline.steps.sniffer¶
topmark / pipeline / steps / sniffer
Pre-read file sniffer.
This module implements the SnifferStep, a lightweight pre-read pipeline step
that inspects files between resolution and full text loading. It performs:
- existence and permission checks
- fast binary sniff using a NUL-byte heuristic
- strict UTF-8 validation on small chunks
- BOM + shebang ordering checks for shebang-aware file types
- raw newline histogram construction (LF/CRLF/CR) with mixed-newline detection
The main step entrypoint is [SnifferStep.run], which delegates to several helpers:
_count_newlines- counts LF/CRLF/CR sequences with CR/LF carry handlinginspect_bom_shebang- inspects the prefix for UTF-8 BOM and shebang ordering_commit_newline_stats- populates newline histogram and derived stats on the context_sniff_stream- orchestrates byte-level sniffing and returns an optional terminalFsStatusthatSnifferStep.runapplies
Sets
FsStatus→ {OK, EMPTY, NOT_FOUND, NO_READ_PERMISSION, UNREADABLE, NO_WRITE_PERMISSION, BINARY, UNICODE_DECODE_ERROR, BOM_BEFORE_SHEBANG, MIXED_LINE_ENDINGS}
SnifferStep ¶
Bases: BaseStep
Pre-read checks (existence/perm, binary/UTF-8, BOM/shebang, newline mix).
Performs fast, bytes-level checks and sets FsStatus. It does not load the full
text image; ReaderStep remains authoritative for ContentStatus.
Axes written
- fs
Sets
- FsStatus: {PENDING, OK, EMPTY, NOT_FOUND, NO_READ_PERMISSION, UNREADABLE, NO_WRITE_PERMISSION, BINARY, BOM_BEFORE_SHEBANG, UNICODE_DECODE_ERROR, MIXED_LINE_ENDINGS}
Source code in src/topmark/pipeline/steps/sniffer.py
may_proceed ¶
Determine if processing can proceed to the read step.
Processing can proceed if: - The file was successfully resolved (ctx.status.resolve is RESOLVED) - A file type is present (ctx.file_type is not None) - A header processor is available (ctx.header_processor is not None)
Note
The file system status (ctx.status.fs) is not strictly required here,
to allow tests to skip the sniffer and invoke the reader directly. In such
cases, the reader is the definitive authority for content checks (existence,
permissions, binary/text, etc).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
ProcessingContext
|
The processing context for the current file. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if |
bool
|
are set. |
Source code in src/topmark/pipeline/steps/sniffer.py
run ¶
Lightweight I/O step between resolver and reader.
Responsibilities: - Confirm file exists and is readable. - Fast text-vs-binary sniff (NUL bytes, incremental UTF-8 decode of tiny chunks). - BOM + shebang ordering check for shebang-aware file types. - Quick newline histogram (bytes-level) and strict mixed-newlines skip. - Establish a tentative newline_style (dominant or default to LF) without loading full text.
Notes:
- Does not populate ctx.file_lines; that is the reader's job.
- If this step sets a non-RESOLVED file status, later steps will early-return.
Source code in src/topmark/pipeline/steps/sniffer.py
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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
hint ¶
Attach sniff outcome hints (non-binding).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
ProcessingContext
|
The processing context. |
required |
Source code in src/topmark/pipeline/steps/sniffer.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
inspect_bom_shebang ¶
Inspect the initial bytes for BOM and shebang ordering.
This helper performs a lightweight inspection of the first few bytes of a file to determine:
- whether a UTF-8 BOM is present
- whether a shebang (
#!) is present - whether the shebang appears immediately after a BOM (i.e. at byte offset 3), which is the problematic ordering for POSIX shebang recognition.
It is intentionally pure and does not mutate the processing context or
apply any policy decisions. Callers are responsible for updating
ProcessingContext flags (leading_bom, has_shebang) and
deciding whether a given BOM/shebang combination should be treated as a
policy violation for the current file type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first_bytes
|
bytes
|
The first bytes of the file being inspected. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
A tuple |
bool
|
|
bool
|
|
tuple[bool, bool, bool]
|
|