def __init__(self): super().__init__() self.counting_iterator = itertools.count() self.queue = queue.Queue() self.__last_started_index = 0 self.__abort_when_found: List[int] = [] self.replies: Dict[int, QtNetwork.QNetworkReply] = {} self.file_buffers = {} # We support an arbitrary number of threads using synchronous GET calls: self.synchronous_lock = threading.Lock() self.synchronous_complete: Dict[int, bool] = {} self.synchronous_result_data: Dict[int, QtCore.QByteArray] = {} # Make sure we exit nicely on quit if QtCore.QCoreApplication.instance() is not None: QtCore.QCoreApplication.instance().aboutToQuit.connect( self.__aboutToQuit) # Create the QNAM on this thread: self.QNAM = QtNetwork.QNetworkAccessManager() self.QNAM.proxyAuthenticationRequired.connect( self.__authenticate_proxy) self.QNAM.authenticationRequired.connect( self.__authenticate_resource) qnam_cache = QtCore.QStandardPaths.writableLocation( QtCore.QStandardPaths.CacheLocation) os.makedirs(qnam_cache, exist_ok=True) self.diskCache = QtNetwork.QNetworkDiskCache() self.diskCache.setCacheDirectory(qnam_cache) self.QNAM.setCache(self.diskCache) self.monitored_connections: List[int] = [] self._setup_proxy() # A helper connection for our blocking interface self.completed.connect(self.__synchronous_process_completion) # Set up our worker connection self.__request_queued.connect(self.__setup_network_request)
def __init__(self): super().__init__() self.counting_iterator = itertools.count() self.queue = queue.Queue() self.__last_started_index = 0 self.__abort_when_found: List[int] = [] self.replies: Dict[int, QtNetwork.QNetworkReply] = {} self.file_buffers = {} # We support an arbitrary number of threads using synchronous GET calls: self.synchronous_lock = threading.Lock() self.synchronous_complete: Dict[int, bool] = {} self.synchronous_result_data: Dict[int, QtCore.QByteArray] = {} # Make sure we exit nicely on quit QtCore.QCoreApplication.instance().aboutToQuit.connect( self.__aboutToQuit) # Create the QNAM on this thread: self.QNAM = QtNetwork.QNetworkAccessManager() self.QNAM.proxyAuthenticationRequired.connect( self.__authenticate_proxy) self.QNAM.authenticationRequired.connect( self.__authenticate_resource) qnam_cache = QtCore.QStandardPaths.writableLocation( QtCore.QStandardPaths.CacheLocation) os.makedirs(qnam_cache, exist_ok=True) self.diskCache = QtNetwork.QNetworkDiskCache() self.diskCache.setCacheDirectory(qnam_cache) self.QNAM.setCache(self.diskCache) # Set up the proxy, if necesssary: noProxyCheck = True systemProxyCheck = False userProxyCheck = False proxy_string = "" if HAVE_FREECAD: pref = FreeCAD.ParamGet( "User parameter:BaseApp/Preferences/Addons") noProxyCheck = pref.GetBool("NoProxyCheck", noProxyCheck) systemProxyCheck = pref.GetBool("SystemProxyCheck", systemProxyCheck) userProxyCheck = pref.GetBool("UserProxyCheck", userProxyCheck) proxy_string = pref.GetString("ProxyUrl", "") # Add some error checking to the proxy setup, since for historical reasons they # are indepdendent booleans, rather than an enumeration: count = [noProxyCheck, systemProxyCheck, userProxyCheck].count(True) if count != 1: FreeCAD.Console.PrintWarning( translate( "AddonsInstaller", "Parameter error: mutually exclusive proxy options set. Resetting to default.", ) + "\n") noProxyCheck = True systemProxyCheck = False userProxyCheck = False pref.SetBool("NoProxyCheck", noProxyCheck) pref.SetBool("SystemProxyCheck", systemProxyCheck) pref.SetBool("UserProxyCheck", userProxyCheck) if userProxyCheck and not proxy_string: FreeCAD.Console.PrintWarning( translate( "AddonsInstaller", "Parameter error: user proxy indicated, but no proxy provided. Resetting to default.", ) + "\n") noProxyCheck = True userProxyCheck = False pref.SetBool("NoProxyCheck", noProxyCheck) pref.SetBool("UserProxyCheck", userProxyCheck) else: print("Please select a proxy type:") print("1) No proxy") print("2) Use system proxy settings") print("3) Custom proxy settings") result = input("Choice: ") if result == "1": pass elif result == "2": noProxyCheck = False systemProxyCheck = True elif result == "3": noProxyCheck = False userProxyCheck = True proxy_string = input( "Enter your proxy server (host:port): ") else: print(f"Got {result}, expected 1, 2, or 3.") app.quit() if noProxyCheck: pass elif systemProxyCheck: query = QtNetwork.QNetworkProxyQuery( QtCore.QUrl("https://github.com/FreeCAD/FreeCAD")) proxy = QtNetwork.QNetworkProxyFactory.systemProxyForQuery( query) if proxy and proxy[0]: self.QNAM.setProxy( proxy[0]) # This may still be QNetworkProxy.NoProxy elif userProxyCheck: host, _, port_string = proxy_string.rpartition(":") port = 0 if not port_string else int(port_string) # For now assume an HttpProxy, but eventually this should be a parameter proxy = QtNetwork.QNetworkProxy( QtNetwork.QNetworkProxy.HttpProxy, host, port) self.QNAM.setProxy(proxy) # A helper connection for our blocking interface self.completed.connect(self.__synchronous_process_completion) # Set up our worker connection self.__request_queued.connect(self.__setup_network_request)
def run(self): """Do not call directly: use start() to begin the event loop on a new thread.""" # Create the QNAM on this thread: self.thread = QtCore.QThread.currentThread() self.QNAM = QtNetwork.QNetworkAccessManager() self.QNAM.proxyAuthenticationRequired.connect( self.__authenticate_proxy) self.QNAM.authenticationRequired.connect( self.__authenticate_resource) # A helper connection for our blocking interface self.completed.connect(self.__synchronous_process_completion) # Set up the proxy, if necesssary: noProxyCheck = True systemProxyCheck = False userProxyCheck = False proxy_string = "" if HAVE_FREECAD: pref = FreeCAD.ParamGet( "User parameter:BaseApp/Preferences/Addons") noProxyCheck = pref.GetBool("NoProxyCheck", noProxyCheck) systemProxyCheck = pref.GetBool("SystemProxyCheck", systemProxyCheck) userProxyCheck = pref.GetBool("UserProxyCheck", userProxyCheck) proxy_string = pref.GetString("ProxyUrl", "") else: print("Please select a proxy type:") print("1) No proxy") print("2) Use system proxy settings") print("3) Custom proxy settings") result = input("Choice: ") if result == "1": pass elif result == "2": noProxyCheck = False systemProxyCheck = True elif result == "3": noProxyCheck = False userProxyCheck = True proxy_string = input( "Enter your proxy server (host:port): ") else: print(f"Got {result}, expected 1, 2, or 3.") app.quit() if noProxyCheck: pass elif systemProxyCheck: query = QtNetwork.QNetworkProxyQuery( QtCore.QUrl("https://github.com/FreeCAD/FreeCAD")) proxy = QtNetwork.QNetworkProxyFactory.systemProxyForQuery( query) if proxy and proxy[0]: self.QNAM.setProxy( proxy[0]) # This may still be QNetworkProxy.NoProxy elif userProxyCheck: host, _, port_string = proxy_string.rpartition(":") port = 0 if not port_string else int(port_string) # For now assume an HttpProxy, but eventually this should be a parameter proxy = QtNetwork.QNetworkProxy( QtNetwork.QNetworkProxy.HttpProxy, host, port) self.QNAM.setProxy(proxy) qnam_cache = QtCore.QStandardPaths.writableLocation( QtCore.QStandardPaths.CacheLocation) os.makedirs(qnam_cache, exist_ok=True) diskCache = QtNetwork.QNetworkDiskCache() diskCache.setCacheDirectory(qnam_cache) self.QNAM.setCache(diskCache) # Start an event loop while True: if QtCore.QThread.currentThread().isInterruptionRequested(): # Support shutting down the entire thread, but this should be very rarely used. # Callers should generally just call abort() on each network call they want to # terminate, using requestInterruption() will terminate ALL network requests. if not HAVE_FREECAD: print("Shutting down all active network requests...", flush=True) self.abort_all() self.queue.join() if not HAVE_FREECAD: print("All requests terminated.", flush=True) return try: item = self.queue.get_nowait() if item: if item.index in self.__abort_when_found: self.__abort_when_found.remove(item.index) continue # Do not do anything with this item, it's been aborted... reply = self.QNAM.get(item.request) self.__last_started_index = item.index reply.finished.connect( lambda i=item: self.__reply_finished(i)) reply.redirected.connect( lambda url, r=reply: self.__on_redirect(r, url)) reply.sslErrors.connect(self.__on_ssl_error) if item.track_progress: reply.readyRead.connect( lambda i=item.index: self.__data_incoming(i)) reply.downloadProgress.connect( lambda a, b, i=item.index: self.progress_made. emit(i, a, b)) self.replies[item.index] = reply except queue.Empty: pass QtCore.QCoreApplication.processEvents()