
    `jD                    |    d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
  ej                  e      Z G d d      Zy)	z-Lookup helpers for ISTAT XML export mappings.    )annotationsN)COUNTRY_MAPPINGSFALLBACK_COUNTRY_MAPPING)PROVINCE_MAPPINGS)REGION_MAPPINGSc                  |    e Zd ZdZed	d       Zed
d       Zedd       Zedd       Z	edd       Z
edd       Zy)IstatLookupServicez5Centralized read access to static ISTAT XML mappings.c                F    | xs dj                         j                         S )N )stripupper)values    4/backend/istat/xml_export/services/lookup_service.py
_normalizezIstatLookupService._normalize   s    ""$**,,    c                J    t        j                  | j                  |            S )z;Return XML province metadata for a province sigla, or None.)r   getr   )clssiglas     r   get_provincezIstatLookupService.get_province   s     !$$S^^E%:;;r   c                0   | j                  |      }t        j                  |      }||S t        j                  |      }|Ut        j                  |      }|>t        j                  d|||j                  d      |||j                  d      d       |S y)u9  Return XML country metadata for an ISO-2 code, or None.

        Resolution order:
        1. Direct lookup in ``COUNTRY_MAPPINGS``.
        2. If not found, check ``FALLBACK_COUNTRY_MAPPING`` for a canonical
           ISO-2 alias, then resolve that alias against ``COUNTRY_MAPPINGS``.
        3. If still not found, return ``None`` so callers can raise a
           controlled error — unknown codes are never silently swallowed.

        When a fallback is used, a WARNING is emitted so the substitution is
        visible in production logs and auditable.
        NuJ   ISTAT country fallback applied: '%s' resolved via '%s' → istat_code='%s'
istat_code)original_iso_codefallback_iso_coder   )extra)r   r   r   r   loggerwarning)r   iso_code
normalizedresult	canonicals        r   get_countryzIstatLookupService.get_country   s     ^^H-
 "%%j1M -00<	 %)))4F!`JJ|,-7-6&,jj&>  
  r   c                6    | j                  |      }|r|d   S dS )z=Return the ISTAT XML country code for an ISO-2 code, or None.r   N)r"   )r   r   countrys      r   get_country_codez#IstatLookupService.get_country_codeE   s$     //(+(/w|$9T9r   c                    | j                  |      }|j                         r|j                  d      }t        j                  |      S )z?Return XML region metadata for an internal CODREGIONE, or None.   )r   isdigitzfillr   r   )r   cod_regioner   s      r   
get_regionzIstatLookupService.get_regionK   s?     ^^K0
#))!,J"":..r   c                t    | j                  |      t        fdt        j                         D        d      S )zFReturn XML region metadata for a final ISTAT XML region code, or None.c              3  2   K   | ]  }|d    k(  r|  yw)istat_region_codeN ).0regionr   s     r   	<genexpr>z>IstatLookupService.get_region_by_istat_code.<locals>.<genexpr>X   s'      6F-.*< 6s   N)r   nextr   values)r   r.   r   s     @r   get_region_by_istat_codez+IstatLookupService.get_region_by_istat_codeS   s<     ^^$56
-446
 
 	
r   N)r   
str | Nonereturnstr)r   r6   r7   dict[str, str] | None)r   r6   r7   r9   )r   r6   r7   r6   )r*   r6   r7   r9   )r.   r6   r7   r9   )__name__
__module____qualname____doc__staticmethodr   classmethodr   r"   r%   r+   r5   r/   r   r   r	   r	      s|    ?- - < < ' 'R : :
 / / 

 

r   r	   )r=   
__future__r   logging#istat.xml_export.mappings.countriesr   r   #istat.xml_export.mappings.provincesr   !istat.xml_export.mappings.regionsr   	getLoggerr:   r   r	   r/   r   r   <module>rF      s7    3 "  Z A = 
		8	$O
 O
r   