Update Guild class to hold sql Guilds table information.

This commit is contained in:
alwayscompiling
2022-11-27 00:02:30 -05:00
parent 17867671f1
commit 017cb7c9df
+8 -196
View File
@@ -1,82 +1,20 @@
"""File for Guild class.""" """File for Guild class."""
import json
from classes.table import Table # pylint: disable=import-error
class Guild: class Guild:
"""Stores a guild's information.""" """Stores a guild's information. Used just to pass sql query information around."""
def __init__(self, guild_id: int) -> None: def __init__(self, guild_info: tuple) -> None:
""" """
Default constructor for Guild class. Initializes channel ids as 0 as they won't be Default constructor for Guild class. Populates class from sql query results. Tuple order is important.
registered yet. Sets default username length of 12. Default highscores size to 3.
Args: Args:
guild_id (int): Discord Guild id of new Guild guild_id (int): Discord Guild id of new Guild
""" """
self._category_pad_length: int = 26 self._id: int = guild_info[0]
self._guild_id: int = guild_id self._submission_channel_id: int = guild_info[1]
self._highscore_channel_id: int = 0 self._username_length: int = guild_info[2]
self._submission_channel_id: int = 0 self._verification_channel_id: int = guild_info[3]
self._table_map: dict = {}
self._username_length: int = 12
self._verification_channel_id: int = 0
def __str__(self) -> str:
"""
Returns Guild class as a string for printing.
Returns:
str: Guild class as a string
"""
return self.to_str()
@classmethod
def from_dict(cls, guild_id: int, data: dict):
"""
Creates a Guild class from the passed data.
Args:
guild_id (int): Guild's id
data (dict): Rest of Guild data in dictionary format
Returns:
Guild: Populated Guild object
"""
guild = cls(guild_id)
guild.category_pad_length = data["category_pad_length"]
guild.highscore_channel = data["highscore_channel_id"]
guild.submission_channel = data["submission_channel_id"]
guild.username_length = data["username_length"]
guild.verification_channel = data["verification_channel_id"]
for table_name, table_data in data["tables"].items():
table = Table.from_dict(table_name, table_data)
guild.add_table(table)
return guild
@property
def category_pad_length(self) -> int:
"""
Getter for Category Pad Length.
Returns:
int: Length to pad each category entry.
"""
return self._category_pad_length
@category_pad_length.setter
def category_pad_length(self, length: int) -> None:
"""
Setter for Category Pad Length.
Args:
length (int): New length to set.
"""
self._category_pad_length = length
@property @property
def guild_id(self) -> int: def guild_id(self) -> int:
@@ -86,27 +24,7 @@ class Guild:
Returns: Returns:
int: Guild id int: Guild id
""" """
return self._guild_id return self._id
@property
def highscore_channel(self) -> int:
"""
Getter for highscore channel id.
Returns:
int: highscore channel id
"""
return self._highscore_channel_id
@highscore_channel.setter
def highscore_channel(self, channel: int) -> None:
"""
Setter for highscore channel id.
Args:
channel (int): highscore channel id
"""
self._highscore_channel_id = channel
@property @property
def submission_channel(self) -> int: def submission_channel(self) -> int:
@@ -118,26 +36,6 @@ class Guild:
""" """
return self._submission_channel_id return self._submission_channel_id
@submission_channel.setter
def submission_channel(self, channel: id) -> None:
"""
Setter fro submission channel id.
Args:
channel (id): submission channel id
"""
self._submission_channel_id = channel
@property
def tables(self) -> "list[Table]":
"""
Getter for Tables list.
Returns:
list[Table]: List of Tables in this Guild
"""
return self._table_map.values()
@property @property
def username_length(self) -> int: def username_length(self) -> int:
""" """
@@ -148,16 +46,6 @@ class Guild:
""" """
return self._username_length return self._username_length
@username_length.setter
def username_length(self, length: int) -> None:
"""
Setter for username length.
Args:
length (int): username length
"""
self._username_length = length
@property @property
def verification_channel(self) -> int: def verification_channel(self) -> int:
""" """
@@ -167,79 +55,3 @@ class Guild:
int: verification channel id int: verification channel id
""" """
return self._verification_channel_id return self._verification_channel_id
@verification_channel.setter
def verification_channel(self, channel: int) -> None:
"""
Setter for verification channel id
Args:
channel (int): verification channel id
"""
self._verification_channel_id = channel
def add_table(self, table: Table) -> None:
"""
Adds a Table to table map. Shouldn't exist already.
Args:
table (Table): new Table to add
"""
if table.name not in self._table_map:
self._table_map[table.name] = table
def get_table(self, table_name: str) -> Table:
"""
Getter for specific Table. Returns None if Table is not found.
Args:
table_name (str): Name of Table to find
Returns:
Table: Table corresponding to Name. None if not found.
"""
if table_name in self._table_map:
return self._table_map[table_name]
return None
def get_tables(self) -> dict:
"""
Getter for dict of table names and tables.
Returns:
dict: Dictionary consisting of the Table Names and the Table object itself.
"""
# I don't know if this is const data. Don't want to allow data to be written as a result of this function.
return self._table_map
def to_dict(self) -> dict:
"""
Creates python dictionary object from class data.
Does not include guild_id in dict, it should be the key of above data structure.
Returns:
dict: Guild class in dictionary format
"""
data: dict = {}
data["category_pad_length"] = self.category_pad_length
data["highscore_channel_id"] = self.highscore_channel
data["submission_channel_id"] = self.submission_channel
data["tables"] = {}
data["username_length"] = self.username_length
data["verification_channel_id"] = self.verification_channel
for table in self.tables:
data["tables"][table.name] = table.to_dict()
return data
def to_str(self) -> str:
"""
Returns Guild object as a string.
Returns:
str: string of json representation of Guild
"""
return json.dumps(self.to_dict(), indent=4)