Source code for aeat.application.workflow._engine_helpers
"""Private helpers for workflow-engine formatting and deadline metadata."""from__future__importannotationsfromdatetimeimportdatefromenumimportStrEnumfromtypingimportLiteralfrom...coreimportPeriodCertificateSeverityValue=Literal["OK","WARN","CRITICAL","EXPIRED"]
[docs]classDeadlineRole(StrEnum):"""Role of a deadline within workflow step metadata."""INFORMATIONAL="informational"BINDING="binding"
[docs]classFilingWindowState(StrEnum):"""State of the filing window for a given (modelo, period) at workflow time."""ABSENT="absent"FUTURE="future"OPEN="open"CLOSED="closed"
[docs]defregistry_period_token(period:Period)->tuple[int,str]:"""Resolve a :class:`~core.Period` to ``(filing_year, registry_period)``."""returnperiod.year,period.registry_token
[docs]defregistry_filing_year(period:Period)->int:"""Return the filing year from a typed :class:`~core.Period`."""returnperiod.year
[docs]defclassify_cert_expiry(*,not_after:date,today:date,warn_days:int,critical_days:int,)->tuple[CertificateSeverityValue,int]:"""Classify a certificate's expiry window against operator thresholds."""days_until_expiry=(not_after-today).daysifdays_until_expiry<=0:return("EXPIRED",days_until_expiry)ifdays_until_expiry<=critical_days:return("CRITICAL",days_until_expiry)ifdays_until_expiry<=warn_days:return("WARN",days_until_expiry)return("OK",days_until_expiry)
[docs]defsummary_text(en:str)->str:"""Build a workflow summary string."""returnen
[docs]defenum_value(value:object)->str:"""Return ``Enum.value`` when present, otherwise ``str(value)``."""ifvalueisNone:return""raw=getattr(value,"value",value)returnstr(raw)
[docs]defdraft_blocking_finding_descriptions(draft:object)->tuple[str,...]:"""Summarise the ERROR/WARNING findings that keep a built draft out of the ready state. The ``BUILDING_DRAFT`` gate aborts with ``DRAFT_HAS_ERRORS`` whenever the builder hands back a draft still below ``LISTO_PARA_PRESENTAR``. Without the underlying findings the abort is opaque (operators see only ``status=BORRADOR``), so this projects each blocking finding to a compact ``severity:code (casilla)`` description the abort step can surface. """findings=getattr(draft,"findings",())or()descriptions:list[str]=[]forfindinginfindings:severity=enum_value(getattr(finding,"severity",None))ifseveritynotin{"error","warning"}:continuecode=enum_value(getattr(finding,"code",None))or"unknown"casilla=enum_value(getattr(finding,"casilla_id",None))descriptions.append(f"{severity}:{code}"+(f" ({casilla})"ifcasillaelse""))returntuple(descriptions)