diff --git a/src/webqueue2api/parser/queue.py b/src/webqueue2api/parser/queue.py index 5276b47..36c22eb 100644 --- a/src/webqueue2api/parser/queue.py +++ b/src/webqueue2api/parser/queue.py @@ -32,13 +32,38 @@ def __init__(self, name: str, headers_only: bool = True) -> None: if not self.path.exists(): raise QueueDoesNotExistError(str(self.path)) - self.items = self.get_items(headers_only=headers_only) + self.items = self.__get_items(headers_only=headers_only) + self.json_data = self.__generate_json_data() - self.json_data = { - "name": self.name, - } + def __generate_json_data(self) -> dict: + """Generates a JSON serializable data dictionary of class attributes. - def get_items(self, headers_only: bool) -> list: + Example: + __generate_json_data() returns: + ... + + Returns: + dict: JSON serializable data dictionary of class attributes. + """ + json_data = {} + + # List of attributes that need processing before being added to json_data + attributes_to_process = ["path", "items"] + for attribute in attributes_to_process: + if attribute == "path": + json_data[attribute] = str(self.path) + if attribute == "items": + json_data[attribute] = [item.to_json() for item in self.items] + + # List of attributes to be ignored + attributes_to_ignore = ["to_json"] + attributes_to_process + for attribute in self.__dir__(): + if not attribute.startswith("_") and attribute not in attributes_to_ignore: + json_data[attribute] = self.__getattribute__(attribute) + + return json_data + + def __get_items(self, headers_only: bool) -> list: """Returns a list of items for this Queue Args: