|
| 1 | +from collections import OrderedDict |
| 2 | +from typing import Dict, Optional |
| 3 | + |
| 4 | +from ._json import JsonViewer |
| 5 | + |
| 6 | + |
| 7 | +class Lang(JsonViewer): |
| 8 | + """ |
| 9 | + The Lang class is responsible for managing messages for a specific language. |
| 10 | + It supports organizing messages by categories and provides methods to insert, |
| 11 | + delete, and retrieve messages both within and outside of categories. |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self, lang_code: str) -> None: |
| 15 | + """ |
| 16 | + Initializes the Lang class with a language code and empty structures for storing messages. |
| 17 | +
|
| 18 | + Attributes: |
| 19 | + lang_code (str): The language code representing this Lang object. |
| 20 | + categories (Dict[str, Dict[str, str]]): A nested dictionary to store messages organized by category. |
| 21 | + messages (OrderedDict): A dictionary to store uncategorized messages. |
| 22 | + """ |
| 23 | + self.lang_code = lang_code |
| 24 | + self.categories: Dict[str, Dict[str, str]] = OrderedDict() |
| 25 | + self.messages = OrderedDict() |
| 26 | + |
| 27 | + def insert(self, k: str, v: str, category: Optional[str] = None) -> None: |
| 28 | + """ |
| 29 | + Inserts a message into the language, either into a specified category or into the general message store. |
| 30 | +
|
| 31 | + Args: |
| 32 | + k (str): The key or identifier for the message. |
| 33 | + v (str): The actual message text. |
| 34 | + category (Optional[str]): The category under which the message will be stored. |
| 35 | + If None, the message is stored without a category. |
| 36 | + """ |
| 37 | + if category is not None: |
| 38 | + return self.insert_to_category(category=category, k=k, v=v) |
| 39 | + |
| 40 | + return self.messages.update({k: v}) |
| 41 | + |
| 42 | + def delete(self, k: str, category: Optional[str] = None) -> None: |
| 43 | + """ |
| 44 | + Deletes a message from the language, either from a specified category or from the general message store. |
| 45 | +
|
| 46 | + Args: |
| 47 | + k (str): The key or identifier for the message to delete. |
| 48 | + category (Optional[str]): The category from which the message will be deleted. |
| 49 | + If None, the message is deleted from the general store. |
| 50 | + """ |
| 51 | + if category is not None: |
| 52 | + return self.delete_from_category(category=category, k=k) |
| 53 | + |
| 54 | + del self.messages[k] |
| 55 | + |
| 56 | + def get(self, k: str, category: Optional[str] = None) -> Optional[str]: |
| 57 | + """ |
| 58 | + Retrieves a message from the language, either from a specified category or from the general message store. |
| 59 | +
|
| 60 | + Args: |
| 61 | + k (str): The key or identifier for the message. |
| 62 | + category (Optional[str]): The category from which to retrieve the message. |
| 63 | + If None, the message is retrieved from the general store. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + Optional[str]: The message text if found, otherwise None. |
| 67 | + """ |
| 68 | + if category is not None: |
| 69 | + return self.get_from_category(category=category, k=k) |
| 70 | + |
| 71 | + return self.messages.get(k, None) |
| 72 | + |
| 73 | + def get_from_category(self, category: str, k: str) -> Optional[str]: |
| 74 | + """ |
| 75 | + Retrieves a message from a specified category. |
| 76 | +
|
| 77 | + Args: |
| 78 | + category (str): The category from which to retrieve the message. |
| 79 | + k (str): The key or identifier for the message. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + Optional[str]: The message text if found, otherwise None. |
| 83 | + """ |
| 84 | + return self.categories.get(category).get(k) |
| 85 | + |
| 86 | + def insert_to_category(self, category: str, k: str, v: str) -> None: |
| 87 | + """ |
| 88 | + Inserts a message into a specific category. |
| 89 | +
|
| 90 | + Args: |
| 91 | + category (str): The category under which to store the message. |
| 92 | + k (str): The key or identifier for the message. |
| 93 | + v (str): The actual message text. |
| 94 | + """ |
| 95 | + if category not in self.categories: |
| 96 | + self.categories.update({category: {}}) |
| 97 | + |
| 98 | + return self.categories[category].update({k: v}) |
| 99 | + |
| 100 | + def delete_from_category(self, category: str, k: str) -> None: |
| 101 | + """ |
| 102 | + Deletes a message from a specific category. |
| 103 | +
|
| 104 | + Args: |
| 105 | + category (str): The category from which to delete the message. |
| 106 | + k (str): The key or identifier for the message. |
| 107 | + """ |
| 108 | + del self.categories[category][k] |
0 commit comments