def _getConnection(): '''Gets or creates a database connection and returns it The connection is stored on the thread so we don't have to create and close it every time we need to use the database, as that happens multiple times a second. Returns: Database connection sqlite3.Connection ''' t = QThread.currentThread() if not hasattr(t, "dbConnection"): setattr(t, "dbConnection", _initConnection()) return getattr(t, "dbConnection")
def main(): loadtesterplugins() qt.mainthread = QThread.currentThread() # Store current thread qt.initialize_view() # Get the view ready dispatch_thread = QThread() # The QThread to put the dispatcher on qt.maindispatch = QTDispatcher(qt.mainview) # Set up the dispatcher qt.qapp.lastWindowClosed.connect(dispatch_thread.quit) # Connect the close signal to the thread quit signal qt.maindispatch.moveToThread(dispatch_thread) # Move the dispatcher to the new thread dispatch_thread.start() # Start the thread qt.mainview.show() # Show the main window res = qt.qapp.exec_() # Start the event loop, exits when the last window has been closed dispatch_thread.wait() # Wait for the dispatcher thread to finish sys.exit(res)
def get(key=None): '''Returns either the full config or a single key. This function always returns the most up to date config stored on the thread object. Args: key: The key we are interested in (default: {None}) Returns: Either the value of the key specified or if no key is specified the full config dict is returned. dict ''' t = QThread.currentThread() if not key: return getattr(t, "config") else: return getattr(t, "config")[key]
def isInternal(): '''Checks whether the current clipboard data is set from withing the application. Using the custom clipboard format we have registered, we detect whether the change of the clipboard is due to internal reasons, in which case it is being skipped in the monitorClipboard function. Returns: True if the clipboard change is internal, False otherwise bool ''' t = QThread.currentThread() if not hasattr(t, "customClipboardFormatID"): t.customClipboardFormatID = registerCustomClipboardFormat() if getattr(t, "customClipboardFormatID") in getClipboardFormats(): return True return False
def set(data): '''Sets the clipboard to the passed in data from the history. Currently, only the text data is supported, so if a file has been copied only the path to it would be set to clipboard. Once image support is added, similar to the file case, only a path to the saved image file would be set to the clipboard. In the future, I might find I prefer pasting actual files and images, instead of just paths, in which case, all I need to do is check if there is a file or image stored, read that and put it on the clipboard. Args: data: The data received from the clipboard history in the same format as in the getData function ''' t = QThread.currentThread() if not hasattr(t, "customClipboardFormatID"): t.customClipboardFormatID = registerCustomClipboardFormat() openClipboard() win32clipboard.EmptyClipboard() # Setting the actual clipboard data if data["unicode"]: win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, data["unicode"]) if data["text"]: win32clipboard.SetClipboardData(win32clipboard.CF_TEXT, data["text"] if isinstance(data["text"], basestring) else str(data["text"][0])) if data["html"]: win32clipboard.SetClipboardData(49416, data["html"]) # Setting custom clipboard format to identify that it's an internal change win32clipboard.SetClipboardData(getattr(t, "customClipboardFormatID"), "1") win32clipboard.CloseClipboard()
def monitorClipboard(): '''Polls the clipboard for changes and saves them to the history. The while loop exits once the "do_run" attribute of the current thread is set to False from the main thread. On exit we are checking whether we have a database connection stored on the thread and if we do, we close it. ''' t = QThread.currentThread() history = getHistory() data = history[-1] if history else {} while getattr(t, "do_run", True): newData = getData() if newData and newData != data and not isInternal(): save() data = newData time.sleep(config.get("poll_clipboard_interval")) if hasattr(t, "dbConnection"): getattr(t, "dbConnection").close() logging.info("Closed dbConnection in clipboard thread")
def currentThreadName(): return QThread.currentThread().objectName()
def _check_thread(self): if QThread.currentThread() != self._processor.thread(): err = 'Method must be called from thread owning the loop.' raise RuntimeError(err)
def currentThreadName(): return QThread.currentThread().objectName()