Source code for cadrumo.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__importannotationsfrompathlibimportPathfrom.....core.atomic_writeimportatomic_write_textfrom.....core.config_supportimportcoerce_output_language_settingfrom.....core.external_constantsimportUTF_8_ENCODINGfrom.....core.fsyncimportfsync_parent_dirfrom.....core.loggingimportget_loggerfrom..storage_path_definitionsimportBUCKET_OUTPUT_LANGUAGE_HINT_FILENAMEfrom.directory_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``."""ifisinstance(value,bool)ornotisinstance(value,str):returnNonenormalized=coerce_output_language_setting(value)returnnormalized.valueifnormalizedisnotNoneelseNone
[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",encoding=UTF_8_ENCODING)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