-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Queue module in webqueue2api package
- Loading branch information
Showing
2 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import os, re | ||
| from .Item import Item | ||
|
|
||
| def isValidItemName(name: str) -> bool: | ||
| """Returns true if file name is a valid item name | ||
| Example: | ||
| isValidItemName("21") -> true | ||
| isValidItemName("twentyone") -> false | ||
| Args: | ||
| name (str): The name to test. | ||
| Returns: | ||
| bool: Name is valid item name. | ||
| """ | ||
| itemPattern = re.compile("^[0123456789]{1,3}$") | ||
| return True if itemPattern.match(name) else False | ||
|
|
||
| queueDirectory = "/home/pier/e/queue/Mail" | ||
|
|
||
| class Queue: | ||
| """A collection of items. | ||
| Example: | ||
| # Create a queue (ce) | ||
| >>> queue = Queue("ce") | ||
| Attributes: | ||
| name: The name of the queue. | ||
| items: A list of Items in the queue. | ||
| jsonData: A JSON serializable representation of the Queue. | ||
| """ | ||
|
|
||
| def __init__(self, name: str) -> None: | ||
| self.name = name | ||
| self.__directory = queueDirectory + "/" + self.name + "/" | ||
| self.items = self.__getItems() | ||
|
|
||
| self.jsonData = { | ||
| "name": self.name, | ||
| "length": len(self) | ||
| } | ||
|
|
||
| def __getItems(self) -> list: | ||
| """Returns a list of items for this Queue | ||
| Returns: | ||
| list: a list of items for this Queue | ||
| """ | ||
| items = [] | ||
|
|
||
| for item in os.listdir(self.__directory): | ||
| itemPath = self.__directory + "/" + item | ||
|
|
||
| isFile = True if os.path.isfile(itemPath) else False | ||
|
|
||
| if isFile and isValidItemName(item): | ||
| items.append(Item(self.name, item)) | ||
|
|
||
| return items | ||
|
|
||
| def toJson(self) -> dict: | ||
| """Return JSON safe representation of the Queue | ||
| The JSON representation of every item in the Queue is added to the | ||
| Queue's JSON data then the Queue's JSON data is returned. | ||
| Returns: | ||
| dict: JSON safe representation of the Queue | ||
| """ | ||
| items = [] | ||
| for item in self.items: | ||
| items.append(item.toJson()) | ||
| self.jsonData["items"] = items | ||
|
|
||
| return self.jsonData | ||
|
|
||
| def __len__(self) -> int: | ||
| return len(self.items) | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f'{self.name}_queue' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| from webqueue2api.Item import Item | ||
| from webqueue2api.Item import Item | ||
| from webqueue2api.Queue import Queue |