FreeCAD
93 строки · 4.3 Кб
1# SPDX-License-Identifier: LGPL-2.1-or-later
2# ***************************************************************************
3# * *
4# * Copyright (c) 2022-2023 FreeCAD Project Association *
5# * *
6# * This file is part of FreeCAD. *
7# * *
8# * FreeCAD is free software: you can redistribute it and/or modify it *
9# * under the terms of the GNU Lesser General Public License as *
10# * published by the Free Software Foundation, either version 2.1 of the *
11# * License, or (at your option) any later version. *
12# * *
13# * FreeCAD is distributed in the hope that it will be useful, but *
14# * WITHOUT ANY WARRANTY; without even the implied warranty of *
15# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16# * Lesser General Public License for more details. *
17# * *
18# * You should have received a copy of the GNU Lesser General Public *
19# * License along with FreeCAD. If not, see *
20# * <https://www.gnu.org/licenses/>. *
21# * *
22# ***************************************************************************
23
24""" Misc. worker thread classes for the FreeCAD Addon Manager. """
25
26from typing import Optional
27
28import FreeCAD
29from PySide import QtCore
30import NetworkManager
31import time
32
33translate = FreeCAD.Qt.translate
34
35
36class ConnectionChecker(QtCore.QThread):
37"""A worker thread for checking the connection to GitHub as a proxy for overall
38network connectivity. It has two signals: success() and failure(str). The failure
39signal contains a translated error message suitable for display to an end user."""
40
41success = QtCore.Signal()
42failure = QtCore.Signal(str)
43
44def __init__(self):
45QtCore.QThread.__init__(self)
46self.done = False
47self.request_id = None
48self.data = None
49
50def run(self):
51"""Not generally called directly: create a new ConnectionChecker object and call start()
52on it to spawn a child thread."""
53
54FreeCAD.Console.PrintLog("Checking network connection...\n")
55url = "https://api.github.com/zen"
56self.done = False
57NetworkManager.AM_NETWORK_MANAGER.completed.connect(self.connection_data_received)
58self.request_id = NetworkManager.AM_NETWORK_MANAGER.submit_unmonitored_get(
59url, timeout_ms=10000
60)
61while not self.done:
62if QtCore.QThread.currentThread().isInterruptionRequested():
63FreeCAD.Console.PrintLog("Connection check cancelled\n")
64NetworkManager.AM_NETWORK_MANAGER.abort(self.request_id)
65self.disconnect_network_manager()
66return
67QtCore.QCoreApplication.processEvents()
68time.sleep(0.1)
69if not self.data:
70self.failure.emit(
71translate(
72"AddonsInstaller",
73"Unable to read data from GitHub: check your internet connection and proxy "
74"settings and try again.",
75)
76)
77self.disconnect_network_manager()
78return
79FreeCAD.Console.PrintLog(f"GitHub's zen message response: {self.data.decode('utf-8')}\n")
80self.disconnect_network_manager()
81self.success.emit()
82
83def connection_data_received(self, id: int, status: int, data: QtCore.QByteArray):
84if self.request_id is not None and self.request_id == id:
85if status == 200:
86self.data = data.data()
87else:
88FreeCAD.Console.PrintWarning(f"No data received: status returned was {status}\n")
89self.data = None
90self.done = True
91
92def disconnect_network_manager(self):
93NetworkManager.AM_NETWORK_MANAGER.completed.disconnect(self.connection_data_received)
94