def __init__(self, name, ownerUID, theUID=None, spreadsheetPath=None): # Make sure owner has a valid UUID, converting a hex text to a # UUID object, if needed. validOwnerUID = sobj.getValidUUID(ownerUID) # If we have a valid UUID... if validOwnerUID is not None: # Set the owner (User) UID. self.ownerUID = validOwnerUID # If a UUID was passed in... if theUID is not None: # Make sure the argument is a valid UUID, converting a hex text to a # UUID object, if needed. validUID = sobj.getValidUUID(theUID) # If a validUID was found, use it. if validUID is not None: self.uid = validUID # Otherwise, generate a new random UUID using uuid4(). else: self.uid = uuid.uuid4() # Otherwise, generate a new random UUID using uuid4(). else: self.uid = uuid.uuid4() # Set the project name. self.name = name # Set the spreadsheetPath. self.spreadsheetPath = spreadsheetPath # Set the creation time for now. self.createdTime = now_utc() # Set the updating time to None. self.updatedTime = None # Set the spreadsheet upload time to None. self.dataUploadTime = None # If we have passed in a spreadsheet path... if self.spreadsheetPath is not None: # Set the data spreadsheet upload time for now. self.dataUploadTime = now_utc() # Set the type prefix to 'user'. self.typePrefix = 'project' # Set the file suffix to '.usr'. self.fileSuffix = '.prj' # Set the instance label to the username. self.instanceLabel = name
def delete(self, theUID, saveHandleChanges=True): # Make sure the argument is a valid UUID, converting a hex text to a # UUID object, if needed. validUID = sobj.getValidUUID(theUID) # If we have a valid UUID... if validUID is not None: # Get the handle (if any) matching the UID. theHandle = self.getHandleByUID(validUID) # If we found a matching handle... if theHandle is not None: # If we are using Redis... if self.dbMode == 'redis': # Delete the key using the handle. theHandle.redisDelete(self.redisDb) # Otherwise (we are using files)... else: # Delete the file using the handle. theHandle.fileDelete('.') # Delete the handle from the dictionary. del self.handleDict[validUID] # Do a save of the database so change is kept. if saveHandleChanges: self.save()
def add(self, theObject, theUID, theTypeLabel='obj', theFileSuffix='.obj', theInstanceLabel='', saveHandleChanges=True): # Make sure the argument is a valid UUID, converting a hex text to a # UUID object, if needed. validUID = sobj.getValidUUID(theUID) # If we have a valid UUID... if validUID is not None: # Create the new StoreObjectHandle. newHandle = StoreObjectHandle(validUID, theTypeLabel, theFileSuffix, theInstanceLabel) # Add the handle to the dictionary. self.handleDict[validUID] = newHandle # If we are using Redis... if self.dbMode == 'redis': # Put the object in Redis. newHandle.redisStore(theObject, self.redisDb) # Otherwise (we are using files)... else: # Put the object in a file. newHandle.fileStore('.', theObject) # Do a save of the database so change is kept. if saveHandleChanges: self.save()
def getHandleByUID(self, theUID): # Make sure the argument is a valid UUID, converting a hex text to a # UUID object, if needed. validUID = sobj.getValidUUID(theUID) # If we have a valid UUID... if validUID is not None: return self.handleDict.get(validUID, None) else: return None
def getProjectEntriesByUser(self, ownerUID): # Make sure the argument is a valid UUID, converting a hex text to a # UUID object, if needed. validUID = sobj.getValidUUID(ownerUID) # If we have a valid UUID... if validUID is not None: # Get ProjectSO entries for each Project in the dictionary. projectEntries = [self.theObjectDict[theKey] \ for theKey in self.theObjectDict \ if self.theObjectDict[theKey].ownerUID == validUID] return projectEntries # Otherwise, return an empty list. else: return []
def update(self, theUID, theObject): # Make sure the argument is a valid UUID, converting a hex text to a # UUID object, if needed. validUID = sobj.getValidUUID(theUID) # If we have a valid UUID... if validUID is not None: # Get the handle (if any) matching the UID. theHandle = self.getHandleByUID(validUID) # If we found a matching handle... if theHandle is not None: # If we are using Redis... if self.dbMode == 'redis': # Overwrite the old copy of the object using the handle. theHandle.redisStore(theObject, self.redisDb) # Otherwise (we are using files)... else: # Overwrite the old copy of the object using the handle. theHandle.fileStore('.', theObject)
def deleteByUID(self, theUID): # Make sure the argument is a valid UUID, converting a hex text to a # UUID object, if needed. validUID = sobj.getValidUUID(theUID) # If we have a valid UUID... if validUID is not None: # Get the object pointed to by the UID. theObject = self.theObjectDict[validUID] # If a match is found... if theObject is not None: # Remove entries from both theUserDict and usernameHashes # attributes. theUsername = theObject.username del self.theObjectDict[validUID] del self.usernameHashes[theUsername] # Update our DataStore representation if we are there. if self.inDataStore(): self.updateDataStore()
def retrieve(self, theUID): # Make sure the argument is a valid UUID, converting a hex text to a # UUID object, if needed. validUID = sobj.getValidUUID(theUID) # If we have a valid UUID... if validUID is not None: # Get the handle (if any) matching the UID. theHandle = self.getHandleByUID(validUID) # If we found a matching handle... if theHandle is not None: # If we are using Redis... if self.dbMode == 'redis': # Return the object pointed to by the handle. return theHandle.redisRetrieve(self.redisDb) # Otherwise (we are using files)... else: # Return the object pointed to by the handle. return theHandle.fileRetrieve('.') # Return None (a failure to find a match). return None