import types class message: def __init__(self, *args, **kwargs) -> None: if "clone_it" in kwargs: self.__from_clone(kwargs["clone_it"]) return self.__from_dicts(args[0], args[1]) def __from_clone(self, clone_it: object) -> None: self.__sections = clone_it.sections self.__default = clone_it.default def __from_dicts(self, default: dict, sections: dict) -> None: sections_proxy = dict() for key, value in sections.items(): sections_proxy[key] = types.MappingProxyType(value) self.__sections = types.MappingProxyType(sections_proxy) self.__default = types.MappingProxyType(default) def get_key(self, key: str, section: str | None = None) -> str: if section is None: return self.get_from_default(key) return self.get_from_section(section, key) def key_exists(self, key: str, section: str | None = None) -> bool: if section is None: return key in self.default if not section in self.sections: return False return key in self.sections[section] def get_from_default(self, key: str) -> str: if not key in self.__default: raise KeyError("Not found key " + key + " in message.") return self.__default[key] def get_from_section(self, section: str, key: str) -> str: if not section in self.__sections: raise KeyError("Not found section " + section + " in message.") if not key in self.__sections[section]: raise KeyError( \ "Not found key " \ + key \ + " in section " \ + section \ + " of message." \ ) return self.__sections[section][key] @property def default(self) -> types.MappingProxyType: return self.__default @property def sections(self) -> types.MappingProxyType: return self.__sections