Abstract class for profile items like Maker, Tool, and Work.
This base class provides a unified way to access and update profile data, ensuring
that attribute access always returns the latest data from the configuration file.
Source code in src\omoospace\common.py
| class ProfileItem:
"""Abstract class for profile items like Maker, Tool, and Work.
This base class provides a unified way to access and update profile data, ensuring
that attribute access always returns the latest data from the configuration file.
"""
"""The name used in the profile dictionary (e.g., "makers", "tools", "works")."""
_dict_name: str
_item_name: str
def __init__(self, omoospace: "Omoospace", name: str) -> None:
self._item_name = name
self._omoospace = omoospace
# init item with name if not find in profile
item_dict = self._omoospace.get(self._dict_name) or {}
if self._item_name not in item_dict:
item_dict[self._item_name] = {}
self._omoospace.set(self._dict_name, item_dict)
def __repr__(self):
return self.name
def _key(self, key) -> str:
return key_dict[key][self._omoospace.language]
@property
def data(self):
# e.g. makers
item_dict = self._omoospace.get(self._dict_name)
if item_dict is None:
raise AttributeError(f"{self._dict_name} not found in profile.")
# e.g. maker
data = item_dict.get(self._item_name)
if data is None:
raise AttributeError(f"{self._item_name} not found in {self._dict_name}.")
return data
@data.setter
def data(self, value: Any):
"""Update the profile with current data."""
items = self._omoospace.get(self._dict_name) or {}
items[self._item_name] = value
self._omoospace.set(self._dict_name, items)
def get(self, key: str) -> Any:
"""Get the latest data for this item from the profile file."""
return self.data.get(self._key(key)) if isinstance(self.data, dict) else None
def set(self, key: str, value: Any):
"""Update the profile with current data."""
data = self.data if isinstance(self.data, dict) else {}
data[self._key(key)] = value
self.data = data
def remove(self):
"""Remove this item from the profile."""
item_dict = self._omoospace.get(self._dict_name)
if item_dict is None:
raise AttributeError(f"{self._dict_name} not found in profile.")
if self._item_name in item_dict:
del item_dict[self._item_name]
self._omoospace.set(self._dict_name, item_dict)
else:
raise AttributeError(f"{self._item_name} not found in {self._dict_name}.")
@property
def name(self) -> str:
"""Get the name from the latest profile data."""
return self._item_name
@name.setter
def name(self, value: str):
item_dict = self._omoospace.get(self._dict_name)
if self.name in item_dict:
del item_dict[self.name]
item_dict[value] = self.data
self._omoospace.set(self._dict_name, item_dict)
self._item_name = value
|
name: str
property
writable
Get the name from the latest profile data.
get(key)
Get the latest data for this item from the profile file.
Source code in src\omoospace\common.py
| def get(self, key: str) -> Any:
"""Get the latest data for this item from the profile file."""
return self.data.get(self._key(key)) if isinstance(self.data, dict) else None
|
remove()
Remove this item from the profile.
Source code in src\omoospace\common.py
| def remove(self):
"""Remove this item from the profile."""
item_dict = self._omoospace.get(self._dict_name)
if item_dict is None:
raise AttributeError(f"{self._dict_name} not found in profile.")
if self._item_name in item_dict:
del item_dict[self._item_name]
self._omoospace.set(self._dict_name, item_dict)
else:
raise AttributeError(f"{self._item_name} not found in {self._dict_name}.")
|
set(key, value)
Update the profile with current data.
Source code in src\omoospace\common.py
| def set(self, key: str, value: Any):
"""Update the profile with current data."""
data = self.data if isinstance(self.data, dict) else {}
data[self._key(key)] = value
self.data = data
|