def __init__(self, serverObject, parent=None): """ If you specify a parent, remember the instance isn't garbage-collected before it's unparented. If you don't, remember the instance IS garbage collected as soon as it goes out of scope -- so don't except any signals from it. If you aren't using the signals you can use the instance without spesifying a parent. :param serverObject: the server object to create a connection with. :type serverObject: ServerObject :param parent: the parent caller. :type parent: QObject """ QObject.__init__(self, parent) self.lumaConnection = LumaConnection(serverObject) self.logger = logging.getLogger(__name__)
class LumaConnectionWrapper(QObject): """Connection wrapper for `LumaConnection` The erapper provides async-versions of time-consuming methods which use signals to return the result and also sync-versions which use ``qApp.processEvents`` to avoid blocking the GUI. If your method can work with signals to get the result, please use the async-methods. That way we avoid calling ``qApp.processEvents`` which leads to recursion if other methods are called while waiting for the result which again mens your results will be delayed until that method is finished. The xAsync-methods takes an ``identStr`` which is signaled along with the result of the operation for identifying purposes. """ #: The signal to connect to retirve bind success from `bindAsync` bindFinished = pyqtSignal(bool, Exception, str) #: The signal to connect to retrive results from `searchAsync` searchFinished = pyqtSignal(bool, list, Exception, str) i = 0 def __init__(self, serverObject, parent=None): """ If you specify a parent, remember the instance isn't garbage-collected before it's unparented. If you don't, remember the instance IS garbage collected as soon as it goes out of scope -- so don't except any signals from it. If you aren't using the signals you can use the instance without spesifying a parent. :param serverObject: the server object to create a connection with. :type serverObject: ServerObject :param parent: the parent caller. :type parent: QObject """ QObject.__init__(self, parent) self.lumaConnection = LumaConnection(serverObject) self.logger = logging.getLogger(__name__) ########### # BIND ########### def bindSync(self): """Equivalent to `LumaConnection.bind` but doesn't block the GUI through calling ``qApp.processEvents`` while the bind is in progress. """ bindWorker = BindWorker(self.lumaConnection) thread = self.__createThread(bindWorker) thread.start() while not thread.isFinished(): self.__whileWaiting() # Make sure cleanup() is called since the # finished()-signal connected to cleanup() # might not be emitted. if self.parent() == None: thread.cleanup() return (bindWorker.success, bindWorker.exception) def bindAsync(self, identStr=""): """Non-blocking. Listen to `LumaConnectionWrapper.bindFinished` for the result. Only use the exception passed if ``success`` is False. """ bindWorker = BindWorker(self.lumaConnection, identStr) bindWorker.workDone.connect(self.bindFinished) thread = self.__createThread(bindWorker) thread.start() ########### # SEARCH ########### def searchSync(self, base="", scope=ldap.SCOPE_BASE, filter="(objectClass=*)", attrList=None, attrsonly=0, sizelimit=0): """Equivalent to `LumaConnection.search`. See `bindSync` for details. """ searchWorker = SearchWorker(self.lumaConnection, base, scope, filter, attrList, attrsonly, sizelimit) thread = self.__createThread(searchWorker) thread.start() while not thread.isFinished(): self.__whileWaiting() # Make sure cleanup() is called since the # finished()-signal connected to cleanup() # might not be emitted. if self.parent() == None: thread.cleanup() return (searchWorker.success, searchWorker.resultList, searchWorker.exception) def searchAsync(self, base="", scope=ldap.SCOPE_BASE, filter="(objectClass=*)", attrList=None, attrsonly=0, sizelimit=0, identStr=""): """Non-blocking. Listen to LumaConnectionWrapper.searchFinished for the result. Only use the exception passed if ``success`` is False. """ searchWorker = SearchWorker(self.lumaConnection, base, scope, filter, attrList, attrsonly, sizelimit, identStr) searchWorker.workDone.connect(self.searchFinished) thread = self.__createThread(searchWorker) thread.start() def getBaseDNListSync(self): # TODO MAKE ASYNC-VERSION baseWorker = BaseDNWorker(self.lumaConnection) thread = self.__createThread(baseWorker) thread.start() while not thread.isFinished(): self.__whileWaiting() # Make sure cleanup() is called since the # finished()-signal connected to cleanup() # might not be emitted. if self.parent() == None: thread.cleanup() return (baseWorker.success, baseWorker.resultList, baseWorker.exception) ########### # Sync-only-methods # (resonably quick, so no immediate need for async-versions). ########### def delete(self, dnDelete=None): return self.lumaConnection.delete(dnDelete) def modify(self, dn, modlist=None): return self.lumaConnection.modify(dn, modlist) def add(self, dn, modlist): return self.lumaConnection.add(dn, modlist) def updateDataObject(self, smartDataObject): return self.lumaConnection.updateDataObject(smartDataObject) def addDataObject(self, dataObject): return self.lumaConnection.addDataObject(dataObject) def unbind(self): self.lumaConnection.unbind() ########### # Internal methods ########### def __whileWaiting(self): """When using sync-methods we runs this while waiting for `LumaConnection` to return data. This keeps the GUI responsive. """ qApp.processEvents() time.sleep(0.05) def __createThread(self, worker): # Create the thread workerThread = WorkerThread() # Move worker to thread workerThread.setWorker(worker) return workerThread
class LumaConnectionWrapper(QObject): """Connection wrapper for `LumaConnection` The erapper provides async-versions of time-consuming methods which use signals to return the result and also sync-versions which use ``qApp.processEvents`` to avoid blocking the GUI. If your method can work with signals to get the result, please use the async-methods. That way we avoid calling ``qApp.processEvents`` which leads to recursion if other methods are called while waiting for the result which again mens your results will be delayed until that method is finished. The xAsync-methods takes an ``identStr`` which is signaled along with the result of the operation for identifying purposes. """ #: The signal to connect to retirve bind success from `bindAsync` bindFinished = pyqtSignal(bool, Exception, str) #: The signal to connect to retrive results from `searchAsync` searchFinished = pyqtSignal(bool, list, Exception, str) i = 0 def __init__(self, serverObject, parent=None): """ If you specify a parent, remember the instance isn't garbage-collected before it's unparented. If you don't, remember the instance IS garbage collected as soon as it goes out of scope -- so don't except any signals from it. If you aren't using the signals you can use the instance without spesifying a parent. :param serverObject: the server object to create a connection with. :type serverObject: ServerObject :param parent: the parent caller. :type parent: QObject """ QObject.__init__(self, parent) self.lumaConnection = LumaConnection(serverObject) self.logger = logging.getLogger(__name__) ########### # BIND ########### def bindSync(self): """Equivalent to `LumaConnection.bind` but doesn't block the GUI through calling ``qApp.processEvents`` while the bind is in progress. """ bindWorker = BindWorker(self.lumaConnection) thread = self.__createThread(bindWorker) thread.start() while not thread.isFinished(): self.__whileWaiting() # Make sure cleanup() is called since the # finished()-signal connected to cleanup() # might not be emitted. if self.parent() == None: thread.cleanup() return (bindWorker.success, bindWorker.exception) def bindAsync(self, identStr=""): """Non-blocking. Listen to `LumaConnectionWrapper.bindFinished` for the result. Only use the exception passed if ``success`` is False. """ bindWorker = BindWorker(self.lumaConnection, identStr) bindWorker.workDone.connect(self.bindFinished) thread = self.__createThread(bindWorker) thread.start() ########### # SEARCH ########### def searchSync( self, base="", scope=ldap.SCOPE_BASE, filter="(objectClass=*)", attrList=None, attrsonly=0, sizelimit=0 ): """Equivalent to `LumaConnection.search`. See `bindSync` for details. """ searchWorker = SearchWorker(self.lumaConnection, base, scope, filter, attrList, attrsonly, sizelimit) thread = self.__createThread(searchWorker) thread.start() while not thread.isFinished(): self.__whileWaiting() # Make sure cleanup() is called since the # finished()-signal connected to cleanup() # might not be emitted. if self.parent() == None: thread.cleanup() return (searchWorker.success, searchWorker.resultList, searchWorker.exception) def searchAsync( self, base="", scope=ldap.SCOPE_BASE, filter="(objectClass=*)", attrList=None, attrsonly=0, sizelimit=0, identStr="" ): """Non-blocking. Listen to LumaConnectionWrapper.searchFinished for the result. Only use the exception passed if ``success`` is False. """ searchWorker = SearchWorker( self.lumaConnection, base, scope, filter, attrList, attrsonly, sizelimit, identStr ) searchWorker.workDone.connect(self.searchFinished) thread = self.__createThread(searchWorker) thread.start() def getBaseDNListSync(self): # TODO MAKE ASYNC-VERSION baseWorker = BaseDNWorker(self.lumaConnection) thread = self.__createThread(baseWorker) thread.start() while not thread.isFinished(): self.__whileWaiting() # Make sure cleanup() is called since the # finished()-signal connected to cleanup() # might not be emitted. if self.parent() == None: thread.cleanup() return (baseWorker.success, baseWorker.resultList, baseWorker.exception) ########### # Sync-only-methods # (resonably quick, so no immediate need for async-versions). ########### def delete(self, dnDelete=None): return self.lumaConnection.delete(dnDelete) def modify(self, dn, modlist=None): return self.lumaConnection.modify(dn, modlist) def add(self, dn, modlist): return self.lumaConnection.add(dn, modlist) def updateDataObject(self, smartDataObject): return self.lumaConnection.updateDataObject(smartDataObject) def addDataObject(self, dataObject): return self.lumaConnection.addDataObject(dataObject) def unbind(self): self.lumaConnection.unbind() ########### # Internal methods ########### def __whileWaiting(self): """When using sync-methods we runs this while waiting for `LumaConnection` to return data. This keeps the GUI responsive. """ qApp.processEvents() time.sleep(0.05) def __createThread(self, worker): # Create the thread workerThread = WorkerThread() # Move worker to thread workerThread.setWorker(worker) return workerThread