def test_change(self): # Create an observable dict observable_dict = ObservableDict({"good": "bye"}) # Subscribe to change events subscription = observable_dict.subscribe("change", self.callback) # Change an item in it observable_dict["good"] = "morning" # Ensure this triggered the callback self.assertEqual(("good", "morning"), self.callback_result)
def test_remove(self): # Create an observable dict observable_dict = ObservableDict({"good": "bye"}) # Subscribe to remove events subscription = observable_dict.subscribe("remove", self.callback) # Remove an item from it del observable_dict["good"] # Ensure this triggered the callback self.assertEqual(("good", "bye"), self.callback_result)
def test_add(self): # Create an observable dict observable_dict = ObservableDict() # Subscribe to add events subscription = observable_dict.subscribe("add", self.callback) # Add an item to it observable_dict["foo"] = "bar" # Ensure this triggered the callback self.assertEqual(("foo", "bar"), self.callback_result)
def test_propagation(self): class Alerter(SimpleObservable): def alert(self): self._notify(None, "alert", "Alert!") # Create an observable dict that contains other observables observable_dict = ObservableDict({ "this": Alerter(), "that": Alerter() }) # Subscribe to all events subscription = observable_dict.subscribe("__all__", self.callback) # Trigger an alert on something in the dict observable_dict["that"].alert() # Ensure this triggered the callback self.assertEqual((("that", None), "alert", "Alert!"), self.callback_result)
class archiveStore(object): def __init__(self): self.collection = ObservableDict() self.collection.addCbPost(self,self.onachiveColection) def addCollection(self,archive): if not isinstance(archive,archiveCollection): return CollectionKeys = set(self.collection.keys()) newCollection = archiveCollection() newCollectionPath = archive.path.get() if newCollectionPath in CollectionKeys: return self.collection[newCollectionPath] newCollection.path.update(newCollectionPath) self.collection[newCollectionPath] = newCollection return self.collection[newCollectionPath] def onachiveColection(self): # cleans up structure for item in self.collection.keys(): itemName = self.collection[item].path.get() if itemName != item: print "shoudl not happen"
class archiveCollection(object): def __init__(self): self.path = Observable(None) self.archives = ObservableDict() def addArchive(self,archiveAdd): if not isinstance(archiveAdd,archiveInstance): return CollectionKeys = set(self.archives.keys()) newCollection = archiveInstance() newName = archiveAdd.Name.get() if newName in CollectionKeys: return self.archives[newName] newCollection.update(archiveAdd) newCollection.Directory.update(self.path.get()) self.archives[newName] = newCollection return self.archives[newName]
class mountedModel(object): def __init__(self): self.ByTarget = ObservableDict() self.BySource = ObservableDict() def _readMtabLine(self, line): stripedLine = line.strip() splitLine = stripedLine.split(" ") lenSplitLine = len(splitLine) if lenSplitLine < 3: return None Source = splitLine[0] Target = splitLine[1] FileSystem = splitLine[2] info = {"Source": Source, "Target": Target, "FileSystem": FileSystem} return info def readMtab(self): fp = open("/etc/mtab") ByTarget = {} BySource = {} for line in fp: result = self._readMtabLine(line) if result == None: continue ByTarget[result["Target"]] = result BySource[result["Source"]] = result # Shoudl handle updating here self.ByTarget.update(ByTarget) self.BySource.update(BySource) def mounted(self, HostRootSpace, target): self.readMtab() targetKeys = set(self.ByTarget.keys()) sourceKeys = set(self.BySource.keys()) if not target in self.ByTarget.keys(): return False if not HostRootSpace in self.BySource.keys(): return False return True
def __init__(self): self.ByTarget = ObservableDict() self.BySource = ObservableDict()
def __init__(self): self.collection = ObservableDict() self.collection.addCbPost(self,self.onachiveColection)
def __init__(self): self.path = Observable(None) self.archives = ObservableDict()
def __init__(self): self.callbackKey = GenKey() self.vmsbyName = ObservableDict() self.vmsbyId = ObservableDict() self.vmsByUuid = ObservableDict()
class vhostMdl: def __init__(self): self.callbackKey = GenKey() self.vmsbyName = ObservableDict() self.vmsbyId = ObservableDict() self.vmsByUuid = ObservableDict() def getVmMatch(self,vmModel): Uuid = vmModel.libvirtUuid.get() candidateUuid = [] candidates = [] if Uuid != None: if Uuid in self.vmsByUuid: candidateUuid.append(self.vmsByUuid[Uuid]) candidateId = [] ID = vmModel.libvirtId.get() if ID != None and ID != -1: if ID in self.vmsbyId: candidateId.append(self.vmsbyId[ID]) candidateName = [] Name = vmModel.libvirtName.get() if Name != None: if Name in self.vmsbyName: candidateName.append(self.vmsbyName[Name]) candidateUuidSet = set(candidateUuid) candidateUuidSetLen = len(candidateUuidSet) candidateIdSet = set(candidateId) candidateIdSetLen = len(candidateIdSet) candidateNameSet = set(candidateName) candidateNameSetLen = len(candidateNameSet) if ((candidateNameSetLen > 0) and (candidateIdSetLen > 0) and (candidateUuidSetLen > 0)): completeSet = candidateUuidSet.intersection(candidateNameSet).intersection(candidateIdSet) if len(completeSet) == 1: return completeSet.pop() if ((candidateNameSetLen > 0) and (candidateUuidSetLen > 0)): nameUuidSet = candidateUuidSet.intersection(candidateNameSet) if len(nameUuidSet) == 1: return nameUuidSet.pop() if ((candidateNameSetLen > 0) and (candidateIdSetLen > 0)): nameIdSet = candidateIdSet.intersection(candidateNameSet) if len(nameIdSet) == 1: return nameIdSet.pop() if (candidateUuidSetLen > 0): nameUuidSetLen = len(candidateUuidSet) #print "nameUuidSetLen",nameUuidSetLen if nameUuidSetLen == 1: return candidateUuidSet.pop() if (candidateNameSetLen > 0): nameIdSetLen = len(candidateNameSet) #print "nameIdSetLen",nameIdSetLen if nameIdSetLen == 1: return candidateNameSet.pop() if (candidateIdSetLen > 0): nameIdSetLen = len(candidateIdSet) #print "nameIdSetLen",nameIdSetLen if nameIdSetLen == 1: return candidateIdSet.pop() lenAllSets = candidateUuidSetLen + candidateIdSetLen + candidateNameSetLen #print lenAllSets if lenAllSets == 0: # If we have no results return return None if candidateUuidSetLen == 1: return candidateUuidSet.pop() # This code will never be exxecuted candidates = candidateUuid + candidateId + candidateName if len(candidates) > 0: return candidates[0] return None def addVM(self,vmModel): #debugVm(vmModel) match = self.getVmMatch(vmModel) if match != None: vmModel.update(match) return match newOne = vmMdl() newOne.libvirtUuid.addCallback(self.callbackKey,partialfn(self._onUuidPost,newOne)) newOne.libvirtId.addCallback(self.callbackKey,partialfn(self._onIdPost,newOne)) newOne.libvirtName.addCallback(self.callbackKey,partialfn(self._onNamePost,newOne)) vmModel.update(newOne) return newOne def _onIdPost(self,NewItem): identifier = NewItem.libvirtId.get() validIdentifier = True if identifier == None: validIdentifier = False if identifier == -1: validIdentifier = False #print "_onIdPdddddddddddddost" ,identifier matches = self.getVmMatch(NewItem) if matches == None: #print "fddddddddddddddddddddxxssddddd" #debugVm(NewItem) self.vmsbyId[identifier] = NewItem matches = self.vmsbyId[identifier] OldId = matches.libvirtId.get() if OldId != None: #print 'herere;',debugVm(matches) if OldId != identifier: if OldId in self.vmsbyId.keys(): if validIdentifier: self.vmsbyId[identifier] = self.vmsbyId[OldId] del self.vmsbyId[OldId] if not validIdentifier: return None if not identifier in self.vmsbyId.keys(): self.vmsbyId[identifier] = matches #NewItem.update(self.vmsbyId[identifier]) return self.vmsbyId[identifier] def _onUuidPost(self,NewItem): Uuid = NewItem.libvirtUuid.get() #print "_onUuidPost" ,Uuid validIdentifier = True if Uuid == None: validIdentifier = False matches = self.getVmMatch(NewItem) #print "matches", matches if matches == None: #debugVm(NewItem) if not validIdentifier: return None if not Uuid in self.vmsByUuid.keys(): self.vmsByUuid[Uuid] = NewItem matches = self.getVmMatch(NewItem) for UuidNow in self.vmsByUuid.keys(): CurrentUuId = self.vmsByUuid[UuidNow].libvirtUuid.get() if CurrentUuId != UuidNow: del self.vmsByUuid[UuidNow] if not Uuid in self.vmsByUuid.keys(): self.vmsByUuid[Uuid] = matches #NewItem.update(self.vmsByUuid[Uuid]) self.vmsByUuid[Uuid] return self.vmsByUuid[Uuid] def _onNamePost(self,NewItem): Name = NewItem.libvirtName.get() #print "_onNamePost" ,Name validIdentifier = True if Name == None: validIdentifier = False matches = self.getVmMatch(NewItem) if matches == None: vmModel = vmMdl() #vmModel.libvirtName.update(Name) Uuid = NewItem.libvirtUuid.get() if Uuid != None: vmModel.libvirtUuid.update(Uuid) ID = NewItem.libvirtId.get() if ID != None and ID != -1: vmModel.libvirtId.update(ID) #print "fddddddddddddddddddddddddd" #debugVm(NewItem) if validIdentifier: self.vmsbyName[Name] = NewItem matches = self.vmsbyName[Name] else: return None OldName = matches.libvirtName.get() if OldName != None: #print 'herere;',debugVm(matches) if OldName != Name: if OldName in self.vmsbyName.keys(): if validIdentifier: self.vmsbyName[Name] = self.vmsbyName[OldName] del self.vmsbyName[OldName] if not validIdentifier: return None #print "OldName",OldName,"Name",Name if not Name in self.vmsbyName.keys(): self.vmsbyName[Name] = matches #NewItem.update(self.vmsbyName[Name]) return self.vmsbyName[Name]