Skip to content

Commit

Permalink
Automate generation of json_data in Queue module
Browse files Browse the repository at this point in the history
  • Loading branch information
campb303 committed Jun 21, 2021
1 parent 83be4a2 commit 3a87891
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/webqueue2api/parser/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3a87891

Please sign in to comment.