cadrumo.adapters.inbound.pdf.label_regex module

Shared label-anchored regex and decimal parsing primitives.

The casilla-complete parser under adapters.inbound.declaracion owns target dispatch, page provenance, and missing, malformed, and ambiguous result classification. This module supplies only the reusable regex fragments and printed-decimal parser those extraction paths compose.

The Spanish amount capture group SPANISH_AMOUNT_GROUP is the canonical AEAT printed-amount format. Extractor modules import it and compose per-casilla patterns on top of it.

SPANISH_AMOUNT_GROUP

Capture group for AEAT-printed monetary amounts (Spanish locale).

Matches optional sign, 1-3 leading digits, zero or more groups of <thousands-separator><3 digits> where the separator is . (ASCII full stop), U+00A0 NBSP, or U+202F narrow NBSP, and a mandatory ,<2 digits> decimal tail. The separator class deliberately excludes ASCII space and tab so the regex cannot cross AEAT column-separator whitespace.

TEXT_VALUE_GROUP

Capture group for the last whitespace-delimited token on a line.

pdfplumber collapses AEAT’s column-separator whitespace to single spaces, so the “value-to-the-right-of-the-label” invariant reduces to “the final token on the line”. Multi-token textual values (e.g. "La Rioja") need a richer bbox-anchored primitive instead.

PRESENTADOR_NIF_LABEL

Non-capturing alternation of AEAT’s Spanish and English presentador-NIF labels.

Matches NIF, NIF Presentador, and the English-render Tax identification number(NIF)of filer (with or without the trailing of filer, and tolerant of pdfplumber’s missing separators around the parenthetical). Carries no capture group and no tax-id value class, so a caller composes it with its own value pattern and separator expectations.

MODELO_LABEL

Non-capturing alternation of AEAT’s Spanish and English form-code labels.

EJERCICIO_LABEL

Non-capturing alternation of AEAT’s Spanish and English tax-year labels.

parse_spanish_decimal(raw)[source]

Parse an AEAT-formatted decimal string into a decimal.Decimal.

Accepts the canonical Spanish form 1.234,56, whitespace-separated thousands 1 234,56 (any unicode whitespace including U+00A0 NBSP and U+202F narrow NBSP), and US-style 1234.56. The parser is intentionally more permissive than the regex capture in SPANISH_AMOUNT_GROUP so it can recover values from messy input that has already been delimited by an upstream label match.

Parameters:

raw (str) – Raw numeric substring captured from the PDF.

Return type:

Decimal | None

Returns:

The parsed value, or None if raw is empty, a bare sign, or otherwise unparseable.