71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
"""File for Table class."""
|
|
|
|
import json
|
|
|
|
from data.category import Category # pylint: disable=import-error
|
|
|
|
|
|
class Table:
|
|
"""Stores a table's information a list of categories."""
|
|
|
|
def __init__(self, name: str, message_id: int, category_map: dict) -> None:
|
|
self._name: str = name
|
|
self._message_id: int = message_id
|
|
self._categories_map: dict = category_map
|
|
|
|
def __str__(self) -> str:
|
|
return json.dumps(self.to_dict(), indent=4)
|
|
|
|
@classmethod
|
|
def from_dict(cls, table_name: str, data: dict):
|
|
"""Fills out table from passed dictionary."""
|
|
message_id = data["message_id"]
|
|
category_map = {}
|
|
for cat_name, cat in data["categories"].items():
|
|
category = Category.from_dict(cat_name, cat)
|
|
category_map[cat_name] = category
|
|
|
|
return cls(table_name, message_id, category_map)
|
|
|
|
@property
|
|
def categories(self) -> "list[Category]":
|
|
"""Returns list of categories."""
|
|
return self._categories_map.values()
|
|
|
|
@property
|
|
def message_id(self) -> int:
|
|
"""Getter for message id."""
|
|
return self._message_id
|
|
|
|
@message_id.setter
|
|
def message_id(self, m_id: int) -> None:
|
|
"""Setter for message id."""
|
|
self._message_id = m_id
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
"""Getter for name."""
|
|
return self._name
|
|
|
|
def add_category(self, category: Category) -> None:
|
|
"""Adds a category to dictionary of categories. Shouldn't already exist."""
|
|
if category.name not in self._categories_map:
|
|
self._categories_map[category.name] = category
|
|
|
|
def get_category(self, category_name: str) -> Category:
|
|
"""Retrieves category with the gven name."""
|
|
return self._categories_map[category_name]
|
|
|
|
def to_dict(self) -> dict:
|
|
"""
|
|
Saves table as a python dictionary.
|
|
Does not include table name. It should be the key of above data structure.
|
|
"""
|
|
data = {}
|
|
data["message_id"] = self.message_id
|
|
data["categories"] = {}
|
|
for category in self.categories:
|
|
data["categories"][category.name] = category.to_dict()
|
|
|
|
return data
|