def get_active_msgids(self, msgid_or_symbol: str) -> List[str]: """Return msgids but the input can be a symbol.""" # Only msgid can have a digit as second letter is_msgid = msgid_or_symbol[1:].isdigit() if is_msgid: msgid = msgid_or_symbol.upper() symbol = self.__msgid_to_symbol.get(msgid) else: msgid = self.__symbol_to_msgid.get(msgid_or_symbol) symbol = msgid_or_symbol if not msgid or not symbol: error_msg = f"No such message id or symbol '{msgid_or_symbol}'." raise UnknownMessageError(error_msg) return self.__old_names.get(msgid, [msgid])
def check_message_id(self, msgid): """returns the Message object for this message. msgid may be either a numeric or symbolic id. Raises UnknownMessageError if the message id is not defined. """ if msgid[1:].isdigit(): msgid = msgid.upper() for source in (self._alternative_names, self._messages): try: return source[msgid] except KeyError: pass raise UnknownMessageError('No such message id %s' % msgid)
def get_message_definitions(self, msgid_or_symbol: str) -> list: """Returns the Message object for this message. :param str msgid_or_symbol: msgid_or_symbol may be either a numeric or symbolic id. :raises UnknownMessageError: if the message id is not defined. :rtype: List of MessageDefinition :return: A message definition corresponding to msgid_or_symbol """ if msgid_or_symbol[1:].isdigit(): msgid_or_symbol = msgid_or_symbol.upper() for source in (self._alternative_names, self._messages_definitions): try: return [source[msgid_or_symbol]] except KeyError: pass error_msg = "No such message id or symbol '{msgid_or_symbol}'.".format( msgid_or_symbol=msgid_or_symbol) raise UnknownMessageError(error_msg)
def get_active_msgids(self, msgid_or_symbol: str) -> List[str]: """Return msgids but the input can be a symbol. The cache has no limit as its size will likely stay minimal. For each message we store about 1000 characters, so even if we would have 1000 messages the cache would only take up ~= 1 Mb. """ msgid: Optional[str] if msgid_or_symbol[1:].isdigit(): # Only msgid can have a digit as second letter msgid = msgid_or_symbol.upper() symbol = self.__msgid_to_symbol.get(msgid) else: msgid = self.__symbol_to_msgid.get(msgid_or_symbol) symbol = msgid_or_symbol if not msgid or not symbol: error_msg = f"No such message id or symbol '{msgid_or_symbol}'." raise UnknownMessageError(error_msg) return self.__old_names.get(msgid, [msgid])
def get_active_msgids(self, msgid_or_symbol: str) -> list[str]: """Return msgids but the input can be a symbol. self.__active_msgids is used to implement a primitive cache for this function. """ try: return self.__active_msgids[msgid_or_symbol] except KeyError: pass # If we don't have a cached value yet we compute it msgid: str | None deletion_reason = None moved_reason = None if msgid_or_symbol[1:].isdigit(): # Only msgid can have a digit as second letter msgid = msgid_or_symbol.upper() symbol = self.__msgid_to_symbol.get(msgid) if not symbol: deletion_reason = is_deleted_msgid(msgid) if deletion_reason is None: moved_reason = is_moved_msgid(msgid) else: symbol = msgid_or_symbol msgid = self.__symbol_to_msgid.get(msgid_or_symbol) if not msgid: deletion_reason = is_deleted_symbol(symbol) if deletion_reason is None: moved_reason = is_moved_symbol(symbol) if not msgid or not symbol: if deletion_reason is not None: raise DeletedMessageError(msgid_or_symbol, deletion_reason) if moved_reason is not None: raise MessageBecameExtensionError(msgid_or_symbol, moved_reason) error_msg = f"No such message id or symbol '{msgid_or_symbol}'." raise UnknownMessageError(error_msg) ids = self.__old_names.get(msgid, [msgid]) # Add to cache self.__active_msgids[msgid_or_symbol] = ids return ids
def get_msgid(self, symbol: str) -> str: try: return self.__symbol_to_msgid[symbol] except KeyError as e: msg = f"'{symbol}' is not stored in the message store." raise UnknownMessageError(msg) from e
def get_symbol(self, msgid: str) -> str: try: return self.__msgid_to_symbol[msgid] except KeyError as e: msg = f"'{msgid}' is not stored in the message store." raise UnknownMessageError(msg) from e