Skip to content

Commit

Permalink
Create utility function for checking valid item names
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Campbell committed Oct 30, 2020
1 parent f07895a commit 50a899f
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions api/ECNQueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@
queuesToIgnore = ["archives", "drafts", "inbox", "coral"]



#------------------------------------------------------------------------------#
# Utilities
#------------------------------------------------------------------------------#

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



#------------------------------------------------------------------------------#
# Classes
#------------------------------------------------------------------------------#
Expand Down Expand Up @@ -1187,8 +1210,6 @@ def __repr__(self) -> str:
return self.queue + str(self.number)

# TODO: Make Queue iterable using __iter__. See: https://thispointer.com/python-how-to-make-a-class-iterable-create-iterator-class-for-it/


class Queue:
"""A collection of items.
Expand Down Expand Up @@ -1225,10 +1246,7 @@ def __getItems(self) -> list:

isFile = True if os.path.isfile(itemPath) else False

itemPattern = re.compile("^[0123456789]{1,3}$")
isItem = True if itemPattern.match(item) else False

if isFile and isItem:
if isFile and isValidItemName(item):
items.append(Item(self.name, item))

return items
Expand All @@ -1252,6 +1270,9 @@ def toJson(self) -> dict:
def __len__(self) -> int:
return len(self.items)

def __repr__(self) -> str:
return f'{self.name}_queue'

def getValidQueues() -> list:
"""Returns a list of queues on the filesystem excluding ignored queues.
Expand Down Expand Up @@ -1284,7 +1305,4 @@ def loadQueues() -> list:
for queue in getValidQueues():
queues.append(Queue(queue))

return queues

if __name__ == "__main__":
print("Stop")
return queues

0 comments on commit 50a899f

Please sign in to comment.