Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add YAML support for storage. #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions pylangs/_lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_from_category(self, category: str, k: str) -> Optional[str]:
Returns:
Optional[str]: The message text if found, otherwise None.
"""
return self.categories.get(category).get(k)
return self.categories.get(category, {}).get(k)

def insert_to_category(self, category: str, k: str, v: str) -> None:
"""
Expand All @@ -93,9 +93,9 @@ def insert_to_category(self, category: str, k: str, v: str) -> None:
v (str): The actual message text.
"""
if category not in self.categories:
self.categories.update({category: {}})
self.categories[category] = {}

return self.categories[category].update({k: v})
self.categories[category][k] = v

def delete_from_category(self, category: str, k: str) -> None:
"""
Expand All @@ -105,4 +105,11 @@ def delete_from_category(self, category: str, k: str) -> None:
category (str): The category from which to delete the message.
k (str): The key or identifier for the message.
"""
del self.categories[category][k]
if category in self.categories:
del self.categories[category][k]

def to_dict(self) -> Dict:
return {
'messages': dict(self.messages),
'categories': {cat: dict(msgs) for cat, msgs in self.categories.items()}
}
52 changes: 38 additions & 14 deletions pylangs/_langs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from collections import OrderedDict
from ._lang import Lang
from ._json import JsonViewer

from typing import Dict, Optional

import yaml

class Langs(JsonViewer):
"""
Expand All @@ -23,9 +22,7 @@ def __init__(self) -> None:
"""
self.langs: Dict[str, Lang] = OrderedDict()

def insert(
self, lang_code: str, k: str, v: str, category: Optional[str] = None
) -> None:
def insert(self, lang_code: str, k: str, v: str, category: Optional[str] = None) -> None:
"""
Inserts a message into the specified language and category.

Expand All @@ -34,11 +31,9 @@ def insert(
k (str): The key or identifier for the message.
v (str): The actual message text.
category (Optional[str]): The category under which the message falls (e.g., 'GENERAL', 'SUPPORT').
If None, it will be stored without a category.
"""
lang = self.__get_lang(lang_code)

return lang.insert(k=k, v=v, category=category)
lang.insert(k=k, v=v, category=category)

def delete(self, lang_code: str, k: str, category: Optional[str] = None) -> None:
"""
Expand All @@ -51,12 +46,9 @@ def delete(self, lang_code: str, k: str, category: Optional[str] = None) -> None
be deleted without considering a specific category.
"""
lang = self.__get_lang(lang_code)
lang.delete(k=k, category=category)

return lang.delete(k=k, category=category)

def get(
self, lang_code: str, k: str, category: Optional[str] = None
) -> Optional[str]:
def get(self, lang_code: str, k: str, category: Optional[str] = None) -> Optional[str]:
"""
Retrieves a message from the specified language and category.

Expand All @@ -67,9 +59,41 @@ def get(
be retrieved without considering a specific category.
"""
lang = self.__get_lang(lang_code)

return lang.get(k=k, category=category)

def load_from_yaml(self, file_path: str) -> None:
"""
Loads messages from a YAML file into the Langs object.

Args:
file_path (str): The path to the YAML file to load into the Langs object.
"""
with open(file_path, 'r', encoding='utf-8') as file:
data = yaml.safe_load(file)

for lang_code, messages in data.items():
lang = self.__get_lang(lang_code)
for category, items in messages.items():
if isinstance(items, dict):
for k, v in items.items():
lang.insert(k, v, category=category)
else:
lang.insert(category, items)

def save(self, file_path: str) -> None:
"""
Saves the current language data into a YAML file.

Args:
file_path (str): The file path where the YAML data will be saved.
"""
data = {}
for lang_code, lang_obj in self.langs.items():
data[lang_code] = lang_obj.to_dict()

with open(file_path, 'w', encoding='utf-8') as yaml_file:
yaml.dump(data, yaml_file, allow_unicode=True)

def __get_lang(self, lang_code: str) -> Lang:
if lang_code.lower() in self.langs:
return self.langs.get(lang_code.lower())
Expand Down