Source code for cadrumo.domain.calculations.registry.modelo_obligation_scope
"""Registry-backed product-scope partitions for Modelo obligations.The ``modelo-obligation-scope-mapping`` governed fact owns the distinctionbetween recognized obligations that have no registry definition and thesubset deliberately outside the product's filing scope. This module is thedomain service that resolves that declaration through the published authority.The core ``Modelo`` type remains a syntax/value primitive and does not loadthe registry merely to construct an identifier."""from__future__importannotationsfromcollections.abcimportIterator,Mapping,Setfromdatetimeimportdatefromtypingimportoverridefrom....core.errors.hierarchyimportCoreValidationErrorfrom....core.modeloimportModelofrom....core.time.clockimporttoday_madridfrom.facts.resolutionimportMappingFactQuery,ResolvedMappingFactfrom.governed_fact_scopeimportGovernedFactSource,governed_facts_in_scopefrom.schema_baseimportDateAxis__all__=["NON_REGISTRY_MODELOS","OUT_OF_SCOPE_OBLIGATIONS","UNMODELED_OBLIGATIONS","resolve_modelo_obligation_scope",]def_csv(value:str)->tuple[str,...]:values=tuple(token.strip()fortokeninvalue.split(",")iftoken.strip())ifnotvalues:raiseCoreValidationError("Modelo catalogue declaration must not be empty")returnvalues
[docs]defresolve_modelo_obligation_scope(*,effective_date:date|None=None,authority:GovernedFactSource|None=None,)->tuple[Mapping[Modelo,str],frozenset[Modelo]]:"""Resolve the current Modelo obligation-scope partitions. The authority owns publication identity and cache invalidation. Resolving on each view access therefore keeps the partitions aligned with the current published artifact instead of introducing a second process-lifetime cache in a value-type module. """selected_authority=authorityorgoverned_facts_in_scope()ifselected_authorityisNone:raiseCoreValidationError("Modelo obligation scope requires an explicit authority operation or scope")resolved=selected_authority.resolve_governed_fact(MappingFactQuery(fact_id="modelo-obligation-scope-mapping",date_axis=DateAxis.FILING_PERIOD,effective_date=effective_dateortoday_madrid(),),)ifnotisinstance(resolved,ResolvedMappingFact):raiseCoreValidationError("Modelo obligation scope did not resolve as a mapping")declarations={str(entry.key):str(entry.value)forentryinresolved.payload.entries}catalogue=frozenset(_csv(declarations["catalogue.codes"]))suppressed=frozenset(_csv(declarations["scope.suppressed.codes"]))registry_out=frozenset(_csv(declarations["scope.registry_out_of_scope.codes"]))reasons:dict[str,str]={}groups:dict[str,dict[str,str]]={}forkey,valueindeclarations.items():ifkey.startswith("scope.code.")andkey.endswith(".reason"):reasons[key.removeprefix("scope.code.").removesuffix(".reason")]=valueelifkey.startswith("scope.group."):_,_,group,field=key.split(".",3)groups.setdefault(group,{})[field]=valueforgroup,fieldsingroups.items():reason=fields.get("reason","").strip()ifnotreason:raiseCoreValidationError(f"Modelo scope group {group!r} has no reason")forcodein_csv(fields.get("codes","")):ifcodeinreasonsandreasons[code]!=reason:raiseCoreValidationError(f"Modelo scope code {code!r} has conflicting reasons")reasons[code]=reasonifnotset(reasons).issubset(catalogue)ornotsuppressed.issubset(reasons)ornotregistry_out.issubset(reasons):raiseCoreValidationError("Modelo scope partitions disagree with the published catalogue")out_of_scope={Modelo(code):reasonforcode,reasoninreasons.items()ifcodenotinsuppressed}non_registry=frozenset(Modelo(code)forcodeinreasonsifcodenotinregistry_out)returnout_of_scope,non_registry