Source code for aeat.adapters.persistence.storage.bucket._output_language_hint
"""Bucket-local last-known output-language hint.The encrypted user-profile record remains the authority for``preferences.output_language``. This sidecar carries only the lastsuccessfully persisted supported language code so critical storagefailures can render in the profile language when the bucket DEK cannotbe unwrapped."""from__future__importannotationsimportosimportsecretsfrompathlibimportPathfrom.....core.external_constantsimportSUPPORTED_OUTPUT_LANGUAGES,UTF_8_ENCODINGfrom.....core.locksimportfsync_parent_dirfrom.....core.loggingimportget_loggerfrom.._namespace_registryimportBUCKET_OUTPUT_LANGUAGE_HINT_FILENAMEfrom._layoutimportbucket_paths_log=get_logger(__name__)
[docs]defnormalize_output_language_hint(value:object)->str|None:"""Return ``value`` as a supported output-language code, or ``None``."""raw=str(value).strip().lower()ifrawinSUPPORTED_OUTPUT_LANGUAGES:returnrawreturnNone
[docs]defbucket_output_language_hint_path(*,storage_root:Path,bucket_id:str)->Path:"""Return the bucket-local output-language hint path."""returnbucket_paths(storage_root,bucket_id).bucket_dir/BUCKET_OUTPUT_LANGUAGE_HINT_FILENAME
[docs]defread_bucket_output_language_hint(*,storage_root:Path,bucket_id:str)->str|None:"""Read a supported bucket output-language hint, failing soft on absence or drift."""try:text=bucket_output_language_hint_path(storage_root=storage_root,bucket_id=bucket_id).read_text(encoding=UTF_8_ENCODING,)exceptExceptionasexc:_log.debug("bucket output-language hint unavailable bucket_id=%s error_type=%s",bucket_id,type(exc).__name__,exc_info=True,)returnNonereturnnormalize_output_language_hint(text)
[docs]defwrite_bucket_output_language_hint(*,storage_root:Path,bucket_id:str,language:object)->bool:"""Atomically persist a supported bucket output-language hint. Returns ``True`` when a hint was written and ``False`` when ``language`` is unsupported. Unsupported values never reach disk. """normalized=normalize_output_language_hint(language)ifnormalizedisNone:returnFalsetarget=bucket_output_language_hint_path(storage_root=storage_root,bucket_id=bucket_id)_atomic_write_text(target,normalized+"\n")returnTrue
[docs]defclear_bucket_output_language_hint(*,storage_root:Path,bucket_id:str)->None:"""Remove the bucket output-language hint if it exists."""target=bucket_output_language_hint_path(storage_root=storage_root,bucket_id=bucket_id)try:target.unlink()fsync_parent_dir(target)exceptFileNotFoundError:return
def_atomic_write_text(target:Path,text:str)->None:target.parent.mkdir(parents=True,exist_ok=True)tmp=target.with_name(f"{target.name}.{os.getpid()}.{secrets.token_hex(4)}.tmp")try:withopen(tmp,"w",encoding=UTF_8_ENCODING)ashandle:handle.write(text)handle.flush()os.fsync(handle.fileno())os.replace(tmp,target)fsync_parent_dir(target)exceptBaseException:_log.error("bucket output-language hint write failed target=%s",target,exc_info=True)try:tmp.unlink()exceptOSError:_log.debug("bucket output-language hint temp cleanup failed",exc_info=True)raise__all__=["bucket_output_language_hint_path","clear_bucket_output_language_hint","normalize_output_language_hint","read_bucket_output_language_hint","write_bucket_output_language_hint",]